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