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.collect.testing.Helpers;
20import com.google.common.testing.NullPointerTester;
21import com.google.common.testing.SerializableTester;
22
23import junit.framework.TestCase;
24
25import java.util.Arrays;
26import java.util.Comparator;
27import java.util.List;
28
29/**
30 * Unit test for {@link UnsignedBytes}.
31 *
32 * @author Kevin Bourrillion
33 */
34public class UnsignedBytesTest extends TestCase {
35  private static final byte LEAST = 0;
36  private static final byte GREATEST = (byte) 255;
37
38  // Only in this class, VALUES must be strictly ascending
39  private static final byte[] VALUES =
40      {LEAST, 127, (byte) 128, (byte) 129, GREATEST};
41
42  public void testToInt() {
43    assertEquals(0, UnsignedBytes.toInt((byte) 0));
44    assertEquals(1, UnsignedBytes.toInt((byte) 1));
45    assertEquals(127, UnsignedBytes.toInt((byte) 127));
46    assertEquals(128, UnsignedBytes.toInt((byte) -128));
47    assertEquals(129, UnsignedBytes.toInt((byte) -127));
48    assertEquals(255, UnsignedBytes.toInt((byte) -1));
49  }
50
51  public void testCheckedCast() {
52    for (byte value : VALUES) {
53      assertEquals(value,
54          UnsignedBytes.checkedCast(UnsignedBytes.toInt(value)));
55    }
56    assertCastFails(256L);
57    assertCastFails(-1L);
58    assertCastFails(Long.MAX_VALUE);
59    assertCastFails(Long.MIN_VALUE);
60  }
61
62  public void testSaturatedCast() {
63    for (byte value : VALUES) {
64      assertEquals(value,
65          UnsignedBytes.saturatedCast(UnsignedBytes.toInt(value)));
66    }
67    assertEquals(GREATEST, UnsignedBytes.saturatedCast(256L));
68    assertEquals(LEAST, UnsignedBytes.saturatedCast(-1L));
69    assertEquals(GREATEST, UnsignedBytes.saturatedCast(Long.MAX_VALUE));
70    assertEquals(LEAST, UnsignedBytes.saturatedCast(Long.MIN_VALUE));
71  }
72
73  private void assertCastFails(long value) {
74    try {
75      UnsignedBytes.checkedCast(value);
76      fail("Cast to byte should have failed: " + value);
77    } catch (IllegalArgumentException ex) {
78      assertTrue(value + " not found in exception text: " + ex.getMessage(),
79          ex.getMessage().contains(String.valueOf(value)));
80    }
81  }
82
83  public void testCompare() {
84    // This is the only ordering for primitives that does not have a
85    // corresponding Comparable wrapper in java.lang.
86    for (int i = 0; i < VALUES.length; i++) {
87      for (int j = 0; j < VALUES.length; j++) {
88        byte x = VALUES[i];
89        byte y = VALUES[j];
90        // note: spec requires only that the sign is the same
91        assertEquals(x + ", " + y,
92                     Math.signum(UnsignedBytes.compare(x, y)),
93                     Math.signum(Ints.compare(i, j)));
94      }
95    }
96  }
97
98  public void testMax_noArgs() {
99    try {
100      UnsignedBytes.max();
101      fail();
102    } catch (IllegalArgumentException expected) {
103    }
104  }
105
106  public void testMax() {
107    assertEquals(LEAST, UnsignedBytes.max(LEAST));
108    assertEquals(GREATEST, UnsignedBytes.max(GREATEST));
109    assertEquals((byte) 255, UnsignedBytes.max(
110        (byte) 0, (byte) -128, (byte) -1, (byte) 127, (byte) 1));
111  }
112
113  public void testMin_noArgs() {
114    try {
115      UnsignedBytes.min();
116      fail();
117    } catch (IllegalArgumentException expected) {
118    }
119  }
120
121  public void testMin() {
122    assertEquals(LEAST, UnsignedBytes.min(LEAST));
123    assertEquals(GREATEST, UnsignedBytes.min(GREATEST));
124    assertEquals((byte) 0, UnsignedBytes.min(
125        (byte) 0, (byte) -128, (byte) -1, (byte) 127, (byte) 1));
126  }
127
128  public void testJoin() {
129    assertEquals("", UnsignedBytes.join(",", new byte[] {}));
130    assertEquals("1", UnsignedBytes.join(",", new byte[] {(byte) 1}));
131    assertEquals("1,2", UnsignedBytes.join(",", (byte) 1, (byte) 2));
132    assertEquals("123", UnsignedBytes.join("", (byte) 1, (byte) 2, (byte) 3));
133    assertEquals("128,255", UnsignedBytes.join(",", (byte) 128, (byte) -1));
134  }
135
136  public void testLexicographicalComparatorDefaultChoice() {
137    Comparator<byte[]> defaultComparator =
138        UnsignedBytes.lexicographicalComparator();
139    Comparator<byte[]> unsafeComparator =
140        UnsignedBytes.LexicographicalComparatorHolder.UnsafeComparator.INSTANCE;
141    assertSame(defaultComparator, unsafeComparator);
142  }
143
144  public void testLexicographicalComparator() {
145    List<byte[]> ordered = Arrays.asList(
146        new byte[] {},
147        new byte[] {LEAST},
148        new byte[] {LEAST, LEAST},
149        new byte[] {LEAST, (byte) 1},
150        new byte[] {(byte) 1},
151        new byte[] {(byte) 1, LEAST},
152        new byte[] {GREATEST, GREATEST - (byte) 1},
153        new byte[] {GREATEST, GREATEST},
154        new byte[] {GREATEST, GREATEST, GREATEST});
155
156    // The Unsafe implementation if it's available. Otherwise, the Java implementation.
157    Comparator<byte[]> comparator = UnsignedBytes.lexicographicalComparator();
158    Helpers.testComparator(comparator, ordered);
159    assertSame(comparator, SerializableTester.reserialize(comparator));
160
161    // The Java implementation.
162    Comparator<byte[]> javaImpl = UnsignedBytes.lexicographicalComparatorJavaImpl();
163    Helpers.testComparator(javaImpl, ordered);
164    assertSame(javaImpl, SerializableTester.reserialize(javaImpl));
165  }
166
167  public void testNulls() throws Exception {
168    NullPointerTester tester = new NullPointerTester();
169    tester.setDefault(byte[].class, new byte[0]);
170    tester.testAllPublicStaticMethods(UnsignedBytes.class);
171  }
172}
173