/*
 * Copyright 2014 Takao Nakaguchi
 *
 * 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.
 */
package org.trie4j.bv;

import org.junit.Assert;
import org.junit.Test;

public class LongsSuccinctBitVectorTest extends AbstractSuccinctBitVectorTest{
	protected SuccinctBitVector create(){
		return new LongsSuccinctBitVector();
	}

	protected SuccinctBitVector create(int initialCapacity){
		return new LongsSuccinctBitVector(initialCapacity);
	}

	protected SuccinctBitVector create(byte[] bytes, int bitsSize){
		return new LongsSuccinctBitVector(bytes, bitsSize);
	}

	@Test
	public void test_longsSize() throws Exception{
		Assert.assertEquals(0, new LongsSuccinctBitVector(0).getLongs().length);
		Assert.assertEquals(1, new LongsSuccinctBitVector(1).getLongs().length);
		Assert.assertEquals(1, new LongsSuccinctBitVector(64).getLongs().length);
		Assert.assertEquals(2, new LongsSuccinctBitVector(65).getLongs().length);
	}

	@Test
	public void test_count0CacheSize() throws Exception{
		Assert.assertEquals(0, new LongsSuccinctBitVector(0).getCountCache0().length);
		Assert.assertEquals(1, new LongsSuccinctBitVector(1).getCountCache0().length);
		Assert.assertEquals(1, new LongsSuccinctBitVector(64).getCountCache0().length);
		Assert.assertEquals(2, new LongsSuccinctBitVector(
				LongsSuccinctBitVector.CACHE_WIDTH_BITS + 1).getCountCache0().length);
	}

	@Test
	public void test_append_countCache0_1() throws Exception{
		LongsSuccinctBitVector sbv = new LongsSuccinctBitVector();
		for(int i = 0; i < LongsSuccinctBitVector.CACHE_WIDTH_BITS; i++){
			sbv.append0();
			Assert.assertEquals(i + 1, sbv.getCountCache0()[0]);
		}
		for(int i = 0; i < LongsSuccinctBitVector.CACHE_WIDTH_BITS; i++){
			sbv.append0();
			Assert.assertEquals(LongsSuccinctBitVector.CACHE_WIDTH_BITS, sbv.getCountCache0()[0]);
			Assert.assertEquals(LongsSuccinctBitVector.CACHE_WIDTH_BITS + i + 1, sbv.getCountCache0()[1]);
		}
	}

	@Test
	public void test_append_countCache0_2() throws Exception{
		LongsSuccinctBitVector sbv = new LongsSuccinctBitVector();
		for(int i = 0; i < 10000; i++){
			sbv.append0();
			Assert.assertEquals(i + 1, sbv.getCountCache0()[i / LongsSuccinctBitVector.CACHE_WIDTH_BITS]);
		}
	}

	@Test
	public void test_append_countCache0_3() throws Exception{
		LongsSuccinctBitVector sbv = new LongsSuccinctBitVector();
		for(int i = 0; i < 10000; i++){
			sbv.append0();
			sbv.append1();
			String msg = i + "th";
			Assert.assertEquals(msg, i + 1, sbv.getCountCache0()[i * 2 / LongsSuccinctBitVector.CACHE_WIDTH_BITS]);
		}
	}

	@Test
	public void test_append_indexCache0_1() throws Exception{
		LongsSuccinctBitVector sbv = new LongsSuccinctBitVector();
		for(int i = 0; i < LongsSuccinctBitVector.BITS0_COUNT_IN_EACH_INDEX * 3; i++){
			sbv.append0();
		}
		Assert.assertEquals(
				LongsSuccinctBitVector.BITS0_COUNT_IN_EACH_INDEX - 1,
				sbv.getIndexCache0()[0]);
		Assert.assertEquals(
				LongsSuccinctBitVector.BITS0_COUNT_IN_EACH_INDEX * 2 - 1,
				sbv.getIndexCache0()[1]);
		Assert.assertEquals(
				LongsSuccinctBitVector.BITS0_COUNT_IN_EACH_INDEX * 3 - 1,
				sbv.getIndexCache0()[2]);
	}
}

@@ -3282,32 +3282,96 @@
 t.assertEquals(%0A
+%09%09%09%090,%0A%09%09%09%09sbv.getIndexCache0().get(0));%0A%09%09Assert.assertEquals(%0A
 %09%09%09%09LongsSuccinc
@@ -3432,27 +3432,31 @@
 ndexCache0()
-%5B0%5D
+.get(1)
 );%0A%09%09Assert.
@@ -3551,27 +3551,31 @@
 ndexCache0()
-%5B1%5D
+.get(2)
 );%0A%09%09Assert.
@@ -3678,11 +3678,15 @@
 e0()
-%5B2%5D
+.get(3)
 );%0A%09

fix test case.