1 package org.apache.lucene.util;
3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.io.IOException;
9 import org.apache.lucene.util.LuceneTestCase;
10 import org.apache.lucene.util.Vint8;
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
20 * http://www.apache.org/licenses/LICENSE-2.0
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.
30 * Tests the {@link Vint8} class.
32 public class Vint8Test extends LuceneTestCase {
35 * Tests the position wrapper.
36 * @throws Exception For any reason.
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);
46 private static int[] testValues = {
48 -1, 0, (1 << 7) - 1, 1 << 7, (1 << 14) - 1, 1 << 14,
49 (1 << 21) - 1, 1 << 21, (1 << 28) - 1, 1 << 28
51 private static int[] bytesNeededTestValues = {
52 5, 5, 1, 1, 2, 2, 3, 3, 4, 4, 5
56 * Tests the {@code bytesNeeded} method.
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]));
67 * Tests encoding and decoding to and from a stream.
70 public void testStreamEncodingAndDecoding() throws IOException {
71 ByteArrayOutputStream baos = new ByteArrayOutputStream(256);
73 for (int j = 0; j < testValues.length; j++) {
74 Vint8.encode(testValues[j], baos);
75 expectedSize += bytesNeededTestValues[j];
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));
82 assertEquals(0, bais.available());
86 * Tests encoding and decoding to and from an array.
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];
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));
101 assertEquals(expectedSize, pos.pos);
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.
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,
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];
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] + ", ");
135 Vint8.Position pos = new Vint8.Position();
136 for (int j = 0; j < testValues.length; j++) {
137 assertEquals(testValues[j], Vint8.decode(encodedTestValues, pos));
141 } // end class Vint8Test