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