package org.apache.lucene.util; /** * Copyright 2005 Paul Elschot * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import junit.framework.TestCase; import junit.framework.TestSuite; import junit.textui.TestRunner; import java.util.BitSet; public class TestSortedVIntList extends TestCase { /** Main for running test case by itself. */ public static void main(String args[]) { TestRunner.run(new TestSuite(TestSortedVIntList.class)); } private void tstIteratorAndSize( SortedVIntList vintList, int[] ints, int expectedSize, int expectedByteSize) { intIterator ii = vintList.getIntIterator(); for (int i = 0; i < expectedSize; i++) { assertTrue("No end of iterator at: " + i, ii.hasNext()); assertEquals(Integer.toString(i), ints[i], ii.next()); } assertFalse("End of iterator", ii.hasNext()); assertEquals("Size", expectedSize, vintList.size()); assertEquals("Byte size", expectedByteSize, vintList.getByteSize()); } public void tstBitSet(int [] ints, int expectedByteSize) { int expectedSize = ints.length; final int MAX_INT_FOR_BITSET = 1024 * 1024; BitSet bs = new BitSet(); for (int i = 0; i < expectedSize; i++) { if (ints[i] > MAX_INT_FOR_BITSET) { return; // do not perform BitSet test: too much memory would be used } if ((i > 0) && (ints[i-1] == ints[i])) { return; // do not perform BitSet test: BitSet cannot store duplicate } bs.set(ints[i]); } SortedVIntList vintList = new SortedVIntList(bs); tstIteratorAndSize(vintList, ints, expectedSize, expectedByteSize); } public void tst(int [] ints, int expectedByteSize) { int expectedSize = ints.length; SortedVIntList vintList = new SortedVIntList(ints, expectedSize); tstIteratorAndSize(vintList, ints, expectedSize, expectedByteSize); tstBitSet(ints, expectedByteSize); } public void tstIAExc(int [] ints) { try { new SortedVIntList(ints, ints.length); } catch (IllegalArgumentException e) { return; } fail("Expected IllegalArgumentException"); } public void test01() { int[] ia = new int[] {}; tst(ia, ia.length); } public void test02() { int[] ia = new int[] {0}; tst(ia, ia.length); } public void test03() { int[] ia = new int[] {0,Integer.MAX_VALUE}; tst(ia, 1 + 5); } public void test04a() { int[] ia = new int[] {0, 16384-1}; tst(ia, ia.length + 1); } public void test04b() { int[] ia = new int[] {0, 16384}; tst(ia, ia.length + 2); } public void test05() { int[] ia = new int[] {0,1,1,2,3,5,8}; tst(ia, ia.length); } public void test06() { int[] ia = new int[] {0,100,101,200,300,500/*+1*/,8000/*+1*/,8001,30000/*+2*/}; tst(ia, ia.length + 4); } public void test10() { int[] ia = new int[] {-1}; tstIAExc(ia); } public void test11() { int[] ia = new int[] {1,0}; tstIAExc(ia); } public void test12() { int[] ia = new int[] {0,1,1,2,3,5,8,0}; tstIAExc(ia); } }