add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / facet / src / test / org / apache / lucene / util / Vint8Test.java
1 package org.apache.lucene.util;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.io.IOException;
6
7 import org.junit.Test;
8
9 import org.apache.lucene.util.LuceneTestCase;
10 import org.apache.lucene.util.Vint8;
11
12 /**
13  * Licensed to the Apache Software Foundation (ASF) under one or more
14  * contributor license agreements.  See the NOTICE file distributed with
15  * this work for additional information regarding copyright ownership.
16  * The ASF licenses this file to You under the Apache License, Version 2.0
17  * (the "License"); you may not use this file except in compliance with
18  * the License.  You may obtain a copy of the License at
19  *
20  *     http://www.apache.org/licenses/LICENSE-2.0
21  *
22  * Unless required by applicable law or agreed to in writing, software
23  * distributed under the License is distributed on an "AS IS" BASIS,
24  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25  * See the License for the specific language governing permissions and
26  * limitations under the License.
27  */
28
29 /**
30  * Tests the {@link Vint8} class.
31  */
32 public class Vint8Test extends LuceneTestCase {
33
34   /**
35    * Tests the position wrapper.
36    * @throws Exception For any reason.
37    */
38   @Test
39   public void testPosition() throws Exception {
40     Vint8.Position pos = new Vint8.Position();
41     assertEquals(0, pos.pos);
42     pos = new Vint8.Position(12345);
43     assertEquals(12345, pos.pos);
44   }
45
46   private static int[] testValues = {
47     -1000000000,
48     -1, 0, (1 << 7) - 1, 1 << 7, (1 << 14) - 1, 1 << 14,
49     (1 << 21) - 1, 1 << 21, (1 << 28) - 1, 1 << 28
50   };
51   private static int[] bytesNeededTestValues = {
52     5, 5, 1, 1, 2, 2, 3, 3, 4, 4, 5
53   };
54
55   /**
56    * Tests the {@code bytesNeeded} method.
57    */
58   @Test
59   public void testBytesNeeded() {
60     assertEquals(5, Vint8.MAXIMUM_BYTES_NEEDED);
61     for (int j = 0; j < testValues.length; j++) {
62       assertEquals(bytesNeededTestValues[j], Vint8.bytesNeeded(testValues[j]));
63     }
64   }
65
66   /**
67    * Tests encoding and decoding to and from a stream.
68    */
69   @Test
70   public void testStreamEncodingAndDecoding() throws IOException {
71     ByteArrayOutputStream baos = new ByteArrayOutputStream(256);
72     int expectedSize = 0;
73     for (int j = 0; j < testValues.length; j++) {
74       Vint8.encode(testValues[j], baos);
75       expectedSize += bytesNeededTestValues[j];
76     }
77     assertEquals(expectedSize, baos.size());
78     ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
79     for (int j = 0; j < testValues.length; j++) {
80       assertEquals(testValues[j], Vint8.decode(bais));
81     }
82     assertEquals(0, bais.available());
83   }
84
85   /**
86    * Tests encoding and decoding to and from an array.
87    */
88   @Test
89   public void testArrayEncodingAndDecoding() throws IOException {
90     byte[] byteArray = new byte[256];
91     int position = 0, expectedSize = 0;
92     for (int j = 0; j < testValues.length; j++) {
93       position += Vint8.encode(testValues[j], byteArray, position);
94       expectedSize += bytesNeededTestValues[j];
95     }
96     assertEquals(expectedSize, position);
97     Vint8.Position pos = new Vint8.Position();
98     for (int j = 0; j < testValues.length; j++) {
99       assertEquals(testValues[j], Vint8.decode(byteArray, pos));
100     }
101     assertEquals(expectedSize, pos.pos);
102   }
103
104   /**
105    * The result of encoding the test values with the current algorithm. If these
106    * values are changed to match an algorithm change, compatibility with legacy
107    * data will be broken.
108    */
109   private static final byte[] encodedTestValues = {
110     -4, -93, -108, -20, 0, -1, -1, -1, -1, 127, 0, 127, -127, 0, -1, 127,
111     -127, -128, 0, -1, -1, 127, -127, -128, -128, 0, -1, -1, -1, 127, -127,
112     -128, -128, -128, 0
113   };
114
115   /**
116    * Tests algorithm.
117    */
118   @Test
119   public void testLegacyCompatibility() throws IOException {
120     /* To generate the encoded test values:
121     byte[] byteArray = new byte[256];
122     int position = 0, expectedSize = 0;
123     for (int j = 0; j < testValues.length; j++) {
124       position += Vint8.encode(testValues[j], byteArray, position);
125       expectedSize += bytesNeededTestValues[j];
126     }
127     assertEquals(expectedSize, position);
128     Vint8.Position pos = new Vint8.Position();
129     for (int j = 0; j < expectedSize; j++) {
130       System.out.print(byteArray[j] + ", ");
131     }
132     System.out.flush();
133     pos.pos = 0;
134     */
135     Vint8.Position pos = new Vint8.Position();
136     for (int j = 0; j < testValues.length; j++) {
137       assertEquals(testValues[j], Vint8.decode(encodedTestValues, pos));
138     }
139   }
140
141 } // end class Vint8Test