|
Java example source code file (LongsTest.java)
The LongsTest.java Java example source code
/*
* Copyright (C) 2008 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.common.primitives;
import static java.lang.Long.MAX_VALUE;
import static java.lang.Long.MIN_VALUE;
import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.base.Converter;
import com.google.common.collect.testing.Helpers;
import com.google.common.testing.NullPointerTester;
import com.google.common.testing.SerializableTester;
import junit.framework.TestCase;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
/**
* Unit test for {@link Longs}.
*
* @author Kevin Bourrillion
*/
@GwtCompatible(emulated = true)
@SuppressWarnings("cast") // redundant casts are intentional and harmless
public class LongsTest extends TestCase {
private static final long[] EMPTY = {};
private static final long[] ARRAY1 = {(long) 1};
private static final long[] ARRAY234
= {(long) 2, (long) 3, (long) 4};
private static final long[] VALUES =
{ MIN_VALUE, (long) -1, (long) 0, (long) 1, MAX_VALUE };
@GwtIncompatible // Long.hashCode returns different values in GWT.
public void testHashCode() {
for (long value : VALUES) {
assertEquals("hashCode for " + value,
((Long) value).hashCode(), Longs.hashCode(value));
}
}
public void testCompare() {
for (long x : VALUES) {
for (long y : VALUES) {
// note: spec requires only that the sign is the same
assertEquals(x + ", " + y,
Long.valueOf(x).compareTo(y),
Longs.compare(x, y));
}
}
}
public void testContains() {
assertFalse(Longs.contains(EMPTY, (long) 1));
assertFalse(Longs.contains(ARRAY1, (long) 2));
assertFalse(Longs.contains(ARRAY234, (long) 1));
assertTrue(Longs.contains(new long[] {(long) -1}, (long) -1));
assertTrue(Longs.contains(ARRAY234, (long) 2));
assertTrue(Longs.contains(ARRAY234, (long) 3));
assertTrue(Longs.contains(ARRAY234, (long) 4));
}
public void testIndexOf() {
assertEquals(-1, Longs.indexOf(EMPTY, (long) 1));
assertEquals(-1, Longs.indexOf(ARRAY1, (long) 2));
assertEquals(-1, Longs.indexOf(ARRAY234, (long) 1));
assertEquals(0, Longs.indexOf(
new long[] {(long) -1}, (long) -1));
assertEquals(0, Longs.indexOf(ARRAY234, (long) 2));
assertEquals(1, Longs.indexOf(ARRAY234, (long) 3));
assertEquals(2, Longs.indexOf(ARRAY234, (long) 4));
assertEquals(1, Longs.indexOf(
new long[] { (long) 2, (long) 3, (long) 2, (long) 3 },
(long) 3));
}
public void testIndexOf_arrayTarget() {
assertEquals(0, Longs.indexOf(EMPTY, EMPTY));
assertEquals(0, Longs.indexOf(ARRAY234, EMPTY));
assertEquals(-1, Longs.indexOf(EMPTY, ARRAY234));
assertEquals(-1, Longs.indexOf(ARRAY234, ARRAY1));
assertEquals(-1, Longs.indexOf(ARRAY1, ARRAY234));
assertEquals(0, Longs.indexOf(ARRAY1, ARRAY1));
assertEquals(0, Longs.indexOf(ARRAY234, ARRAY234));
assertEquals(0, Longs.indexOf(
ARRAY234, new long[] { (long) 2, (long) 3 }));
assertEquals(1, Longs.indexOf(
ARRAY234, new long[] { (long) 3, (long) 4 }));
assertEquals(1, Longs.indexOf(ARRAY234, new long[] { (long) 3 }));
assertEquals(2, Longs.indexOf(ARRAY234, new long[] { (long) 4 }));
assertEquals(1, Longs.indexOf(new long[] { (long) 2, (long) 3,
(long) 3, (long) 3, (long) 3 },
new long[] { (long) 3 }
));
assertEquals(2, Longs.indexOf(
new long[] { (long) 2, (long) 3, (long) 2,
(long) 3, (long) 4, (long) 2, (long) 3},
new long[] { (long) 2, (long) 3, (long) 4}
));
assertEquals(1, Longs.indexOf(
new long[] { (long) 2, (long) 2, (long) 3,
(long) 4, (long) 2, (long) 3, (long) 4},
new long[] { (long) 2, (long) 3, (long) 4}
));
assertEquals(-1, Longs.indexOf(
new long[] { (long) 4, (long) 3, (long) 2},
new long[] { (long) 2, (long) 3, (long) 4}
));
}
public void testLastIndexOf() {
assertEquals(-1, Longs.lastIndexOf(EMPTY, (long) 1));
assertEquals(-1, Longs.lastIndexOf(ARRAY1, (long) 2));
assertEquals(-1, Longs.lastIndexOf(ARRAY234, (long) 1));
assertEquals(0, Longs.lastIndexOf(
new long[] {(long) -1}, (long) -1));
assertEquals(0, Longs.lastIndexOf(ARRAY234, (long) 2));
assertEquals(1, Longs.lastIndexOf(ARRAY234, (long) 3));
assertEquals(2, Longs.lastIndexOf(ARRAY234, (long) 4));
assertEquals(3, Longs.lastIndexOf(
new long[] { (long) 2, (long) 3, (long) 2, (long) 3 },
(long) 3));
}
public void testMax_noArgs() {
try {
Longs.max();
fail();
} catch (IllegalArgumentException expected) {
}
}
public void testMax() {
assertEquals(MIN_VALUE, Longs.max(MIN_VALUE));
assertEquals(MAX_VALUE, Longs.max(MAX_VALUE));
assertEquals((long) 9, Longs.max(
(long) 8, (long) 6, (long) 7,
(long) 5, (long) 3, (long) 0, (long) 9));
}
public void testMin_noArgs() {
try {
Longs.min();
fail();
} catch (IllegalArgumentException expected) {
}
}
public void testMin() {
assertEquals(MIN_VALUE, Longs.min(MIN_VALUE));
assertEquals(MAX_VALUE, Longs.min(MAX_VALUE));
assertEquals((long) 0, Longs.min(
(long) 8, (long) 6, (long) 7,
(long) 5, (long) 3, (long) 0, (long) 9));
}
public void testConcat() {
assertTrue(Arrays.equals(EMPTY, Longs.concat()));
assertTrue(Arrays.equals(EMPTY, Longs.concat(EMPTY)));
assertTrue(Arrays.equals(EMPTY, Longs.concat(EMPTY, EMPTY, EMPTY)));
assertTrue(Arrays.equals(ARRAY1, Longs.concat(ARRAY1)));
assertNotSame(ARRAY1, Longs.concat(ARRAY1));
assertTrue(Arrays.equals(ARRAY1, Longs.concat(EMPTY, ARRAY1, EMPTY)));
assertTrue(Arrays.equals(
new long[] {(long) 1, (long) 1, (long) 1},
Longs.concat(ARRAY1, ARRAY1, ARRAY1)));
assertTrue(Arrays.equals(
new long[] {(long) 1, (long) 2, (long) 3, (long) 4},
Longs.concat(ARRAY1, ARRAY234)));
}
private static void assertByteArrayEquals(byte[] expected, byte[] actual) {
assertTrue(
"Expected: " + Arrays.toString(expected) + ", but got: " + Arrays.toString(actual),
Arrays.equals(expected, actual));
}
public void testToByteArray() {
assertByteArrayEquals(
new byte[] {0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19},
Longs.toByteArray(0x1213141516171819L));
assertByteArrayEquals(
new byte[] {
(byte) 0xFF, (byte) 0xEE, (byte) 0xDD, (byte) 0xCC,
(byte) 0xBB, (byte) 0xAA, (byte) 0x99, (byte) 0x88},
Longs.toByteArray(0xFFEEDDCCBBAA9988L));
}
public void testFromByteArray() {
assertEquals(0x1213141516171819L, Longs.fromByteArray(
new byte[] {0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x33}));
assertEquals(0xFFEEDDCCBBAA9988L, Longs.fromByteArray(
new byte[] {
(byte) 0xFF, (byte) 0xEE, (byte) 0xDD, (byte) 0xCC,
(byte) 0xBB, (byte) 0xAA, (byte) 0x99, (byte) 0x88}));
}
public void testFromByteArrayFails() {
try {
Longs.fromByteArray(new byte[Longs.BYTES - 1]);
fail();
} catch (IllegalArgumentException expected) {
}
}
public void testFromBytes() {
assertEquals(0x1213141516171819L, Longs.fromBytes(
(byte) 0x12, (byte) 0x13, (byte) 0x14, (byte) 0x15,
(byte) 0x16, (byte) 0x17, (byte) 0x18, (byte) 0x19));
assertEquals(0xFFEEDDCCBBAA9988L, Longs.fromBytes(
(byte) 0xFF, (byte) 0xEE, (byte) 0xDD, (byte) 0xCC,
(byte) 0xBB, (byte) 0xAA, (byte) 0x99, (byte) 0x88));
}
public void testByteArrayRoundTrips() {
Random r = new Random(5);
byte[] b = new byte[Longs.BYTES];
// total overkill, but, it takes 0.1 sec so why not...
for (int i = 0; i < 10000; i++) {
long num = r.nextLong();
assertEquals(num, Longs.fromByteArray(Longs.toByteArray(num)));
r.nextBytes(b);
long value = Longs.fromByteArray(b);
assertTrue("" + value, Arrays.equals(b, Longs.toByteArray(value)));
}
}
public void testEnsureCapacity() {
assertSame(EMPTY, Longs.ensureCapacity(EMPTY, 0, 1));
assertSame(ARRAY1, Longs.ensureCapacity(ARRAY1, 0, 1));
assertSame(ARRAY1, Longs.ensureCapacity(ARRAY1, 1, 1));
assertTrue(Arrays.equals(
new long[] {(long) 1, (long) 0, (long) 0},
Longs.ensureCapacity(ARRAY1, 2, 1)));
}
public void testEnsureCapacity_fail() {
try {
Longs.ensureCapacity(ARRAY1, -1, 1);
fail();
} catch (IllegalArgumentException expected) {
}
try {
// notice that this should even fail when no growth was needed
Longs.ensureCapacity(ARRAY1, 1, -1);
fail();
} catch (IllegalArgumentException expected) {
}
}
public void testJoin() {
assertEquals("", Longs.join(",", EMPTY));
assertEquals("1", Longs.join(",", ARRAY1));
assertEquals("1,2", Longs.join(",", (long) 1, (long) 2));
assertEquals("123",
Longs.join("", (long) 1, (long) 2, (long) 3));
}
public void testLexicographicalComparator() {
List<long[]> ordered = Arrays.asList(
new long[] {},
new long[] {MIN_VALUE},
new long[] {MIN_VALUE, MIN_VALUE},
new long[] {MIN_VALUE, (long) 1},
new long[] {(long) 1},
new long[] {(long) 1, MIN_VALUE},
new long[] {MAX_VALUE, MAX_VALUE - (long) 1},
new long[] {MAX_VALUE, MAX_VALUE},
new long[] {MAX_VALUE, MAX_VALUE, MAX_VALUE});
Comparator<long[]> comparator = Longs.lexicographicalComparator();
Helpers.testComparator(comparator, ordered);
}
@GwtIncompatible // SerializableTester
public void testLexicographicalComparatorSerializable() {
Comparator<long[]> comparator = Longs.lexicographicalComparator();
assertSame(comparator, SerializableTester.reserialize(comparator));
}
@GwtIncompatible // SerializableTester
public void testStringConverterSerialization() {
SerializableTester.reserializeAndAssert(Longs.stringConverter());
}
public void testToArray() {
// need explicit type parameter to avoid javac warning!?
List<Long> none = Arrays.
Other Java examples (source code examples)Here is a short list of links related to this Java LongsTest.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.