|
Java example source code file (BytesTest.java)
The BytesTest.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.collect.testing.Helpers; import com.google.common.testing.NullPointerTester; import junit.framework.TestCase; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; /** * Unit test for {@link Bytes}. * * @author Kevin Bourrillion */ @GwtCompatible(emulated = true) public class BytesTest extends TestCase { private static final byte[] EMPTY = {}; private static final byte[] ARRAY1 = {(byte) 1}; private static final byte[] ARRAY234 = {(byte) 2, (byte) 3, (byte) 4}; private static final byte[] VALUES = { Byte.MIN_VALUE, -1, 0, 1, Byte.MAX_VALUE }; public void testHashCode() { for (byte value : VALUES) { assertEquals(((Byte) value).hashCode(), Bytes.hashCode(value)); } } public void testContains() { assertFalse(Bytes.contains(EMPTY, (byte) 1)); assertFalse(Bytes.contains(ARRAY1, (byte) 2)); assertFalse(Bytes.contains(ARRAY234, (byte) 1)); assertTrue(Bytes.contains(new byte[] {(byte) -1}, (byte) -1)); assertTrue(Bytes.contains(ARRAY234, (byte) 2)); assertTrue(Bytes.contains(ARRAY234, (byte) 3)); assertTrue(Bytes.contains(ARRAY234, (byte) 4)); } public void testIndexOf() { assertEquals(-1, Bytes.indexOf(EMPTY, (byte) 1)); assertEquals(-1, Bytes.indexOf(ARRAY1, (byte) 2)); assertEquals(-1, Bytes.indexOf(ARRAY234, (byte) 1)); assertEquals(0, Bytes.indexOf( new byte[] {(byte) -1}, (byte) -1)); assertEquals(0, Bytes.indexOf(ARRAY234, (byte) 2)); assertEquals(1, Bytes.indexOf(ARRAY234, (byte) 3)); assertEquals(2, Bytes.indexOf(ARRAY234, (byte) 4)); assertEquals(1, Bytes.indexOf( new byte[] { (byte) 2, (byte) 3, (byte) 2, (byte) 3 }, (byte) 3)); } public void testIndexOf_arrayTarget() { assertEquals(0, Bytes.indexOf(EMPTY, EMPTY)); assertEquals(0, Bytes.indexOf(ARRAY234, EMPTY)); assertEquals(-1, Bytes.indexOf(EMPTY, ARRAY234)); assertEquals(-1, Bytes.indexOf(ARRAY234, ARRAY1)); assertEquals(-1, Bytes.indexOf(ARRAY1, ARRAY234)); assertEquals(0, Bytes.indexOf(ARRAY1, ARRAY1)); assertEquals(0, Bytes.indexOf(ARRAY234, ARRAY234)); assertEquals(0, Bytes.indexOf( ARRAY234, new byte[] { (byte) 2, (byte) 3 })); assertEquals(1, Bytes.indexOf( ARRAY234, new byte[] { (byte) 3, (byte) 4 })); assertEquals(1, Bytes.indexOf(ARRAY234, new byte[] { (byte) 3 })); assertEquals(2, Bytes.indexOf(ARRAY234, new byte[] { (byte) 4 })); assertEquals(1, Bytes.indexOf(new byte[] { (byte) 2, (byte) 3, (byte) 3, (byte) 3, (byte) 3 }, new byte[] { (byte) 3 } )); assertEquals(2, Bytes.indexOf( new byte[] { (byte) 2, (byte) 3, (byte) 2, (byte) 3, (byte) 4, (byte) 2, (byte) 3}, new byte[] { (byte) 2, (byte) 3, (byte) 4} )); assertEquals(1, Bytes.indexOf( new byte[] { (byte) 2, (byte) 2, (byte) 3, (byte) 4, (byte) 2, (byte) 3, (byte) 4}, new byte[] { (byte) 2, (byte) 3, (byte) 4} )); assertEquals(-1, Bytes.indexOf( new byte[] { (byte) 4, (byte) 3, (byte) 2}, new byte[] { (byte) 2, (byte) 3, (byte) 4} )); } public void testLastIndexOf() { assertEquals(-1, Bytes.lastIndexOf(EMPTY, (byte) 1)); assertEquals(-1, Bytes.lastIndexOf(ARRAY1, (byte) 2)); assertEquals(-1, Bytes.lastIndexOf(ARRAY234, (byte) 1)); assertEquals(0, Bytes.lastIndexOf( new byte[] {(byte) -1}, (byte) -1)); assertEquals(0, Bytes.lastIndexOf(ARRAY234, (byte) 2)); assertEquals(1, Bytes.lastIndexOf(ARRAY234, (byte) 3)); assertEquals(2, Bytes.lastIndexOf(ARRAY234, (byte) 4)); assertEquals(3, Bytes.lastIndexOf( new byte[] { (byte) 2, (byte) 3, (byte) 2, (byte) 3 }, (byte) 3)); } public void testConcat() { assertTrue(Arrays.equals(EMPTY, Bytes.concat())); assertTrue(Arrays.equals(EMPTY, Bytes.concat(EMPTY))); assertTrue(Arrays.equals(EMPTY, Bytes.concat(EMPTY, EMPTY, EMPTY))); assertTrue(Arrays.equals(ARRAY1, Bytes.concat(ARRAY1))); assertNotSame(ARRAY1, Bytes.concat(ARRAY1)); assertTrue(Arrays.equals(ARRAY1, Bytes.concat(EMPTY, ARRAY1, EMPTY))); assertTrue(Arrays.equals( new byte[] {(byte) 1, (byte) 1, (byte) 1}, Bytes.concat(ARRAY1, ARRAY1, ARRAY1))); assertTrue(Arrays.equals( new byte[] {(byte) 1, (byte) 2, (byte) 3, (byte) 4}, Bytes.concat(ARRAY1, ARRAY234))); } public void testEnsureCapacity() { assertSame(EMPTY, Bytes.ensureCapacity(EMPTY, 0, 1)); assertSame(ARRAY1, Bytes.ensureCapacity(ARRAY1, 0, 1)); assertSame(ARRAY1, Bytes.ensureCapacity(ARRAY1, 1, 1)); assertTrue(Arrays.equals( new byte[] {(byte) 1, (byte) 0, (byte) 0}, Bytes.ensureCapacity(ARRAY1, 2, 1))); } public void testEnsureCapacity_fail() { try { Bytes.ensureCapacity(ARRAY1, -1, 1); fail(); } catch (IllegalArgumentException expected) { } try { // notice that this should even fail when no growth was needed Bytes.ensureCapacity(ARRAY1, 1, -1); fail(); } catch (IllegalArgumentException expected) { } } public void testToArray() { // need explicit type parameter to avoid javac warning!? List<Byte> none = Arrays. Other Java examples (source code examples)Here is a short list of links related to this Java BytesTest.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.