Reputation: 35
String title ="A";
int year = 2009;
String director = "Zebra";
String title1 ="B";
int year1 = 2010;
String director1 = "Zzz";
VideoObj a = new VideoObj(title,year,director);
VideoObj b = new VideoObj(title2,year,director);
Assert.assertTrue( a.compareTo(b) == -b.compareTo(a) );
What does the meaning of - in the Assert.assertTrue( a.compareTo(b) == -b.compareTo(a) );
statement???
Please can i get an explanation?
Thanks
Upvotes: 1
Views: 2040
Reputation: 881423
The meaning of the -
is negation, plain and simple. I've seen that idiom in numeric comparisons that return a -1
, 0
or +1
value, depending on the comparison. Although it would work just as well for those that return a magnitude as well, -n
, 0
, +n
(where n
is identical).
It's simply checking that the compareTo
operations give you the opposite value when you reverse the operands. In other words, it's an assertion that compareTo
functions as expected (but not necessarily as guaranteed - see below).
If a < b
and b < a
(ie, a
is both less than, and greater than b
), then you have a serious problem that needs fixing :-)
From the CompareTo
documentation:
The implementor must ensure sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) for all x and y.
In the foregoing description, the notation sgn(expression) designates the mathematical signum function, which is defined to return one of -1, 0, or 1 according to whether the value of expression is negative, zero or positive.
Based on that, the assertion is not quite right, it should be comparing Integer.signum (a.compareTo (b))
rather than just a.compareTo (b)
, unless it has extra knowledge that the implementation of CompareTo
is guaranteed to return symmetrical (around zero) values.
Upvotes: 4
Reputation: 42597
I think this is testing that the compareTo()
method is implemented properly.
For any implementation of the Comparable
interface
a.compareTo(b) == -b.compareTo(a)
should always be true. If it wasn't, you would get nonsensical results when performing operations that use compareTo(), such as sorting your objects.
Update: OK,OK, only the sign needs to be opposite for Comparable to work correctly! - but testing that the values are equals and have the opposite sign ensures this - it's just a stricter test than is, er, strictly required.
Upvotes: 0
Reputation: 7985
Assert.assertTrue will raise an exception if the comparison you are passing is false
This line is checking if you are getting consistent values for compareTo.
CompareTo should be symmetrical and you can verify this by changing the order of the operands and multiplying the result by -1
Upvotes: 1
Reputation: 198093
As written, the statement doesn't even do what it looks like it's supposed to do...
The line should be
Assert.assertTrue(Integer.signum(a.compareTo(b)) == -Integer.signum(b.compareTo(a));
and it tests that the ordering is antisymmetric, that is, if a < b
, then b > a
, and so on, as an ordering must be by definition.
Upvotes: 1
Reputation: 12725
a.compareTo(b);
this method: returns 1 if a>b returns -1 if b>a returns 0 if a=b
so it will be change a>b to b>a and b>a to a>b
a.compareTo(b) and -b.compareTo(a) are the same.
Upvotes: 0
Reputation: 236004
It'd be useful to see the implementation of compareTo
, but basically the assertion is testing that a <= b
implies that b >= a
for the defined comparison criteria (not the literal <=
and >=
operators on numbers). If the comparison fails, it means that the compareTo
method is not behaving as expected.
With the given information, one possibility is that VideoObj a
is "less than" VideoObj b
if a
's title is less than b
's title under lexicographical ordering.
Upvotes: 0