pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / facet / src / test / org / apache / lucene / util / UnsafeByteArrayOutputStreamTest.java
1 package org.apache.lucene.util;
2
3 import java.io.IOException;
4
5 import org.junit.Test;
6
7 import org.apache.lucene.util.LuceneTestCase;
8 import org.apache.lucene.util.UnsafeByteArrayOutputStream;
9
10 /**
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements.  See the NOTICE file distributed with
13  * this work for additional information regarding copyright ownership.
14  * The ASF licenses this file to You under the Apache License, Version 2.0
15  * (the "License"); you may not use this file except in compliance with
16  * the License.  You may obtain a copy of the License at
17  *
18  *     http://www.apache.org/licenses/LICENSE-2.0
19  *
20  * Unless required by applicable law or agreed to in writing, software
21  * distributed under the License is distributed on an "AS IS" BASIS,
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23  * See the License for the specific language governing permissions and
24  * limitations under the License.
25  */
26
27 public class UnsafeByteArrayOutputStreamTest extends LuceneTestCase {
28
29   @Test
30   public void testSimpleWrite() throws IOException {
31     int length = 100;
32     byte[] buffer = new byte[length];
33     UnsafeByteArrayOutputStream ubaos = new UnsafeByteArrayOutputStream(buffer);
34
35     for (int i = 0; i < 100; i++) {
36       ubaos.write((byte) i);
37     }
38
39     byte[] result = ubaos.toByteArray();
40
41     assertEquals(length, ubaos.length());
42
43     for (int j = 0; j < length; ++j) {
44       assertEquals(result[j], j);
45     }
46   }
47
48   @Test
49   public void testArrayWrite() throws IOException {
50     int length = 100;
51     byte[] buffer = new byte[length];
52     UnsafeByteArrayOutputStream ubaos = new UnsafeByteArrayOutputStream(buffer);
53
54     for (int i = 0; i < 100; i++) {
55       ubaos.write((byte) i);
56     }
57
58     int length2 = 10;
59     byte[] buffer2 = new byte[length2];
60     for (int i = 0; i < length2; i++) {
61       buffer2[i] = (byte) (8 + i);
62     }
63
64     ubaos.write(buffer2);
65
66     byte[] result = ubaos.toByteArray();
67
68     assertEquals(length + length2, ubaos.length());
69
70     for (int j = 0; j < length; ++j) {
71       assertEquals(result[j], j);
72     }
73     for (int j = 0; j < length2; ++j) {
74       assertEquals(result[j + length], buffer2[j]);
75     }
76   }
77
78   @Test
79   public void testArrayWriteStartNotZero() throws IOException {
80     int length = 100;
81     byte[] buffer = new byte[length];
82     UnsafeByteArrayOutputStream ubaos = new UnsafeByteArrayOutputStream(buffer);
83
84     for (int i = 0; i < 100; i++) {
85       ubaos.write((byte) i);
86     }
87
88     int length2 = 1000;
89     byte[] buffer2 = new byte[length2];
90     for (int i = 0; i < length2; i++) {
91       buffer2[i] = (byte) (8 + i);
92     }
93
94     int length3 = 5;
95     int start = 2;
96     ubaos.write(buffer2, start, length3);
97
98     byte[] result = ubaos.toByteArray();
99
100     assertEquals(length + length3, ubaos.length());
101
102     for (int j = 0; j < length; ++j) {
103       assertEquals(result[j], j);
104     }
105     for (int j = 0; j < length3; ++j) {
106       assertEquals(result[j + length], buffer2[j + start]);
107     }
108   }
109
110   @Test
111   public void testBufferGrow() throws IOException {
112     int length = 100;
113     byte[] buffer = new byte[length / 10];
114     UnsafeByteArrayOutputStream ubaos = new UnsafeByteArrayOutputStream(buffer);
115
116     for (int i = 0; i < length; i++) {
117       ubaos.write((byte) i);
118     }
119
120     byte[] result = ubaos.toByteArray();
121
122     assertEquals(length, ubaos.length());
123
124     for (int j = 0; j < length; ++j) {
125       assertEquals(result[j], j);
126     }
127
128     buffer = ubaos.toByteArray();
129
130     int length2 = 10;
131     byte[] buffer2 = new byte[length2];
132     for (int i = 0; i < length2; i++) {
133       buffer2[i] = (byte) (8 + i);
134     }
135
136     ubaos.reInit(buffer2);
137     for (int i = 0; i < length2; i++) {
138       ubaos.write(7 + i);
139     }
140
141     byte[] result2 = ubaos.toByteArray();
142
143     assertEquals(length2, ubaos.length());
144
145     for (int j = 0; j < length2; ++j) {
146       assertEquals(result2[j], j + 7);
147     }
148
149     for (int i = 0; i < length; i++) {
150       assertEquals(buffer[i], i);
151     }
152   }
153   
154   @Test
155   public void testStartPos() throws Exception {
156     byte[] buf = new byte[10];
157     for (int i = 0; i < buf.length; i++) {
158       buf[i] = (byte) i;
159     }
160     
161     int startPos = 3;
162     UnsafeByteArrayOutputStream ubaos = new UnsafeByteArrayOutputStream(buf, startPos);
163     int numValues = 5;
164     for (int i = 0; i < numValues; i++) {
165       ubaos.write((i + 1) * 2);
166     }
167
168     // the length of the buffer should be whatever was written after startPos
169     // and before that.
170     assertEquals("invalid buffer length", startPos + numValues, ubaos.length());
171
172     assertEquals("invalid startPos", startPos, ubaos.getStartPos());
173
174     byte[] bytes = ubaos.toByteArray();
175     for (int i = 0; i < startPos; i++) {
176       assertEquals(i, bytes[i]);
177     }
178     
179     for (int i = startPos, j = 0; j < numValues; i++, j++) {
180       assertEquals((j + 1) * 2, bytes[i]);
181     }
182
183     for (int i = startPos + numValues; i < buf.length; i++) {
184       assertEquals(i, bytes[i]);
185     }
186
187   }
188
189   @Test
190   public void testDefaultCtor() throws Exception {
191     UnsafeByteArrayOutputStream ubaos = new UnsafeByteArrayOutputStream();
192     int numValues = 5;
193     for (int i = 0; i < numValues; i++) {
194       ubaos.write(i);
195     }
196
197     assertEquals("invalid buffer length", numValues, ubaos.length());
198     
199     byte[] bytes = ubaos.toByteArray();
200     for (int i = 0; i < numValues; i++) {
201       assertEquals(i, bytes[i]);
202     }
203   }
204   
205   @Test(expected=IllegalArgumentException.class)
206   public void testIllegalBufferSize() throws Exception {
207     UnsafeByteArrayOutputStream ubaos = new UnsafeByteArrayOutputStream();
208     ubaos.reInit(new byte[0]);
209   }
210
211 }