20
20
import static com .google .common .primitives .UnsignedBytes .min ;
21
21
import static com .google .common .truth .Truth .assertThat ;
22
22
import static com .google .common .truth .Truth .assertWithMessage ;
23
+ import static java .lang .Math .signum ;
23
24
import static org .junit .Assert .assertThrows ;
24
25
25
26
import com .google .common .collect .testing .Helpers ;
@@ -95,8 +96,8 @@ public void testCompare() {
95
96
byte y = VALUES [j ];
96
97
// note: spec requires only that the sign is the same
97
98
assertWithMessage (x + ", " + y )
98
- .that (Math . signum (UnsignedBytes .compare (x , y )))
99
- .isEqualTo (Math . signum (Integer .compare (i , j )));
99
+ .that (signum (UnsignedBytes .compare (x , y )))
100
+ .isEqualTo (signum (Integer .compare (i , j )));
100
101
}
101
102
}
102
103
}
@@ -264,7 +265,7 @@ public void testLexicographicalComparator() {
264
265
new byte [] {GREATEST , GREATEST },
265
266
new byte [] {GREATEST , GREATEST , GREATEST });
266
267
267
- // The Unsafe implementation if it's available. Otherwise, the Java implementation.
268
+ // The VarHandle, Unsafe, or Java implementation.
268
269
Comparator <byte []> comparator = UnsignedBytes .lexicographicalComparator ();
269
270
Helpers .testComparator (comparator , ordered );
270
271
assertThat (SerializableTester .reserialize (comparator )).isSameInstanceAs (comparator );
@@ -275,26 +276,42 @@ public void testLexicographicalComparator() {
275
276
assertThat (SerializableTester .reserialize (javaImpl )).isSameInstanceAs (javaImpl );
276
277
}
277
278
278
- public void testLexicographicalComparatorLongInputs () {
279
- Random rnd = new Random ();
280
- for (Comparator <byte []> comparator :
281
- Arrays .asList (
282
- UnsignedBytes .lexicographicalComparator (),
283
- UnsignedBytes .lexicographicalComparatorJavaImpl ())) {
284
- for (int trials = 10 ; trials -- > 0 ; ) {
285
- byte [] left = new byte [1 + rnd .nextInt (32 )];
286
- rnd .nextBytes (left );
287
- byte [] right = left .clone ();
288
- assertThat (comparator .compare (left , right )).isEqualTo (0 );
289
- int i = rnd .nextInt (left .length );
290
- left [i ] ^= (byte ) (1 + rnd .nextInt (255 ));
291
- assertThat (comparator .compare (left , right )).isNotEqualTo (0 );
292
- assertThat (UnsignedBytes .compare (left [i ], right [i ]) > 0 )
293
- .isEqualTo (comparator .compare (left , right ) > 0 );
294
- }
279
+ public void testLexicographicalComparatorLongPseudorandomInputs () {
280
+ Comparator <byte []> comparator1 = UnsignedBytes .lexicographicalComparator ();
281
+ Comparator <byte []> comparator2 = UnsignedBytes .lexicographicalComparatorJavaImpl ();
282
+ Random rnd = new Random (714958103 );
283
+ for (int trial = 0 ; trial < 100 ; trial ++) {
284
+ byte [] left = new byte [1 + rnd .nextInt (32 )];
285
+ rnd .nextBytes (left );
286
+ byte [] right = left .clone ();
287
+ assertThat (comparator1 .compare (left , right )).isEqualTo (0 );
288
+ assertThat (comparator2 .compare (left , right )).isEqualTo (0 );
289
+ int i = rnd .nextInt (left .length );
290
+ left [i ] ^= (byte ) (1 + rnd .nextInt (255 ));
291
+ assertThat (signum (comparator1 .compare (left , right )))
292
+ .isEqualTo (signum (UnsignedBytes .compare (left [i ], right [i ])));
293
+ assertThat (signum (comparator2 .compare (left , right )))
294
+ .isEqualTo (signum (UnsignedBytes .compare (left [i ], right [i ])));
295
295
}
296
296
}
297
297
298
+ public void testLexicographicalComparatorLongHandwrittenInputs () {
299
+ Comparator <byte []> comparator1 = UnsignedBytes .lexicographicalComparator ();
300
+ Comparator <byte []> comparator2 = UnsignedBytes .lexicographicalComparatorJavaImpl ();
301
+
302
+ /*
303
+ * These arrays are set up to test that the comparator compares bytes within a word in the
304
+ * correct order—in order words, that it doesn't mix up big-endian and little-endian. The first
305
+ * array has a smaller element at one index, and then the second array has a smaller elements at
306
+ * the next.
307
+ */
308
+ byte [] a0 = {0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 99 , 15 , 16 , 17 };
309
+ byte [] b0 = {0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 99 , 14 , 15 , 16 , 17 };
310
+
311
+ assertThat (comparator1 .compare (a0 , b0 )).isLessThan (0 );
312
+ assertThat (comparator2 .compare (a0 , b0 )).isLessThan (0 );
313
+ }
314
+
298
315
public void testSort () {
299
316
testSort (new byte [] {}, new byte [] {});
300
317
testSort (new byte [] {2 }, new byte [] {2 });
0 commit comments