|
23 | 23 |
|
24 | 24 | import org.junit.Test; |
25 | 25 |
|
26 | | -import static org.junit.Assert.assertArrayEquals; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.List; |
| 28 | + |
| 29 | +import static junit.framework.TestCase.assertTrue; |
| 30 | +import static org.junit.Assert.*; |
27 | 31 |
|
28 | 32 | public class UtilTest { |
| 33 | + private static final byte[] EMPTY = {}; |
| 34 | + private static final byte[] ARRAY1 = {(byte) 1}; |
| 35 | + private static final byte[] ARRAY234 = {(byte) 2, (byte) 3, (byte) 4}; |
| 36 | + |
| 37 | + @Test |
| 38 | + public void testIndexOf() { |
| 39 | + assertEquals(-1, indexOf(EMPTY, (byte) 1)); |
| 40 | + assertEquals(-1, indexOf(ARRAY1, (byte) 2)); |
| 41 | + assertEquals(-1, indexOf(ARRAY234, (byte) 1)); |
| 42 | + assertEquals(0, indexOf( |
| 43 | + new byte[]{(byte) -1}, (byte) -1)); |
| 44 | + assertEquals(0, indexOf(ARRAY234, (byte) 2)); |
| 45 | + assertEquals(1, indexOf(ARRAY234, (byte) 3)); |
| 46 | + assertEquals(2, indexOf(ARRAY234, (byte) 4)); |
| 47 | + assertEquals(1, indexOf( |
| 48 | + new byte[]{(byte) 2, (byte) 3, (byte) 2, (byte) 3}, |
| 49 | + (byte) 3)); |
| 50 | + } |
| 51 | + |
| 52 | + private int indexOf(byte[] empty, byte b) { |
| 53 | + return Util.indexOf(empty, b, 0, empty.length); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testIndexOf_arrayTarget() { |
| 58 | + assertEquals(0, Util.indexOf(EMPTY, EMPTY)); |
| 59 | + assertEquals(0, Util.indexOf(ARRAY234, EMPTY)); |
| 60 | + assertEquals(-1, Util.indexOf(EMPTY, ARRAY234)); |
| 61 | + assertEquals(-1, Util.indexOf(ARRAY234, ARRAY1)); |
| 62 | + assertEquals(-1, Util.indexOf(ARRAY1, ARRAY234)); |
| 63 | + assertEquals(0, Util.indexOf(ARRAY1, ARRAY1)); |
| 64 | + assertEquals(0, Util.indexOf(ARRAY234, ARRAY234)); |
| 65 | + assertEquals(0, Util.indexOf( |
| 66 | + ARRAY234, new byte[]{(byte) 2, (byte) 3})); |
| 67 | + assertEquals(1, Util.indexOf( |
| 68 | + ARRAY234, new byte[]{(byte) 3, (byte) 4})); |
| 69 | + assertEquals(1, Util.indexOf(ARRAY234, new byte[]{(byte) 3})); |
| 70 | + assertEquals(2, Util.indexOf(ARRAY234, new byte[]{(byte) 4})); |
| 71 | + assertEquals(1, Util.indexOf(new byte[]{(byte) 2, (byte) 3, |
| 72 | + (byte) 3, (byte) 3, (byte) 3}, |
| 73 | + new byte[]{(byte) 3} |
| 74 | + )); |
| 75 | + assertEquals(2, Util.indexOf( |
| 76 | + new byte[]{(byte) 2, (byte) 3, (byte) 2, |
| 77 | + (byte) 3, (byte) 4, (byte) 2, (byte) 3}, |
| 78 | + new byte[]{(byte) 2, (byte) 3, (byte) 4} |
| 79 | + )); |
| 80 | + assertEquals(1, Util.indexOf( |
| 81 | + new byte[]{(byte) 2, (byte) 2, (byte) 3, |
| 82 | + (byte) 4, (byte) 2, (byte) 3, (byte) 4}, |
| 83 | + new byte[]{(byte) 2, (byte) 3, (byte) 4} |
| 84 | + )); |
| 85 | + assertEquals(-1, Util.indexOf( |
| 86 | + new byte[]{(byte) 4, (byte) 3, (byte) 2}, |
| 87 | + new byte[]{(byte) 2, (byte) 3, (byte) 4} |
| 88 | + )); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testLastIndexOf() { |
| 93 | + assertEquals(-1, lastIndexOf(EMPTY, (byte) 1)); |
| 94 | + assertEquals(-1, lastIndexOf(ARRAY1, (byte) 2)); |
| 95 | + assertEquals(-1, lastIndexOf(ARRAY234, (byte) 1)); |
| 96 | + assertEquals(0, lastIndexOf( |
| 97 | + new byte[]{(byte) -1}, (byte) -1)); |
| 98 | + assertEquals(0, lastIndexOf(ARRAY234, (byte) 2)); |
| 99 | + assertEquals(1, lastIndexOf(ARRAY234, (byte) 3)); |
| 100 | + assertEquals(2, lastIndexOf(ARRAY234, (byte) 4)); |
| 101 | + assertEquals(3, lastIndexOf( |
| 102 | + new byte[]{(byte) 2, (byte) 3, (byte) 2, (byte) 3}, |
| 103 | + (byte) 3)); |
| 104 | + } |
| 105 | + |
| 106 | + private int lastIndexOf(byte[] empty, byte b) { |
| 107 | + return Util.lastIndexOf(empty, b, 0, empty.length); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void testConcat() { |
| 112 | + assertTrue(Arrays.equals(EMPTY, Util.concat())); |
| 113 | + assertTrue(Arrays.equals(EMPTY, Util.concat(EMPTY))); |
| 114 | + assertTrue(Arrays.equals(EMPTY, Util.concat(EMPTY, EMPTY, EMPTY))); |
| 115 | + assertTrue(Arrays.equals(ARRAY1, Util.concat(ARRAY1))); |
| 116 | + assertNotSame(ARRAY1, Util.concat(ARRAY1)); |
| 117 | + assertTrue(Arrays.equals(ARRAY1, Util.concat(EMPTY, ARRAY1, EMPTY))); |
| 118 | + assertTrue(Arrays.equals( |
| 119 | + new byte[]{(byte) 1, (byte) 1, (byte) 1}, |
| 120 | + Util.concat(ARRAY1, ARRAY1, ARRAY1))); |
| 121 | + assertTrue(Arrays.equals( |
| 122 | + new byte[]{(byte) 1, (byte) 2, (byte) 3, (byte) 4}, |
| 123 | + Util.concat(ARRAY1, ARRAY234))); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testToArray() { |
| 128 | + // need explicit type parameter to avoid javac warning!? |
| 129 | + List<Byte> none = Arrays.asList(); |
| 130 | + assertTrue(Arrays.equals(EMPTY, Util.toArray(none))); |
| 131 | + |
| 132 | + List<Byte> one = Arrays.asList((byte) 1); |
| 133 | + assertTrue(Arrays.equals(ARRAY1, Util.toArray(one))); |
| 134 | + |
| 135 | + byte[] array = {(byte) 0, (byte) 1, (byte) 0x55}; |
| 136 | + |
| 137 | + List<Byte> three = Arrays.asList((byte) 0, (byte) 1, (byte) 0x55); |
| 138 | + assertTrue(Arrays.equals(array, Util.toArray(three))); |
| 139 | + |
| 140 | + assertTrue(Arrays.equals(array, Util.toArray(Util.toList(array)))); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void testToArray_withNull() { |
| 145 | + List<Byte> list = Arrays.asList((byte) 0, (byte) 1, null); |
| 146 | + try { |
| 147 | + Util.toArray(list); |
| 148 | + fail(); |
| 149 | + } catch (NullPointerException expected) { |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + public void testToArray_withConversion() { |
| 155 | + byte[] array = {(byte) 0, (byte) 1, (byte) 2}; |
| 156 | + |
| 157 | + List<Byte> bytes = Arrays.asList((byte) 0, (byte) 1, (byte) 2); |
| 158 | + assertTrue(Arrays.equals(array, Util.toArray(bytes))); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void testAsList_toArray_roundTrip() { |
| 163 | + byte[] array = {(byte) 0, (byte) 1, (byte) 2}; |
| 164 | + List<Byte> list = Util.toList(array); |
| 165 | + byte[] newArray = Util.toArray(list); |
| 166 | + |
| 167 | + // Make sure it returned a copy |
| 168 | + list.set(0, (byte) 4); |
| 169 | + assertTrue(Arrays.equals( |
| 170 | + new byte[]{(byte) 0, (byte) 1, (byte) 2}, newArray)); |
| 171 | + newArray[1] = (byte) 5; |
| 172 | + assertEquals((byte) 1, (byte) list.get(1)); |
| 173 | + } |
| 174 | + |
| 175 | + @Test |
| 176 | + // This test stems from a real bug found by andrewk |
| 177 | + public void testAsList_subList_toArray_roundTrip() { |
| 178 | + byte[] array = {(byte) 0, (byte) 1, (byte) 2, (byte) 3}; |
| 179 | + List<Byte> list = Util.toList(array); |
| 180 | + assertTrue(Arrays.equals(new byte[]{(byte) 1, (byte) 2}, |
| 181 | + Util.toArray(list.subList(1, 3)))); |
| 182 | + assertTrue(Arrays.equals(new byte[]{}, |
| 183 | + Util.toArray(list.subList(2, 2)))); |
| 184 | + } |
| 185 | + |
29 | 186 | @Test(expected = IllegalStateException.class) |
30 | 187 | public void readFromStream() throws Exception { |
31 | 188 | Util.readFromStream(null); |
|
0 commit comments