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