• 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 com.google.common.annotations.GwtCompatible;
20 import com.google.common.annotations.GwtIncompatible;
21 import com.google.common.base.Converter;
22 import com.google.common.collect.testing.Helpers;
23 import com.google.common.testing.NullPointerTester;
24 import com.google.common.testing.SerializableTester;
25 import java.util.Arrays;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.Comparator;
29 import java.util.List;
30 import java.util.Random;
31 import junit.framework.TestCase;
32 
33 /**
34  * Unit test for {@link Ints}.
35  *
36  * @author Kevin Bourrillion
37  */
38 @GwtCompatible(emulated = true)
39 @SuppressWarnings("cast") // redundant casts are intentional and harmless
40 public class IntsTest extends TestCase {
41   private static final int[] EMPTY = {};
42   private static final int[] ARRAY1 = {(int) 1};
43   private static final int[] ARRAY234 = {(int) 2, (int) 3, (int) 4};
44 
45   private static final int LEAST = Integer.MIN_VALUE;
46   private static final int GREATEST = Integer.MAX_VALUE;
47 
48   private static final int[] VALUES = {LEAST, (int) -1, (int) 0, (int) 1, GREATEST};
49 
testHashCode()50   public void testHashCode() {
51     for (int value : VALUES) {
52       assertEquals(((Integer) value).hashCode(), Ints.hashCode(value));
53     }
54   }
55 
testCheckedCast()56   public void testCheckedCast() {
57     for (int value : VALUES) {
58       assertEquals(value, Ints.checkedCast((long) value));
59     }
60     assertCastFails(GREATEST + 1L);
61     assertCastFails(LEAST - 1L);
62     assertCastFails(Long.MAX_VALUE);
63     assertCastFails(Long.MIN_VALUE);
64   }
65 
testSaturatedCast()66   public void testSaturatedCast() {
67     for (int value : VALUES) {
68       assertEquals(value, Ints.saturatedCast((long) value));
69     }
70     assertEquals(GREATEST, Ints.saturatedCast(GREATEST + 1L));
71     assertEquals(LEAST, Ints.saturatedCast(LEAST - 1L));
72     assertEquals(GREATEST, Ints.saturatedCast(Long.MAX_VALUE));
73     assertEquals(LEAST, Ints.saturatedCast(Long.MIN_VALUE));
74   }
75 
assertCastFails(long value)76   private static void assertCastFails(long value) {
77     try {
78       Ints.checkedCast(value);
79       fail("Cast to int should have failed: " + value);
80     } catch (IllegalArgumentException ex) {
81       assertTrue(
82           value + " not found in exception text: " + ex.getMessage(),
83           ex.getMessage().contains(String.valueOf(value)));
84     }
85   }
86 
testCompare()87   public void testCompare() {
88     for (int x : VALUES) {
89       for (int y : VALUES) {
90         // note: spec requires only that the sign is the same
91         assertEquals(x + ", " + y, Integer.valueOf(x).compareTo(y), Ints.compare(x, y));
92       }
93     }
94   }
95 
testContains()96   public void testContains() {
97     assertFalse(Ints.contains(EMPTY, (int) 1));
98     assertFalse(Ints.contains(ARRAY1, (int) 2));
99     assertFalse(Ints.contains(ARRAY234, (int) 1));
100     assertTrue(Ints.contains(new int[] {(int) -1}, (int) -1));
101     assertTrue(Ints.contains(ARRAY234, (int) 2));
102     assertTrue(Ints.contains(ARRAY234, (int) 3));
103     assertTrue(Ints.contains(ARRAY234, (int) 4));
104   }
105 
testIndexOf()106   public void testIndexOf() {
107     assertEquals(-1, Ints.indexOf(EMPTY, (int) 1));
108     assertEquals(-1, Ints.indexOf(ARRAY1, (int) 2));
109     assertEquals(-1, Ints.indexOf(ARRAY234, (int) 1));
110     assertEquals(0, Ints.indexOf(new int[] {(int) -1}, (int) -1));
111     assertEquals(0, Ints.indexOf(ARRAY234, (int) 2));
112     assertEquals(1, Ints.indexOf(ARRAY234, (int) 3));
113     assertEquals(2, Ints.indexOf(ARRAY234, (int) 4));
114     assertEquals(1, Ints.indexOf(new int[] {(int) 2, (int) 3, (int) 2, (int) 3}, (int) 3));
115   }
116 
testIndexOf_arrayTarget()117   public void testIndexOf_arrayTarget() {
118     assertEquals(0, Ints.indexOf(EMPTY, EMPTY));
119     assertEquals(0, Ints.indexOf(ARRAY234, EMPTY));
120     assertEquals(-1, Ints.indexOf(EMPTY, ARRAY234));
121     assertEquals(-1, Ints.indexOf(ARRAY234, ARRAY1));
122     assertEquals(-1, Ints.indexOf(ARRAY1, ARRAY234));
123     assertEquals(0, Ints.indexOf(ARRAY1, ARRAY1));
124     assertEquals(0, Ints.indexOf(ARRAY234, ARRAY234));
125     assertEquals(0, Ints.indexOf(ARRAY234, new int[] {(int) 2, (int) 3}));
126     assertEquals(1, Ints.indexOf(ARRAY234, new int[] {(int) 3, (int) 4}));
127     assertEquals(1, Ints.indexOf(ARRAY234, new int[] {(int) 3}));
128     assertEquals(2, Ints.indexOf(ARRAY234, new int[] {(int) 4}));
129     assertEquals(
130         1,
131         Ints.indexOf(new int[] {(int) 2, (int) 3, (int) 3, (int) 3, (int) 3}, new int[] {(int) 3}));
132     assertEquals(
133         2,
134         Ints.indexOf(
135             new int[] {(int) 2, (int) 3, (int) 2, (int) 3, (int) 4, (int) 2, (int) 3},
136             new int[] {(int) 2, (int) 3, (int) 4}));
137     assertEquals(
138         1,
139         Ints.indexOf(
140             new int[] {(int) 2, (int) 2, (int) 3, (int) 4, (int) 2, (int) 3, (int) 4},
141             new int[] {(int) 2, (int) 3, (int) 4}));
142     assertEquals(
143         -1,
144         Ints.indexOf(new int[] {(int) 4, (int) 3, (int) 2}, new int[] {(int) 2, (int) 3, (int) 4}));
145   }
146 
testLastIndexOf()147   public void testLastIndexOf() {
148     assertEquals(-1, Ints.lastIndexOf(EMPTY, (int) 1));
149     assertEquals(-1, Ints.lastIndexOf(ARRAY1, (int) 2));
150     assertEquals(-1, Ints.lastIndexOf(ARRAY234, (int) 1));
151     assertEquals(0, Ints.lastIndexOf(new int[] {(int) -1}, (int) -1));
152     assertEquals(0, Ints.lastIndexOf(ARRAY234, (int) 2));
153     assertEquals(1, Ints.lastIndexOf(ARRAY234, (int) 3));
154     assertEquals(2, Ints.lastIndexOf(ARRAY234, (int) 4));
155     assertEquals(3, Ints.lastIndexOf(new int[] {(int) 2, (int) 3, (int) 2, (int) 3}, (int) 3));
156   }
157 
158   @GwtIncompatible
testMax_noArgs()159   public void testMax_noArgs() {
160     try {
161       Ints.max();
162       fail();
163     } catch (IllegalArgumentException expected) {
164     }
165   }
166 
testMax()167   public void testMax() {
168     assertEquals(LEAST, Ints.max(LEAST));
169     assertEquals(GREATEST, Ints.max(GREATEST));
170     assertEquals((int) 9, Ints.max((int) 8, (int) 6, (int) 7, (int) 5, (int) 3, (int) 0, (int) 9));
171   }
172 
173   @GwtIncompatible
testMin_noArgs()174   public void testMin_noArgs() {
175     try {
176       Ints.min();
177       fail();
178     } catch (IllegalArgumentException expected) {
179     }
180   }
181 
testMin()182   public void testMin() {
183     assertEquals(LEAST, Ints.min(LEAST));
184     assertEquals(GREATEST, Ints.min(GREATEST));
185     assertEquals((int) 0, Ints.min((int) 8, (int) 6, (int) 7, (int) 5, (int) 3, (int) 0, (int) 9));
186   }
187 
testConstrainToRange()188   public void testConstrainToRange() {
189     assertEquals((int) 1, Ints.constrainToRange((int) 1, (int) 0, (int) 5));
190     assertEquals((int) 1, Ints.constrainToRange((int) 1, (int) 1, (int) 5));
191     assertEquals((int) 3, Ints.constrainToRange((int) 1, (int) 3, (int) 5));
192     assertEquals((int) -1, Ints.constrainToRange((int) 0, (int) -5, (int) -1));
193     assertEquals((int) 2, Ints.constrainToRange((int) 5, (int) 2, (int) 2));
194     try {
195       Ints.constrainToRange((int) 1, (int) 3, (int) 2);
196       fail();
197     } catch (IllegalArgumentException expected) {
198     }
199   }
200 
testConcat()201   public void testConcat() {
202     assertTrue(Arrays.equals(EMPTY, Ints.concat()));
203     assertTrue(Arrays.equals(EMPTY, Ints.concat(EMPTY)));
204     assertTrue(Arrays.equals(EMPTY, Ints.concat(EMPTY, EMPTY, EMPTY)));
205     assertTrue(Arrays.equals(ARRAY1, Ints.concat(ARRAY1)));
206     assertNotSame(ARRAY1, Ints.concat(ARRAY1));
207     assertTrue(Arrays.equals(ARRAY1, Ints.concat(EMPTY, ARRAY1, EMPTY)));
208     assertTrue(
209         Arrays.equals(new int[] {(int) 1, (int) 1, (int) 1}, Ints.concat(ARRAY1, ARRAY1, ARRAY1)));
210     assertTrue(
211         Arrays.equals(
212             new int[] {(int) 1, (int) 2, (int) 3, (int) 4}, Ints.concat(ARRAY1, ARRAY234)));
213   }
214 
testToByteArray()215   public void testToByteArray() {
216     assertTrue(Arrays.equals(new byte[] {0x12, 0x13, 0x14, 0x15}, Ints.toByteArray(0x12131415)));
217     assertTrue(
218         Arrays.equals(
219             new byte[] {(byte) 0xFF, (byte) 0xEE, (byte) 0xDD, (byte) 0xCC},
220             Ints.toByteArray(0xFFEEDDCC)));
221   }
222 
testFromByteArray()223   public void testFromByteArray() {
224     assertEquals(0x12131415, Ints.fromByteArray(new byte[] {0x12, 0x13, 0x14, 0x15, 0x33}));
225     assertEquals(
226         0xFFEEDDCC,
227         Ints.fromByteArray(new byte[] {(byte) 0xFF, (byte) 0xEE, (byte) 0xDD, (byte) 0xCC}));
228   }
229 
testFromByteArrayFails()230   public void testFromByteArrayFails() {
231     try {
232       Ints.fromByteArray(new byte[Ints.BYTES - 1]);
233       fail();
234     } catch (IllegalArgumentException expected) {
235     }
236   }
237 
testFromBytes()238   public void testFromBytes() {
239     assertEquals(0x12131415, Ints.fromBytes((byte) 0x12, (byte) 0x13, (byte) 0x14, (byte) 0x15));
240     assertEquals(0xFFEEDDCC, Ints.fromBytes((byte) 0xFF, (byte) 0xEE, (byte) 0xDD, (byte) 0xCC));
241   }
242 
testByteArrayRoundTrips()243   public void testByteArrayRoundTrips() {
244     Random r = new Random(5);
245     byte[] b = new byte[Ints.BYTES];
246 
247     // total overkill, but, it takes 0.1 sec so why not...
248     for (int i = 0; i < 10000; i++) {
249       int num = r.nextInt();
250       assertEquals(num, Ints.fromByteArray(Ints.toByteArray(num)));
251 
252       r.nextBytes(b);
253       assertTrue(Arrays.equals(b, Ints.toByteArray(Ints.fromByteArray(b))));
254     }
255   }
256 
testEnsureCapacity()257   public void testEnsureCapacity() {
258     assertSame(EMPTY, Ints.ensureCapacity(EMPTY, 0, 1));
259     assertSame(ARRAY1, Ints.ensureCapacity(ARRAY1, 0, 1));
260     assertSame(ARRAY1, Ints.ensureCapacity(ARRAY1, 1, 1));
261     assertTrue(
262         Arrays.equals(new int[] {(int) 1, (int) 0, (int) 0}, Ints.ensureCapacity(ARRAY1, 2, 1)));
263   }
264 
testEnsureCapacity_fail()265   public void testEnsureCapacity_fail() {
266     try {
267       Ints.ensureCapacity(ARRAY1, -1, 1);
268       fail();
269     } catch (IllegalArgumentException expected) {
270     }
271     try {
272       // notice that this should even fail when no growth was needed
273       Ints.ensureCapacity(ARRAY1, 1, -1);
274       fail();
275     } catch (IllegalArgumentException expected) {
276     }
277   }
278 
testJoin()279   public void testJoin() {
280     assertEquals("", Ints.join(",", EMPTY));
281     assertEquals("1", Ints.join(",", ARRAY1));
282     assertEquals("1,2", Ints.join(",", (int) 1, (int) 2));
283     assertEquals("123", Ints.join("", (int) 1, (int) 2, (int) 3));
284   }
285 
testLexicographicalComparator()286   public void testLexicographicalComparator() {
287     List<int[]> ordered =
288         Arrays.asList(
289             new int[] {},
290             new int[] {LEAST},
291             new int[] {LEAST, LEAST},
292             new int[] {LEAST, (int) 1},
293             new int[] {(int) 1},
294             new int[] {(int) 1, LEAST},
295             new int[] {GREATEST, GREATEST - (int) 1},
296             new int[] {GREATEST, GREATEST},
297             new int[] {GREATEST, GREATEST, GREATEST});
298 
299     Comparator<int[]> comparator = Ints.lexicographicalComparator();
300     Helpers.testComparator(comparator, ordered);
301   }
302 
303   @GwtIncompatible // SerializableTester
testLexicographicalComparatorSerializable()304   public void testLexicographicalComparatorSerializable() {
305     Comparator<int[]> comparator = Ints.lexicographicalComparator();
306     assertSame(comparator, SerializableTester.reserialize(comparator));
307   }
308 
testReverse()309   public void testReverse() {
310     testReverse(new int[] {}, new int[] {});
311     testReverse(new int[] {1}, new int[] {1});
312     testReverse(new int[] {1, 2}, new int[] {2, 1});
313     testReverse(new int[] {3, 1, 1}, new int[] {1, 1, 3});
314     testReverse(new int[] {-1, 1, -2, 2}, new int[] {2, -2, 1, -1});
315   }
316 
testReverse(int[] input, int[] expectedOutput)317   private static void testReverse(int[] input, int[] expectedOutput) {
318     input = Arrays.copyOf(input, input.length);
319     Ints.reverse(input);
320     assertTrue(Arrays.equals(expectedOutput, input));
321   }
322 
testReverse(int[] input, int fromIndex, int toIndex, int[] expectedOutput)323   private static void testReverse(int[] input, int fromIndex, int toIndex, int[] expectedOutput) {
324     input = Arrays.copyOf(input, input.length);
325     Ints.reverse(input, fromIndex, toIndex);
326     assertTrue(Arrays.equals(expectedOutput, input));
327   }
328 
testReverseIndexed()329   public void testReverseIndexed() {
330     testReverse(new int[] {}, 0, 0, new int[] {});
331     testReverse(new int[] {1}, 0, 1, new int[] {1});
332     testReverse(new int[] {1, 2}, 0, 2, new int[] {2, 1});
333     testReverse(new int[] {3, 1, 1}, 0, 2, new int[] {1, 3, 1});
334     testReverse(new int[] {3, 1, 1}, 0, 1, new int[] {3, 1, 1});
335     testReverse(new int[] {-1, 1, -2, 2}, 1, 3, new int[] {-1, -2, 1, 2});
336   }
337 
testSortDescending()338   public void testSortDescending() {
339     testSortDescending(new int[] {}, new int[] {});
340     testSortDescending(new int[] {1}, new int[] {1});
341     testSortDescending(new int[] {1, 2}, new int[] {2, 1});
342     testSortDescending(new int[] {1, 3, 1}, new int[] {3, 1, 1});
343     testSortDescending(new int[] {-1, 1, -2, 2}, new int[] {2, 1, -1, -2});
344   }
345 
testSortDescending(int[] input, int[] expectedOutput)346   private static void testSortDescending(int[] input, int[] expectedOutput) {
347     input = Arrays.copyOf(input, input.length);
348     Ints.sortDescending(input);
349     assertTrue(Arrays.equals(expectedOutput, input));
350   }
351 
testSortDescending( int[] input, int fromIndex, int toIndex, int[] expectedOutput)352   private static void testSortDescending(
353       int[] input, int fromIndex, int toIndex, int[] expectedOutput) {
354     input = Arrays.copyOf(input, input.length);
355     Ints.sortDescending(input, fromIndex, toIndex);
356     assertTrue(Arrays.equals(expectedOutput, input));
357   }
358 
testSortDescendingIndexed()359   public void testSortDescendingIndexed() {
360     testSortDescending(new int[] {}, 0, 0, new int[] {});
361     testSortDescending(new int[] {1}, 0, 1, new int[] {1});
362     testSortDescending(new int[] {1, 2}, 0, 2, new int[] {2, 1});
363     testSortDescending(new int[] {1, 3, 1}, 0, 2, new int[] {3, 1, 1});
364     testSortDescending(new int[] {1, 3, 1}, 0, 1, new int[] {1, 3, 1});
365     testSortDescending(new int[] {-1, -2, 1, 2}, 1, 3, new int[] {-1, 1, -2, 2});
366   }
367 
368   @GwtIncompatible // SerializableTester
testStringConverterSerialization()369   public void testStringConverterSerialization() {
370     SerializableTester.reserializeAndAssert(Ints.stringConverter());
371   }
372 
testToArray()373   public void testToArray() {
374     // need explicit type parameter to avoid javac warning!?
375     List<Integer> none = Arrays.<Integer>asList();
376     assertTrue(Arrays.equals(EMPTY, Ints.toArray(none)));
377 
378     List<Integer> one = Arrays.asList((int) 1);
379     assertTrue(Arrays.equals(ARRAY1, Ints.toArray(one)));
380 
381     int[] array = {(int) 0, (int) 1, (int) 0xdeadbeef};
382 
383     List<Integer> three = Arrays.asList((int) 0, (int) 1, (int) 0xdeadbeef);
384     assertTrue(Arrays.equals(array, Ints.toArray(three)));
385 
386     assertTrue(Arrays.equals(array, Ints.toArray(Ints.asList(array))));
387   }
388 
testToArray_threadSafe()389   public void testToArray_threadSafe() {
390     for (int delta : new int[] {+1, 0, -1}) {
391       for (int i = 0; i < VALUES.length; i++) {
392         List<Integer> list = Ints.asList(VALUES).subList(0, i);
393         Collection<Integer> misleadingSize = Helpers.misleadingSizeCollection(delta);
394         misleadingSize.addAll(list);
395         int[] arr = Ints.toArray(misleadingSize);
396         assertEquals(i, arr.length);
397         for (int j = 0; j < i; j++) {
398           assertEquals(VALUES[j], arr[j]);
399         }
400       }
401     }
402   }
403 
testToArray_withNull()404   public void testToArray_withNull() {
405     List<Integer> list = Arrays.asList((int) 0, (int) 1, null);
406     try {
407       Ints.toArray(list);
408       fail();
409     } catch (NullPointerException expected) {
410     }
411   }
412 
testToArray_withConversion()413   public void testToArray_withConversion() {
414     int[] array = {0, 1, 2};
415 
416     List<Byte> bytes = Arrays.asList((byte) 0, (byte) 1, (byte) 2);
417     List<Short> shorts = Arrays.asList((short) 0, (short) 1, (short) 2);
418     List<Integer> ints = Arrays.asList(0, 1, 2);
419     List<Float> floats = Arrays.asList((float) 0, (float) 1, (float) 2);
420     List<Long> longs = Arrays.asList((long) 0, (long) 1, (long) 2);
421     List<Double> doubles = Arrays.asList((double) 0, (double) 1, (double) 2);
422 
423     assertTrue(Arrays.equals(array, Ints.toArray(bytes)));
424     assertTrue(Arrays.equals(array, Ints.toArray(shorts)));
425     assertTrue(Arrays.equals(array, Ints.toArray(ints)));
426     assertTrue(Arrays.equals(array, Ints.toArray(floats)));
427     assertTrue(Arrays.equals(array, Ints.toArray(longs)));
428     assertTrue(Arrays.equals(array, Ints.toArray(doubles)));
429   }
430 
testAsList_isAView()431   public void testAsList_isAView() {
432     int[] array = {(int) 0, (int) 1};
433     List<Integer> list = Ints.asList(array);
434     list.set(0, (int) 2);
435     assertTrue(Arrays.equals(new int[] {(int) 2, (int) 1}, array));
436     array[1] = (int) 3;
437     assertEquals(Arrays.asList((int) 2, (int) 3), list);
438   }
439 
testAsList_toArray_roundTrip()440   public void testAsList_toArray_roundTrip() {
441     int[] array = {(int) 0, (int) 1, (int) 2};
442     List<Integer> list = Ints.asList(array);
443     int[] newArray = Ints.toArray(list);
444 
445     // Make sure it returned a copy
446     list.set(0, (int) 4);
447     assertTrue(Arrays.equals(new int[] {(int) 0, (int) 1, (int) 2}, newArray));
448     newArray[1] = (int) 5;
449     assertEquals((int) 1, (int) list.get(1));
450   }
451 
452   // This test stems from a real bug found by andrewk
testAsList_subList_toArray_roundTrip()453   public void testAsList_subList_toArray_roundTrip() {
454     int[] array = {(int) 0, (int) 1, (int) 2, (int) 3};
455     List<Integer> list = Ints.asList(array);
456     assertTrue(Arrays.equals(new int[] {(int) 1, (int) 2}, Ints.toArray(list.subList(1, 3))));
457     assertTrue(Arrays.equals(new int[] {}, Ints.toArray(list.subList(2, 2))));
458   }
459 
testAsListEmpty()460   public void testAsListEmpty() {
461     assertSame(Collections.emptyList(), Ints.asList(EMPTY));
462   }
463 
464   @GwtIncompatible // NullPointerTester
testNulls()465   public void testNulls() {
466     new NullPointerTester().testAllPublicStaticMethods(Ints.class);
467   }
468 
testStringConverter_convert()469   public void testStringConverter_convert() {
470     Converter<String, Integer> converter = Ints.stringConverter();
471     assertEquals((Integer) 1, converter.convert("1"));
472     assertEquals((Integer) 0, converter.convert("0"));
473     assertEquals((Integer) (-1), converter.convert("-1"));
474     assertEquals((Integer) 255, converter.convert("0xff"));
475     assertEquals((Integer) 255, converter.convert("0xFF"));
476     assertEquals((Integer) (-255), converter.convert("-0xFF"));
477     assertEquals((Integer) 255, converter.convert("#0000FF"));
478     assertEquals((Integer) 438, converter.convert("0666"));
479   }
480 
testStringConverter_convertError()481   public void testStringConverter_convertError() {
482     try {
483       Ints.stringConverter().convert("notanumber");
484       fail();
485     } catch (NumberFormatException expected) {
486     }
487   }
488 
testStringConverter_nullConversions()489   public void testStringConverter_nullConversions() {
490     assertNull(Ints.stringConverter().convert(null));
491     assertNull(Ints.stringConverter().reverse().convert(null));
492   }
493 
testStringConverter_reverse()494   public void testStringConverter_reverse() {
495     Converter<String, Integer> converter = Ints.stringConverter();
496     assertEquals("1", converter.reverse().convert(1));
497     assertEquals("0", converter.reverse().convert(0));
498     assertEquals("-1", converter.reverse().convert(-1));
499     assertEquals("255", converter.reverse().convert(0xff));
500     assertEquals("255", converter.reverse().convert(0xFF));
501     assertEquals("-255", converter.reverse().convert(-0xFF));
502     assertEquals("438", converter.reverse().convert(0666));
503   }
504 
505   @GwtIncompatible // NullPointerTester
testStringConverter_nullPointerTester()506   public void testStringConverter_nullPointerTester() throws Exception {
507     NullPointerTester tester = new NullPointerTester();
508     tester.testAllPublicInstanceMethods(Ints.stringConverter());
509   }
510 
testTryParse()511   public void testTryParse() {
512     tryParseAndAssertEquals(0, "0");
513     tryParseAndAssertEquals(0, "-0");
514     tryParseAndAssertEquals(1, "1");
515     tryParseAndAssertEquals(-1, "-1");
516     tryParseAndAssertEquals(8900, "8900");
517     tryParseAndAssertEquals(-8900, "-8900");
518     tryParseAndAssertEquals(GREATEST, Integer.toString(GREATEST));
519     tryParseAndAssertEquals(LEAST, Integer.toString(LEAST));
520     assertNull(Ints.tryParse(""));
521     assertNull(Ints.tryParse("-"));
522     assertNull(Ints.tryParse("+1"));
523     assertNull(Ints.tryParse("9999999999999999"));
524     assertNull("Max integer + 1", Ints.tryParse(Long.toString(((long) GREATEST) + 1)));
525     assertNull("Max integer * 10", Ints.tryParse(Long.toString(((long) GREATEST) * 10)));
526     assertNull("Min integer - 1", Ints.tryParse(Long.toString(((long) LEAST) - 1)));
527     assertNull("Min integer * 10", Ints.tryParse(Long.toString(((long) LEAST) * 10)));
528     assertNull("Max long", Ints.tryParse(Long.toString(Long.MAX_VALUE)));
529     assertNull("Min long", Ints.tryParse(Long.toString(Long.MIN_VALUE)));
530     assertNull(Ints.tryParse("\u0662\u06f3"));
531   }
532 
533   /**
534    * Applies {@link Ints#tryParse(String)} to the given string and asserts that the result is as
535    * expected.
536    */
tryParseAndAssertEquals(Integer expected, String value)537   private static void tryParseAndAssertEquals(Integer expected, String value) {
538     assertEquals(expected, Ints.tryParse(value));
539   }
540 
testTryParse_radix()541   public void testTryParse_radix() {
542     for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) {
543       radixEncodeParseAndAssertEquals(0, radix);
544       radixEncodeParseAndAssertEquals(8000, radix);
545       radixEncodeParseAndAssertEquals(-8000, radix);
546       radixEncodeParseAndAssertEquals(GREATEST, radix);
547       radixEncodeParseAndAssertEquals(LEAST, radix);
548       assertNull("Radix: " + radix, Ints.tryParse("9999999999999999", radix));
549       assertNull(
550           "Radix: " + radix, Ints.tryParse(Long.toString((long) GREATEST + 1, radix), radix));
551       assertNull("Radix: " + radix, Ints.tryParse(Long.toString((long) LEAST - 1, radix), radix));
552     }
553     assertNull("Hex string and dec parm", Ints.tryParse("FFFF", 10));
554     assertEquals("Mixed hex case", 65535, (int) Ints.tryParse("ffFF", 16));
555   }
556 
557   /**
558    * Encodes the an integer as a string with given radix, then uses {@link Ints#tryParse(String,
559    * int)} to parse the result. Asserts the result is the same as what we started with.
560    */
radixEncodeParseAndAssertEquals(Integer value, int radix)561   private static void radixEncodeParseAndAssertEquals(Integer value, int radix) {
562     assertEquals("Radix: " + radix, value, Ints.tryParse(Integer.toString(value, radix), radix));
563   }
564 
testTryParse_radixTooBig()565   public void testTryParse_radixTooBig() {
566     try {
567       Ints.tryParse("0", Character.MAX_RADIX + 1);
568       fail();
569     } catch (IllegalArgumentException expected) {
570     }
571   }
572 
testTryParse_radixTooSmall()573   public void testTryParse_radixTooSmall() {
574     try {
575       Ints.tryParse("0", Character.MIN_RADIX - 1);
576       fail();
577     } catch (IllegalArgumentException expected) {
578     }
579   }
580 
testTryParse_withNullGwt()581   public void testTryParse_withNullGwt() {
582     assertNull(Ints.tryParse("null"));
583     try {
584       Ints.tryParse(null);
585       fail("Expected NPE");
586     } catch (NullPointerException expected) {
587     }
588   }
589 }
590