1 28 29 package org.jruby.util; 30 31 import java.io.UnsupportedEncodingException ; 32 import java.util.Arrays ; 33 34 import junit.framework.TestCase; 35 36 public class ByteListTest extends TestCase { 37 public void testEmptyByteListHasZeroLength() { 38 assertEquals(0, new ByteList().length()); 39 assertEquals(0, new ByteList(32).length()); 40 } 41 42 public void testByteListWithInitialByteArray() { 43 byte[] bytes = new byte[] {0x0f, 0x01, 0x02, 0x03, 0x04, 0x05}; 44 45 ByteList b = new ByteList(bytes); 46 assertEquals(bytes.length, b.length()); 47 b.set(0, 0x00); 48 assertEquals(0x0f, bytes[0]); 49 assertEquals(0x00, b.get(0)); 50 51 ByteList b2 = new ByteList(b); 52 assertEquals(b.length(), b2.length()); 53 b2.set(0, 0x0f); 54 assertEquals(0x0f, b2.get(0)); 55 assertEquals(0x00, b.get(0)); 56 } 57 58 public void testByteListWithSubrangeOfInitialBytes() { 59 byte[] bytes = new byte[] {0x0f, 0x01, 0x02, 0x03, 0x04, 0x05}; 60 ByteList b = new ByteList(bytes, 2, 2); 61 assertEquals(2, b.length()); 62 assertEquals(0x02, b.get(0)); 63 assertEquals(0x03, b.get(1)); 64 65 ByteList b2 = new ByteList(b, 1, 1); 66 assertEquals(1, b2.length()); 67 assertEquals(0x03, b2.get(0)); 68 } 69 70 public void testByteListAppendSingleByte() { 71 byte[] bytes = new byte[] {0x0f, 0x01, 0x02, 0x03, 0x04, 0x05}; 72 ByteList b = new ByteList(1); 73 74 for (int i = 0; i < bytes.length; i++) { 75 b.append(bytes[i]); 76 } 77 78 assertEquals(new ByteList(bytes), b); 79 } 80 81 public void testByteListAppendSingleIntTruncates() { 82 int[] ints = new int[] { 0x1001, 0x1002, 0x1003 }; 83 84 ByteList b = new ByteList(1); 85 for (int i = 0; i < ints.length; i++) { 86 b.append(ints[i]); 87 } 88 89 assertEquals(0x01, b.get(0)); 90 assertEquals(0x02, b.get(1)); 91 assertEquals(0x03, b.get(2)); 92 } 93 94 public void testByteListPrependSingleByte() { 95 byte[] bytes = new byte[] {0x01, 0x02, 0x03}; 96 ByteList b = new ByteList(1); 97 98 for (int i = 0; i < bytes.length; i++) { 99 b.prepend(bytes[i]); 100 } 101 102 assertEquals(0x03, b.get(0)); 103 assertEquals(0x02, b.get(1)); 104 assertEquals(0x01, b.get(2)); 105 106 bytes = new byte[] {0x0f, 0x01, 0x02, 0x03, 0x04, 0x05}; 107 b = new ByteList(bytes); 108 b.prepend((byte) 0x0e); 109 assertEquals(7, b.length()); 110 assertEquals(0x0e, b.get(0)); 111 assertEquals(0x05, b.get(6)); 112 } 113 114 public void testByteListAppendArray() { 115 ByteList b = new ByteList(1); 116 117 b.append(new byte[] {0x01, 0x02, 0x03}); 118 119 assertEquals(3, b.length()); 120 assertEquals(0x01, b.get(0)); 121 assertEquals(0x02, b.get(1)); 122 assertEquals(0x03, b.get(2)); 123 } 124 125 public void testByteListAppendArrayIndexLength() { 126 ByteList b = new ByteList(1); 127 128 b.append(new byte[] {0x0f, 0x01, 0x02, 0x03, 0x04, 0x05}, 1, 4); 129 assertEquals(4, b.length()); 130 assertEquals(0x01, b.get(0)); 131 assertEquals(0x02, b.get(1)); 132 assertEquals(0x03, b.get(2)); 133 assertEquals(0x04, b.get(3)); 134 } 135 136 public void testLengthExpandFillsWithZeros() { 137 ByteList b = new ByteList(); 138 b.length(10); 139 assertEquals(0, b.get(9)); 140 } 141 142 public void testGetAndSetOutsideOfLengthShouldFail() { 143 ByteList b = new ByteList(10); 144 assertEquals(0, b.length()); 145 try { 146 b.get(0); 147 fail("should throw IndexOfOfBoundsException"); 148 } catch (IndexOutOfBoundsException oob) { 149 } 150 try { 151 b.set(0, 1); 152 fail("should throw IndexOfOfBoundsException"); 153 } catch (IndexOutOfBoundsException oob) { 154 } 155 } 156 157 public void testMethodsThatTakeByteArrayDoNotAllowNull() { 158 ByteList b = new ByteList(); 159 try { 160 new ByteList((byte[]) null); 161 fail("should throw NPE"); 162 } catch (NullPointerException npe) { 163 } 164 try { 165 new ByteList((byte[]) null, 0, 0); 166 fail("should throw NPE"); 167 } catch (NullPointerException npe) { 168 } 169 try { 170 b.append((byte[])null); 171 fail("should throw NPE"); 172 } catch (NullPointerException npe) { 173 } 174 try { 175 b.append((byte[])null, 0, 0); 176 fail("should throw NPE"); 177 } catch (NullPointerException npe) { 178 } 179 try { 180 b.replace(null); 181 fail("should throw NPE"); 182 } catch (NullPointerException npe) { 183 } 184 } 185 186 public void testReplaceSetsNewContents() { 187 ByteList b = new ByteList(1); 188 189 b.replace(new byte[] {0x01, 0x02, 0x03, 0x04, 0x05}); 190 assertEquals(5, b.length()); 191 assertEquals(0x01, b.get(0)); 192 assertEquals(0x05, b.get(4)); 193 } 194 195 public void testReplaceIndexOffset() { 196 ByteList base = new ByteList(new byte[] {0x01,0x02,0x03}); 197 ByteList b = (ByteList) base.clone(); 198 b.replace(1, 1, new byte[] {0x04,0x05}); 199 assertEquals(new ByteList(new byte[] {0x01,0x04,0x05,0x03}), b); 200 b = (ByteList) base.clone(); 201 b.replace(0, 3, new byte[] {0x00, 0x00, 0x00}, 1, 2); 202 assertEquals(new ByteList(new byte[] {0x00, 0x00}), b); 203 } 204 205 private ByteList S(String s) throws UnsupportedEncodingException { 206 return new ByteList(s.getBytes("ISO8859_1")); 207 } 208 private String S(ByteList b) throws UnsupportedEncodingException { 209 return new String (b.bytes(), "ISO8859_1"); 210 } 211 212 public void testReplaceStrings() throws UnsupportedEncodingException { 213 ByteList b = S("FooBar"); 214 b.replace(0, 3, S("A")); 215 assertEquals("ABar", S(b)); 216 b.replace(0, 1, S("Foo")); 217 assertEquals("FooBar", S(b)); 218 b.replace(1, 2, S("zz")); 219 assertEquals("FzzBar", S(b)); 220 b.replace(1, 0, S("u")); 221 assertEquals("FuzzBar", S(b)); 222 } 223 224 public void testReplaceLengthOutOfBounds() { 225 ByteList b = new ByteList(new byte[] {0x01,0x02,0x03}); 226 try { 227 b.replace(0, 5, new byte[] { 0x00, 0x00, 0x00 }, 1, 2); 228 fail("should have thrown exception"); 229 } catch (IndexOutOfBoundsException e) { 230 } 231 } 232 233 public void testEquals() { 234 assertTrue(new ByteList().equals(new ByteList(10))); 235 assertFalse(new ByteList().equals(new ByteList(new byte[] {0x01}))); 236 byte[] bytes = new byte[] {0x01, 0x02, 0x03, 0x04}; 237 ByteList b1 = new ByteList(bytes); 238 ByteList b2 = new ByteList(); 239 b2.append(bytes); 240 assertTrue(b1.equals(b2)); 241 } 242 243 public void testBytesCreatesACopyOfInternalBytes() { 244 byte[] bytes = new byte[] {0x01,0x02,0x03}; 245 ByteList b = new ByteList(bytes); 246 byte[] copy = b.bytes(); 247 assertTrue(Arrays.equals(bytes, copy)); 248 b.set(1, 0); 249 assertTrue(Arrays.equals(bytes, copy)); 250 } 251 252 public void testCloneCopiesBytes() { 253 ByteList b = new ByteList(new byte[] {0x01,0x02,0x03}); 254 ByteList b2 = (ByteList) b.clone(); 255 b.set(1, 0); 256 assertFalse(b.equals(b2)); 257 } 258 } 259 | Popular Tags |