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