diff options
Diffstat (limited to 'guava-tests/test/com/google/common/primitives')
23 files changed, 245 insertions, 1062 deletions
diff --git a/guava-tests/test/com/google/common/primitives/BooleansTest.java b/guava-tests/test/com/google/common/primitives/BooleansTest.java index f6c5d99..b20a01c 100644 --- a/guava-tests/test/com/google/common/primitives/BooleansTest.java +++ b/guava-tests/test/com/google/common/primitives/BooleansTest.java @@ -39,7 +39,6 @@ import java.util.List; public class BooleansTest extends TestCase { private static final boolean[] EMPTY = {}; private static final boolean[] ARRAY_FALSE = {false}; - private static final boolean[] ARRAY_TRUE = {true}; private static final boolean[] ARRAY_FALSE_FALSE = {false, false}; private static final boolean[] ARRAY_FALSE_TRUE = {false, true}; @@ -70,16 +69,6 @@ public class BooleansTest extends TestCase { } public void testIndexOf() { - assertEquals(-1, Booleans.indexOf(EMPTY, ARRAY_FALSE)); - assertEquals(-1, Booleans.indexOf(ARRAY_FALSE, ARRAY_FALSE_TRUE)); - assertEquals(0, Booleans.indexOf(ARRAY_FALSE_FALSE, ARRAY_FALSE)); - assertEquals(0, Booleans.indexOf(ARRAY_FALSE, ARRAY_FALSE)); - assertEquals(0, Booleans.indexOf(ARRAY_FALSE_TRUE, ARRAY_FALSE)); - assertEquals(1, Booleans.indexOf(ARRAY_FALSE_TRUE, ARRAY_TRUE)); - assertEquals(0, Booleans.indexOf(ARRAY_TRUE, new boolean[0])); - } - - public void testIndexOf_arrays() { assertEquals(-1, Booleans.indexOf(EMPTY, false)); assertEquals(-1, Booleans.indexOf(ARRAY_FALSE, true)); assertEquals(-1, Booleans.indexOf(ARRAY_FALSE_FALSE, true)); @@ -211,85 +200,14 @@ public class BooleansTest extends TestCase { } } - public void testAsListIsEmpty() { - assertTrue(Booleans.asList(EMPTY).isEmpty()); - assertFalse(Booleans.asList(ARRAY_FALSE).isEmpty()); - } - - public void testAsListSize() { - assertEquals(0, Booleans.asList(EMPTY).size()); - assertEquals(1, Booleans.asList(ARRAY_FALSE).size()); - assertEquals(2, Booleans.asList(ARRAY_FALSE_TRUE).size()); - } - - public void testAsListIndexOf() { - assertEquals(-1, Booleans.asList(EMPTY).indexOf("wrong type")); - assertEquals(-1, Booleans.asList(EMPTY).indexOf(true)); - assertEquals(-1, Booleans.asList(ARRAY_FALSE).indexOf(true)); - assertEquals(0, Booleans.asList(ARRAY_FALSE).indexOf(false)); - assertEquals(1, Booleans.asList(ARRAY_FALSE_TRUE).indexOf(true)); - } - - public void testAsListLastIndexOf() { - assertEquals(-1, Booleans.asList(EMPTY).indexOf("wrong type")); - assertEquals(-1, Booleans.asList(EMPTY).indexOf(true)); - assertEquals(-1, Booleans.asList(ARRAY_FALSE).lastIndexOf(true)); - assertEquals(1, Booleans.asList(ARRAY_FALSE_TRUE).lastIndexOf(true)); - assertEquals(1, Booleans.asList(ARRAY_FALSE_FALSE).lastIndexOf(false)); - } - - public void testAsListContains() { - assertFalse(Booleans.asList(EMPTY).contains("wrong type")); - assertFalse(Booleans.asList(EMPTY).contains(true)); - assertFalse(Booleans.asList(ARRAY_FALSE).contains(true)); - assertTrue(Booleans.asList(ARRAY_TRUE).contains(true)); - assertTrue(Booleans.asList(ARRAY_FALSE_TRUE).contains(false)); - assertTrue(Booleans.asList(ARRAY_FALSE_TRUE).contains(true)); - } - - public void testAsListEquals() { - assertEquals(Booleans.asList(EMPTY), Collections.emptyList()); - assertEquals(Booleans.asList(ARRAY_FALSE), Booleans.asList(ARRAY_FALSE)); - assertFalse(Booleans.asList(ARRAY_FALSE).equals(ARRAY_FALSE)); - assertFalse(Booleans.asList(ARRAY_FALSE).equals(null)); - assertFalse(Booleans.asList(ARRAY_FALSE).equals(Booleans.asList(ARRAY_FALSE_TRUE))); - assertFalse(Booleans.asList(ARRAY_FALSE_FALSE).equals(Booleans.asList(ARRAY_FALSE_TRUE))); - assertEquals(1, Booleans.asList(ARRAY_FALSE_TRUE).lastIndexOf(true)); - List<Boolean> reference = Booleans.asList(ARRAY_FALSE); - assertEquals(Booleans.asList(ARRAY_FALSE), reference); - assertEquals(reference, reference); - } - - public void testAsListHashcode() { - assertEquals(1, Booleans.asList(EMPTY).hashCode()); - assertEquals(Booleans.asList(ARRAY_FALSE).hashCode(), Booleans.asList(ARRAY_FALSE).hashCode()); - List<Boolean> reference = Booleans.asList(ARRAY_FALSE); - assertEquals(Booleans.asList(ARRAY_FALSE).hashCode(), reference.hashCode()); - } - - public void testAsListToString() { - assertEquals("[false]", Booleans.asList(ARRAY_FALSE).toString()); - assertEquals("[false, true]", Booleans.asList(ARRAY_FALSE_TRUE).toString()); - } - - public void testAsListSet() { - List<Boolean> list = Booleans.asList(ARRAY_FALSE); - assertFalse(list.set(0, true)); - assertTrue(list.set(0, false)); - try { - list.set(0, null); - fail(); - } catch (NullPointerException expected) { - } - try { - list.set(1, true); - fail(); - } catch (IndexOutOfBoundsException expected) { - } + public void testAsListEmpty() { + assertSame(Collections.emptyList(), Booleans.asList(EMPTY)); } @GwtIncompatible("NullPointerTester") - public void testNulls() { - new NullPointerTester().testAllPublicStaticMethods(Booleans.class); + public void testNulls() throws Exception { + NullPointerTester tester = new NullPointerTester(); + tester.setDefault(boolean[].class, new boolean[0]); + tester.testAllPublicStaticMethods(Booleans.class); } } diff --git a/guava-tests/test/com/google/common/primitives/ByteArrayAsListTest.java b/guava-tests/test/com/google/common/primitives/ByteArrayAsListTest.java index 585c4c8..49ffa74 100644 --- a/guava-tests/test/com/google/common/primitives/ByteArrayAsListTest.java +++ b/guava-tests/test/com/google/common/primitives/ByteArrayAsListTest.java @@ -19,7 +19,6 @@ package com.google.common.primitives; import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.annotations.GwtCompatible; -import com.google.common.annotations.GwtIncompatible; import com.google.common.collect.ImmutableList; import com.google.common.collect.testing.ListTestSuiteBuilder; import com.google.common.collect.testing.SampleElements; @@ -39,7 +38,7 @@ import java.util.List; * * @author Kevin Bourrillion */ -@GwtCompatible(emulated = true) +@GwtCompatible public class ByteArrayAsListTest extends TestCase { private static List<Byte> asList(Byte[] values) { @@ -50,7 +49,6 @@ public class ByteArrayAsListTest extends TestCase { return Bytes.asList(temp); } - @GwtIncompatible("suite") public static Test suite() { List<ListTestSuiteBuilder<Byte>> builders = ImmutableList.of( diff --git a/guava-tests/test/com/google/common/primitives/BytesTest.java b/guava-tests/test/com/google/common/primitives/BytesTest.java index e268427..a30061c 100644 --- a/guava-tests/test/com/google/common/primitives/BytesTest.java +++ b/guava-tests/test/com/google/common/primitives/BytesTest.java @@ -200,24 +200,6 @@ public class BytesTest extends TestCase { } } - public void testToArray_withConversion() { - byte[] array = {(byte) 0, (byte) 1, (byte) 2}; - - List<Byte> bytes = Arrays.asList((byte) 0, (byte) 1, (byte) 2); - List<Short> shorts = Arrays.asList((short) 0, (short) 1, (short) 2); - List<Integer> ints = Arrays.asList(0, 1, 2); - List<Float> floats = Arrays.asList((float) 0, (float) 1, (float) 2); - List<Long> longs = Arrays.asList((long) 0, (long) 1, (long) 2); - List<Double> doubles = Arrays.asList((double) 0, (double) 1, (double) 2); - - assertTrue(Arrays.equals(array, Bytes.toArray(bytes))); - assertTrue(Arrays.equals(array, Bytes.toArray(shorts))); - assertTrue(Arrays.equals(array, Bytes.toArray(ints))); - assertTrue(Arrays.equals(array, Bytes.toArray(floats))); - assertTrue(Arrays.equals(array, Bytes.toArray(longs))); - assertTrue(Arrays.equals(array, Bytes.toArray(doubles))); - } - public void testAsList_isAView() { byte[] array = {(byte) 0, (byte) 1}; List<Byte> list = Bytes.asList(array); @@ -255,7 +237,9 @@ public class BytesTest extends TestCase { } @GwtIncompatible("NullPointerTester") - public void testNulls() { - new NullPointerTester().testAllPublicStaticMethods(Bytes.class); + public void testNulls() throws Exception { + NullPointerTester tester = new NullPointerTester(); + tester.setDefault(byte[].class, new byte[0]); + tester.testAllPublicStaticMethods(Bytes.class); } } diff --git a/guava-tests/test/com/google/common/primitives/CharArrayAsListTest.java b/guava-tests/test/com/google/common/primitives/CharArrayAsListTest.java index c2eae49..23b88b1 100644 --- a/guava-tests/test/com/google/common/primitives/CharArrayAsListTest.java +++ b/guava-tests/test/com/google/common/primitives/CharArrayAsListTest.java @@ -19,7 +19,6 @@ package com.google.common.primitives; import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.annotations.GwtCompatible; -import com.google.common.annotations.GwtIncompatible; import com.google.common.collect.ImmutableList; import com.google.common.collect.testing.ListTestSuiteBuilder; import com.google.common.collect.testing.SampleElements; @@ -39,7 +38,7 @@ import java.util.List; * * @author Kevin Bourrillion */ -@GwtCompatible(emulated = true) +@GwtCompatible public class CharArrayAsListTest extends TestCase { private static List<Character> asList(Character[] values) { @@ -50,7 +49,6 @@ public class CharArrayAsListTest extends TestCase { return Chars.asList(temp); } - @GwtIncompatible("suite") public static Test suite() { List<ListTestSuiteBuilder<Character>> builders = ImmutableList.of( diff --git a/guava-tests/test/com/google/common/primitives/CharsTest.java b/guava-tests/test/com/google/common/primitives/CharsTest.java index e6d7439..adc1898 100644 --- a/guava-tests/test/com/google/common/primitives/CharsTest.java +++ b/guava-tests/test/com/google/common/primitives/CharsTest.java @@ -390,7 +390,9 @@ public class CharsTest extends TestCase { } @GwtIncompatible("NullPointerTester") - public void testNulls() { - new NullPointerTester().testAllPublicStaticMethods(Chars.class); + public void testNulls() throws Exception { + NullPointerTester tester = new NullPointerTester(); + tester.setDefault(char[].class, new char[0]); + tester.testAllPublicStaticMethods(Chars.class); } } diff --git a/guava-tests/test/com/google/common/primitives/DoubleArrayAsListTest.java b/guava-tests/test/com/google/common/primitives/DoubleArrayAsListTest.java index a20f7f5..1fa39d7 100644 --- a/guava-tests/test/com/google/common/primitives/DoubleArrayAsListTest.java +++ b/guava-tests/test/com/google/common/primitives/DoubleArrayAsListTest.java @@ -19,7 +19,6 @@ package com.google.common.primitives; import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.annotations.GwtCompatible; -import com.google.common.annotations.GwtIncompatible; import com.google.common.collect.ImmutableList; import com.google.common.collect.testing.ListTestSuiteBuilder; import com.google.common.collect.testing.SampleElements; @@ -39,7 +38,7 @@ import java.util.List; * * @author Kevin Bourrillion */ -@GwtCompatible(emulated = true) +@GwtCompatible public class DoubleArrayAsListTest extends TestCase { private static List<Double> asList(Double[] values) { @@ -50,7 +49,6 @@ public class DoubleArrayAsListTest extends TestCase { return Doubles.asList(temp); } - @GwtIncompatible("suite") public static Test suite() { List<ListTestSuiteBuilder<Double>> builders = ImmutableList.of( diff --git a/guava-tests/test/com/google/common/primitives/DoublesTest.java b/guava-tests/test/com/google/common/primitives/DoublesTest.java index 5e1e4c8..68fdc59 100644 --- a/guava-tests/test/com/google/common/primitives/DoublesTest.java +++ b/guava-tests/test/com/google/common/primitives/DoublesTest.java @@ -17,11 +17,10 @@ package com.google.common.primitives; import static java.lang.Double.NaN; -import static org.truth0.Truth.ASSERT; +import static org.junit.contrib.truth.Truth.ASSERT; import com.google.common.annotations.GwtCompatible; import com.google.common.annotations.GwtIncompatible; -import com.google.common.collect.ImmutableList; import com.google.common.collect.testing.Helpers; import com.google.common.testing.NullPointerTester; import com.google.common.testing.SerializableTester; @@ -51,10 +50,9 @@ public class DoublesTest extends TestCase { private static final double GREATEST = Double.POSITIVE_INFINITY; private static final double[] NUMBERS = new double[] { - LEAST, -Double.MAX_VALUE, -1.0, -0.5, -0.1, -0.0, 0.0, 0.1, 0.5, 1.0, - Double.MAX_VALUE, GREATEST, Double.MIN_NORMAL, -Double.MIN_NORMAL, - Double.MIN_VALUE, -Double.MIN_VALUE, Integer.MIN_VALUE, - Integer.MAX_VALUE, Long.MIN_VALUE, Long.MAX_VALUE + LEAST, -Double.MAX_VALUE, -1.0, -0.0, 0.0, 1.0, Double.MAX_VALUE, GREATEST, + Double.MIN_NORMAL, -Double.MIN_NORMAL, Double.MIN_VALUE, -Double.MIN_VALUE, + Integer.MIN_VALUE, Integer.MAX_VALUE, Long.MIN_VALUE, Long.MAX_VALUE }; private static final double[] VALUES @@ -339,31 +337,13 @@ public class DoublesTest extends TestCase { } } - public void testToArray_withConversion() { - double[] array = {(double) 0, (double) 1, (double) 2}; - - List<Byte> bytes = Arrays.asList((byte) 0, (byte) 1, (byte) 2); - List<Short> shorts = Arrays.asList((short) 0, (short) 1, (short) 2); - List<Integer> ints = Arrays.asList(0, 1, 2); - List<Float> floats = Arrays.asList((float) 0, (float) 1, (float) 2); - List<Long> longs = Arrays.asList((long) 0, (long) 1, (long) 2); - List<Double> doubles = Arrays.asList((double) 0, (double) 1, (double) 2); - - assertTrue(Arrays.equals(array, Doubles.toArray(bytes))); - assertTrue(Arrays.equals(array, Doubles.toArray(shorts))); - assertTrue(Arrays.equals(array, Doubles.toArray(ints))); - assertTrue(Arrays.equals(array, Doubles.toArray(floats))); - assertTrue(Arrays.equals(array, Doubles.toArray(longs))); - assertTrue(Arrays.equals(array, Doubles.toArray(doubles))); - } - public void testAsList_isAView() { double[] array = {(double) 0, (double) 1}; List<Double> list = Doubles.asList(array); list.set(0, (double) 2); assertTrue(Arrays.equals(new double[] {(double) 2, (double) 1}, array)); array[1] = (double) 3; - ASSERT.that(list).has().allOf((double) 2, (double) 3).inOrder(); + ASSERT.that(list).hasContentsInOrder((double) 2, (double) 3); } public void testAsList_toArray_roundTrip() { @@ -393,108 +373,10 @@ public class DoublesTest extends TestCase { assertSame(Collections.emptyList(), Doubles.asList(EMPTY)); } - /** - * A reference implementation for {@code tryParse} that just catches the exception from - * {@link Double#valueOf}. - */ - private static Double referenceTryParse(String input) { - if (input.trim().length() < input.length()) { - return null; - } - try { - return Double.valueOf(input); - } catch (NumberFormatException e) { - return null; - } - } - - @GwtIncompatible("Doubles.tryParse") - private static void checkTryParse(String input) { - Double expected = referenceTryParse(input); - assertEquals(expected, Doubles.tryParse(input)); - assertEquals(expected != null, - Doubles.FLOATING_POINT_PATTERN.matcher(input).matches()); - } - - @GwtIncompatible("Doubles.tryParse") - private static void checkTryParse(double expected, String input) { - assertEquals(Double.valueOf(expected), Doubles.tryParse(input)); - assertTrue(Doubles.FLOATING_POINT_PATTERN.matcher(input).matches()); - } - - @GwtIncompatible("Doubles.tryParse") - public void testTryParseHex() { - for (String signChar : ImmutableList.of("", "+", "-")) { - for (String hexPrefix : ImmutableList.of("0x", "0X")) { - for (String iPart : ImmutableList.of("", "0", "1", "F", "f", "c4", "CE")) { - for (String fPart : ImmutableList.of("", ".", ".F", ".52", ".a")) { - for (String expMarker : ImmutableList.of("p", "P")) { - for (String exponent : ImmutableList.of("0", "-5", "+20", "52")) { - for (String typePart : ImmutableList.of("", "D", "F", "d", "f")) { - checkTryParse( - signChar + hexPrefix + iPart + fPart + expMarker + exponent + typePart); - } - } - } - } - } - } - } - } - - @GwtIncompatible("Doubles.tryParse") - public void testTryParseAllCodePoints() { - // Exercise non-ASCII digit test cases and the like. - char[] tmp = new char[2]; - for (int i = Character.MIN_CODE_POINT; i < Character.MAX_CODE_POINT; i++) { - Character.toChars(i, tmp, 0); - checkTryParse(String.copyValueOf(tmp, 0, Character.charCount(i))); - } - } - - @GwtIncompatible("Doubles.tryParse") - public void testTryParseOfToStringIsOriginal() { - for (double d : NUMBERS) { - checkTryParse(d, Double.toString(d)); - } - } - - @GwtIncompatible("Doubles.tryParse") - public void testTryParseOfToHexStringIsOriginal() { - for (double d : NUMBERS) { - checkTryParse(d, Double.toHexString(d)); - } - } - - @GwtIncompatible("Doubles.tryParse") - public void testTryParseNaN() { - checkTryParse("NaN"); - checkTryParse("+NaN"); - checkTryParse("-NaN"); - } - - @GwtIncompatible("Doubles.tryParse") - public void testTryParseInfinity() { - checkTryParse(Double.POSITIVE_INFINITY, "Infinity"); - checkTryParse(Double.POSITIVE_INFINITY, "+Infinity"); - checkTryParse(Double.NEGATIVE_INFINITY, "-Infinity"); - } - - private static final String[] BAD_TRY_PARSE_INPUTS = - { "", "+-", "+-0", " 5", "32 ", " 55 ", "infinity", "POSITIVE_INFINITY", "0x9A", "0x9A.bE-5", - ".", ".e5", "NaNd", "InfinityF" }; - - @GwtIncompatible("Doubles.tryParse") - public void testTryParseFailures() { - for (String badInput : BAD_TRY_PARSE_INPUTS) { - assertFalse(Doubles.FLOATING_POINT_PATTERN.matcher(badInput).matches()); - assertEquals(referenceTryParse(badInput), Doubles.tryParse(badInput)); - assertNull(Doubles.tryParse(badInput)); - } - } - @GwtIncompatible("NullPointerTester") - public void testNulls() { - new NullPointerTester().testAllPublicStaticMethods(Doubles.class); + public void testNulls() throws Exception { + NullPointerTester tester = new NullPointerTester(); + tester.setDefault(double[].class, new double[0]); + tester.testAllPublicStaticMethods(Doubles.class); } } diff --git a/guava-tests/test/com/google/common/primitives/FloatArrayAsListTest.java b/guava-tests/test/com/google/common/primitives/FloatArrayAsListTest.java index 06dd93b..ded5a77 100644 --- a/guava-tests/test/com/google/common/primitives/FloatArrayAsListTest.java +++ b/guava-tests/test/com/google/common/primitives/FloatArrayAsListTest.java @@ -19,7 +19,6 @@ package com.google.common.primitives; import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.annotations.GwtCompatible; -import com.google.common.annotations.GwtIncompatible; import com.google.common.collect.ImmutableList; import com.google.common.collect.testing.ListTestSuiteBuilder; import com.google.common.collect.testing.SampleElements; @@ -39,7 +38,7 @@ import java.util.List; * * @author Kevin Bourrillion */ -@GwtCompatible(emulated = true) +@GwtCompatible public class FloatArrayAsListTest extends TestCase { private static List<Float> asList(Float[] values) { @@ -50,7 +49,6 @@ public class FloatArrayAsListTest extends TestCase { return Floats.asList(temp); } - @GwtIncompatible("suite") public static Test suite() { List<ListTestSuiteBuilder<Float>> builders = ImmutableList.of( diff --git a/guava-tests/test/com/google/common/primitives/FloatsTest.java b/guava-tests/test/com/google/common/primitives/FloatsTest.java index e4d99ab..55f5bbc 100644 --- a/guava-tests/test/com/google/common/primitives/FloatsTest.java +++ b/guava-tests/test/com/google/common/primitives/FloatsTest.java @@ -17,11 +17,10 @@ package com.google.common.primitives; import static java.lang.Float.NaN; -import static org.truth0.Truth.ASSERT; +import static org.junit.contrib.truth.Truth.ASSERT; import com.google.common.annotations.GwtCompatible; import com.google.common.annotations.GwtIncompatible; -import com.google.common.collect.ImmutableList; import com.google.common.collect.testing.Helpers; import com.google.common.testing.NullPointerTester; import com.google.common.testing.SerializableTester; @@ -330,31 +329,13 @@ public class FloatsTest extends TestCase { } } - public void testToArray_withConversion() { - float[] array = {(float) 0, (float) 1, (float) 2}; - - List<Byte> bytes = Arrays.asList((byte) 0, (byte) 1, (byte) 2); - List<Short> shorts = Arrays.asList((short) 0, (short) 1, (short) 2); - List<Integer> ints = Arrays.asList(0, 1, 2); - List<Float> floats = Arrays.asList((float) 0, (float) 1, (float) 2); - List<Long> longs = Arrays.asList((long) 0, (long) 1, (long) 2); - List<Double> doubles = Arrays.asList((double) 0, (double) 1, (double) 2); - - assertTrue(Arrays.equals(array, Floats.toArray(bytes))); - assertTrue(Arrays.equals(array, Floats.toArray(shorts))); - assertTrue(Arrays.equals(array, Floats.toArray(ints))); - assertTrue(Arrays.equals(array, Floats.toArray(floats))); - assertTrue(Arrays.equals(array, Floats.toArray(longs))); - assertTrue(Arrays.equals(array, Floats.toArray(doubles))); - } - public void testAsList_isAView() { float[] array = {(float) 0, (float) 1}; List<Float> list = Floats.asList(array); list.set(0, (float) 2); assertTrue(Arrays.equals(new float[] {(float) 2, (float) 1}, array)); array[1] = (float) 3; - ASSERT.that(list).has().allOf((float) 2, (float) 3).inOrder(); + ASSERT.that(list).hasContentsInOrder((float) 2, (float) 3); } public void testAsList_toArray_roundTrip() { @@ -384,103 +365,10 @@ public class FloatsTest extends TestCase { assertSame(Collections.emptyList(), Floats.asList(EMPTY)); } - /** - * A reference implementation for {@code tryParse} that just catches the exception from - * {@link Float#valueOf}. - */ - private static Float referenceTryParse(String input) { - if (input.trim().length() < input.length()) { - return null; - } - try { - return Float.valueOf(input); - } catch (NumberFormatException e) { - return null; - } - } - - @GwtIncompatible("Floats.tryParse") - private static void checkTryParse(String input) { - assertEquals(referenceTryParse(input), Floats.tryParse(input)); - } - - @GwtIncompatible("Floats.tryParse") - private static void checkTryParse(float expected, String input) { - assertEquals(Float.valueOf(expected), Floats.tryParse(input)); - } - - @GwtIncompatible("Floats.tryParse") - public void testTryParseHex() { - for (String signChar : ImmutableList.of("", "+", "-")) { - for (String hexPrefix : ImmutableList.of("0x", "0X")) { - for (String iPart : ImmutableList.of("", "0", "1", "F", "f", "c4", "CE")) { - for (String fPart : ImmutableList.of("", ".", ".F", ".52", ".a")) { - for (String expMarker : ImmutableList.of("p", "P")) { - for (String exponent : ImmutableList.of("0", "-5", "+20", "52")) { - for (String typePart : ImmutableList.of("", "D", "F", "d", "f")) { - checkTryParse( - signChar + hexPrefix + iPart + fPart + expMarker + exponent + typePart); - } - } - } - } - } - } - } - } - - @GwtIncompatible("Floats.tryParse") - public void testTryParseAllCodePoints() { - // Exercise non-ASCII digit test cases and the like. - char[] tmp = new char[2]; - for (int i = Character.MIN_CODE_POINT; i < Character.MAX_CODE_POINT; i++) { - Character.toChars(i, tmp, 0); - checkTryParse(String.copyValueOf(tmp, 0, Character.charCount(i))); - } - } - - @GwtIncompatible("Floats.tryParse") - public void testTryParseOfToStringIsOriginal() { - for (float f : NUMBERS) { - checkTryParse(f, Float.toString(f)); - } - } - - @GwtIncompatible("Floats.tryParse") - public void testTryParseOfToHexStringIsOriginal() { - for (float f : NUMBERS) { - checkTryParse(f, Float.toHexString(f)); - } - } - - @GwtIncompatible("Floats.tryParse") - public void testTryParseNaN() { - checkTryParse("NaN"); - checkTryParse("+NaN"); - checkTryParse("-NaN"); - } - - @GwtIncompatible("Floats.tryParse") - public void testTryParseInfinity() { - checkTryParse(Float.POSITIVE_INFINITY, "Infinity"); - checkTryParse(Float.POSITIVE_INFINITY, "+Infinity"); - checkTryParse(Float.NEGATIVE_INFINITY, "-Infinity"); - } - - private static final String[] BAD_TRY_PARSE_INPUTS = - { "", "+-", "+-0", " 5", "32 ", " 55 ", "infinity", "POSITIVE_INFINITY", "0x9A", "0x9A.bE-5", - ".", ".e5", "NaNd", "InfinityF" }; - - @GwtIncompatible("Floats.tryParse") - public void testTryParseFailures() { - for (String badInput : BAD_TRY_PARSE_INPUTS) { - assertEquals(referenceTryParse(badInput), Floats.tryParse(badInput)); - assertNull(Floats.tryParse(badInput)); - } - } - @GwtIncompatible("NullPointerTester") - public void testNulls() { - new NullPointerTester().testAllPublicStaticMethods(Floats.class); + public void testNulls() throws Exception { + NullPointerTester tester = new NullPointerTester(); + tester.setDefault(float[].class, new float[0]); + tester.testAllPublicStaticMethods(Floats.class); } } diff --git a/guava-tests/test/com/google/common/primitives/IntArrayAsListTest.java b/guava-tests/test/com/google/common/primitives/IntArrayAsListTest.java index efdbbb2..d384149 100644 --- a/guava-tests/test/com/google/common/primitives/IntArrayAsListTest.java +++ b/guava-tests/test/com/google/common/primitives/IntArrayAsListTest.java @@ -19,7 +19,6 @@ package com.google.common.primitives; import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.annotations.GwtCompatible; -import com.google.common.annotations.GwtIncompatible; import com.google.common.collect.ImmutableList; import com.google.common.collect.testing.ListTestSuiteBuilder; import com.google.common.collect.testing.SampleElements; @@ -39,7 +38,7 @@ import java.util.List; * * @author Kevin Bourrillion */ -@GwtCompatible(emulated = true) +@GwtCompatible @SuppressWarnings("cast") // redundant casts are intentional and harmless public class IntArrayAsListTest extends TestCase { @@ -51,7 +50,6 @@ public class IntArrayAsListTest extends TestCase { return Ints.asList(temp); } - @GwtIncompatible("suite") public static Test suite() { List<ListTestSuiteBuilder<Integer>> builders = ImmutableList.of( diff --git a/guava-tests/test/com/google/common/primitives/IntsTest.java b/guava-tests/test/com/google/common/primitives/IntsTest.java index 6ac5185..8265b80 100644 --- a/guava-tests/test/com/google/common/primitives/IntsTest.java +++ b/guava-tests/test/com/google/common/primitives/IntsTest.java @@ -76,7 +76,7 @@ public class IntsTest extends TestCase { assertEquals(LEAST, Ints.saturatedCast(Long.MIN_VALUE)); } - private static void assertCastFails(long value) { + private void assertCastFails(long value) { try { Ints.checkedCast(value); fail("Cast to int should have failed: " + value); @@ -356,24 +356,6 @@ public class IntsTest extends TestCase { } } - public void testToArray_withConversion() { - int[] array = {0, 1, 2}; - - List<Byte> bytes = Arrays.asList((byte) 0, (byte) 1, (byte) 2); - List<Short> shorts = Arrays.asList((short) 0, (short) 1, (short) 2); - List<Integer> ints = Arrays.asList(0, 1, 2); - List<Float> floats = Arrays.asList((float) 0, (float) 1, (float) 2); - List<Long> longs = Arrays.asList((long) 0, (long) 1, (long) 2); - List<Double> doubles = Arrays.asList((double) 0, (double) 1, (double) 2); - - assertTrue(Arrays.equals(array, Ints.toArray(bytes))); - assertTrue(Arrays.equals(array, Ints.toArray(shorts))); - assertTrue(Arrays.equals(array, Ints.toArray(ints))); - assertTrue(Arrays.equals(array, Ints.toArray(floats))); - assertTrue(Arrays.equals(array, Ints.toArray(longs))); - assertTrue(Arrays.equals(array, Ints.toArray(doubles))); - } - public void testAsList_isAView() { int[] array = {(int) 0, (int) 1}; List<Integer> list = Ints.asList(array); @@ -411,8 +393,10 @@ public class IntsTest extends TestCase { } @GwtIncompatible("NullPointerTester") - public void testNulls() { - new NullPointerTester().testAllPublicStaticMethods(Ints.class); + public void testNulls() throws Exception { + NullPointerTester tester = new NullPointerTester(); + tester.setDefault(int[].class, new int[0]); + tester.testAllPublicStaticMethods(Ints.class); } @GwtIncompatible("AndroidInteger") diff --git a/guava-tests/test/com/google/common/primitives/LongArrayAsListTest.java b/guava-tests/test/com/google/common/primitives/LongArrayAsListTest.java index c5049c4..1fa140f 100644 --- a/guava-tests/test/com/google/common/primitives/LongArrayAsListTest.java +++ b/guava-tests/test/com/google/common/primitives/LongArrayAsListTest.java @@ -19,7 +19,6 @@ package com.google.common.primitives; import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.annotations.GwtCompatible; -import com.google.common.annotations.GwtIncompatible; import com.google.common.collect.ImmutableList; import com.google.common.collect.testing.ListTestSuiteBuilder; import com.google.common.collect.testing.SampleElements; @@ -39,7 +38,7 @@ import java.util.List; * * @author Kevin Bourrillion */ -@GwtCompatible(emulated = true) +@GwtCompatible public class LongArrayAsListTest extends TestCase { private static List<Long> asList(Long[] values) { @@ -50,7 +49,6 @@ public class LongArrayAsListTest extends TestCase { return Longs.asList(temp); } - @GwtIncompatible("suite") public static Test suite() { List<ListTestSuiteBuilder<Long>> builders = ImmutableList.of( diff --git a/guava-tests/test/com/google/common/primitives/LongsTest.java b/guava-tests/test/com/google/common/primitives/LongsTest.java index c5098c4..b92dd67 100644 --- a/guava-tests/test/com/google/common/primitives/LongsTest.java +++ b/guava-tests/test/com/google/common/primitives/LongsTest.java @@ -16,9 +16,6 @@ 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.collect.testing.Helpers; @@ -27,7 +24,6 @@ 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; @@ -48,8 +44,11 @@ public class LongsTest extends TestCase { private static final long[] ARRAY234 = {(long) 2, (long) 3, (long) 4}; + private static final long LEAST = Long.MIN_VALUE; + private static final long GREATEST = Long.MAX_VALUE; + private static final long[] VALUES = - { MIN_VALUE, (long) -1, (long) 0, (long) 1, MAX_VALUE }; + { LEAST, (long) -1, (long) 0, (long) 1, GREATEST }; @GwtIncompatible("Long.hashCode returns different values in GWT.") public void testHashCode() { @@ -151,8 +150,8 @@ public class LongsTest extends TestCase { } public void testMax() { - assertEquals(MIN_VALUE, Longs.max(MIN_VALUE)); - assertEquals(MAX_VALUE, Longs.max(MAX_VALUE)); + assertEquals(LEAST, Longs.max(LEAST)); + assertEquals(GREATEST, Longs.max(GREATEST)); assertEquals((long) 9, Longs.max( (long) 8, (long) 6, (long) 7, (long) 5, (long) 3, (long) 0, (long) 9)); @@ -167,8 +166,8 @@ public class LongsTest extends TestCase { } public void testMin() { - assertEquals(MIN_VALUE, Longs.min(MIN_VALUE)); - assertEquals(MAX_VALUE, Longs.min(MAX_VALUE)); + assertEquals(LEAST, Longs.min(LEAST)); + assertEquals(GREATEST, Longs.min(GREATEST)); assertEquals((long) 0, Longs.min( (long) 8, (long) 6, (long) 7, (long) 5, (long) 3, (long) 0, (long) 9)); @@ -189,23 +188,19 @@ public class LongsTest extends TestCase { 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)); - } - + @GwtIncompatible("Longs.toByteArray") public void testToByteArray() { - assertByteArrayEquals( + assertTrue(Arrays.equals( new byte[] {0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19}, - Longs.toByteArray(0x1213141516171819L)); - assertByteArrayEquals( + Longs.toByteArray(0x1213141516171819L))); + assertTrue(Arrays.equals( new byte[] { (byte) 0xFF, (byte) 0xEE, (byte) 0xDD, (byte) 0xCC, (byte) 0xBB, (byte) 0xAA, (byte) 0x99, (byte) 0x88}, - Longs.toByteArray(0xFFEEDDCCBBAA9988L)); + Longs.toByteArray(0xFFEEDDCCBBAA9988L))); } + @GwtIncompatible("Longs.fromByteArray") public void testFromByteArray() { assertEquals(0x1213141516171819L, Longs.fromByteArray( new byte[] {0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x33})); @@ -221,6 +216,7 @@ public class LongsTest extends TestCase { } } + @GwtIncompatible("Longs.fromBytes") public void testFromBytes() { assertEquals(0x1213141516171819L, Longs.fromBytes( (byte) 0x12, (byte) 0x13, (byte) 0x14, (byte) 0x15, @@ -230,6 +226,7 @@ public class LongsTest extends TestCase { (byte) 0xBB, (byte) 0xAA, (byte) 0x99, (byte) 0x88)); } + @GwtIncompatible("Longs.fromByteArray, Longs.toByteArray") public void testByteArrayRoundTrips() { Random r = new Random(5); byte[] b = new byte[Longs.BYTES]; @@ -279,14 +276,14 @@ public class LongsTest extends TestCase { 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[] {LEAST}, + new long[] {LEAST, LEAST}, + new long[] {LEAST, (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}); + new long[] {(long) 1, LEAST}, + new long[] {GREATEST, GREATEST - (long) 1}, + new long[] {GREATEST, GREATEST}, + new long[] {GREATEST, GREATEST, GREATEST}); Comparator<long[]> comparator = Longs.lexicographicalComparator(); Helpers.testComparator(comparator, ordered); @@ -339,24 +336,6 @@ public class LongsTest extends TestCase { } } - public void testToArray_withConversion() { - long[] array = {(long) 0, (long) 1, (long) 2}; - - List<Byte> bytes = Arrays.asList((byte) 0, (byte) 1, (byte) 2); - List<Short> shorts = Arrays.asList((short) 0, (short) 1, (short) 2); - List<Integer> ints = Arrays.asList(0, 1, 2); - List<Float> floats = Arrays.asList((float) 0, (float) 1, (float) 2); - List<Long> longs = Arrays.asList((long) 0, (long) 1, (long) 2); - List<Double> doubles = Arrays.asList((double) 0, (double) 1, (double) 2); - - assertTrue(Arrays.equals(array, Longs.toArray(bytes))); - assertTrue(Arrays.equals(array, Longs.toArray(shorts))); - assertTrue(Arrays.equals(array, Longs.toArray(ints))); - assertTrue(Arrays.equals(array, Longs.toArray(floats))); - assertTrue(Arrays.equals(array, Longs.toArray(longs))); - assertTrue(Arrays.equals(array, Longs.toArray(doubles))); - } - public void testAsList_isAView() { long[] array = {(long) 0, (long) 1}; List<Long> list = Longs.asList(array); @@ -394,35 +373,9 @@ public class LongsTest extends TestCase { } @GwtIncompatible("NullPointerTester") - public void testNulls() { - new NullPointerTester().testAllPublicStaticMethods(Longs.class); - } - - @GwtIncompatible("AndroidInteger") - public void testTryParse() { - tryParseAndAssertEquals(0L, "0"); - tryParseAndAssertEquals(0L, "-0"); - tryParseAndAssertEquals(1L, "1"); - tryParseAndAssertEquals(-1L, "-1"); - tryParseAndAssertEquals(8900L, "8900"); - tryParseAndAssertEquals(-8900L, "-8900"); - tryParseAndAssertEquals(MAX_VALUE, Long.toString(MAX_VALUE)); - tryParseAndAssertEquals(MIN_VALUE, Long.toString(MIN_VALUE)); - assertNull(Longs.tryParse("")); - assertNull(Longs.tryParse("-")); - assertNull(Longs.tryParse("+1")); - assertNull(Longs.tryParse("999999999999999999999999")); - assertNull("Max integer + 1", - Longs.tryParse(BigInteger.valueOf(MAX_VALUE).add(BigInteger.ONE).toString())); - assertNull("Min integer - 1", - Longs.tryParse(BigInteger.valueOf(MIN_VALUE).subtract(BigInteger.ONE).toString())); - } - - /** - * Applies {@link Longs#tryParse(String)} to the given string and asserts that - * the result is as expected. - */ - private static void tryParseAndAssertEquals(Long expected, String value) { - assertEquals(expected, Longs.tryParse(value)); + public void testNulls() throws Exception { + NullPointerTester tester = new NullPointerTester(); + tester.setDefault(long[].class, new long[0]); + tester.testAllPublicStaticMethods(Longs.class); } } diff --git a/guava-tests/test/com/google/common/primitives/PackageSanityTests.java b/guava-tests/test/com/google/common/primitives/PackageSanityTests.java deleted file mode 100644 index 3f3e745..0000000 --- a/guava-tests/test/com/google/common/primitives/PackageSanityTests.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (C) 2012 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 com.google.common.testing.AbstractPackageSanityTests; - -/** - * Tests basic sanity for each class in the package. - * - * @author Ben Yu - */ - -public class PackageSanityTests extends AbstractPackageSanityTests { - public PackageSanityTests() { - setDefault(String.class, "string"); - } -} diff --git a/guava-tests/test/com/google/common/primitives/PrimitivesTest.java b/guava-tests/test/com/google/common/primitives/PrimitivesTest.java index b3ef33c..9fba2aa 100644 --- a/guava-tests/test/com/google/common/primitives/PrimitivesTest.java +++ b/guava-tests/test/com/google/common/primitives/PrimitivesTest.java @@ -76,7 +76,7 @@ public class PrimitivesTest extends TestCase { } } - public void testNullPointerExceptions() { + public void testNullPointerExceptions() throws Exception { NullPointerTester tester = new NullPointerTester(); tester.testAllPublicStaticMethods(Primitives.class); } diff --git a/guava-tests/test/com/google/common/primitives/ShortArrayAsListTest.java b/guava-tests/test/com/google/common/primitives/ShortArrayAsListTest.java index 78086e4..3fb6a4d 100644 --- a/guava-tests/test/com/google/common/primitives/ShortArrayAsListTest.java +++ b/guava-tests/test/com/google/common/primitives/ShortArrayAsListTest.java @@ -19,7 +19,6 @@ package com.google.common.primitives; import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.annotations.GwtCompatible; -import com.google.common.annotations.GwtIncompatible; import com.google.common.collect.ImmutableList; import com.google.common.collect.testing.ListTestSuiteBuilder; import com.google.common.collect.testing.SampleElements; @@ -39,7 +38,7 @@ import java.util.List; * * @author Kevin Bourrillion */ -@GwtCompatible(emulated = true) +@GwtCompatible public class ShortArrayAsListTest extends TestCase { private static List<Short> asList(Short[] values) { @@ -50,7 +49,6 @@ public class ShortArrayAsListTest extends TestCase { return Shorts.asList(temp); } - @GwtIncompatible("suite") public static Test suite() { List<ListTestSuiteBuilder<Short>> builders = ImmutableList.of( diff --git a/guava-tests/test/com/google/common/primitives/ShortsTest.java b/guava-tests/test/com/google/common/primitives/ShortsTest.java index 3cc7b61..e98510a 100644 --- a/guava-tests/test/com/google/common/primitives/ShortsTest.java +++ b/guava-tests/test/com/google/common/primitives/ShortsTest.java @@ -76,7 +76,7 @@ public class ShortsTest extends TestCase { assertEquals(LEAST, Shorts.saturatedCast(Long.MIN_VALUE)); } - private static void assertCastFails(long value) { + private void assertCastFails(long value) { try { Shorts.checkedCast(value); fail("Cast to short should have failed: " + value); @@ -363,24 +363,6 @@ public class ShortsTest extends TestCase { } } - public void testToArray_withConversion() { - short[] array = {(short) 0, (short) 1, (short) 2}; - - List<Byte> bytes = Arrays.asList((byte) 0, (byte) 1, (byte) 2); - List<Short> shorts = Arrays.asList((short) 0, (short) 1, (short) 2); - List<Integer> ints = Arrays.asList(0, 1, 2); - List<Float> floats = Arrays.asList((float) 0, (float) 1, (float) 2); - List<Long> longs = Arrays.asList((long) 0, (long) 1, (long) 2); - List<Double> doubles = Arrays.asList((double) 0, (double) 1, (double) 2); - - assertTrue(Arrays.equals(array, Shorts.toArray(bytes))); - assertTrue(Arrays.equals(array, Shorts.toArray(shorts))); - assertTrue(Arrays.equals(array, Shorts.toArray(ints))); - assertTrue(Arrays.equals(array, Shorts.toArray(floats))); - assertTrue(Arrays.equals(array, Shorts.toArray(longs))); - assertTrue(Arrays.equals(array, Shorts.toArray(doubles))); - } - public void testAsList_isAView() { short[] array = {(short) 0, (short) 1}; List<Short> list = Shorts.asList(array); @@ -418,7 +400,9 @@ public class ShortsTest extends TestCase { } @GwtIncompatible("NullPointerTester") - public void testNulls() { - new NullPointerTester().testAllPublicStaticMethods(Shorts.class); + public void testNulls() throws Exception { + NullPointerTester tester = new NullPointerTester(); + tester.setDefault(short[].class, new short[0]); + tester.testAllPublicStaticMethods(Shorts.class); } } diff --git a/guava-tests/test/com/google/common/primitives/SignedBytesTest.java b/guava-tests/test/com/google/common/primitives/SignedBytesTest.java index 8e205fe..18863a5 100644 --- a/guava-tests/test/com/google/common/primitives/SignedBytesTest.java +++ b/guava-tests/test/com/google/common/primitives/SignedBytesTest.java @@ -65,7 +65,7 @@ public class SignedBytesTest extends TestCase { assertEquals(LEAST, SignedBytes.saturatedCast(Long.MIN_VALUE)); } - private static void assertCastFails(long value) { + private void assertCastFails(long value) { try { SignedBytes.checkedCast(value); fail("Cast to byte should have failed: " + value); @@ -155,7 +155,9 @@ public class SignedBytesTest extends TestCase { } @GwtIncompatible("NullPointerTester") - public void testNulls() { - new NullPointerTester().testAllPublicStaticMethods(SignedBytes.class); + public void testNulls() throws Exception { + NullPointerTester tester = new NullPointerTester(); + tester.setDefault(byte[].class, new byte[0]); + tester.testAllPublicStaticMethods(SignedBytes.class); } } diff --git a/guava-tests/test/com/google/common/primitives/UnsignedBytesTest.java b/guava-tests/test/com/google/common/primitives/UnsignedBytesTest.java index 916db9e..88ef454 100644 --- a/guava-tests/test/com/google/common/primitives/UnsignedBytesTest.java +++ b/guava-tests/test/com/google/common/primitives/UnsignedBytesTest.java @@ -30,7 +30,6 @@ import java.util.List; * Unit test for {@link UnsignedBytes}. * * @author Kevin Bourrillion - * @author Louis Wasserman */ public class UnsignedBytesTest extends TestCase { private static final byte LEAST = 0; @@ -71,7 +70,7 @@ public class UnsignedBytesTest extends TestCase { assertEquals(LEAST, UnsignedBytes.saturatedCast(Long.MIN_VALUE)); } - private static void assertCastFails(long value) { + private void assertCastFails(long value) { try { UnsignedBytes.checkedCast(value); fail("Cast to byte should have failed: " + value); @@ -124,95 +123,6 @@ public class UnsignedBytesTest extends TestCase { assertEquals(GREATEST, UnsignedBytes.min(GREATEST)); assertEquals((byte) 0, UnsignedBytes.min( (byte) 0, (byte) -128, (byte) -1, (byte) 127, (byte) 1)); - assertEquals((byte) 0, UnsignedBytes.min( - (byte) -1, (byte) 127, (byte) 1, (byte) -128, (byte) 0)); - } - - private static void assertParseFails(String value) { - try { - UnsignedBytes.parseUnsignedByte(value); - fail(); - } catch (NumberFormatException expected) { - } - } - - public void testParseUnsignedByte() { - // We can easily afford to test this exhaustively. - for (int i = 0; i <= 0xff; i++) { - assertEquals((byte) i, UnsignedBytes.parseUnsignedByte(Integer.toString(i))); - } - assertParseFails("1000"); - assertParseFails("-1"); - assertParseFails("-128"); - assertParseFails("256"); - } - - public void testMaxValue() { - assertTrue(UnsignedBytes - .compare(UnsignedBytes.MAX_VALUE, (byte) (UnsignedBytes.MAX_VALUE + 1)) > 0); - } - - private static void assertParseFails(String value, int radix) { - try { - UnsignedBytes.parseUnsignedByte(value, radix); - fail(); - } catch (NumberFormatException expected) { - } - } - - public void testParseUnsignedByteWithRadix() throws NumberFormatException { - // We can easily afford to test this exhaustively. - for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) { - for (int i = 0; i <= 0xff; i++) { - assertEquals((byte) i, UnsignedBytes.parseUnsignedByte(Integer.toString(i, radix), radix)); - } - assertParseFails(Integer.toString(1000, radix), radix); - assertParseFails(Integer.toString(-1, radix), radix); - assertParseFails(Integer.toString(-128, radix), radix); - assertParseFails(Integer.toString(256, radix), radix); - } - } - - public void testParseUnsignedByteThrowsExceptionForInvalidRadix() { - // Valid radix values are Character.MIN_RADIX to Character.MAX_RADIX, - // inclusive. - try { - UnsignedBytes.parseUnsignedByte("0", Character.MIN_RADIX - 1); - fail(); - } catch (NumberFormatException nfe) { - // expected - } - - try { - UnsignedBytes.parseUnsignedByte("0", Character.MAX_RADIX + 1); - fail(); - } catch (NumberFormatException nfe) { - // expected - } - - // The radix is used as an array index, so try a negative value. - try { - UnsignedBytes.parseUnsignedByte("0", -1); - fail(); - } catch (NumberFormatException nfe) { - // expected - } - } - - public void testToString() { - // We can easily afford to test this exhaustively. - for (int i = 0; i <= 0xff; i++) { - assertEquals(Integer.toString(i), UnsignedBytes.toString((byte) i)); - } - } - - public void testToStringWithRadix() { - // We can easily afford to test this exhaustively. - for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) { - for (int i = 0; i <= 0xff; i++) { - assertEquals(Integer.toString(i, radix), UnsignedBytes.toString((byte) i, radix)); - } - } } public void testJoin() { @@ -254,7 +164,9 @@ public class UnsignedBytesTest extends TestCase { assertSame(javaImpl, SerializableTester.reserialize(javaImpl)); } - public void testNulls() { - new NullPointerTester().testAllPublicStaticMethods(UnsignedBytes.class); + public void testNulls() throws Exception { + NullPointerTester tester = new NullPointerTester(); + tester.setDefault(byte[].class, new byte[0]); + tester.testAllPublicStaticMethods(UnsignedBytes.class); } } diff --git a/guava-tests/test/com/google/common/primitives/UnsignedIntegerTest.java b/guava-tests/test/com/google/common/primitives/UnsignedIntegerTest.java index 6297261..fece9c9 100644 --- a/guava-tests/test/com/google/common/primitives/UnsignedIntegerTest.java +++ b/guava-tests/test/com/google/common/primitives/UnsignedIntegerTest.java @@ -1,11 +1,11 @@ /* * Copyright (C) 2011 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 @@ -14,6 +14,8 @@ package com.google.common.primitives; +import junit.framework.TestCase; + import com.google.common.annotations.GwtCompatible; import com.google.common.annotations.GwtIncompatible; import com.google.common.collect.ImmutableSet; @@ -21,90 +23,47 @@ import com.google.common.testing.EqualsTester; import com.google.common.testing.NullPointerTester; import com.google.common.testing.SerializableTester; -import junit.framework.TestCase; - -import java.math.BigInteger; - /** * Tests for {@code UnsignedInteger}. - * + * * @author Louis Wasserman */ @GwtCompatible(emulated = true) public class UnsignedIntegerTest extends TestCase { private static final ImmutableSet<Integer> TEST_INTS; - private static final ImmutableSet<Long> TEST_LONGS; private static int force32(int value) { - // GWT doesn't consistently overflow values to make them 32-bit, so we need to force it. + // GWT doesn't overflow values to make them 32-bit, so we need to force it. return value & 0xffffffff; } static { ImmutableSet.Builder<Integer> testIntsBuilder = ImmutableSet.builder(); - ImmutableSet.Builder<Long> testLongsBuilder = ImmutableSet.builder(); for (int i = -3; i <= 3; i++) { - testIntsBuilder - .add(i) - .add(force32(Integer.MIN_VALUE + i)) - .add(force32(Integer.MAX_VALUE + i)); - testLongsBuilder - .add((long) i) - .add((long) Integer.MIN_VALUE + i) - .add((long) Integer.MAX_VALUE + i) - .add((1L << 32) + i); + testIntsBuilder.add(i).add(-i).add(force32(Integer.MIN_VALUE + i)) + .add(force32(Integer.MAX_VALUE + i)); } TEST_INTS = testIntsBuilder.build(); - TEST_LONGS = testLongsBuilder.build(); } - public void testFromIntBitsAndIntValueAreInverses() { + public void testAsUnsignedAndIntValueAreInverses() { for (int value : TEST_INTS) { - assertEquals(UnsignedInts.toString(value), value, UnsignedInteger.fromIntBits(value) + assertEquals(UnsignedInts.toString(value), value, UnsignedInteger.asUnsigned(value) .intValue()); } } - public void testFromIntBitsLongValue() { + public void testAsUnsignedLongValue() { for (int value : TEST_INTS) { long expected = value & 0xffffffffL; - assertEquals(UnsignedInts.toString(value), expected, UnsignedInteger.fromIntBits(value) + assertEquals(UnsignedInts.toString(value), expected, UnsignedInteger.asUnsigned(value) .longValue()); } } - - public void testValueOfLong() { - long min = 0; - long max = (1L << 32) - 1; - for (long value : TEST_LONGS) { - boolean expectSuccess = value >= min && value <= max; - try { - assertEquals(value, UnsignedInteger.valueOf(value).longValue()); - assertTrue(expectSuccess); - } catch (IllegalArgumentException e) { - assertFalse(expectSuccess); - } - } - } - - public void testValueOfBigInteger() { - long min = 0; - long max = (1L << 32) - 1; - for (long value : TEST_LONGS) { - boolean expectSuccess = value >= min && value <= max; - try { - assertEquals(value, UnsignedInteger.valueOf(BigInteger.valueOf(value)) - .longValue()); - assertTrue(expectSuccess); - } catch (IllegalArgumentException e) { - assertFalse(expectSuccess); - } - } - } public void testToString() { for (int value : TEST_INTS) { - UnsignedInteger unsignedValue = UnsignedInteger.fromIntBits(value); + UnsignedInteger unsignedValue = UnsignedInteger.asUnsigned(value); assertEquals(unsignedValue.bigIntegerValue().toString(), unsignedValue.toString()); } } @@ -113,7 +72,7 @@ public class UnsignedIntegerTest extends TestCase { public void testToStringRadix() { for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) { for (int l : TEST_INTS) { - UnsignedInteger value = UnsignedInteger.fromIntBits(l); + UnsignedInteger value = UnsignedInteger.asUnsigned(l); assertEquals(value.bigIntegerValue().toString(radix), value.toString(radix)); } } @@ -123,7 +82,7 @@ public class UnsignedIntegerTest extends TestCase { int[] radices = {2, 3, 5, 7, 10, 12, 16, 21, 31, 36}; for (int radix : radices) { for (int l : TEST_INTS) { - UnsignedInteger value = UnsignedInteger.fromIntBits(l); + UnsignedInteger value = UnsignedInteger.asUnsigned(l); assertEquals(value.bigIntegerValue().toString(radix), value.toString(radix)); } } @@ -131,66 +90,66 @@ public class UnsignedIntegerTest extends TestCase { public void testFloatValue() { for (int value : TEST_INTS) { - UnsignedInteger unsignedValue = UnsignedInteger.fromIntBits(value); + UnsignedInteger unsignedValue = UnsignedInteger.asUnsigned(value); assertEquals(unsignedValue.bigIntegerValue().floatValue(), unsignedValue.floatValue()); } } public void testDoubleValue() { for (int value : TEST_INTS) { - UnsignedInteger unsignedValue = UnsignedInteger.fromIntBits(value); + UnsignedInteger unsignedValue = UnsignedInteger.asUnsigned(value); assertEquals(unsignedValue.bigIntegerValue().doubleValue(), unsignedValue.doubleValue()); } } - public void testPlus() { + public void testAdd() { for (int a : TEST_INTS) { for (int b : TEST_INTS) { - UnsignedInteger aUnsigned = UnsignedInteger.fromIntBits(a); - UnsignedInteger bUnsigned = UnsignedInteger.fromIntBits(b); + UnsignedInteger aUnsigned = UnsignedInteger.asUnsigned(a); + UnsignedInteger bUnsigned = UnsignedInteger.asUnsigned(b); int expected = aUnsigned.bigIntegerValue().add(bUnsigned.bigIntegerValue()).intValue(); - UnsignedInteger unsignedSum = aUnsigned.plus(bUnsigned); + UnsignedInteger unsignedSum = aUnsigned.add(bUnsigned); assertEquals(expected, unsignedSum.intValue()); } } } - public void testMinus() { + public void testSubtract() { for (int a : TEST_INTS) { for (int b : TEST_INTS) { - UnsignedInteger aUnsigned = UnsignedInteger.fromIntBits(a); - UnsignedInteger bUnsigned = UnsignedInteger.fromIntBits(b); + UnsignedInteger aUnsigned = UnsignedInteger.asUnsigned(a); + UnsignedInteger bUnsigned = UnsignedInteger.asUnsigned(b); int expected = force32(aUnsigned.bigIntegerValue().subtract(bUnsigned.bigIntegerValue()).intValue()); - UnsignedInteger unsignedSub = aUnsigned.minus(bUnsigned); + UnsignedInteger unsignedSub = aUnsigned.subtract(bUnsigned); assertEquals(expected, unsignedSub.intValue()); } } } @GwtIncompatible("multiply") - public void testTimes() { + public void testMultiply() { for (int a : TEST_INTS) { for (int b : TEST_INTS) { - UnsignedInteger aUnsigned = UnsignedInteger.fromIntBits(a); - UnsignedInteger bUnsigned = UnsignedInteger.fromIntBits(b); + UnsignedInteger aUnsigned = UnsignedInteger.asUnsigned(a); + UnsignedInteger bUnsigned = UnsignedInteger.asUnsigned(b); int expected = force32(aUnsigned.bigIntegerValue().multiply(bUnsigned.bigIntegerValue()).intValue()); - UnsignedInteger unsignedMul = aUnsigned.times(bUnsigned); + UnsignedInteger unsignedMul = aUnsigned.multiply(bUnsigned); assertEquals(aUnsigned + " * " + bUnsigned, expected, unsignedMul.intValue()); } } } - public void testDividedBy() { + public void testDivide() { for (int a : TEST_INTS) { for (int b : TEST_INTS) { if (b != 0) { - UnsignedInteger aUnsigned = UnsignedInteger.fromIntBits(a); - UnsignedInteger bUnsigned = UnsignedInteger.fromIntBits(b); + UnsignedInteger aUnsigned = UnsignedInteger.asUnsigned(a); + UnsignedInteger bUnsigned = UnsignedInteger.asUnsigned(b); int expected = aUnsigned.bigIntegerValue().divide(bUnsigned.bigIntegerValue()).intValue(); - UnsignedInteger unsignedDiv = aUnsigned.dividedBy(bUnsigned); + UnsignedInteger unsignedDiv = aUnsigned.divide(bUnsigned); assertEquals(expected, unsignedDiv.intValue()); } } @@ -200,32 +159,31 @@ public class UnsignedIntegerTest extends TestCase { public void testDivideByZeroThrows() { for (int a : TEST_INTS) { try { - UnsignedInteger.fromIntBits(a).divide(UnsignedInteger.ZERO); + UnsignedInteger.asUnsigned(a).divide(UnsignedInteger.ZERO); fail("Expected ArithmeticException"); } catch (ArithmeticException expected) {} } } - public void testMod() { + public void testRemainder() { for (int a : TEST_INTS) { for (int b : TEST_INTS) { if (b != 0) { - UnsignedInteger aUnsigned = UnsignedInteger.fromIntBits(a); - UnsignedInteger bUnsigned = UnsignedInteger.fromIntBits(b); + UnsignedInteger aUnsigned = UnsignedInteger.asUnsigned(a); + UnsignedInteger bUnsigned = UnsignedInteger.asUnsigned(b); int expected = - aUnsigned.bigIntegerValue().mod(bUnsigned.bigIntegerValue()).intValue(); - UnsignedInteger unsignedRem = aUnsigned.mod(bUnsigned); + aUnsigned.bigIntegerValue().remainder(bUnsigned.bigIntegerValue()).intValue(); + UnsignedInteger unsignedRem = aUnsigned.remainder(bUnsigned); assertEquals(expected, unsignedRem.intValue()); } } } } - @SuppressWarnings("ReturnValueIgnored") - public void testModByZero() { + public void testRemainderByZero() { for (int a : TEST_INTS) { try { - UnsignedInteger.fromIntBits(a).mod(UnsignedInteger.ZERO); + UnsignedInteger.asUnsigned(a).remainder(UnsignedInteger.ZERO); fail("Expected ArithmeticException"); } catch (ArithmeticException expected) {} } @@ -234,8 +192,8 @@ public class UnsignedIntegerTest extends TestCase { public void testCompare() { for (int a : TEST_INTS) { for (int b : TEST_INTS) { - UnsignedInteger aUnsigned = UnsignedInteger.fromIntBits(a); - UnsignedInteger bUnsigned = UnsignedInteger.fromIntBits(b); + UnsignedInteger aUnsigned = UnsignedInteger.asUnsigned(a); + UnsignedInteger bUnsigned = UnsignedInteger.asUnsigned(b); assertEquals(aUnsigned.bigIntegerValue().compareTo(bUnsigned.bigIntegerValue()), aUnsigned.compareTo(bUnsigned)); } @@ -243,11 +201,11 @@ public class UnsignedIntegerTest extends TestCase { } @GwtIncompatible("too slow") - public void testEquals() { + public void testEqualsAndValueOf() { EqualsTester equalsTester = new EqualsTester(); for (int a : TEST_INTS) { long value = a & 0xffffffffL; - equalsTester.addEqualityGroup(UnsignedInteger.fromIntBits(a), UnsignedInteger.valueOf(value), + equalsTester.addEqualityGroup(UnsignedInteger.asUnsigned(a), UnsignedInteger.valueOf(value), UnsignedInteger.valueOf(Long.toString(value)), UnsignedInteger.valueOf(Long.toString(value, 16), 16)); } @@ -256,7 +214,7 @@ public class UnsignedIntegerTest extends TestCase { public void testIntValue() { for (int a : TEST_INTS) { - UnsignedInteger aUnsigned = UnsignedInteger.fromIntBits(a); + UnsignedInteger aUnsigned = UnsignedInteger.asUnsigned(a); int intValue = aUnsigned.bigIntegerValue().intValue(); assertEquals(intValue, aUnsigned.intValue()); } @@ -265,12 +223,14 @@ public class UnsignedIntegerTest extends TestCase { @GwtIncompatible("serialization") public void testSerialization() { for (int a : TEST_INTS) { - SerializableTester.reserializeAndAssert(UnsignedInteger.fromIntBits(a)); + SerializableTester.reserializeAndAssert(UnsignedInteger.asUnsigned(a)); } } @GwtIncompatible("NullPointerTester") - public void testNulls() { - new NullPointerTester().testAllPublicStaticMethods(UnsignedInteger.class); + public void testNulls() throws Exception { + NullPointerTester tester = new NullPointerTester(); + tester.setDefault(UnsignedInteger.class, UnsignedInteger.ONE); + tester.testAllPublicStaticMethods(UnsignedInteger.class); } } diff --git a/guava-tests/test/com/google/common/primitives/UnsignedIntsTest.java b/guava-tests/test/com/google/common/primitives/UnsignedIntsTest.java index d1fd13c..4ab36de 100644 --- a/guava-tests/test/com/google/common/primitives/UnsignedIntsTest.java +++ b/guava-tests/test/com/google/common/primitives/UnsignedIntsTest.java @@ -1,11 +1,11 @@ /* * Copyright (C) 2011 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 @@ -14,21 +14,17 @@ package com.google.common.primitives; -import com.google.common.annotations.GwtCompatible; -import com.google.common.annotations.GwtIncompatible; -import com.google.common.collect.testing.Helpers; -import com.google.common.testing.NullPointerTester; +import java.util.Random; import junit.framework.TestCase; -import java.util.Arrays; -import java.util.Comparator; -import java.util.List; -import java.util.Random; +import com.google.common.annotations.GwtCompatible; +import com.google.common.annotations.GwtIncompatible; +import com.google.common.testing.NullPointerTester; /** * Tests for UnsignedInts - * + * * @author Louis Wasserman */ @GwtCompatible(emulated = true) @@ -45,9 +41,6 @@ public class UnsignedIntsTest extends TestCase { 0xfffffffdL, 0xfffffffeL, 0xffffffffL}; - - private static final int LEAST = (int) 0L; - private static final int GREATEST = (int) 0xffffffffL; public void testToLong() { for (long a : UNSIGNED_INTS) { @@ -64,57 +57,6 @@ public class UnsignedIntsTest extends TestCase { } } } - - public void testMax_noArgs() { - try { - UnsignedInts.max(); - fail(); - } catch (IllegalArgumentException expected) { - } - } - - public void testMax() { - assertEquals(LEAST, UnsignedInts.max(LEAST)); - assertEquals(GREATEST, UnsignedInts.max(GREATEST)); - assertEquals((int) 0xff1a618bL, UnsignedInts.max( - (int) 8L, (int) 6L, (int) 7L, - (int) 0x12345678L, (int) 0x5a4316b8L, - (int) 0xff1a618bL, (int) 0L)); - } - - public void testMin_noArgs() { - try { - UnsignedInts.min(); - fail(); - } catch (IllegalArgumentException expected) { - } - } - - public void testMin() { - assertEquals(LEAST, UnsignedInts.min(LEAST)); - assertEquals(GREATEST, UnsignedInts.min(GREATEST)); - assertEquals((int) 0L, UnsignedInts.min( - (int) 8L, (int) 6L, (int) 7L, - (int) 0x12345678L, (int) 0x5a4316b8L, - (int) 0xff1a618bL, (int) 0L)); - } - - public void testLexicographicalComparator() { - List<int[]> ordered = Arrays.asList( - new int[] {}, - new int[] {LEAST}, - new int[] {LEAST, LEAST}, - new int[] {LEAST, (int) 1L}, - new int[] {(int) 1L}, - new int[] {(int) 1L, LEAST}, - new int[] {GREATEST, (GREATEST - (int) 1L)}, - new int[] {GREATEST, GREATEST}, - new int[] {GREATEST, GREATEST, GREATEST} - ); - - Comparator<int[]> comparator = UnsignedInts.lexicographicalComparator(); - Helpers.testComparator(comparator, ordered); - } public void testDivide() { for (long a : UNSIGNED_INTS) { @@ -171,7 +113,7 @@ public class UnsignedIntsTest extends TestCase { } catch (NumberFormatException expected) {} } - public void testParseIntWithRadix() throws NumberFormatException { + public void testParseLongWithRadix() throws NumberFormatException { for (long a : UNSIGNED_INTS) { for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) { assertEquals((int) a, UnsignedInts.parseUnsignedInt(Long.toString(a, radix), radix)); @@ -194,7 +136,7 @@ public class UnsignedIntsTest extends TestCase { } } - public void testParseIntThrowsExceptionForInvalidRadix() { + public void testParseLongThrowsExceptionForInvalidRadix() { // Valid radix values are Character.MIN_RADIX to Character.MAX_RADIX, // inclusive. try { @@ -214,43 +156,6 @@ public class UnsignedIntsTest extends TestCase { } catch (NumberFormatException expected) {} } - public void testDecodeInt() { - assertEquals(0xffffffff, UnsignedInts.decode("0xffffffff")); - assertEquals(01234567, UnsignedInts.decode("01234567")); // octal - assertEquals(0x12345678, UnsignedInts.decode("#12345678")); - assertEquals(76543210, UnsignedInts.decode("76543210")); - assertEquals(0x13579135, UnsignedInts.decode("0x13579135")); - assertEquals(0x13579135, UnsignedInts.decode("0X13579135")); - assertEquals(0, UnsignedInts.decode("0")); - } - - public void testDecodeIntFails() { - try { - // One more than maximum value - UnsignedInts.decode("0xfffffffff"); - fail(); - } catch (NumberFormatException expected) { - } - - try { - UnsignedInts.decode("-5"); - fail(); - } catch (NumberFormatException expected) { - } - - try { - UnsignedInts.decode("-0x5"); - fail(); - } catch (NumberFormatException expected) { - } - - try { - UnsignedInts.decode("-05"); - fail(); - } catch (NumberFormatException expected) { - } - } - public void testToString() { int[] bases = {2, 5, 7, 8, 10, 16}; for (long a : UNSIGNED_INTS) { @@ -260,21 +165,10 @@ public class UnsignedIntsTest extends TestCase { } } - public void testJoin() { - assertEquals("", join()); - assertEquals("1", join(1)); - assertEquals("1,2", join(1, 2)); - assertEquals("4294967295,2147483648", join(-1, Integer.MIN_VALUE)); - - assertEquals("123", UnsignedInts.join("", 1, 2, 3)); - } - - private static String join(int... values) { - return UnsignedInts.join(",", values); - } - @GwtIncompatible("NullPointerTester") - public void testNulls() { - new NullPointerTester().testAllPublicStaticMethods(UnsignedInts.class); + public void testNulls() throws Exception { + NullPointerTester tester = new NullPointerTester(); + tester.setDefault(int[].class, new int[0]); + tester.testAllPublicStaticMethods(UnsignedInts.class); } } diff --git a/guava-tests/test/com/google/common/primitives/UnsignedLongTest.java b/guava-tests/test/com/google/common/primitives/UnsignedLongTest.java index 1df86cb..81f1644 100644 --- a/guava-tests/test/com/google/common/primitives/UnsignedLongTest.java +++ b/guava-tests/test/com/google/common/primitives/UnsignedLongTest.java @@ -14,6 +14,10 @@ package com.google.common.primitives; +import java.math.BigInteger; + +import junit.framework.TestCase; + import com.google.common.annotations.GwtCompatible; import com.google.common.annotations.GwtIncompatible; import com.google.common.collect.ImmutableSet; @@ -21,10 +25,6 @@ import com.google.common.testing.EqualsTester; import com.google.common.testing.NullPointerTester; import com.google.common.testing.SerializableTester; -import junit.framework.TestCase; - -import java.math.BigInteger; - /** * Tests for {@code UnsignedLong}. * @@ -33,11 +33,9 @@ import java.math.BigInteger; @GwtCompatible(emulated = true) public class UnsignedLongTest extends TestCase { private static final ImmutableSet<Long> TEST_LONGS; - private static final ImmutableSet<BigInteger> TEST_BIG_INTEGERS; static { ImmutableSet.Builder<Long> testLongsBuilder = ImmutableSet.builder(); - ImmutableSet.Builder<BigInteger> testBigIntegersBuilder = ImmutableSet.builder(); for (long i = -3; i <= 3; i++) { testLongsBuilder .add(i) @@ -45,24 +43,14 @@ public class UnsignedLongTest extends TestCase { .add(Long.MIN_VALUE + i) .add(Integer.MIN_VALUE + i) .add(Integer.MAX_VALUE + i); - BigInteger bigI = BigInteger.valueOf(i); - testBigIntegersBuilder - .add(bigI) - .add(BigInteger.valueOf(Long.MAX_VALUE).add(bigI)) - .add(BigInteger.valueOf(Long.MIN_VALUE).add(bigI)) - .add(BigInteger.valueOf(Integer.MAX_VALUE).add(bigI)) - .add(BigInteger.valueOf(Integer.MIN_VALUE).add(bigI)) - .add(BigInteger.ONE.shiftLeft(63).add(bigI)) - .add(BigInteger.ONE.shiftLeft(64).add(bigI)); } TEST_LONGS = testLongsBuilder.build(); - TEST_BIG_INTEGERS = testBigIntegersBuilder.build(); } public void testAsUnsignedAndLongValueAreInverses() { for (long value : TEST_LONGS) { assertEquals( - UnsignedLongs.toString(value), value, UnsignedLong.fromLongBits(value).longValue()); + UnsignedLongs.toString(value), value, UnsignedLong.asUnsigned(value).longValue()); } } @@ -72,40 +60,13 @@ public class UnsignedLongTest extends TestCase { ? BigInteger.valueOf(value) : BigInteger.valueOf(value).add(BigInteger.ZERO.setBit(64)); assertEquals(UnsignedLongs.toString(value), expected, - UnsignedLong.fromLongBits(value).bigIntegerValue()); - } - } - - public void testValueOfLong() { - for (long value : TEST_LONGS) { - boolean expectSuccess = value >= 0; - try { - assertEquals(value, UnsignedLong.valueOf(value).longValue()); - assertTrue(expectSuccess); - } catch (IllegalArgumentException e) { - assertFalse(expectSuccess); - } - } - } - - public void testValueOfBigInteger() { - BigInteger min = BigInteger.ZERO; - BigInteger max = UnsignedLong.MAX_VALUE.bigIntegerValue(); - for (BigInteger big : TEST_BIG_INTEGERS) { - boolean expectSuccess = - big.compareTo(min) >= 0 && big.compareTo(max) <= 0; - try { - assertEquals(big, UnsignedLong.valueOf(big).bigIntegerValue()); - assertTrue(expectSuccess); - } catch (IllegalArgumentException e) { - assertFalse(expectSuccess); - } + UnsignedLong.asUnsigned(value).bigIntegerValue()); } } public void testToString() { for (long value : TEST_LONGS) { - UnsignedLong unsignedValue = UnsignedLong.fromLongBits(value); + UnsignedLong unsignedValue = UnsignedLong.asUnsigned(value); assertEquals(unsignedValue.bigIntegerValue().toString(), unsignedValue.toString()); } } @@ -114,7 +75,7 @@ public class UnsignedLongTest extends TestCase { public void testToStringRadix() { for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) { for (long l : TEST_LONGS) { - UnsignedLong value = UnsignedLong.fromLongBits(l); + UnsignedLong value = UnsignedLong.asUnsigned(l); assertEquals(value.bigIntegerValue().toString(radix), value.toString(radix)); } } @@ -124,7 +85,7 @@ public class UnsignedLongTest extends TestCase { int[] radices = {2, 3, 5, 7, 10, 12, 16, 21, 31, 36}; for (int radix : radices) { for (long l : TEST_LONGS) { - UnsignedLong value = UnsignedLong.fromLongBits(l); + UnsignedLong value = UnsignedLong.asUnsigned(l); assertEquals(value.bigIntegerValue().toString(radix), value.toString(radix)); } } @@ -132,112 +93,110 @@ public class UnsignedLongTest extends TestCase { public void testFloatValue() { for (long value : TEST_LONGS) { - UnsignedLong unsignedValue = UnsignedLong.fromLongBits(value); + UnsignedLong unsignedValue = UnsignedLong.asUnsigned(value); assertEquals(unsignedValue.bigIntegerValue().floatValue(), unsignedValue.floatValue()); } } public void testDoubleValue() { for (long value : TEST_LONGS) { - UnsignedLong unsignedValue = UnsignedLong.fromLongBits(value); + UnsignedLong unsignedValue = UnsignedLong.asUnsigned(value); assertEquals(unsignedValue.bigIntegerValue().doubleValue(), unsignedValue.doubleValue()); } } - public void testPlus() { + public void testAdd() { for (long a : TEST_LONGS) { for (long b : TEST_LONGS) { - UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a); - UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b); + UnsignedLong aUnsigned = UnsignedLong.asUnsigned(a); + UnsignedLong bUnsigned = UnsignedLong.asUnsigned(b); long expected = aUnsigned .bigIntegerValue() .add(bUnsigned.bigIntegerValue()) .longValue(); - UnsignedLong unsignedSum = aUnsigned.plus(bUnsigned); + UnsignedLong unsignedSum = aUnsigned.add(bUnsigned); assertEquals(expected, unsignedSum.longValue()); } } } - public void testMinus() { + public void testSubtract() { for (long a : TEST_LONGS) { for (long b : TEST_LONGS) { - UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a); - UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b); + UnsignedLong aUnsigned = UnsignedLong.asUnsigned(a); + UnsignedLong bUnsigned = UnsignedLong.asUnsigned(b); long expected = aUnsigned .bigIntegerValue() .subtract(bUnsigned.bigIntegerValue()) .longValue(); - UnsignedLong unsignedSub = aUnsigned.minus(bUnsigned); + UnsignedLong unsignedSub = aUnsigned.subtract(bUnsigned); assertEquals(expected, unsignedSub.longValue()); } } } - public void testTimes() { + public void testMultiply() { for (long a : TEST_LONGS) { for (long b : TEST_LONGS) { - UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a); - UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b); + UnsignedLong aUnsigned = UnsignedLong.asUnsigned(a); + UnsignedLong bUnsigned = UnsignedLong.asUnsigned(b); long expected = aUnsigned .bigIntegerValue() .multiply(bUnsigned.bigIntegerValue()) .longValue(); - UnsignedLong unsignedMul = aUnsigned.times(bUnsigned); + UnsignedLong unsignedMul = aUnsigned.multiply(bUnsigned); assertEquals(expected, unsignedMul.longValue()); } } } - public void testDividedBy() { + public void testDivide() { for (long a : TEST_LONGS) { for (long b : TEST_LONGS) { if (b != 0) { - UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a); - UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b); + UnsignedLong aUnsigned = UnsignedLong.asUnsigned(a); + UnsignedLong bUnsigned = UnsignedLong.asUnsigned(b); long expected = aUnsigned .bigIntegerValue() .divide(bUnsigned.bigIntegerValue()) .longValue(); - UnsignedLong unsignedDiv = aUnsigned.dividedBy(bUnsigned); + UnsignedLong unsignedDiv = aUnsigned.divide(bUnsigned); assertEquals(expected, unsignedDiv.longValue()); } } } } - @SuppressWarnings("ReturnValueIgnored") public void testDivideByZeroThrows() { for (long a : TEST_LONGS) { try { - UnsignedLong.fromLongBits(a).dividedBy(UnsignedLong.ZERO); + UnsignedLong.asUnsigned(a).divide(UnsignedLong.ZERO); fail("Expected ArithmeticException"); } catch (ArithmeticException expected) {} } } - public void testMod() { + public void testRemainder() { for (long a : TEST_LONGS) { for (long b : TEST_LONGS) { if (b != 0) { - UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a); - UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b); + UnsignedLong aUnsigned = UnsignedLong.asUnsigned(a); + UnsignedLong bUnsigned = UnsignedLong.asUnsigned(b); long expected = aUnsigned .bigIntegerValue() .remainder(bUnsigned.bigIntegerValue()) .longValue(); - UnsignedLong unsignedRem = aUnsigned.mod(bUnsigned); + UnsignedLong unsignedRem = aUnsigned.remainder(bUnsigned); assertEquals(expected, unsignedRem.longValue()); } } } } - @SuppressWarnings("ReturnValueIgnored") - public void testModByZero() { + public void testRemainderByZero() { for (long a : TEST_LONGS) { try { - UnsignedLong.fromLongBits(a).mod(UnsignedLong.ZERO); + UnsignedLong.asUnsigned(a).remainder(UnsignedLong.ZERO); fail("Expected ArithmeticException"); } catch (ArithmeticException expected) {} } @@ -246,8 +205,8 @@ public class UnsignedLongTest extends TestCase { public void testCompare() { for (long a : TEST_LONGS) { for (long b : TEST_LONGS) { - UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a); - UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b); + UnsignedLong aUnsigned = UnsignedLong.asUnsigned(a); + UnsignedLong bUnsigned = UnsignedLong.asUnsigned(b); assertEquals(aUnsigned.bigIntegerValue().compareTo(bUnsigned.bigIntegerValue()), aUnsigned.compareTo(bUnsigned)); } @@ -255,12 +214,12 @@ public class UnsignedLongTest extends TestCase { } @GwtIncompatible("too slow") - public void testEquals() { + public void testEqualsAndValueOf() { EqualsTester equalsTester = new EqualsTester(); for (long a : TEST_LONGS) { BigInteger big = (a >= 0) ? BigInteger.valueOf(a) : BigInteger.valueOf(a).add(BigInteger.ZERO.setBit(64)); - equalsTester.addEqualityGroup(UnsignedLong.fromLongBits(a), UnsignedLong.valueOf(big), + equalsTester.addEqualityGroup(UnsignedLong.asUnsigned(a), UnsignedLong.valueOf(big), UnsignedLong.valueOf(big.toString()), UnsignedLong.valueOf(big.toString(16), 16)); } equalsTester.testEquals(); @@ -268,7 +227,7 @@ public class UnsignedLongTest extends TestCase { public void testIntValue() { for (long a : TEST_LONGS) { - UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a); + UnsignedLong aUnsigned = UnsignedLong.asUnsigned(a); int intValue = aUnsigned.bigIntegerValue().intValue(); assertEquals(intValue, aUnsigned.intValue()); } @@ -277,12 +236,14 @@ public class UnsignedLongTest extends TestCase { @GwtIncompatible("serialization") public void testSerialization() { for (long a : TEST_LONGS) { - SerializableTester.reserializeAndAssert(UnsignedLong.fromLongBits(a)); + SerializableTester.reserializeAndAssert(UnsignedLong.asUnsigned(a)); } } @GwtIncompatible("NullPointerTester") - public void testNulls() { - new NullPointerTester().testAllPublicStaticMethods(UnsignedLong.class); + public void testNulls() throws Exception { + NullPointerTester tester = new NullPointerTester(); + tester.setDefault(UnsignedLong.class, UnsignedLong.ONE); + tester.testAllPublicStaticMethods(UnsignedLong.class); } } diff --git a/guava-tests/test/com/google/common/primitives/UnsignedLongsTest.java b/guava-tests/test/com/google/common/primitives/UnsignedLongsTest.java index 7abee87..5928f7e 100644 --- a/guava-tests/test/com/google/common/primitives/UnsignedLongsTest.java +++ b/guava-tests/test/com/google/common/primitives/UnsignedLongsTest.java @@ -1,11 +1,11 @@ /* * Copyright (C) 2011 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 @@ -16,97 +16,43 @@ package com.google.common.primitives; import static java.math.BigInteger.ONE; -import com.google.common.annotations.GwtCompatible; -import com.google.common.annotations.GwtIncompatible; -import com.google.common.collect.testing.Helpers; -import com.google.common.testing.NullPointerTester; +import java.math.BigInteger; +import java.util.Random; import junit.framework.TestCase; -import java.math.BigInteger; -import java.util.Arrays; -import java.util.Comparator; -import java.util.List; -import java.util.Random; +import com.google.common.annotations.GwtCompatible; +import com.google.common.annotations.GwtIncompatible; +import com.google.common.testing.NullPointerTester; /** * Tests for UnsignedLongs - * + * * @author Brian Milch * @author Louis Wasserman */ @GwtCompatible(emulated = true) public class UnsignedLongsTest extends TestCase { - private static final long LEAST = 0L; - private static final long GREATEST = 0xffffffffffffffffL; - + public void testCompare() { // max value - assertTrue(UnsignedLongs.compare(0, 0xffffffffffffffffL) < 0); - assertTrue(UnsignedLongs.compare(0xffffffffffffffffL, 0) > 0); + assertTrue((UnsignedLongs.compare(0, 0xffffffffffffffffL) < 0)); + assertTrue((UnsignedLongs.compare(0xffffffffffffffffL, 0) > 0)); // both with high bit set - assertTrue(UnsignedLongs.compare(0xff1a618b7f65ea12L, 0xffffffffffffffffL) < 0); - assertTrue(UnsignedLongs.compare(0xffffffffffffffffL, 0xff1a618b7f65ea12L) > 0); + assertTrue((UnsignedLongs.compare(0xff1a618b7f65ea12L, 0xffffffffffffffffL) < 0)); + assertTrue((UnsignedLongs.compare(0xffffffffffffffffL, 0xff1a618b7f65ea12L) > 0)); // one with high bit set - assertTrue(UnsignedLongs.compare(0x5a4316b8c153ac4dL, 0xff1a618b7f65ea12L) < 0); - assertTrue(UnsignedLongs.compare(0xff1a618b7f65ea12L, 0x5a4316b8c153ac4dL) > 0); + assertTrue((UnsignedLongs.compare(0x5a4316b8c153ac4dL, 0xff1a618b7f65ea12L) < 0)); + assertTrue((UnsignedLongs.compare(0xff1a618b7f65ea12L, 0x5a4316b8c153ac4dL) > 0)); // neither with high bit set - assertTrue(UnsignedLongs.compare(0x5a4316b8c153ac4dL, 0x6cf78a4b139a4e2aL) < 0); - assertTrue(UnsignedLongs.compare(0x6cf78a4b139a4e2aL, 0x5a4316b8c153ac4dL) > 0); + assertTrue((UnsignedLongs.compare(0x5a4316b8c153ac4dL, 0x6cf78a4b139a4e2aL) < 0)); + assertTrue((UnsignedLongs.compare(0x6cf78a4b139a4e2aL, 0x5a4316b8c153ac4dL) > 0)); // same value - assertTrue(UnsignedLongs.compare(0xff1a618b7f65ea12L, 0xff1a618b7f65ea12L) == 0); - } - - public void testMax_noArgs() { - try { - UnsignedLongs.max(); - fail(); - } catch (IllegalArgumentException expected) { - } - } - - public void testMax() { - assertEquals(LEAST, UnsignedLongs.max(LEAST)); - assertEquals(GREATEST, UnsignedLongs.max(GREATEST)); - assertEquals(0xff1a618b7f65ea12L, UnsignedLongs.max( - 0x5a4316b8c153ac4dL, 8L, 100L, - 0L, 0x6cf78a4b139a4e2aL, 0xff1a618b7f65ea12L)); - } - - public void testMin_noArgs() { - try { - UnsignedLongs.min(); - fail(); - } catch (IllegalArgumentException expected) { - } - } - - public void testMin() { - assertEquals(LEAST, UnsignedLongs.min(LEAST)); - assertEquals(GREATEST, UnsignedLongs.min(GREATEST)); - assertEquals(0L, UnsignedLongs.min( - 0x5a4316b8c153ac4dL, 8L, 100L, - 0L, 0x6cf78a4b139a4e2aL, 0xff1a618b7f65ea12L)); - } - - public void testLexicographicalComparator() { - List<long[]> ordered = Arrays.asList( - new long[] {}, - new long[] {LEAST}, - new long[] {LEAST, LEAST}, - new long[] {LEAST, (long) 1}, - new long[] {(long) 1}, - new long[] {(long) 1, LEAST}, - new long[] {GREATEST, GREATEST - (long) 1}, - new long[] {GREATEST, GREATEST}, - new long[] {GREATEST, GREATEST, GREATEST}); - - Comparator<long[]> comparator = UnsignedLongs.lexicographicalComparator(); - Helpers.testComparator(comparator, ordered); + assertTrue((UnsignedLongs.compare(0xff1a618b7f65ea12L, 0xff1a618b7f65ea12L) == 0)); } public void testDivide() { @@ -138,65 +84,33 @@ public class UnsignedLongsTest extends TestCase { long dividend = r.nextLong(); long divisor = r.nextLong(); // Test that the Euclidean property is preserved: - assertEquals(0, - dividend - (divisor * UnsignedLongs.divide(dividend, divisor) - + UnsignedLongs.remainder(dividend, divisor))); + assertTrue(dividend - (divisor * UnsignedLongs.divide(dividend, divisor) + + UnsignedLongs.remainder(dividend, divisor)) == 0); } } public void testParseLong() { - assertEquals(0xffffffffffffffffL, UnsignedLongs.parseUnsignedLong("18446744073709551615")); - assertEquals(0x7fffffffffffffffL, UnsignedLongs.parseUnsignedLong("9223372036854775807")); - assertEquals(0xff1a618b7f65ea12L, UnsignedLongs.parseUnsignedLong("18382112080831834642")); - assertEquals(0x5a4316b8c153ac4dL, UnsignedLongs.parseUnsignedLong("6504067269626408013")); - assertEquals(0x6cf78a4b139a4e2aL, UnsignedLongs.parseUnsignedLong("7851896530399809066")); - try { - // One more than maximum value - UnsignedLongs.parseUnsignedLong("18446744073709551616"); - fail(); - } catch (NumberFormatException expected) { + assertEquals(0xffffffffffffffffL, UnsignedLongs.parseUnsignedLong("18446744073709551615")); + assertEquals(0x7fffffffffffffffL, UnsignedLongs.parseUnsignedLong("9223372036854775807")); + assertEquals(0xff1a618b7f65ea12L, UnsignedLongs.parseUnsignedLong("18382112080831834642")); + assertEquals(0x5a4316b8c153ac4dL, UnsignedLongs.parseUnsignedLong("6504067269626408013")); + assertEquals(0x6cf78a4b139a4e2aL, UnsignedLongs.parseUnsignedLong("7851896530399809066")); + } catch (NumberFormatException e) { + fail(e.getMessage()); } - } - public void testDecodeLong() { - assertEquals(0xffffffffffffffffL, UnsignedLongs.decode("0xffffffffffffffff")); - assertEquals(01234567, UnsignedLongs.decode("01234567")); // octal - assertEquals(0x1234567890abcdefL, UnsignedLongs.decode("#1234567890abcdef")); - assertEquals(987654321012345678L, UnsignedLongs.decode("987654321012345678")); - assertEquals(0x135791357913579L, UnsignedLongs.decode("0x135791357913579")); - assertEquals(0x135791357913579L, UnsignedLongs.decode("0X135791357913579")); - assertEquals(0L, UnsignedLongs.decode("0")); - } - - public void testDecodeLongFails() { + boolean overflowCaught = false; try { // One more than maximum value - UnsignedLongs.decode("0xfffffffffffffffff"); - fail(); - } catch (NumberFormatException expected) { - } - - try { - UnsignedLongs.decode("-5"); - fail(); - } catch (NumberFormatException expected) { - } - - try { - UnsignedLongs.decode("-0x5"); - fail(); - } catch (NumberFormatException expected) { - } - - try { - UnsignedLongs.decode("-05"); - fail(); - } catch (NumberFormatException expected) { + UnsignedLongs.parseUnsignedLong("18446744073709551616"); + } catch (NumberFormatException e) { + overflowCaught = true; } + assertTrue(overflowCaught); } - public void testParseLongWithRadix() { + public void testParseLongWithRadix() throws NumberFormatException { assertEquals(0xffffffffffffffffL, UnsignedLongs.parseUnsignedLong("ffffffffffffffff", 16)); assertEquals(0x1234567890abcdefL, UnsignedLongs.parseUnsignedLong("1234567890abcdef", 16)); @@ -213,36 +127,35 @@ public class UnsignedLongsTest extends TestCase { String overflowAsString = overflow.toString(radix); UnsignedLongs.parseUnsignedLong(overflowAsString, radix); fail(); - } catch (NumberFormatException expected) { + } catch (NumberFormatException nfe) { + // expected } } - - try { - UnsignedLongs.parseUnsignedLong("1234567890abcdef1", 16); - fail(); - } catch (NumberFormatException expected) { - } } public void testParseLongThrowsExceptionForInvalidRadix() { - // Valid radix values are Character.MIN_RADIX to Character.MAX_RADIX, inclusive. + // Valid radix values are Character.MIN_RADIX to Character.MAX_RADIX, + // inclusive. try { UnsignedLongs.parseUnsignedLong("0", Character.MIN_RADIX - 1); fail(); - } catch (NumberFormatException expected) { + } catch (NumberFormatException nfe) { + // expected } try { UnsignedLongs.parseUnsignedLong("0", Character.MAX_RADIX + 1); fail(); - } catch (NumberFormatException expected) { + } catch (NumberFormatException nfe) { + // expected } // The radix is used as an array index, so try a negative value. try { UnsignedLongs.parseUnsignedLong("0", -1); fail(); - } catch (NumberFormatException expected) { + } catch (NumberFormatException nfe) { + // expected } } @@ -252,9 +165,8 @@ public class UnsignedLongsTest extends TestCase { "7fffffffffffffff", "ff1a618b7f65ea12", "5a4316b8c153ac4d", - "6cf78a4b139a4e2a" - }; - int[] bases = { 2, 5, 7, 8, 10, 16 }; + "6cf78a4b139a4e2a"}; + int[] bases = {2, 5, 7, 8, 10, 16}; for (int base : bases) { for (String x : tests) { BigInteger xValue = new BigInteger(x, 16); @@ -264,19 +176,11 @@ public class UnsignedLongsTest extends TestCase { } } - public void testJoin() { - assertEquals("", UnsignedLongs.join(",")); - assertEquals("1", UnsignedLongs.join(",", 1)); - assertEquals("1,2", UnsignedLongs.join(",", 1, 2)); - assertEquals("18446744073709551615,9223372036854775808", - UnsignedLongs.join(",", -1, Long.MIN_VALUE)); - assertEquals("123", UnsignedLongs.join("", 1, 2, 3)); - assertEquals("184467440737095516159223372036854775808", - UnsignedLongs.join("", -1, Long.MIN_VALUE)); - } - @GwtIncompatible("NullPointerTester") - public void testNulls() { - new NullPointerTester().testAllPublicStaticMethods(UnsignedLongs.class); + public void testNulls() throws Exception { + NullPointerTester tester = new NullPointerTester(); + tester.setDefault(long[].class, new long[0]); + tester.setDefault(BigInteger.class, BigInteger.ZERO); + tester.testAllPublicStaticMethods(UnsignedLongs.class); } } |