-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
time to test equals() and hashCode() depends exponentially on the number of fields #203
Comments
I have added a patch to the same https://github.com/36893488147419103231/pojo-tester-tester project.
Strictly speaking, it is quadratic rather than linear, but it is not that much important. |
Now it is possible to test equals() and hashCode() in quadratic rather than exponential time (and 20^2 is much better than 2^20 for a POJO with 20 fields corresponding to 20 columns in a DB table). The old thorough testers are preserved.
see pull requests |
I will try to make review on Friday, thanks for contributions! |
Now it is possible to test equals() and hashCode() in quadratic rather than exponential time (and 20^2 is much better than 2^20 for a POJO with 20 fields corresponding to 20 columns in a DB table). The old thorough testers are preserved.
In the project
https://github.com/36893488147419103231/pojo-tester-tester
in the package testertester.equalshashcode there are tests of 3 classes:
with
equals()
andhashCode()
generated by IDE.The first class is tested in no time, the second one takes about a minute, and the test for the third one did not terminate for me (but probably it would in about half an hour, unless it would take too much memory). UPD: bigTest failed after 8m 49s with
java.lang.OutOfMemoryError: GC overhead limit exceeded
.It is important to test
equals()
andhashCode()
in linear time.I propose the following algorithm:
Start with 3 POJO objects, one filled with nulls (zeroes, etc.), and two others filled with values that do not repeat (well, it is difficult to find 20 different boolean values).
Initially for the
class MyPojo {int a,b,c,d;}
we have:On the first iteration, we will compare
P, null, null, null
with:On the second iteration, we will compare
P, Q, null, null
with:On the 3rd iteration, we will compare
P, Q, R, null
with:and so on.
Of course, you must check that a.equals(b) == b.equals(a), and that objects that are equal have equal hash codes.
You may check that the number of different returned hash codes is at least a half of the the number of objects that participated in comparisons.
I'd recommend performing additional comparisons on each iteration, e.g. that a.equals(null) == false, that a.equals(a) is true, and that a.equals(1) is false.
Note. This issue may be related on unrelated to #201
(UPDATE) Note 2. I believe, it is
FieldUtils.permutations()
called fromObjectGenerator.generateDifferentObjects()
that is responsible for exponential growth. It for N fields, it returns 2^N permutations. If it returned a list of size N, consumption of both time and memory would be linear.The text was updated successfully, but these errors were encountered: