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