|
25 | 25 | import org.junit.Test; |
26 | 26 |
|
27 | 27 | import java.math.BigInteger; |
28 | | -import java.nio.charset.Charset; |
29 | | -import java.nio.charset.StandardCharsets; |
30 | 28 | import java.util.Arrays; |
31 | | -import java.util.List; |
32 | 29 | import java.util.Random; |
33 | 30 |
|
34 | | -import static junit.framework.TestCase.assertTrue; |
35 | 31 | import static org.junit.Assert.*; |
36 | 32 |
|
37 | | -public class UtilTest { |
38 | | - private static final byte[] EMPTY = {}; |
39 | | - private static final byte[] ARRAY1 = {(byte) 1}; |
40 | | - private static final byte[] ARRAY234 = {(byte) 2, (byte) 3, (byte) 4}; |
| 33 | +public class UtilByteTest extends AUtilTest { |
41 | 34 |
|
42 | 35 | @Test |
43 | 36 | public void testIndexOf() { |
@@ -111,78 +104,18 @@ private int lastIndexOf(byte[] empty, byte b) { |
111 | 104 |
|
112 | 105 | @Test |
113 | 106 | public void testConcat() { |
114 | | - assertTrue(Arrays.equals(EMPTY, Util.Byte.concat())); |
115 | | - assertTrue(Arrays.equals(EMPTY, Util.Byte.concat(EMPTY))); |
116 | | - assertTrue(Arrays.equals(EMPTY, Util.Byte.concat(EMPTY, EMPTY, EMPTY))); |
117 | | - assertTrue(Arrays.equals(ARRAY1, Util.Byte.concat(ARRAY1))); |
| 107 | + assertArrayEquals(EMPTY, Util.Byte.concat()); |
| 108 | + assertArrayEquals(EMPTY, Util.Byte.concat(EMPTY)); |
| 109 | + assertArrayEquals(EMPTY, Util.Byte.concat(EMPTY, EMPTY, EMPTY)); |
| 110 | + assertArrayEquals(ARRAY1, Util.Byte.concat(ARRAY1)); |
118 | 111 | assertNotSame(ARRAY1, Util.Byte.concat(ARRAY1)); |
119 | | - assertTrue(Arrays.equals(ARRAY1, Util.Byte.concat(EMPTY, ARRAY1, EMPTY))); |
120 | | - assertTrue(Arrays.equals( |
| 112 | + assertArrayEquals(ARRAY1, Util.Byte.concat(EMPTY, ARRAY1, EMPTY)); |
| 113 | + assertArrayEquals( |
121 | 114 | new byte[]{(byte) 1, (byte) 1, (byte) 1}, |
122 | | - Util.Byte.concat(ARRAY1, ARRAY1, ARRAY1))); |
123 | | - assertTrue(Arrays.equals( |
| 115 | + Util.Byte.concat(ARRAY1, ARRAY1, ARRAY1)); |
| 116 | + assertArrayEquals( |
124 | 117 | new byte[]{(byte) 1, (byte) 2, (byte) 3, (byte) 4}, |
125 | | - Util.Byte.concat(ARRAY1, ARRAY234))); |
126 | | - } |
127 | | - |
128 | | - @Test |
129 | | - public void testToArray() { |
130 | | - // need explicit type parameter to avoid javac warning!? |
131 | | - List<Byte> none = Arrays.asList(); |
132 | | - assertTrue(Arrays.equals(EMPTY, Util.Converter.toArray(none))); |
133 | | - |
134 | | - List<Byte> one = Arrays.asList((byte) 1); |
135 | | - assertTrue(Arrays.equals(ARRAY1, Util.Converter.toArray(one))); |
136 | | - |
137 | | - byte[] array = {(byte) 0, (byte) 1, (byte) 0x55}; |
138 | | - |
139 | | - List<Byte> three = Arrays.asList((byte) 0, (byte) 1, (byte) 0x55); |
140 | | - assertTrue(Arrays.equals(array, Util.Converter.toArray(three))); |
141 | | - |
142 | | - assertTrue(Arrays.equals(array, Util.Converter.toArray(Util.Converter.toList(array)))); |
143 | | - } |
144 | | - |
145 | | - @Test |
146 | | - public void testToArray_withNull() { |
147 | | - List<Byte> list = Arrays.asList((byte) 0, (byte) 1, null); |
148 | | - try { |
149 | | - Util.Converter.toArray(list); |
150 | | - fail(); |
151 | | - } catch (NullPointerException expected) { |
152 | | - } |
153 | | - } |
154 | | - |
155 | | - @Test |
156 | | - public void testToArray_withConversion() { |
157 | | - byte[] array = {(byte) 0, (byte) 1, (byte) 2}; |
158 | | - |
159 | | - List<Byte> bytes = Arrays.asList((byte) 0, (byte) 1, (byte) 2); |
160 | | - assertTrue(Arrays.equals(array, Util.Converter.toArray(bytes))); |
161 | | - } |
162 | | - |
163 | | - @Test |
164 | | - public void testAsList_toArray_roundTrip() { |
165 | | - byte[] array = {(byte) 0, (byte) 1, (byte) 2}; |
166 | | - List<Byte> list = Util.Converter.toList(array); |
167 | | - byte[] newArray = Util.Converter.toArray(list); |
168 | | - |
169 | | - // Make sure it returned a copy |
170 | | - list.set(0, (byte) 4); |
171 | | - assertTrue(Arrays.equals( |
172 | | - new byte[]{(byte) 0, (byte) 1, (byte) 2}, newArray)); |
173 | | - newArray[1] = (byte) 5; |
174 | | - assertEquals((byte) 1, (byte) list.get(1)); |
175 | | - } |
176 | | - |
177 | | - @Test |
178 | | - // This test stems from a real bug found by andrewk |
179 | | - public void testAsList_subList_toArray_roundTrip() { |
180 | | - byte[] array = {(byte) 0, (byte) 1, (byte) 2, (byte) 3}; |
181 | | - List<Byte> list = Util.Converter.toList(array); |
182 | | - assertTrue(Arrays.equals(new byte[]{(byte) 1, (byte) 2}, |
183 | | - Util.Converter.toArray(list.subList(1, 3)))); |
184 | | - assertTrue(Arrays.equals(new byte[]{}, |
185 | | - Util.Converter.toArray(list.subList(2, 2)))); |
| 118 | + Util.Byte.concat(ARRAY1, ARRAY234)); |
186 | 119 | } |
187 | 120 |
|
188 | 121 | @Test(expected = IllegalStateException.class) |
@@ -217,13 +150,13 @@ public void testReverseIndexed() { |
217 | 150 | private static void testReverse(byte[] input, byte[] expectedOutput) { |
218 | 151 | input = Arrays.copyOf(input, input.length); |
219 | 152 | Util.Byte.reverse(input, 0, input.length); |
220 | | - assertTrue(Arrays.equals(expectedOutput, input)); |
| 153 | + assertArrayEquals(expectedOutput, input); |
221 | 154 | } |
222 | 155 |
|
223 | 156 | private static void testReverse(byte[] input, int fromIndex, int toIndex, byte[] expectedOutput) { |
224 | 157 | input = Arrays.copyOf(input, input.length); |
225 | 158 | Util.Byte.reverse(input, fromIndex, toIndex); |
226 | | - assertTrue(Arrays.equals(expectedOutput, input)); |
| 159 | + assertArrayEquals(expectedOutput, input); |
227 | 160 | } |
228 | 161 |
|
229 | 162 | @Test |
@@ -301,50 +234,4 @@ public void testRightShiftAgainstRefImpl() { |
301 | 234 | } |
302 | 235 | } |
303 | 236 | } |
304 | | - |
305 | | - @Test |
306 | | - public void testCharToByteArray() { |
307 | | - Charset[] charsets = new Charset[]{StandardCharsets.UTF_8, StandardCharsets.US_ASCII, StandardCharsets.UTF_16}; |
308 | | - for (Charset charset : charsets) { |
309 | | - checkCharArrayToByteArray("".toCharArray(), charset); |
310 | | - checkCharArrayToByteArray("A".toCharArray(), charset); |
311 | | - checkCharArrayToByteArray("12".toCharArray(), charset); |
312 | | - checkCharArrayToByteArray("XYZ".toCharArray(), charset); |
313 | | - checkCharArrayToByteArray("abcdefg".toCharArray(), charset); |
314 | | - checkCharArrayToByteArray("71oh872gdl2dhp81g".toCharArray(), charset); |
315 | | - |
316 | | - } |
317 | | - |
318 | | - checkCharArrayToByteArray("யe2ாமறிந்தиют убSîne klâwenasd1".toCharArray(), StandardCharsets.UTF_8); |
319 | | - } |
320 | | - |
321 | | - private void checkCharArrayToByteArray(char[] subject, Charset charset) { |
322 | | - for (int lenI = 1; lenI < subject.length + 1; lenI++) { |
323 | | - for (int offsetI = 0; offsetI < subject.length; offsetI++) { |
324 | | - if (offsetI + lenI > subject.length) break; |
325 | | - byte[] bytes = Util.Converter.charToByteArray(subject, charset, offsetI, lenI); |
326 | | - assertEquals(Bytes.wrap(bytes), Bytes.wrap(new String(subject).substring(offsetI, offsetI + lenI).getBytes(charset))); |
327 | | - } |
328 | | - } |
329 | | - compareArrayToByteArrayWithoutOffset(subject, charset); |
330 | | - } |
331 | | - |
332 | | - private void compareArrayToByteArrayWithoutOffset(char[] subject, Charset charset) { |
333 | | - assertArrayEquals(Util.Converter.charToByteArray(subject, charset, 0, subject.length), new String(subject).getBytes(charset)); |
334 | | - } |
335 | | - |
336 | | - @Test(expected = IllegalArgumentException.class) |
337 | | - public void testCharToByteArrayIllegalOffset() { |
338 | | - Util.Converter.charToByteArray("abcdef".toCharArray(), StandardCharsets.UTF_8, -1, 1); |
339 | | - } |
340 | | - |
341 | | - @Test(expected = IllegalArgumentException.class) |
342 | | - public void testCharToByteArrayIllegalLength() { |
343 | | - Util.Converter.charToByteArray("abcdef".toCharArray(), StandardCharsets.UTF_8, 0, -1); |
344 | | - } |
345 | | - |
346 | | - @Test(expected = IllegalArgumentException.class) |
347 | | - public void testCharToByteArrayIllegalOffsetPlusLength() { |
348 | | - Util.Converter.charToByteArray("abcdef".toCharArray(), StandardCharsets.UTF_8, 4, 3); |
349 | | - } |
350 | 237 | } |
0 commit comments