1/* 2 * Copyright (C) 2008 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.google.common.primitives; 18 19import com.google.common.annotations.GwtCompatible; 20import com.google.common.annotations.GwtIncompatible; 21import com.google.common.collect.testing.Helpers; 22import com.google.common.testing.NullPointerTester; 23import com.google.common.testing.SerializableTester; 24 25import junit.framework.TestCase; 26 27import java.util.Arrays; 28import java.util.Comparator; 29import java.util.List; 30 31/** 32 * Unit test for {@link SignedBytes}. 33 * 34 * @author Kevin Bourrillion 35 */ 36@GwtCompatible(emulated = true) 37@SuppressWarnings("cast") // redundant casts are intentional and harmless 38public class SignedBytesTest extends TestCase { 39 private static final byte[] EMPTY = {}; 40 private static final byte[] ARRAY1 = {(byte) 1}; 41 42 private static final byte LEAST = Byte.MIN_VALUE; 43 private static final byte GREATEST = Byte.MAX_VALUE; 44 45 private static final byte[] VALUES = 46 {LEAST, -1, 0, 1, GREATEST}; 47 48 public void testCheckedCast() { 49 for (byte value : VALUES) { 50 assertEquals(value, SignedBytes.checkedCast((long) value)); 51 } 52 assertCastFails(GREATEST + 1L); 53 assertCastFails(LEAST - 1L); 54 assertCastFails(Long.MAX_VALUE); 55 assertCastFails(Long.MIN_VALUE); 56 } 57 58 public void testSaturatedCast() { 59 for (byte value : VALUES) { 60 assertEquals(value, SignedBytes.saturatedCast((long) value)); 61 } 62 assertEquals(GREATEST, SignedBytes.saturatedCast(GREATEST + 1L)); 63 assertEquals(LEAST, SignedBytes.saturatedCast(LEAST - 1L)); 64 assertEquals(GREATEST, SignedBytes.saturatedCast(Long.MAX_VALUE)); 65 assertEquals(LEAST, SignedBytes.saturatedCast(Long.MIN_VALUE)); 66 } 67 68 private void assertCastFails(long value) { 69 try { 70 SignedBytes.checkedCast(value); 71 fail("Cast to byte should have failed: " + value); 72 } catch (IllegalArgumentException ex) { 73 assertTrue(value + " not found in exception text: " + ex.getMessage(), 74 ex.getMessage().contains(String.valueOf(value))); 75 } 76 } 77 78 public void testCompare() { 79 for (byte x : VALUES) { 80 for (byte y : VALUES) { 81 // Only compare the sign of the result of compareTo(). 82 int expected = Byte.valueOf(x).compareTo(y); 83 int actual = SignedBytes.compare(x, y); 84 if (expected == 0) { 85 assertEquals(x + ", " + y, expected, actual); 86 } else if (expected < 0) { 87 assertTrue(x + ", " + y + " (expected: " + expected + ", actual" + actual + ")", 88 actual < 0); 89 } else { 90 assertTrue(x + ", " + y + " (expected: " + expected + ", actual" + actual + ")", 91 actual > 0); 92 } 93 } 94 } 95 } 96 97 public void testMax_noArgs() { 98 try { 99 SignedBytes.max(); 100 fail(); 101 } catch (IllegalArgumentException expected) { 102 } 103 } 104 105 public void testMax() { 106 assertEquals(LEAST, SignedBytes.max(LEAST)); 107 assertEquals(GREATEST, SignedBytes.max(GREATEST)); 108 assertEquals((byte) 127, SignedBytes.max( 109 (byte) 0, (byte) -128, (byte) -1, (byte) 127, (byte) 1)); 110 } 111 112 public void testMin_noArgs() { 113 try { 114 SignedBytes.min(); 115 fail(); 116 } catch (IllegalArgumentException expected) { 117 } 118 } 119 120 public void testMin() { 121 assertEquals(LEAST, SignedBytes.min(LEAST)); 122 assertEquals(GREATEST, SignedBytes.min(GREATEST)); 123 assertEquals((byte) -128, SignedBytes.min( 124 (byte) 0, (byte) -128, (byte) -1, (byte) 127, (byte) 1)); 125 } 126 127 public void testJoin() { 128 assertEquals("", SignedBytes.join(",", EMPTY)); 129 assertEquals("1", SignedBytes.join(",", ARRAY1)); 130 assertEquals("1,2", SignedBytes.join(",", (byte) 1, (byte) 2)); 131 assertEquals("123", SignedBytes.join("", (byte) 1, (byte) 2, (byte) 3)); 132 assertEquals("-128,-1", SignedBytes.join(",", (byte) -128, (byte) -1)); 133 } 134 135 public void testLexicographicalComparator() { 136 List<byte[]> ordered = Arrays.asList( 137 new byte[] {}, 138 new byte[] {LEAST}, 139 new byte[] {LEAST, LEAST}, 140 new byte[] {LEAST, (byte) 1}, 141 new byte[] {(byte) 1}, 142 new byte[] {(byte) 1, LEAST}, 143 new byte[] {GREATEST, GREATEST - (byte) 1}, 144 new byte[] {GREATEST, GREATEST}, 145 new byte[] {GREATEST, GREATEST, GREATEST}); 146 147 Comparator<byte[]> comparator = SignedBytes.lexicographicalComparator(); 148 Helpers.testComparator(comparator, ordered); 149 } 150 151 @GwtIncompatible("SerializableTester") 152 public void testLexicographicalComparatorSerializable() { 153 Comparator<byte[]> comparator = SignedBytes.lexicographicalComparator(); 154 assertSame(comparator, SerializableTester.reserialize(comparator)); 155 } 156 157 @GwtIncompatible("NullPointerTester") 158 public void testNulls() throws Exception { 159 NullPointerTester tester = new NullPointerTester(); 160 tester.setDefault(byte[].class, new byte[0]); 161 tester.testAllPublicStaticMethods(SignedBytes.class); 162 } 163} 164