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 ;
23+ import java .math .BigDecimal ;
2224import java .math .BigInteger ;
2325import org .junit .Test ;
2426import org .junit .runner .RunWith ;
@@ -28,20 +30,24 @@ public final class ComparisonFunctionsTest {
2830
2931 @ Test
3032 @ TestParameters ("{x: 1.0, y: 1, expect: 0}" )
33+ @ TestParameters ("{x: -0.0, y: 0, expect: 0}" )
3134 @ TestParameters ("{x: -2.1, y: -2, expect: -1}" )
3235 @ TestParameters ("{x: 2.1, y: -3, expect: 1}" )
3336 @ TestParameters ("{x: -9223372036854778809.0, y: 1, expect: -1}" )
3437 @ TestParameters ("{x: 9223372036854778809.0, y: 1, expect: 1}" )
38+ @ TestParameters ("{x: NaN, y: 0, expect: 1}" )
3539 public void compareDoubleInt (double x , long y , int expect ) {
3640 assertThat (ComparisonFunctions .compareDoubleInt (x , y )).isEqualTo (expect );
3741 assertThat (ComparisonFunctions .compareIntDouble (y , x )).isEqualTo (-1 * expect );
3842 }
3943
4044 @ Test
4145 @ TestParameters ("{x: 1.0, y: 1, expect: 0}" )
46+ @ TestParameters ("{x: -0.0, y: 0, expect: 0}" )
4247 @ TestParameters ("{x: -2.1, y: 0, expect: -1}" )
4348 @ TestParameters ("{x: 2.1, y: 1, expect: 1}" )
4449 @ TestParameters ("{x: 18446744073709653620.1, y: 1, expect: 1}" )
50+ @ TestParameters ("{x: NaN, y: 0, expect: 1}" )
4551 public void compareDoubleUint (double x , long y , int expect ) {
4652 UnsignedLong uy = UnsignedLong .valueOf (y );
4753 assertThat (ComparisonFunctions .compareDoubleUint (x , uy )).isEqualTo (expect );
@@ -93,4 +99,49 @@ public void numericCompareUintInt(long x, long y, int expect) {
9399 assertThat (ComparisonFunctions .numericCompare (ux , y )).isEqualTo (expect );
94100 assertThat (ComparisonFunctions .numericCompare (y , ux )).isEqualTo (-1 * expect );
95101 }
102+
103+ @ Test
104+ @ TestParameters ("{x: -0.0, y: 0.0, expect: 0}" )
105+ @ TestParameters ("{x: 1.0, y: 2.0, expect: -1}" )
106+ @ TestParameters ("{x: NaN, y: 1.0, expect: 1}" )
107+ @ TestParameters ("{x: NaN, y: NaN, expect: 0}" )
108+ public void numericCompareDoubleDouble (double x , double y , int expect ) {
109+ assertThat (ComparisonFunctions .numericCompare (x , y )).isEqualTo (expect );
110+ assertThat (ComparisonFunctions .numericCompare (y , x )).isEqualTo (-1 * expect );
111+ }
112+
113+ @ SuppressWarnings ("ImmutableEnumChecker" ) // Test only
114+ private enum NumericEqualsTestCase {
115+ NEGATIVE_ZERO_AND_POSITIVE_ZERO (-0.0 , 0.0 , true ),
116+ NEGATIVE_ZERO_AND_LONG_ZERO (-0.0 , 0L , true ),
117+ NEGATIVE_ZERO_AND_ULONG_ZERO (-0.0 , UnsignedLong .ZERO , true ),
118+ DOUBLE_ONE_AND_LONG_ONE (1.0 , 1L , true ),
119+ DOUBLE_ONE_AND_ULONG_ONE (1.0 , UnsignedLong .ONE , true ),
120+ DISTINCT_DOUBLES (1.0 , 2.0 , false ),
121+ DISTINCT_DOUBLE_AND_LONG (1.0 , 2L , false ),
122+ NAN_AND_NAN (Double .NaN , Double .NaN , false ),
123+ NAN_AND_LONG_ZERO (Double .NaN , 0L , false ),
124+ NAN_AND_ULONG_ZERO (Double .NaN , UnsignedLong .ZERO , false ),
125+ SHORT_AND_LONG ((short ) 1 , 1L , false ),
126+ SHORT_AND_NAN ((short ) 1 , Double .NaN , false ),
127+ BIG_DECIMAL_AND_DOUBLE (BigDecimal .ONE , 1.0 , false );
128+
129+ private final Number x ;
130+ private final Number y ;
131+ private final boolean expected ;
132+
133+ NumericEqualsTestCase (Number x , Number y , boolean expected ) {
134+ this .x = x ;
135+ this .y = y ;
136+ this .expected = expected ;
137+ }
138+ }
139+
140+ @ Test
141+ public void numericEquals (@ TestParameter NumericEqualsTestCase testCase ) {
142+ assertThat (ComparisonFunctions .numericEquals (testCase .x , testCase .y ))
143+ .isEqualTo (testCase .expected );
144+ assertThat (ComparisonFunctions .numericEquals (testCase .y , testCase .x ))
145+ .isEqualTo (testCase .expected );
146+ }
96147}
0 commit comments