1717import static com .google .common .truth .Truth .assertThat ;
1818
1919import com .google .common .primitives .UnsignedLong ;
20+ import com .google .testing .junit .testparameterinjector .TestParameter ;
2021import com .google .testing .junit .testparameterinjector .TestParameterInjector ;
2122import com .google .testing .junit .testparameterinjector .TestParameters ;
2223import java .math .BigInteger ;
@@ -28,20 +29,24 @@ public final class ComparisonFunctionsTest {
2829
2930 @ Test
3031 @ TestParameters ("{x: 1.0, y: 1, expect: 0}" )
32+ @ TestParameters ("{x: -0.0, y: 0, expect: 0}" )
3133 @ TestParameters ("{x: -2.1, y: -2, expect: -1}" )
3234 @ TestParameters ("{x: 2.1, y: -3, expect: 1}" )
3335 @ TestParameters ("{x: -9223372036854778809.0, y: 1, expect: -1}" )
3436 @ TestParameters ("{x: 9223372036854778809.0, y: 1, expect: 1}" )
37+ @ TestParameters ("{x: NaN, y: 0, expect: 1}" )
3538 public void compareDoubleInt (double x , long y , int expect ) {
3639 assertThat (ComparisonFunctions .compareDoubleInt (x , y )).isEqualTo (expect );
3740 assertThat (ComparisonFunctions .compareIntDouble (y , x )).isEqualTo (-1 * expect );
3841 }
3942
4043 @ Test
4144 @ TestParameters ("{x: 1.0, y: 1, expect: 0}" )
45+ @ TestParameters ("{x: -0.0, y: 0, expect: 0}" )
4246 @ TestParameters ("{x: -2.1, y: 0, expect: -1}" )
4347 @ TestParameters ("{x: 2.1, y: 1, expect: 1}" )
4448 @ TestParameters ("{x: 18446744073709653620.1, y: 1, expect: 1}" )
49+ @ TestParameters ("{x: NaN, y: 0, expect: 1}" )
4550 public void compareDoubleUint (double x , long y , int expect ) {
4651 UnsignedLong uy = UnsignedLong .valueOf (y );
4752 assertThat (ComparisonFunctions .compareDoubleUint (x , uy )).isEqualTo (expect );
@@ -93,4 +98,46 @@ public void numericCompareUintInt(long x, long y, int expect) {
9398 assertThat (ComparisonFunctions .numericCompare (ux , y )).isEqualTo (expect );
9499 assertThat (ComparisonFunctions .numericCompare (y , ux )).isEqualTo (-1 * expect );
95100 }
101+
102+ @ Test
103+ @ TestParameters ("{x: -0.0, y: 0.0, expect: 0}" )
104+ @ TestParameters ("{x: 1.0, y: 2.0, expect: -1}" )
105+ @ TestParameters ("{x: NaN, y: 1.0, expect: 1}" )
106+ @ TestParameters ("{x: NaN, y: NaN, expect: 0}" )
107+ public void numericCompare_doubleDouble (double x , double y , int expect ) {
108+ assertThat (ComparisonFunctions .numericCompare (x , y )).isEqualTo (expect );
109+ assertThat (ComparisonFunctions .numericCompare (y , x )).isEqualTo (-1 * expect );
110+ }
111+
112+ @ SuppressWarnings ("ImmutableEnumChecker" ) // Test only
113+ private enum NumericEqualsTestCase {
114+ NEGATIVE_ZERO_AND_POSITIVE_ZERO (-0.0 , 0.0 , true ),
115+ NEGATIVE_ZERO_AND_LONG_ZERO (-0.0 , 0L , true ),
116+ NEGATIVE_ZERO_AND_ULONG_ZERO (-0.0 , UnsignedLong .ZERO , true ),
117+ DOUBLE_ONE_AND_LONG_ONE (1.0 , 1L , true ),
118+ DOUBLE_ONE_AND_ULONG_ONE (1.0 , UnsignedLong .ONE , true ),
119+ DISTINCT_DOUBLES (1.0 , 2.0 , false ),
120+ DISTINCT_DOUBLE_AND_LONG (1.0 , 2L , false ),
121+ NAN_AND_NAN (Double .NaN , Double .NaN , false ),
122+ NAN_AND_LONG_ZERO (Double .NaN , 0L , false ),
123+ NAN_AND_ULONG_ZERO (Double .NaN , UnsignedLong .ZERO , false );
124+
125+ final Number x ;
126+ final Number y ;
127+ final boolean expected ;
128+
129+ NumericEqualsTestCase (Number x , Number y , boolean expected ) {
130+ this .x = x ;
131+ this .y = y ;
132+ this .expected = expected ;
133+ }
134+ }
135+
136+ @ Test
137+ public void numericEquals (@ TestParameter NumericEqualsTestCase testCase ) {
138+ assertThat (ComparisonFunctions .numericEquals (testCase .x , testCase .y ))
139+ .isEqualTo (testCase .expected );
140+ assertThat (ComparisonFunctions .numericEquals (testCase .y , testCase .x ))
141+ .isEqualTo (testCase .expected );
142+ }
96143}
0 commit comments