|
Java example source code file (IntsTest.java)
The IntsTest.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 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.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 Ints}. * * @author Kevin Bourrillion */ @GwtCompatible(emulated = true) @SuppressWarnings("cast") // redundant casts are intentional and harmless public class IntsTest extends TestCase { private static final int[] EMPTY = {}; private static final int[] ARRAY1 = {(int) 1}; private static final int[] ARRAY234 = {(int) 2, (int) 3, (int) 4}; private static final int LEAST = Integer.MIN_VALUE; private static final int GREATEST = Integer.MAX_VALUE; private static final int[] VALUES = { LEAST, (int) -1, (int) 0, (int) 1, GREATEST }; public void testHashCode() { for (int value : VALUES) { assertEquals(((Integer) value).hashCode(), Ints.hashCode(value)); } } public void testCheckedCast() { for (int value : VALUES) { assertEquals(value, Ints.checkedCast((long) value)); } assertCastFails(GREATEST + 1L); assertCastFails(LEAST - 1L); assertCastFails(Long.MAX_VALUE); assertCastFails(Long.MIN_VALUE); } public void testSaturatedCast() { for (int value : VALUES) { assertEquals(value, Ints.saturatedCast((long) value)); } assertEquals(GREATEST, Ints.saturatedCast(GREATEST + 1L)); assertEquals(LEAST, Ints.saturatedCast(LEAST - 1L)); assertEquals(GREATEST, Ints.saturatedCast(Long.MAX_VALUE)); assertEquals(LEAST, Ints.saturatedCast(Long.MIN_VALUE)); } private static void assertCastFails(long value) { try { Ints.checkedCast(value); fail("Cast to int should have failed: " + value); } catch (IllegalArgumentException ex) { assertTrue(value + " not found in exception text: " + ex.getMessage(), ex.getMessage().contains(String.valueOf(value))); } } public void testCompare() { for (int x : VALUES) { for (int y : VALUES) { // note: spec requires only that the sign is the same assertEquals(x + ", " + y, Integer.valueOf(x).compareTo(y), Ints.compare(x, y)); } } } public void testContains() { assertFalse(Ints.contains(EMPTY, (int) 1)); assertFalse(Ints.contains(ARRAY1, (int) 2)); assertFalse(Ints.contains(ARRAY234, (int) 1)); assertTrue(Ints.contains(new int[] {(int) -1}, (int) -1)); assertTrue(Ints.contains(ARRAY234, (int) 2)); assertTrue(Ints.contains(ARRAY234, (int) 3)); assertTrue(Ints.contains(ARRAY234, (int) 4)); } public void testIndexOf() { assertEquals(-1, Ints.indexOf(EMPTY, (int) 1)); assertEquals(-1, Ints.indexOf(ARRAY1, (int) 2)); assertEquals(-1, Ints.indexOf(ARRAY234, (int) 1)); assertEquals(0, Ints.indexOf( new int[] {(int) -1}, (int) -1)); assertEquals(0, Ints.indexOf(ARRAY234, (int) 2)); assertEquals(1, Ints.indexOf(ARRAY234, (int) 3)); assertEquals(2, Ints.indexOf(ARRAY234, (int) 4)); assertEquals(1, Ints.indexOf( new int[] { (int) 2, (int) 3, (int) 2, (int) 3 }, (int) 3)); } public void testIndexOf_arrayTarget() { assertEquals(0, Ints.indexOf(EMPTY, EMPTY)); assertEquals(0, Ints.indexOf(ARRAY234, EMPTY)); assertEquals(-1, Ints.indexOf(EMPTY, ARRAY234)); assertEquals(-1, Ints.indexOf(ARRAY234, ARRAY1)); assertEquals(-1, Ints.indexOf(ARRAY1, ARRAY234)); assertEquals(0, Ints.indexOf(ARRAY1, ARRAY1)); assertEquals(0, Ints.indexOf(ARRAY234, ARRAY234)); assertEquals(0, Ints.indexOf( ARRAY234, new int[] { (int) 2, (int) 3 })); assertEquals(1, Ints.indexOf( ARRAY234, new int[] { (int) 3, (int) 4 })); assertEquals(1, Ints.indexOf(ARRAY234, new int[] { (int) 3 })); assertEquals(2, Ints.indexOf(ARRAY234, new int[] { (int) 4 })); assertEquals(1, Ints.indexOf(new int[] { (int) 2, (int) 3, (int) 3, (int) 3, (int) 3 }, new int[] { (int) 3 } )); assertEquals(2, Ints.indexOf( new int[] { (int) 2, (int) 3, (int) 2, (int) 3, (int) 4, (int) 2, (int) 3}, new int[] { (int) 2, (int) 3, (int) 4} )); assertEquals(1, Ints.indexOf( new int[] { (int) 2, (int) 2, (int) 3, (int) 4, (int) 2, (int) 3, (int) 4}, new int[] { (int) 2, (int) 3, (int) 4} )); assertEquals(-1, Ints.indexOf( new int[] { (int) 4, (int) 3, (int) 2}, new int[] { (int) 2, (int) 3, (int) 4} )); } public void testLastIndexOf() { assertEquals(-1, Ints.lastIndexOf(EMPTY, (int) 1)); assertEquals(-1, Ints.lastIndexOf(ARRAY1, (int) 2)); assertEquals(-1, Ints.lastIndexOf(ARRAY234, (int) 1)); assertEquals(0, Ints.lastIndexOf( new int[] {(int) -1}, (int) -1)); assertEquals(0, Ints.lastIndexOf(ARRAY234, (int) 2)); assertEquals(1, Ints.lastIndexOf(ARRAY234, (int) 3)); assertEquals(2, Ints.lastIndexOf(ARRAY234, (int) 4)); assertEquals(3, Ints.lastIndexOf( new int[] { (int) 2, (int) 3, (int) 2, (int) 3 }, (int) 3)); } public void testMax_noArgs() { try { Ints.max(); fail(); } catch (IllegalArgumentException expected) { } } public void testMax() { assertEquals(LEAST, Ints.max(LEAST)); assertEquals(GREATEST, Ints.max(GREATEST)); assertEquals((int) 9, Ints.max( (int) 8, (int) 6, (int) 7, (int) 5, (int) 3, (int) 0, (int) 9)); } public void testMin_noArgs() { try { Ints.min(); fail(); } catch (IllegalArgumentException expected) { } } public void testMin() { assertEquals(LEAST, Ints.min(LEAST)); assertEquals(GREATEST, Ints.min(GREATEST)); assertEquals((int) 0, Ints.min( (int) 8, (int) 6, (int) 7, (int) 5, (int) 3, (int) 0, (int) 9)); } public void testConcat() { assertTrue(Arrays.equals(EMPTY, Ints.concat())); assertTrue(Arrays.equals(EMPTY, Ints.concat(EMPTY))); assertTrue(Arrays.equals(EMPTY, Ints.concat(EMPTY, EMPTY, EMPTY))); assertTrue(Arrays.equals(ARRAY1, Ints.concat(ARRAY1))); assertNotSame(ARRAY1, Ints.concat(ARRAY1)); assertTrue(Arrays.equals(ARRAY1, Ints.concat(EMPTY, ARRAY1, EMPTY))); assertTrue(Arrays.equals( new int[] {(int) 1, (int) 1, (int) 1}, Ints.concat(ARRAY1, ARRAY1, ARRAY1))); assertTrue(Arrays.equals( new int[] {(int) 1, (int) 2, (int) 3, (int) 4}, Ints.concat(ARRAY1, ARRAY234))); } @GwtIncompatible // Ints.toByteArray public void testToByteArray() { assertTrue(Arrays.equals( new byte[] {0x12, 0x13, 0x14, 0x15}, Ints.toByteArray(0x12131415))); assertTrue(Arrays.equals( new byte[] {(byte) 0xFF, (byte) 0xEE, (byte) 0xDD, (byte) 0xCC}, Ints.toByteArray(0xFFEEDDCC))); } @GwtIncompatible // Ints.fromByteArray public void testFromByteArray() { assertEquals(0x12131415, Ints.fromByteArray(new byte[] {0x12, 0x13, 0x14, 0x15, 0x33})); assertEquals(0xFFEEDDCC, Ints.fromByteArray( new byte[] {(byte) 0xFF, (byte) 0xEE, (byte) 0xDD, (byte) 0xCC})); } @GwtIncompatible // Ints.fromByteArray public void testFromByteArrayFails() { try { Ints.fromByteArray(new byte[Ints.BYTES - 1]); fail(); } catch (IllegalArgumentException expected) { } } @GwtIncompatible // Ints.fromBytes public void testFromBytes() { assertEquals(0x12131415, Ints.fromBytes( (byte) 0x12, (byte) 0x13, (byte) 0x14, (byte) 0x15)); assertEquals(0xFFEEDDCC, Ints.fromBytes( (byte) 0xFF, (byte) 0xEE, (byte) 0xDD, (byte) 0xCC)); } @GwtIncompatible // Ints.fromByteArray, Ints.toByteArray public void testByteArrayRoundTrips() { Random r = new Random(5); byte[] b = new byte[Ints.BYTES]; // total overkill, but, it takes 0.1 sec so why not... for (int i = 0; i < 10000; i++) { int num = r.nextInt(); assertEquals(num, Ints.fromByteArray(Ints.toByteArray(num))); r.nextBytes(b); assertTrue(Arrays.equals(b, Ints.toByteArray(Ints.fromByteArray(b)))); } } public void testEnsureCapacity() { assertSame(EMPTY, Ints.ensureCapacity(EMPTY, 0, 1)); assertSame(ARRAY1, Ints.ensureCapacity(ARRAY1, 0, 1)); assertSame(ARRAY1, Ints.ensureCapacity(ARRAY1, 1, 1)); assertTrue(Arrays.equals( new int[] {(int) 1, (int) 0, (int) 0}, Ints.ensureCapacity(ARRAY1, 2, 1))); } public void testEnsureCapacity_fail() { try { Ints.ensureCapacity(ARRAY1, -1, 1); fail(); } catch (IllegalArgumentException expected) { } try { // notice that this should even fail when no growth was needed Ints.ensureCapacity(ARRAY1, 1, -1); fail(); } catch (IllegalArgumentException expected) { } } public void testJoin() { assertEquals("", Ints.join(",", EMPTY)); assertEquals("1", Ints.join(",", ARRAY1)); assertEquals("1,2", Ints.join(",", (int) 1, (int) 2)); assertEquals("123", Ints.join("", (int) 1, (int) 2, (int) 3)); } public void testLexicographicalComparator() { List<int[]> ordered = Arrays.asList( new int[] {}, new int[] {LEAST}, new int[] {LEAST, LEAST}, new int[] {LEAST, (int) 1}, new int[] {(int) 1}, new int[] {(int) 1, LEAST}, new int[] {GREATEST, GREATEST - (int) 1}, new int[] {GREATEST, GREATEST}, new int[] {GREATEST, GREATEST, GREATEST}); Comparator<int[]> comparator = Ints.lexicographicalComparator(); Helpers.testComparator(comparator, ordered); } @GwtIncompatible // SerializableTester public void testLexicographicalComparatorSerializable() { Comparator<int[]> comparator = Ints.lexicographicalComparator(); assertSame(comparator, SerializableTester.reserialize(comparator)); } @GwtIncompatible // SerializableTester public void testStringConverterSerialization() { SerializableTester.reserializeAndAssert(Ints.stringConverter()); } public void testToArray() { // need explicit type parameter to avoid javac warning!? List<Integer> none = Arrays. Other Java examples (source code examples)Here is a short list of links related to this Java IntsTest.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.