• 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.collect.testing.Helpers;
22 import com.google.common.testing.NullPointerTester;
23 import com.google.common.testing.SerializableTester;
24 
25 import junit.framework.TestCase;
26 
27 import java.util.Arrays;
28 import java.util.Collection;
29 import java.util.Collections;
30 import java.util.Comparator;
31 import java.util.List;
32 import java.util.Random;
33 
34 /**
35  * Unit test for {@link Longs}.
36  *
37  * @author Kevin Bourrillion
38  */
39 @GwtCompatible(emulated = true)
40 @SuppressWarnings("cast") // redundant casts are intentional and harmless
41 public class LongsTest extends TestCase {
42   private static final long[] EMPTY = {};
43   private static final long[] ARRAY1 = {(long) 1};
44   private static final long[] ARRAY234
45       = {(long) 2, (long) 3, (long) 4};
46 
47   private static final long LEAST = Long.MIN_VALUE;
48   private static final long GREATEST = Long.MAX_VALUE;
49 
50   private static final long[] VALUES =
51       { LEAST, (long) -1, (long) 0, (long) 1, GREATEST };
52 
53   @GwtIncompatible("Long.hashCode returns different values in GWT.")
testHashCode()54   public void testHashCode() {
55     for (long value : VALUES) {
56       assertEquals("hashCode for " + value,
57           ((Long) value).hashCode(), Longs.hashCode(value));
58     }
59   }
60 
testCompare()61   public void testCompare() {
62     for (long x : VALUES) {
63       for (long y : VALUES) {
64         // note: spec requires only that the sign is the same
65         assertEquals(x + ", " + y,
66                      Long.valueOf(x).compareTo(y),
67                      Longs.compare(x, y));
68       }
69     }
70   }
71 
testContains()72   public void testContains() {
73     assertFalse(Longs.contains(EMPTY, (long) 1));
74     assertFalse(Longs.contains(ARRAY1, (long) 2));
75     assertFalse(Longs.contains(ARRAY234, (long) 1));
76     assertTrue(Longs.contains(new long[] {(long) -1}, (long) -1));
77     assertTrue(Longs.contains(ARRAY234, (long) 2));
78     assertTrue(Longs.contains(ARRAY234, (long) 3));
79     assertTrue(Longs.contains(ARRAY234, (long) 4));
80   }
81 
testIndexOf()82   public void testIndexOf() {
83     assertEquals(-1, Longs.indexOf(EMPTY, (long) 1));
84     assertEquals(-1, Longs.indexOf(ARRAY1, (long) 2));
85     assertEquals(-1, Longs.indexOf(ARRAY234, (long) 1));
86     assertEquals(0, Longs.indexOf(
87         new long[] {(long) -1}, (long) -1));
88     assertEquals(0, Longs.indexOf(ARRAY234, (long) 2));
89     assertEquals(1, Longs.indexOf(ARRAY234, (long) 3));
90     assertEquals(2, Longs.indexOf(ARRAY234, (long) 4));
91     assertEquals(1, Longs.indexOf(
92         new long[] { (long) 2, (long) 3, (long) 2, (long) 3 },
93         (long) 3));
94   }
95 
testIndexOf_arrayTarget()96   public void testIndexOf_arrayTarget() {
97     assertEquals(0, Longs.indexOf(EMPTY, EMPTY));
98     assertEquals(0, Longs.indexOf(ARRAY234, EMPTY));
99     assertEquals(-1, Longs.indexOf(EMPTY, ARRAY234));
100     assertEquals(-1, Longs.indexOf(ARRAY234, ARRAY1));
101     assertEquals(-1, Longs.indexOf(ARRAY1, ARRAY234));
102     assertEquals(0, Longs.indexOf(ARRAY1, ARRAY1));
103     assertEquals(0, Longs.indexOf(ARRAY234, ARRAY234));
104     assertEquals(0, Longs.indexOf(
105         ARRAY234, new long[] { (long) 2, (long) 3 }));
106     assertEquals(1, Longs.indexOf(
107         ARRAY234, new long[] { (long) 3, (long) 4 }));
108     assertEquals(1, Longs.indexOf(ARRAY234, new long[] { (long) 3 }));
109     assertEquals(2, Longs.indexOf(ARRAY234, new long[] { (long) 4 }));
110     assertEquals(1, Longs.indexOf(new long[] { (long) 2, (long) 3,
111         (long) 3, (long) 3, (long) 3 },
112         new long[] { (long) 3 }
113     ));
114     assertEquals(2, Longs.indexOf(
115         new long[] { (long) 2, (long) 3, (long) 2,
116             (long) 3, (long) 4, (long) 2, (long) 3},
117         new long[] { (long) 2, (long) 3, (long) 4}
118     ));
119     assertEquals(1, Longs.indexOf(
120         new long[] { (long) 2, (long) 2, (long) 3,
121             (long) 4, (long) 2, (long) 3, (long) 4},
122         new long[] { (long) 2, (long) 3, (long) 4}
123     ));
124     assertEquals(-1, Longs.indexOf(
125         new long[] { (long) 4, (long) 3, (long) 2},
126         new long[] { (long) 2, (long) 3, (long) 4}
127     ));
128   }
129 
testLastIndexOf()130   public void testLastIndexOf() {
131     assertEquals(-1, Longs.lastIndexOf(EMPTY, (long) 1));
132     assertEquals(-1, Longs.lastIndexOf(ARRAY1, (long) 2));
133     assertEquals(-1, Longs.lastIndexOf(ARRAY234, (long) 1));
134     assertEquals(0, Longs.lastIndexOf(
135         new long[] {(long) -1}, (long) -1));
136     assertEquals(0, Longs.lastIndexOf(ARRAY234, (long) 2));
137     assertEquals(1, Longs.lastIndexOf(ARRAY234, (long) 3));
138     assertEquals(2, Longs.lastIndexOf(ARRAY234, (long) 4));
139     assertEquals(3, Longs.lastIndexOf(
140         new long[] { (long) 2, (long) 3, (long) 2, (long) 3 },
141         (long) 3));
142   }
143 
testMax_noArgs()144   public void testMax_noArgs() {
145     try {
146       Longs.max();
147       fail();
148     } catch (IllegalArgumentException expected) {
149     }
150   }
151 
testMax()152   public void testMax() {
153     assertEquals(LEAST, Longs.max(LEAST));
154     assertEquals(GREATEST, Longs.max(GREATEST));
155     assertEquals((long) 9, Longs.max(
156         (long) 8, (long) 6, (long) 7,
157         (long) 5, (long) 3, (long) 0, (long) 9));
158   }
159 
testMin_noArgs()160   public void testMin_noArgs() {
161     try {
162       Longs.min();
163       fail();
164     } catch (IllegalArgumentException expected) {
165     }
166   }
167 
testMin()168   public void testMin() {
169     assertEquals(LEAST, Longs.min(LEAST));
170     assertEquals(GREATEST, Longs.min(GREATEST));
171     assertEquals((long) 0, Longs.min(
172         (long) 8, (long) 6, (long) 7,
173         (long) 5, (long) 3, (long) 0, (long) 9));
174   }
175 
testConcat()176   public void testConcat() {
177     assertTrue(Arrays.equals(EMPTY, Longs.concat()));
178     assertTrue(Arrays.equals(EMPTY, Longs.concat(EMPTY)));
179     assertTrue(Arrays.equals(EMPTY, Longs.concat(EMPTY, EMPTY, EMPTY)));
180     assertTrue(Arrays.equals(ARRAY1, Longs.concat(ARRAY1)));
181     assertNotSame(ARRAY1, Longs.concat(ARRAY1));
182     assertTrue(Arrays.equals(ARRAY1, Longs.concat(EMPTY, ARRAY1, EMPTY)));
183     assertTrue(Arrays.equals(
184         new long[] {(long) 1, (long) 1, (long) 1},
185         Longs.concat(ARRAY1, ARRAY1, ARRAY1)));
186     assertTrue(Arrays.equals(
187         new long[] {(long) 1, (long) 2, (long) 3, (long) 4},
188         Longs.concat(ARRAY1, ARRAY234)));
189   }
190 
191   @GwtIncompatible("Longs.toByteArray")
testToByteArray()192   public void testToByteArray() {
193     assertTrue(Arrays.equals(
194         new byte[] {0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19},
195         Longs.toByteArray(0x1213141516171819L)));
196     assertTrue(Arrays.equals(
197         new byte[] {
198             (byte) 0xFF, (byte) 0xEE, (byte) 0xDD, (byte) 0xCC,
199             (byte) 0xBB, (byte) 0xAA, (byte) 0x99, (byte) 0x88},
200         Longs.toByteArray(0xFFEEDDCCBBAA9988L)));
201   }
202 
203   @GwtIncompatible("Longs.fromByteArray")
testFromByteArray()204   public void testFromByteArray() {
205     assertEquals(0x1213141516171819L, Longs.fromByteArray(
206         new byte[] {0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x33}));
207     assertEquals(0xFFEEDDCCBBAA9988L, Longs.fromByteArray(
208         new byte[] {
209             (byte) 0xFF, (byte) 0xEE, (byte) 0xDD, (byte) 0xCC,
210             (byte) 0xBB, (byte) 0xAA, (byte) 0x99, (byte) 0x88}));
211 
212     try {
213       Longs.fromByteArray(new byte[Longs.BYTES - 1]);
214       fail();
215     } catch (IllegalArgumentException expected) {
216     }
217   }
218 
219   @GwtIncompatible("Longs.fromBytes")
testFromBytes()220   public void testFromBytes() {
221     assertEquals(0x1213141516171819L, Longs.fromBytes(
222         (byte) 0x12, (byte) 0x13, (byte) 0x14, (byte) 0x15,
223         (byte) 0x16, (byte) 0x17, (byte) 0x18, (byte) 0x19));
224     assertEquals(0xFFEEDDCCBBAA9988L, Longs.fromBytes(
225         (byte) 0xFF, (byte) 0xEE, (byte) 0xDD, (byte) 0xCC,
226         (byte) 0xBB, (byte) 0xAA, (byte) 0x99, (byte) 0x88));
227   }
228 
229   @GwtIncompatible("Longs.fromByteArray, Longs.toByteArray")
testByteArrayRoundTrips()230   public void testByteArrayRoundTrips() {
231     Random r = new Random(5);
232     byte[] b = new byte[Longs.BYTES];
233 
234     // total overkill, but, it takes 0.1 sec so why not...
235     for (int i = 0; i < 10000; i++) {
236       long num = r.nextLong();
237       assertEquals(num, Longs.fromByteArray(Longs.toByteArray(num)));
238 
239       r.nextBytes(b);
240       long value = Longs.fromByteArray(b);
241       assertTrue("" + value, Arrays.equals(b, Longs.toByteArray(value)));
242     }
243   }
244 
testEnsureCapacity()245   public void testEnsureCapacity() {
246     assertSame(EMPTY, Longs.ensureCapacity(EMPTY, 0, 1));
247     assertSame(ARRAY1, Longs.ensureCapacity(ARRAY1, 0, 1));
248     assertSame(ARRAY1, Longs.ensureCapacity(ARRAY1, 1, 1));
249     assertTrue(Arrays.equals(
250         new long[] {(long) 1, (long) 0, (long) 0},
251         Longs.ensureCapacity(ARRAY1, 2, 1)));
252   }
253 
testEnsureCapacity_fail()254   public void testEnsureCapacity_fail() {
255     try {
256       Longs.ensureCapacity(ARRAY1, -1, 1);
257       fail();
258     } catch (IllegalArgumentException expected) {
259     }
260     try {
261       // notice that this should even fail when no growth was needed
262       Longs.ensureCapacity(ARRAY1, 1, -1);
263       fail();
264     } catch (IllegalArgumentException expected) {
265     }
266   }
267 
testJoin()268   public void testJoin() {
269     assertEquals("", Longs.join(",", EMPTY));
270     assertEquals("1", Longs.join(",", ARRAY1));
271     assertEquals("1,2", Longs.join(",", (long) 1, (long) 2));
272     assertEquals("123",
273         Longs.join("", (long) 1, (long) 2, (long) 3));
274   }
275 
testLexicographicalComparator()276   public void testLexicographicalComparator() {
277     List<long[]> ordered = Arrays.asList(
278         new long[] {},
279         new long[] {LEAST},
280         new long[] {LEAST, LEAST},
281         new long[] {LEAST, (long) 1},
282         new long[] {(long) 1},
283         new long[] {(long) 1, LEAST},
284         new long[] {GREATEST, GREATEST - (long) 1},
285         new long[] {GREATEST, GREATEST},
286         new long[] {GREATEST, GREATEST, GREATEST});
287 
288     Comparator<long[]> comparator = Longs.lexicographicalComparator();
289     Helpers.testComparator(comparator, ordered);
290   }
291 
292   @GwtIncompatible("SerializableTester")
testLexicographicalComparatorSerializable()293   public void testLexicographicalComparatorSerializable() {
294     Comparator<long[]> comparator = Longs.lexicographicalComparator();
295     assertSame(comparator, SerializableTester.reserialize(comparator));
296   }
297 
testToArray()298   public void testToArray() {
299     // need explicit type parameter to avoid javac warning!?
300     List<Long> none = Arrays.<Long>asList();
301     assertTrue(Arrays.equals(EMPTY, Longs.toArray(none)));
302 
303     List<Long> one = Arrays.asList((long) 1);
304     assertTrue(Arrays.equals(ARRAY1, Longs.toArray(one)));
305 
306     long[] array = {(long) 0, (long) 1, 0x0FF1C1AL};
307 
308     List<Long> three = Arrays.asList((long) 0, (long) 1, 0x0FF1C1AL);
309     assertTrue(Arrays.equals(array, Longs.toArray(three)));
310 
311     assertTrue(Arrays.equals(array, Longs.toArray(Longs.asList(array))));
312   }
313 
testToArray_threadSafe()314   public void testToArray_threadSafe() {
315     for (int delta : new int[] { +1, 0, -1 }) {
316       for (int i = 0; i < VALUES.length; i++) {
317         List<Long> list = Longs.asList(VALUES).subList(0, i);
318         Collection<Long> misleadingSize =
319             Helpers.misleadingSizeCollection(delta);
320         misleadingSize.addAll(list);
321         long[] arr = Longs.toArray(misleadingSize);
322         assertEquals(i, arr.length);
323         for (int j = 0; j < i; j++) {
324           assertEquals(VALUES[j], arr[j]);
325         }
326       }
327     }
328   }
329 
testToArray_withNull()330   public void testToArray_withNull() {
331     List<Long> list = Arrays.asList((long) 0, (long) 1, null);
332     try {
333       Longs.toArray(list);
334       fail();
335     } catch (NullPointerException expected) {
336     }
337   }
338 
testAsList_isAView()339   public void testAsList_isAView() {
340     long[] array = {(long) 0, (long) 1};
341     List<Long> list = Longs.asList(array);
342     list.set(0, (long) 2);
343     assertTrue(Arrays.equals(new long[] {(long) 2, (long) 1}, array));
344     array[1] = (long) 3;
345     assertEquals(Arrays.asList((long) 2, (long) 3), list);
346   }
347 
testAsList_toArray_roundTrip()348   public void testAsList_toArray_roundTrip() {
349     long[] array = { (long) 0, (long) 1, (long) 2 };
350     List<Long> list = Longs.asList(array);
351     long[] newArray = Longs.toArray(list);
352 
353     // Make sure it returned a copy
354     list.set(0, (long) 4);
355     assertTrue(Arrays.equals(
356         new long[] { (long) 0, (long) 1, (long) 2 }, newArray));
357     newArray[1] = (long) 5;
358     assertEquals((long) 1, (long) list.get(1));
359   }
360 
361   // This test stems from a real bug found by andrewk
testAsList_subList_toArray_roundTrip()362   public void testAsList_subList_toArray_roundTrip() {
363     long[] array = { (long) 0, (long) 1, (long) 2, (long) 3 };
364     List<Long> list = Longs.asList(array);
365     assertTrue(Arrays.equals(new long[] { (long) 1, (long) 2 },
366         Longs.toArray(list.subList(1, 3))));
367     assertTrue(Arrays.equals(new long[] {},
368         Longs.toArray(list.subList(2, 2))));
369   }
370 
testAsListEmpty()371   public void testAsListEmpty() {
372     assertSame(Collections.emptyList(), Longs.asList(EMPTY));
373   }
374 
375   @GwtIncompatible("NullPointerTester")
testNulls()376   public void testNulls() throws Exception {
377     NullPointerTester tester = new NullPointerTester();
378     tester.setDefault(long[].class, new long[0]);
379     tester.testAllPublicStaticMethods(Longs.class);
380   }
381 }
382