add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / facet / src / test / org / apache / lucene / util / UnsafeByteArrayInputStreamTest.java
1 package org.apache.lucene.util;
2
3 import java.io.IOException;
4 import java.util.Arrays;
5
6 import org.junit.Test;
7
8 import org.apache.lucene.util.LuceneTestCase;
9 import org.apache.lucene.util.UnsafeByteArrayInputStream;
10
11 /**
12  * Licensed to the Apache Software Foundation (ASF) under one or more
13  * contributor license agreements.  See the NOTICE file distributed with
14  * this work for additional information regarding copyright ownership.
15  * The ASF licenses this file to You under the Apache License, Version 2.0
16  * (the "License"); you may not use this file except in compliance with
17  * the License.  You may obtain a copy of the License at
18  *
19  *     http://www.apache.org/licenses/LICENSE-2.0
20  *
21  * Unless required by applicable law or agreed to in writing, software
22  * distributed under the License is distributed on an "AS IS" BASIS,
23  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24  * See the License for the specific language governing permissions and
25  * limitations under the License.
26  */
27
28 public class UnsafeByteArrayInputStreamTest extends LuceneTestCase {
29
30   @Test
31   public void testSimple() throws IOException {
32     int length = 256;
33     byte[] buffer = new byte[length];
34     for (int i = 0; i < length; ++i) {
35       buffer[i] = (byte) i;
36     }
37     byte[] result = new byte[buffer.length];
38     UnsafeByteArrayInputStream ubais = new UnsafeByteArrayInputStream(buffer);
39     
40     int index = 0;
41     int by = ubais.read();
42     while (by >= 0) {
43       result[index++] = (byte) (by);
44       by = ubais.read();
45     }
46     
47     assertEquals(length, index);
48     assertTrue(Arrays.equals(buffer, result));
49   }
50   
51   @Test
52   public void testStartPos() throws IOException {
53     int length = 100;
54     byte[] buffer = new byte[length];
55     for (int i = 0; i < length; ++i) {
56       buffer[i] = (byte) i;
57     }
58     int startPos = 5;
59     byte[] result = new byte[buffer.length];
60     UnsafeByteArrayInputStream ubais = new UnsafeByteArrayInputStream(buffer, startPos, length);
61     
62     int index = 0;
63     int by = ubais.read();
64     while (by >= 0) {
65       result[index++] = (byte) (by);
66       by = ubais.read();
67     }
68     
69     assertEquals(length - startPos, index);
70     for (int i = startPos; i < length; i++) {
71       assertEquals(buffer[i], result[i - startPos]);
72     }
73   }
74   
75   @Test
76   public void testReinit() throws IOException {
77     int length = 100;
78     byte[] buffer = new byte[length];
79     for (int i = 0; i < length; ++i) {
80       buffer[i] = (byte) i;
81     }
82     byte[] result = new byte[buffer.length];
83     UnsafeByteArrayInputStream ubais = new UnsafeByteArrayInputStream(buffer);
84
85     int index = 0;
86     int by = ubais.read();
87     while (by >= 0) {
88       result[index++] = (byte) (by);
89       by = ubais.read();
90     }
91
92     assertEquals(length, index);
93     assertTrue(Arrays.equals(buffer, result));
94
95     int length2 = 50;
96     byte[] buffer2 = new byte[length2];
97     for (int i = 0; i < length2; ++i) {
98       buffer2[i] = (byte) (90 + i);
99     }
100     byte[] result2 = new byte[buffer2.length];
101     ubais.reInit(buffer2);
102
103     int index2 = 0;
104     int by2 = ubais.read();
105     while (by2 >= 0) {
106       result2[index2++] = (byte) (by2);
107       by2 = ubais.read();
108     }
109
110     assertEquals(length2, index2);
111     assertTrue(Arrays.equals(buffer2, result2));
112   }
113
114   @Test
115   public void testDefaultCtor() throws Exception {
116     UnsafeByteArrayInputStream ubais = new UnsafeByteArrayInputStream();
117     assertEquals(0, ubais.available());
118     assertEquals(-1, ubais.read());
119   }
120
121   @Test
122   public void testMark() throws Exception {
123     byte[] bytes = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
124     UnsafeByteArrayInputStream ubais = new UnsafeByteArrayInputStream(bytes);
125     assertTrue(ubais.markSupported());
126     int markIndex = 3;
127     // Advance the index
128     for (int i = 0; i < markIndex; i++) {
129       ubais.read();
130     }
131     ubais.mark(markIndex);
132     for (int i = markIndex; i < bytes.length; i++) {
133       ubais.read();
134     }
135     ubais.reset();
136     assertEquals(bytes.length - markIndex, ubais.available());
137   }
138   
139 }