KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > util > ByteListTest


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2007 Nick Sieger <nicksieger@gmail.com>
15  *
16  * Alternatively, the contents of this file may be used under the terms of
17  * either of the GNU General Public License Version 2 or later (the "GPL"),
18  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
19  * in which case the provisions of the GPL or the LGPL are applicable instead
20  * of those above. If you wish to allow use of your version of this file only
21  * under the terms of either the GPL or the LGPL, and not to allow others to
22  * use your version of this file under the terms of the CPL, indicate your
23  * decision by deleting the provisions above and replace them with the notice
24  * and other provisions required by the GPL or the LGPL. If you do not delete
25  * the provisions above, a recipient may use your version of this file under
26  * the terms of any one of the CPL, the GPL or the LGPL.
27  ***** END LICENSE BLOCK *****/

28
29 package org.jruby.util;
30
31 import java.io.UnsupportedEncodingException JavaDoc;
32 import java.util.Arrays JavaDoc;
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 JavaDoc oob) {
149         }
150         try {
151             b.set(0, 1);
152             fail("should throw IndexOfOfBoundsException");
153         } catch (IndexOutOfBoundsException JavaDoc 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 JavaDoc npe) {
163         }
164         try {
165             new ByteList((byte[]) null, 0, 0);
166             fail("should throw NPE");
167         } catch (NullPointerException JavaDoc npe) {
168         }
169         try {
170             b.append((byte[])null);
171             fail("should throw NPE");
172         } catch (NullPointerException JavaDoc npe) {
173         }
174         try {
175             b.append((byte[])null, 0, 0);
176             fail("should throw NPE");
177         } catch (NullPointerException JavaDoc npe) {
178         }
179         try {
180             b.replace(null);
181             fail("should throw NPE");
182         } catch (NullPointerException JavaDoc 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 JavaDoc s) throws UnsupportedEncodingException JavaDoc {
206         return new ByteList(s.getBytes("ISO8859_1"));
207     }
208     private String JavaDoc S(ByteList b) throws UnsupportedEncodingException JavaDoc {
209         return new String JavaDoc(b.bytes(), "ISO8859_1");
210     }
211
212     public void testReplaceStrings() throws UnsupportedEncodingException JavaDoc {
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 JavaDoc 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