• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.primitives;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static com.google.common.truth.Truth.assertWithMessage;
21 
22 import com.google.common.annotations.GwtCompatible;
23 import com.google.common.annotations.GwtIncompatible;
24 import com.google.common.annotations.J2ktIncompatible;
25 import com.google.common.base.Converter;
26 import com.google.common.collect.testing.Helpers;
27 import com.google.common.testing.NullPointerTester;
28 import com.google.common.testing.SerializableTester;
29 import java.util.Arrays;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.Comparator;
33 import java.util.List;
34 import java.util.Random;
35 import junit.framework.TestCase;
36 import org.checkerframework.checker.nullness.qual.Nullable;
37 
38 /**
39  * Unit test for {@link Shorts}.
40  *
41  * @author Kevin Bourrillion
42  */
43 @ElementTypesAreNonnullByDefault
44 @GwtCompatible(emulated = true)
45 public class ShortsTest extends TestCase {
46   private static final short[] EMPTY = {};
47   private static final short[] ARRAY1 = {(short) 1};
48   private static final short[] ARRAY234 = {(short) 2, (short) 3, (short) 4};
49 
50   private static final short LEAST = Short.MIN_VALUE;
51   private static final short GREATEST = Short.MAX_VALUE;
52 
53   private static final short[] VALUES = {LEAST, (short) -1, (short) 0, (short) 1, GREATEST};
54 
testHashCode()55   public void testHashCode() {
56     for (short value : VALUES) {
57       assertThat(Shorts.hashCode(value)).isEqualTo(((Short) value).hashCode());
58     }
59   }
60 
testCheckedCast()61   public void testCheckedCast() {
62     for (short value : VALUES) {
63       assertThat(Shorts.checkedCast((long) value)).isEqualTo(value);
64     }
65     assertCastFails(GREATEST + 1L);
66     assertCastFails(LEAST - 1L);
67     assertCastFails(Long.MAX_VALUE);
68     assertCastFails(Long.MIN_VALUE);
69   }
70 
testSaturatedCast()71   public void testSaturatedCast() {
72     for (short value : VALUES) {
73       assertThat(Shorts.saturatedCast((long) value)).isEqualTo(value);
74     }
75     assertThat(Shorts.saturatedCast(GREATEST + 1L)).isEqualTo(GREATEST);
76     assertThat(Shorts.saturatedCast(LEAST - 1L)).isEqualTo(LEAST);
77     assertThat(Shorts.saturatedCast(Long.MAX_VALUE)).isEqualTo(GREATEST);
78     assertThat(Shorts.saturatedCast(Long.MIN_VALUE)).isEqualTo(LEAST);
79   }
80 
assertCastFails(long value)81   private static void assertCastFails(long value) {
82     try {
83       Shorts.checkedCast(value);
84       fail("Cast to short should have failed: " + value);
85     } catch (IllegalArgumentException ex) {
86       assertWithMessage(value + " not found in exception text: " + ex.getMessage())
87           .that(ex.getMessage().contains(String.valueOf(value)))
88           .isTrue();
89     }
90   }
91 
testCompare()92   public void testCompare() {
93     for (short x : VALUES) {
94       for (short y : VALUES) {
95         // Only compare the sign of the result of compareTo().
96         int expected = Short.valueOf(x).compareTo(y);
97         int actual = Shorts.compare(x, y);
98         if (expected == 0) {
99           assertWithMessage(x + ", " + y).that(actual).isEqualTo(expected);
100         } else if (expected < 0) {
101           assertWithMessage(x + ", " + y + " (expected: " + expected + ", actual" + actual + ")")
102               .that(actual < 0)
103               .isTrue();
104         } else {
105           assertWithMessage(x + ", " + y + " (expected: " + expected + ", actual" + actual + ")")
106               .that(actual > 0)
107               .isTrue();
108         }
109       }
110     }
111   }
112 
testContains()113   public void testContains() {
114     assertThat(Shorts.contains(EMPTY, (short) 1)).isFalse();
115     assertThat(Shorts.contains(ARRAY1, (short) 2)).isFalse();
116     assertThat(Shorts.contains(ARRAY234, (short) 1)).isFalse();
117     assertThat(Shorts.contains(new short[] {(short) -1}, (short) -1)).isTrue();
118     assertThat(Shorts.contains(ARRAY234, (short) 2)).isTrue();
119     assertThat(Shorts.contains(ARRAY234, (short) 3)).isTrue();
120     assertThat(Shorts.contains(ARRAY234, (short) 4)).isTrue();
121   }
122 
testIndexOf()123   public void testIndexOf() {
124     assertThat(Shorts.indexOf(EMPTY, (short) 1)).isEqualTo(-1);
125     assertThat(Shorts.indexOf(ARRAY1, (short) 2)).isEqualTo(-1);
126     assertThat(Shorts.indexOf(ARRAY234, (short) 1)).isEqualTo(-1);
127     assertThat(Shorts.indexOf(new short[] {(short) -1}, (short) -1)).isEqualTo(0);
128     assertThat(Shorts.indexOf(ARRAY234, (short) 2)).isEqualTo(0);
129     assertThat(Shorts.indexOf(ARRAY234, (short) 3)).isEqualTo(1);
130     assertThat(Shorts.indexOf(ARRAY234, (short) 4)).isEqualTo(2);
131     assertThat(Shorts.indexOf(new short[] {(short) 2, (short) 3, (short) 2, (short) 3}, (short) 3))
132         .isEqualTo(1);
133   }
134 
testIndexOf_arrayTarget()135   public void testIndexOf_arrayTarget() {
136     assertThat(Shorts.indexOf(EMPTY, EMPTY)).isEqualTo(0);
137     assertThat(Shorts.indexOf(ARRAY234, EMPTY)).isEqualTo(0);
138     assertThat(Shorts.indexOf(EMPTY, ARRAY234)).isEqualTo(-1);
139     assertThat(Shorts.indexOf(ARRAY234, ARRAY1)).isEqualTo(-1);
140     assertThat(Shorts.indexOf(ARRAY1, ARRAY234)).isEqualTo(-1);
141     assertThat(Shorts.indexOf(ARRAY1, ARRAY1)).isEqualTo(0);
142     assertThat(Shorts.indexOf(ARRAY234, ARRAY234)).isEqualTo(0);
143     assertThat(Shorts.indexOf(ARRAY234, new short[] {(short) 2, (short) 3})).isEqualTo(0);
144     assertThat(Shorts.indexOf(ARRAY234, new short[] {(short) 3, (short) 4})).isEqualTo(1);
145     assertThat(Shorts.indexOf(ARRAY234, new short[] {(short) 3})).isEqualTo(1);
146     assertThat(Shorts.indexOf(ARRAY234, new short[] {(short) 4})).isEqualTo(2);
147     assertThat(
148             Shorts.indexOf(
149                 new short[] {(short) 2, (short) 3, (short) 3, (short) 3, (short) 3},
150                 new short[] {(short) 3}))
151         .isEqualTo(1);
152     assertThat(
153             Shorts.indexOf(
154                 new short[] {
155                   (short) 2, (short) 3, (short) 2, (short) 3, (short) 4, (short) 2, (short) 3
156                 },
157                 new short[] {(short) 2, (short) 3, (short) 4}))
158         .isEqualTo(2);
159     assertThat(
160             Shorts.indexOf(
161                 new short[] {
162                   (short) 2, (short) 2, (short) 3, (short) 4, (short) 2, (short) 3, (short) 4
163                 },
164                 new short[] {(short) 2, (short) 3, (short) 4}))
165         .isEqualTo(1);
166     assertThat(
167             Shorts.indexOf(
168                 new short[] {(short) 4, (short) 3, (short) 2},
169                 new short[] {(short) 2, (short) 3, (short) 4}))
170         .isEqualTo(-1);
171   }
172 
testLastIndexOf()173   public void testLastIndexOf() {
174     assertThat(Shorts.lastIndexOf(EMPTY, (short) 1)).isEqualTo(-1);
175     assertThat(Shorts.lastIndexOf(ARRAY1, (short) 2)).isEqualTo(-1);
176     assertThat(Shorts.lastIndexOf(ARRAY234, (short) 1)).isEqualTo(-1);
177     assertThat(Shorts.lastIndexOf(new short[] {(short) -1}, (short) -1)).isEqualTo(0);
178     assertThat(Shorts.lastIndexOf(ARRAY234, (short) 2)).isEqualTo(0);
179     assertThat(Shorts.lastIndexOf(ARRAY234, (short) 3)).isEqualTo(1);
180     assertThat(Shorts.lastIndexOf(ARRAY234, (short) 4)).isEqualTo(2);
181     assertThat(
182             Shorts.lastIndexOf(new short[] {(short) 2, (short) 3, (short) 2, (short) 3}, (short) 3))
183         .isEqualTo(3);
184   }
185 
186   @GwtIncompatible
testMax_noArgs()187   public void testMax_noArgs() {
188     try {
189       Shorts.max();
190       fail();
191     } catch (IllegalArgumentException expected) {
192     }
193   }
194 
testMax()195   public void testMax() {
196     assertThat(Shorts.max(LEAST)).isEqualTo(LEAST);
197     assertThat(Shorts.max(GREATEST)).isEqualTo(GREATEST);
198     assertThat(
199             Shorts.max((short) 8, (short) 6, (short) 7, (short) 5, (short) 3, (short) 0, (short) 9))
200         .isEqualTo((short) 9);
201   }
202 
203   @GwtIncompatible
testMin_noArgs()204   public void testMin_noArgs() {
205     try {
206       Shorts.min();
207       fail();
208     } catch (IllegalArgumentException expected) {
209     }
210   }
211 
testMin()212   public void testMin() {
213     assertThat(Shorts.min(LEAST)).isEqualTo(LEAST);
214     assertThat(Shorts.min(GREATEST)).isEqualTo(GREATEST);
215     assertThat(
216             Shorts.min((short) 8, (short) 6, (short) 7, (short) 5, (short) 3, (short) 0, (short) 9))
217         .isEqualTo((short) 0);
218   }
219 
testConstrainToRange()220   public void testConstrainToRange() {
221     assertThat(Shorts.constrainToRange((short) 1, (short) 0, (short) 5)).isEqualTo((short) 1);
222     assertThat(Shorts.constrainToRange((short) 1, (short) 1, (short) 5)).isEqualTo((short) 1);
223     assertThat(Shorts.constrainToRange((short) 1, (short) 3, (short) 5)).isEqualTo((short) 3);
224     assertThat(Shorts.constrainToRange((short) 0, (short) -5, (short) -1)).isEqualTo((short) -1);
225     assertThat(Shorts.constrainToRange((short) 5, (short) 2, (short) 2)).isEqualTo((short) 2);
226     try {
227       Shorts.constrainToRange((short) 1, (short) 3, (short) 2);
228       fail();
229     } catch (IllegalArgumentException expected) {
230     }
231   }
232 
testConcat()233   public void testConcat() {
234     assertThat(Shorts.concat()).isEqualTo(EMPTY);
235     assertThat(Shorts.concat(EMPTY)).isEqualTo(EMPTY);
236     assertThat(Shorts.concat(EMPTY, EMPTY, EMPTY)).isEqualTo(EMPTY);
237     assertThat(Shorts.concat(ARRAY1)).isEqualTo(ARRAY1);
238     assertThat(Shorts.concat(ARRAY1)).isNotSameInstanceAs(ARRAY1);
239     assertThat(Shorts.concat(EMPTY, ARRAY1, EMPTY)).isEqualTo(ARRAY1);
240     assertThat(Shorts.concat(ARRAY1, ARRAY1, ARRAY1))
241         .isEqualTo(new short[] {(short) 1, (short) 1, (short) 1});
242     assertThat(Shorts.concat(ARRAY1, ARRAY234))
243         .isEqualTo(new short[] {(short) 1, (short) 2, (short) 3, (short) 4});
244   }
245 
246   @GwtIncompatible // different overflow behavior; could probably be made to work by using ~~
testConcat_overflow_negative()247   public void testConcat_overflow_negative() {
248     int dim1 = 1 << 16;
249     int dim2 = 1 << 15;
250     assertThat(dim1 * dim2).isLessThan(0);
251     testConcatOverflow(dim1, dim2);
252   }
253 
254   @GwtIncompatible // different overflow behavior; could probably be made to work by using ~~
testConcat_overflow_nonNegative()255   public void testConcat_overflow_nonNegative() {
256     int dim1 = 1 << 16;
257     int dim2 = 1 << 16;
258     assertThat(dim1 * dim2).isAtLeast(0);
259     testConcatOverflow(dim1, dim2);
260   }
261 
testConcatOverflow(int arraysDim1, int arraysDim2)262   private static void testConcatOverflow(int arraysDim1, int arraysDim2) {
263     assertThat((long) arraysDim1 * arraysDim2).isNotEqualTo((long) (arraysDim1 * arraysDim2));
264 
265     short[][] arrays = new short[arraysDim1][];
266     // it's shared to avoid using too much memory in tests
267     short[] sharedArray = new short[arraysDim2];
268     Arrays.fill(arrays, sharedArray);
269 
270     try {
271       Shorts.concat(arrays);
272       fail();
273     } catch (IllegalArgumentException expected) {
274     }
275   }
276 
277   @GwtIncompatible // Shorts.toByteArray
testToByteArray()278   public void testToByteArray() {
279     assertThat(Shorts.toByteArray((short) 0x2345)).isEqualTo(new byte[] {0x23, 0x45});
280     assertThat(Shorts.toByteArray((short) 0xFEDC)).isEqualTo(new byte[] {(byte) 0xFE, (byte) 0xDC});
281   }
282 
283   @GwtIncompatible // Shorts.fromByteArray
testFromByteArray()284   public void testFromByteArray() {
285     assertThat(Shorts.fromByteArray(new byte[] {0x23, 0x45})).isEqualTo((short) 0x2345);
286     assertThat(Shorts.fromByteArray(new byte[] {(byte) 0xFE, (byte) 0xDC}))
287         .isEqualTo((short) 0xFEDC);
288   }
289 
290   @GwtIncompatible // Shorts.fromByteArray
testFromByteArrayFails()291   public void testFromByteArrayFails() {
292     try {
293       Shorts.fromByteArray(new byte[] {0x01});
294       fail();
295     } catch (IllegalArgumentException expected) {
296     }
297   }
298 
299   @GwtIncompatible // Shorts.fromBytes
testFromBytes()300   public void testFromBytes() {
301     assertThat(Shorts.fromBytes((byte) 0x23, (byte) 0x45)).isEqualTo((short) 0x2345);
302     assertThat(Shorts.fromBytes((byte) 0xFE, (byte) 0xDC)).isEqualTo((short) 0xFEDC);
303   }
304 
305   @GwtIncompatible // Shorts.fromByteArray, Shorts.toByteArray
testByteArrayRoundTrips()306   public void testByteArrayRoundTrips() {
307     Random r = new Random(5);
308     byte[] b = new byte[Shorts.BYTES];
309 
310     // total overkill, but, it takes 0.1 sec so why not...
311     for (int i = 0; i < 10000; i++) {
312       short num = (short) r.nextInt();
313       assertThat(Shorts.fromByteArray(Shorts.toByteArray(num))).isEqualTo(num);
314 
315       r.nextBytes(b);
316       assertThat(Shorts.toByteArray(Shorts.fromByteArray(b))).isEqualTo(b);
317     }
318   }
319 
testEnsureCapacity()320   public void testEnsureCapacity() {
321     assertThat(Shorts.ensureCapacity(EMPTY, 0, 1)).isSameInstanceAs(EMPTY);
322     assertThat(Shorts.ensureCapacity(ARRAY1, 0, 1)).isSameInstanceAs(ARRAY1);
323     assertThat(Shorts.ensureCapacity(ARRAY1, 1, 1)).isSameInstanceAs(ARRAY1);
324     assertThat(Shorts.ensureCapacity(ARRAY1, 2, 1))
325         .isEqualTo(new short[] {(short) 1, (short) 0, (short) 0});
326   }
327 
testEnsureCapacity_fail()328   public void testEnsureCapacity_fail() {
329     try {
330       Shorts.ensureCapacity(ARRAY1, -1, 1);
331       fail();
332     } catch (IllegalArgumentException expected) {
333     }
334     try {
335       // notice that this should even fail when no growth was needed
336       Shorts.ensureCapacity(ARRAY1, 1, -1);
337       fail();
338     } catch (IllegalArgumentException expected) {
339     }
340   }
341 
testJoin()342   public void testJoin() {
343     assertThat(Shorts.join(",", EMPTY)).isEmpty();
344     assertThat(Shorts.join(",", ARRAY1)).isEqualTo("1");
345     assertThat(Shorts.join(",", (short) 1, (short) 2)).isEqualTo("1,2");
346     assertThat(Shorts.join("", (short) 1, (short) 2, (short) 3)).isEqualTo("123");
347   }
348 
testLexicographicalComparator()349   public void testLexicographicalComparator() {
350     List<short[]> ordered =
351         Arrays.asList(
352             new short[] {},
353             new short[] {LEAST},
354             new short[] {LEAST, LEAST},
355             new short[] {LEAST, (short) 1},
356             new short[] {(short) 1},
357             new short[] {(short) 1, LEAST},
358             new short[] {GREATEST, GREATEST - (short) 1},
359             new short[] {GREATEST, GREATEST},
360             new short[] {GREATEST, GREATEST, GREATEST});
361 
362     Comparator<short[]> comparator = Shorts.lexicographicalComparator();
363     Helpers.testComparator(comparator, ordered);
364   }
365 
366   @J2ktIncompatible
367   @GwtIncompatible // SerializableTester
testLexicographicalComparatorSerializable()368   public void testLexicographicalComparatorSerializable() {
369     Comparator<short[]> comparator = Shorts.lexicographicalComparator();
370     assertThat(SerializableTester.reserialize(comparator)).isSameInstanceAs(comparator);
371   }
372 
testReverse()373   public void testReverse() {
374     testReverse(new short[] {}, new short[] {});
375     testReverse(new short[] {1}, new short[] {1});
376     testReverse(new short[] {1, 2}, new short[] {2, 1});
377     testReverse(new short[] {3, 1, 1}, new short[] {1, 1, 3});
378     testReverse(new short[] {-1, 1, -2, 2}, new short[] {2, -2, 1, -1});
379   }
380 
testReverse(short[] input, short[] expectedOutput)381   private static void testReverse(short[] input, short[] expectedOutput) {
382     input = Arrays.copyOf(input, input.length);
383     Shorts.reverse(input);
384     assertThat(input).isEqualTo(expectedOutput);
385   }
386 
testReverse( short[] input, int fromIndex, int toIndex, short[] expectedOutput)387   private static void testReverse(
388       short[] input, int fromIndex, int toIndex, short[] expectedOutput) {
389     input = Arrays.copyOf(input, input.length);
390     Shorts.reverse(input, fromIndex, toIndex);
391     assertThat(input).isEqualTo(expectedOutput);
392   }
393 
testReverseIndexed()394   public void testReverseIndexed() {
395     testReverse(new short[] {}, 0, 0, new short[] {});
396     testReverse(new short[] {1}, 0, 1, new short[] {1});
397     testReverse(new short[] {1, 2}, 0, 2, new short[] {2, 1});
398     testReverse(new short[] {3, 1, 1}, 0, 2, new short[] {1, 3, 1});
399     testReverse(new short[] {3, 1, 1}, 0, 1, new short[] {3, 1, 1});
400     testReverse(new short[] {-1, 1, -2, 2}, 1, 3, new short[] {-1, -2, 1, 2});
401   }
402 
testRotate(short[] input, int distance, short[] expectedOutput)403   private static void testRotate(short[] input, int distance, short[] expectedOutput) {
404     input = Arrays.copyOf(input, input.length);
405     Shorts.rotate(input, distance);
406     assertThat(input).isEqualTo(expectedOutput);
407   }
408 
testRotate( short[] input, int distance, int fromIndex, int toIndex, short[] expectedOutput)409   private static void testRotate(
410       short[] input, int distance, int fromIndex, int toIndex, short[] expectedOutput) {
411     input = Arrays.copyOf(input, input.length);
412     Shorts.rotate(input, distance, fromIndex, toIndex);
413     assertThat(input).isEqualTo(expectedOutput);
414   }
415 
testRotate()416   public void testRotate() {
417     testRotate(new short[] {}, -1, new short[] {});
418     testRotate(new short[] {}, 0, new short[] {});
419     testRotate(new short[] {}, 1, new short[] {});
420 
421     testRotate(new short[] {1}, -2, new short[] {1});
422     testRotate(new short[] {1}, -1, new short[] {1});
423     testRotate(new short[] {1}, 0, new short[] {1});
424     testRotate(new short[] {1}, 1, new short[] {1});
425     testRotate(new short[] {1}, 2, new short[] {1});
426 
427     testRotate(new short[] {1, 2}, -3, new short[] {2, 1});
428     testRotate(new short[] {1, 2}, -1, new short[] {2, 1});
429     testRotate(new short[] {1, 2}, -2, new short[] {1, 2});
430     testRotate(new short[] {1, 2}, 0, new short[] {1, 2});
431     testRotate(new short[] {1, 2}, 1, new short[] {2, 1});
432     testRotate(new short[] {1, 2}, 2, new short[] {1, 2});
433     testRotate(new short[] {1, 2}, 3, new short[] {2, 1});
434 
435     testRotate(new short[] {1, 2, 3}, -5, new short[] {3, 1, 2});
436     testRotate(new short[] {1, 2, 3}, -4, new short[] {2, 3, 1});
437     testRotate(new short[] {1, 2, 3}, -3, new short[] {1, 2, 3});
438     testRotate(new short[] {1, 2, 3}, -2, new short[] {3, 1, 2});
439     testRotate(new short[] {1, 2, 3}, -1, new short[] {2, 3, 1});
440     testRotate(new short[] {1, 2, 3}, 0, new short[] {1, 2, 3});
441     testRotate(new short[] {1, 2, 3}, 1, new short[] {3, 1, 2});
442     testRotate(new short[] {1, 2, 3}, 2, new short[] {2, 3, 1});
443     testRotate(new short[] {1, 2, 3}, 3, new short[] {1, 2, 3});
444     testRotate(new short[] {1, 2, 3}, 4, new short[] {3, 1, 2});
445     testRotate(new short[] {1, 2, 3}, 5, new short[] {2, 3, 1});
446 
447     testRotate(new short[] {1, 2, 3, 4}, -9, new short[] {2, 3, 4, 1});
448     testRotate(new short[] {1, 2, 3, 4}, -5, new short[] {2, 3, 4, 1});
449     testRotate(new short[] {1, 2, 3, 4}, -1, new short[] {2, 3, 4, 1});
450     testRotate(new short[] {1, 2, 3, 4}, 0, new short[] {1, 2, 3, 4});
451     testRotate(new short[] {1, 2, 3, 4}, 1, new short[] {4, 1, 2, 3});
452     testRotate(new short[] {1, 2, 3, 4}, 5, new short[] {4, 1, 2, 3});
453     testRotate(new short[] {1, 2, 3, 4}, 9, new short[] {4, 1, 2, 3});
454 
455     testRotate(new short[] {1, 2, 3, 4, 5}, -6, new short[] {2, 3, 4, 5, 1});
456     testRotate(new short[] {1, 2, 3, 4, 5}, -4, new short[] {5, 1, 2, 3, 4});
457     testRotate(new short[] {1, 2, 3, 4, 5}, -3, new short[] {4, 5, 1, 2, 3});
458     testRotate(new short[] {1, 2, 3, 4, 5}, -1, new short[] {2, 3, 4, 5, 1});
459     testRotate(new short[] {1, 2, 3, 4, 5}, 0, new short[] {1, 2, 3, 4, 5});
460     testRotate(new short[] {1, 2, 3, 4, 5}, 1, new short[] {5, 1, 2, 3, 4});
461     testRotate(new short[] {1, 2, 3, 4, 5}, 3, new short[] {3, 4, 5, 1, 2});
462     testRotate(new short[] {1, 2, 3, 4, 5}, 4, new short[] {2, 3, 4, 5, 1});
463     testRotate(new short[] {1, 2, 3, 4, 5}, 6, new short[] {5, 1, 2, 3, 4});
464   }
465 
testRotateIndexed()466   public void testRotateIndexed() {
467     testRotate(new short[] {}, 0, 0, 0, new short[] {});
468 
469     testRotate(new short[] {1}, 0, 0, 1, new short[] {1});
470     testRotate(new short[] {1}, 1, 0, 1, new short[] {1});
471     testRotate(new short[] {1}, 1, 1, 1, new short[] {1});
472 
473     // Rotate the central 5 elements, leaving the ends as-is
474     testRotate(new short[] {0, 1, 2, 3, 4, 5, 6}, -6, 1, 6, new short[] {0, 2, 3, 4, 5, 1, 6});
475     testRotate(new short[] {0, 1, 2, 3, 4, 5, 6}, -1, 1, 6, new short[] {0, 2, 3, 4, 5, 1, 6});
476     testRotate(new short[] {0, 1, 2, 3, 4, 5, 6}, 0, 1, 6, new short[] {0, 1, 2, 3, 4, 5, 6});
477     testRotate(new short[] {0, 1, 2, 3, 4, 5, 6}, 5, 1, 6, new short[] {0, 1, 2, 3, 4, 5, 6});
478     testRotate(new short[] {0, 1, 2, 3, 4, 5, 6}, 14, 1, 6, new short[] {0, 2, 3, 4, 5, 1, 6});
479 
480     // Rotate the first three elements
481     testRotate(new short[] {0, 1, 2, 3, 4, 5, 6}, -2, 0, 3, new short[] {2, 0, 1, 3, 4, 5, 6});
482     testRotate(new short[] {0, 1, 2, 3, 4, 5, 6}, -1, 0, 3, new short[] {1, 2, 0, 3, 4, 5, 6});
483     testRotate(new short[] {0, 1, 2, 3, 4, 5, 6}, 0, 0, 3, new short[] {0, 1, 2, 3, 4, 5, 6});
484     testRotate(new short[] {0, 1, 2, 3, 4, 5, 6}, 1, 0, 3, new short[] {2, 0, 1, 3, 4, 5, 6});
485     testRotate(new short[] {0, 1, 2, 3, 4, 5, 6}, 2, 0, 3, new short[] {1, 2, 0, 3, 4, 5, 6});
486 
487     // Rotate the last four elements
488     testRotate(new short[] {0, 1, 2, 3, 4, 5, 6}, -6, 3, 7, new short[] {0, 1, 2, 5, 6, 3, 4});
489     testRotate(new short[] {0, 1, 2, 3, 4, 5, 6}, -5, 3, 7, new short[] {0, 1, 2, 4, 5, 6, 3});
490     testRotate(new short[] {0, 1, 2, 3, 4, 5, 6}, -4, 3, 7, new short[] {0, 1, 2, 3, 4, 5, 6});
491     testRotate(new short[] {0, 1, 2, 3, 4, 5, 6}, -3, 3, 7, new short[] {0, 1, 2, 6, 3, 4, 5});
492     testRotate(new short[] {0, 1, 2, 3, 4, 5, 6}, -2, 3, 7, new short[] {0, 1, 2, 5, 6, 3, 4});
493     testRotate(new short[] {0, 1, 2, 3, 4, 5, 6}, -1, 3, 7, new short[] {0, 1, 2, 4, 5, 6, 3});
494     testRotate(new short[] {0, 1, 2, 3, 4, 5, 6}, 0, 3, 7, new short[] {0, 1, 2, 3, 4, 5, 6});
495     testRotate(new short[] {0, 1, 2, 3, 4, 5, 6}, 1, 3, 7, new short[] {0, 1, 2, 6, 3, 4, 5});
496     testRotate(new short[] {0, 1, 2, 3, 4, 5, 6}, 2, 3, 7, new short[] {0, 1, 2, 5, 6, 3, 4});
497     testRotate(new short[] {0, 1, 2, 3, 4, 5, 6}, 3, 3, 7, new short[] {0, 1, 2, 4, 5, 6, 3});
498   }
499 
testSortDescending()500   public void testSortDescending() {
501     testSortDescending(new short[] {}, new short[] {});
502     testSortDescending(new short[] {1}, new short[] {1});
503     testSortDescending(new short[] {1, 2}, new short[] {2, 1});
504     testSortDescending(new short[] {1, 3, 1}, new short[] {3, 1, 1});
505     testSortDescending(new short[] {-1, 1, -2, 2}, new short[] {2, 1, -1, -2});
506   }
507 
testSortDescending(short[] input, short[] expectedOutput)508   private static void testSortDescending(short[] input, short[] expectedOutput) {
509     input = Arrays.copyOf(input, input.length);
510     Shorts.sortDescending(input);
511     assertThat(input).isEqualTo(expectedOutput);
512   }
513 
testSortDescending( short[] input, int fromIndex, int toIndex, short[] expectedOutput)514   private static void testSortDescending(
515       short[] input, int fromIndex, int toIndex, short[] expectedOutput) {
516     input = Arrays.copyOf(input, input.length);
517     Shorts.sortDescending(input, fromIndex, toIndex);
518     assertThat(input).isEqualTo(expectedOutput);
519   }
520 
testSortDescendingIndexed()521   public void testSortDescendingIndexed() {
522     testSortDescending(new short[] {}, 0, 0, new short[] {});
523     testSortDescending(new short[] {1}, 0, 1, new short[] {1});
524     testSortDescending(new short[] {1, 2}, 0, 2, new short[] {2, 1});
525     testSortDescending(new short[] {1, 3, 1}, 0, 2, new short[] {3, 1, 1});
526     testSortDescending(new short[] {1, 3, 1}, 0, 1, new short[] {1, 3, 1});
527     testSortDescending(new short[] {-1, -2, 1, 2}, 1, 3, new short[] {-1, 1, -2, 2});
528   }
529 
530   @J2ktIncompatible
531   @GwtIncompatible // SerializableTester
testStringConverterSerialization()532   public void testStringConverterSerialization() {
533     SerializableTester.reserializeAndAssert(Shorts.stringConverter());
534   }
535 
testToArray()536   public void testToArray() {
537     // need explicit type parameter to avoid javac warning!?
538     List<Short> none = Arrays.<Short>asList();
539     assertThat(Shorts.toArray(none)).isEqualTo(EMPTY);
540 
541     List<Short> one = Arrays.asList((short) 1);
542     assertThat(Shorts.toArray(one)).isEqualTo(ARRAY1);
543 
544     short[] array = {(short) 0, (short) 1, (short) 3};
545 
546     List<Short> three = Arrays.asList((short) 0, (short) 1, (short) 3);
547     assertThat(Shorts.toArray(three)).isEqualTo(array);
548 
549     assertThat(Shorts.toArray(Shorts.asList(array))).isEqualTo(array);
550   }
551 
testToArray_threadSafe()552   public void testToArray_threadSafe() {
553     for (int delta : new int[] {+1, 0, -1}) {
554       for (int i = 0; i < VALUES.length; i++) {
555         List<Short> list = Shorts.asList(VALUES).subList(0, i);
556         Collection<Short> misleadingSize = Helpers.misleadingSizeCollection(delta);
557         misleadingSize.addAll(list);
558         short[] arr = Shorts.toArray(misleadingSize);
559         assertThat(arr).hasLength(i);
560         for (int j = 0; j < i; j++) {
561           assertThat(arr[j]).isEqualTo(VALUES[j]);
562         }
563       }
564     }
565   }
566 
testToArray_withNull()567   public void testToArray_withNull() {
568     List<@Nullable Short> list = Arrays.asList((short) 0, (short) 1, null);
569     try {
570       Shorts.toArray(list);
571       fail();
572     } catch (NullPointerException expected) {
573     }
574   }
575 
testToArray_withConversion()576   public void testToArray_withConversion() {
577     short[] array = {(short) 0, (short) 1, (short) 2};
578 
579     List<Byte> bytes = Arrays.asList((byte) 0, (byte) 1, (byte) 2);
580     List<Short> shorts = Arrays.asList((short) 0, (short) 1, (short) 2);
581     List<Integer> ints = Arrays.asList(0, 1, 2);
582     List<Float> floats = Arrays.asList((float) 0, (float) 1, (float) 2);
583     List<Long> longs = Arrays.asList((long) 0, (long) 1, (long) 2);
584     List<Double> doubles = Arrays.asList((double) 0, (double) 1, (double) 2);
585 
586     assertThat(Shorts.toArray(bytes)).isEqualTo(array);
587     assertThat(Shorts.toArray(shorts)).isEqualTo(array);
588     assertThat(Shorts.toArray(ints)).isEqualTo(array);
589     assertThat(Shorts.toArray(floats)).isEqualTo(array);
590     assertThat(Shorts.toArray(longs)).isEqualTo(array);
591     assertThat(Shorts.toArray(doubles)).isEqualTo(array);
592   }
593 
594   @J2ktIncompatible // b/239034072: Kotlin varargs copy parameter arrays.
testAsList_isAView()595   public void testAsList_isAView() {
596     short[] array = {(short) 0, (short) 1};
597     List<Short> list = Shorts.asList(array);
598     list.set(0, (short) 2);
599     assertThat(array).isEqualTo(new short[] {(short) 2, (short) 1});
600     array[1] = (short) 3;
601     assertThat(list).containsExactly((short) 2, (short) 3).inOrder();
602   }
603 
testAsList_toArray_roundTrip()604   public void testAsList_toArray_roundTrip() {
605     short[] array = {(short) 0, (short) 1, (short) 2};
606     List<Short> list = Shorts.asList(array);
607     short[] newArray = Shorts.toArray(list);
608 
609     // Make sure it returned a copy
610     list.set(0, (short) 4);
611     assertThat(newArray).isEqualTo(new short[] {(short) 0, (short) 1, (short) 2});
612     newArray[1] = (short) 5;
613     assertThat((short) list.get(1)).isEqualTo((short) 1);
614   }
615 
616   // This test stems from a real bug found by andrewk
testAsList_subList_toArray_roundTrip()617   public void testAsList_subList_toArray_roundTrip() {
618     short[] array = {(short) 0, (short) 1, (short) 2, (short) 3};
619     List<Short> list = Shorts.asList(array);
620     assertThat(Shorts.toArray(list.subList(1, 3))).isEqualTo(new short[] {(short) 1, (short) 2});
621     assertThat(Shorts.toArray(list.subList(2, 2))).isEqualTo(new short[] {});
622   }
623 
testAsListEmpty()624   public void testAsListEmpty() {
625     assertThat(Shorts.asList(EMPTY)).isSameInstanceAs(Collections.emptyList());
626   }
627 
628   @J2ktIncompatible
629   @GwtIncompatible // NullPointerTester
testNulls()630   public void testNulls() {
631     new NullPointerTester().testAllPublicStaticMethods(Shorts.class);
632   }
633 
testStringConverter_convert()634   public void testStringConverter_convert() {
635     Converter<String, Short> converter = Shorts.stringConverter();
636     assertThat(converter.convert("1")).isEqualTo((Short) (short) 1);
637     assertThat(converter.convert("0")).isEqualTo((Short) (short) 0);
638     assertThat(converter.convert("-1")).isEqualTo((Short) (short) (-1));
639     assertThat(converter.convert("0xff")).isEqualTo((Short) (short) 255);
640     assertThat(converter.convert("0xFF")).isEqualTo((Short) (short) 255);
641     assertThat(converter.convert("-0xFF")).isEqualTo((Short) (short) (-255));
642     assertThat(converter.convert("#0000FF")).isEqualTo((Short) (short) 255);
643     assertThat(converter.convert("0666")).isEqualTo((Short) (short) 438);
644   }
645 
testStringConverter_convertError()646   public void testStringConverter_convertError() {
647     try {
648       Shorts.stringConverter().convert("notanumber");
649       fail();
650     } catch (NumberFormatException expected) {
651     }
652   }
653 
testStringConverter_nullConversions()654   public void testStringConverter_nullConversions() {
655     assertThat(Shorts.stringConverter().convert(null)).isNull();
656     assertThat(Shorts.stringConverter().reverse().convert(null)).isNull();
657   }
658 
testStringConverter_reverse()659   public void testStringConverter_reverse() {
660     Converter<String, Short> converter = Shorts.stringConverter();
661     assertThat(converter.reverse().convert((short) 1)).isEqualTo("1");
662     assertThat(converter.reverse().convert((short) 0)).isEqualTo("0");
663     assertThat(converter.reverse().convert((short) -1)).isEqualTo("-1");
664     assertThat(converter.reverse().convert((short) 0xff)).isEqualTo("255");
665     assertThat(converter.reverse().convert((short) 0xFF)).isEqualTo("255");
666     assertThat(converter.reverse().convert((short) -0xFF)).isEqualTo("-255");
667     assertThat(converter.reverse().convert((short) 0666)).isEqualTo("438");
668   }
669 
670   @J2ktIncompatible
671   @GwtIncompatible // NullPointerTester
testStringConverter_nullPointerTester()672   public void testStringConverter_nullPointerTester() throws Exception {
673     NullPointerTester tester = new NullPointerTester();
674     tester.testAllPublicInstanceMethods(Shorts.stringConverter());
675   }
676 }
677