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 17 package com.google.common.primitives; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static com.google.common.truth.Truth.assertWithMessage; 21 22 import com.google.common.annotations.GwtCompatible; 23 import com.google.common.annotations.GwtIncompatible; 24 import com.google.common.annotations.J2ktIncompatible; 25 import com.google.common.collect.testing.Helpers; 26 import com.google.common.testing.NullPointerTester; 27 import com.google.common.testing.SerializableTester; 28 import java.util.Arrays; 29 import java.util.Comparator; 30 import java.util.List; 31 import junit.framework.TestCase; 32 33 /** 34 * Unit test for {@link SignedBytes}. 35 * 36 * @author Kevin Bourrillion 37 */ 38 @ElementTypesAreNonnullByDefault 39 @GwtCompatible(emulated = true) 40 @SuppressWarnings("cast") // redundant casts are intentional and harmless 41 public class SignedBytesTest extends TestCase { 42 private static final byte[] EMPTY = {}; 43 private static final byte[] ARRAY1 = {(byte) 1}; 44 45 private static final byte LEAST = Byte.MIN_VALUE; 46 private static final byte GREATEST = Byte.MAX_VALUE; 47 48 private static final byte[] VALUES = {LEAST, -1, 0, 1, GREATEST}; 49 testCheckedCast()50 public void testCheckedCast() { 51 for (byte value : VALUES) { 52 assertThat(SignedBytes.checkedCast((long) value)).isEqualTo(value); 53 } 54 assertCastFails(GREATEST + 1L); 55 assertCastFails(LEAST - 1L); 56 assertCastFails(Long.MAX_VALUE); 57 assertCastFails(Long.MIN_VALUE); 58 } 59 testSaturatedCast()60 public void testSaturatedCast() { 61 for (byte value : VALUES) { 62 assertThat(SignedBytes.saturatedCast((long) value)).isEqualTo(value); 63 } 64 assertThat(SignedBytes.saturatedCast(GREATEST + 1L)).isEqualTo(GREATEST); 65 assertThat(SignedBytes.saturatedCast(LEAST - 1L)).isEqualTo(LEAST); 66 assertThat(SignedBytes.saturatedCast(Long.MAX_VALUE)).isEqualTo(GREATEST); 67 assertThat(SignedBytes.saturatedCast(Long.MIN_VALUE)).isEqualTo(LEAST); 68 } 69 assertCastFails(long value)70 private static void assertCastFails(long value) { 71 try { 72 SignedBytes.checkedCast(value); 73 fail("Cast to byte should have failed: " + value); 74 } catch (IllegalArgumentException ex) { 75 assertWithMessage(value + " not found in exception text: " + ex.getMessage()) 76 .that(ex.getMessage().contains(String.valueOf(value))) 77 .isTrue(); 78 } 79 } 80 testCompare()81 public void testCompare() { 82 for (byte x : VALUES) { 83 for (byte y : VALUES) { 84 // Only compare the sign of the result of compareTo(). 85 int expected = Byte.valueOf(x).compareTo(y); 86 int actual = SignedBytes.compare(x, y); 87 if (expected == 0) { 88 assertWithMessage(x + ", " + y).that(actual).isEqualTo(expected); 89 } else if (expected < 0) { 90 assertWithMessage(x + ", " + y + " (expected: " + expected + ", actual" + actual + ")") 91 .that(actual < 0) 92 .isTrue(); 93 } else { 94 assertWithMessage(x + ", " + y + " (expected: " + expected + ", actual" + actual + ")") 95 .that(actual > 0) 96 .isTrue(); 97 } 98 } 99 } 100 } 101 testMax_noArgs()102 public void testMax_noArgs() { 103 try { 104 SignedBytes.max(); 105 fail(); 106 } catch (IllegalArgumentException expected) { 107 } 108 } 109 testMax()110 public void testMax() { 111 assertThat(SignedBytes.max(LEAST)).isEqualTo(LEAST); 112 assertThat(SignedBytes.max(GREATEST)).isEqualTo(GREATEST); 113 assertThat(SignedBytes.max((byte) 0, (byte) -128, (byte) -1, (byte) 127, (byte) 1)) 114 .isEqualTo((byte) 127); 115 } 116 testMin_noArgs()117 public void testMin_noArgs() { 118 try { 119 SignedBytes.min(); 120 fail(); 121 } catch (IllegalArgumentException expected) { 122 } 123 } 124 testMin()125 public void testMin() { 126 assertThat(SignedBytes.min(LEAST)).isEqualTo(LEAST); 127 assertThat(SignedBytes.min(GREATEST)).isEqualTo(GREATEST); 128 assertThat(SignedBytes.min((byte) 0, (byte) -128, (byte) -1, (byte) 127, (byte) 1)) 129 .isEqualTo((byte) -128); 130 } 131 testJoin()132 public void testJoin() { 133 assertThat(SignedBytes.join(",", EMPTY)).isEmpty(); 134 assertThat(SignedBytes.join(",", ARRAY1)).isEqualTo("1"); 135 assertThat(SignedBytes.join(",", (byte) 1, (byte) 2)).isEqualTo("1,2"); 136 assertThat(SignedBytes.join("", (byte) 1, (byte) 2, (byte) 3)).isEqualTo("123"); 137 assertThat(SignedBytes.join(",", (byte) -128, (byte) -1)).isEqualTo("-128,-1"); 138 } 139 140 @J2ktIncompatible // b/285319375 testLexicographicalComparator()141 public void testLexicographicalComparator() { 142 List<byte[]> ordered = 143 Arrays.asList( 144 new byte[] {}, 145 new byte[] {LEAST}, 146 new byte[] {LEAST, LEAST}, 147 new byte[] {LEAST, (byte) 1}, 148 new byte[] {(byte) 1}, 149 new byte[] {(byte) 1, LEAST}, 150 new byte[] {GREATEST, GREATEST - (byte) 1}, 151 new byte[] {GREATEST, GREATEST}, 152 new byte[] {GREATEST, GREATEST, GREATEST}); 153 154 Comparator<byte[]> comparator = SignedBytes.lexicographicalComparator(); 155 Helpers.testComparator(comparator, ordered); 156 } 157 158 @J2ktIncompatible 159 @GwtIncompatible // SerializableTester testLexicographicalComparatorSerializable()160 public void testLexicographicalComparatorSerializable() { 161 Comparator<byte[]> comparator = SignedBytes.lexicographicalComparator(); 162 assertThat(SerializableTester.reserialize(comparator)).isSameInstanceAs(comparator); 163 } 164 testSortDescending()165 public void testSortDescending() { 166 testSortDescending(new byte[] {}, new byte[] {}); 167 testSortDescending(new byte[] {1}, new byte[] {1}); 168 testSortDescending(new byte[] {1, 2}, new byte[] {2, 1}); 169 testSortDescending(new byte[] {1, 3, 1}, new byte[] {3, 1, 1}); 170 testSortDescending(new byte[] {-1, 1, -2, 2}, new byte[] {2, 1, -1, -2}); 171 } 172 testSortDescending(byte[] input, byte[] expectedOutput)173 private static void testSortDescending(byte[] input, byte[] expectedOutput) { 174 input = Arrays.copyOf(input, input.length); 175 SignedBytes.sortDescending(input); 176 assertThat(input).isEqualTo(expectedOutput); 177 } 178 testSortDescending( byte[] input, int fromIndex, int toIndex, byte[] expectedOutput)179 private static void testSortDescending( 180 byte[] input, int fromIndex, int toIndex, byte[] expectedOutput) { 181 input = Arrays.copyOf(input, input.length); 182 SignedBytes.sortDescending(input, fromIndex, toIndex); 183 assertThat(input).isEqualTo(expectedOutput); 184 } 185 testSortDescendingIndexed()186 public void testSortDescendingIndexed() { 187 testSortDescending(new byte[] {}, 0, 0, new byte[] {}); 188 testSortDescending(new byte[] {1}, 0, 1, new byte[] {1}); 189 testSortDescending(new byte[] {1, 2}, 0, 2, new byte[] {2, 1}); 190 testSortDescending(new byte[] {1, 3, 1}, 0, 2, new byte[] {3, 1, 1}); 191 testSortDescending(new byte[] {1, 3, 1}, 0, 1, new byte[] {1, 3, 1}); 192 testSortDescending(new byte[] {-1, -2, 1, 2}, 1, 3, new byte[] {-1, 1, -2, 2}); 193 } 194 195 @J2ktIncompatible 196 @GwtIncompatible // NullPointerTester testNulls()197 public void testNulls() { 198 new NullPointerTester().testAllPublicStaticMethods(SignedBytes.class); 199 } 200 } 201