/*
 * Copyright 2004 The Apache Software Foundation
 * 
 * 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.
 */
using System;
using NUnit.Framework;
using Directory = clucene.net.Directory;
using RAMDirectory = clucene.net.RAMDirectory;
namespace clucene.net
{
	
	/// <summary> <code>TestBitVector</code> tests the <code>BitVector</code>, obviously.
	/// 
	/// </summary>
	/// <author>  "Peter Mularien" <pmularien@deploy.com>
	/// </author>
	/// <version>  $Id: TestBitVector.cs,v 1.3 2006/01/11 05:15:09 ustramooner Exp $
	/// </version>
	[TestFixture]
    public class TestBitVector
	{
		/// <summary> Test the default constructor on BitVectors of various sizes.</summary>
		/// <throws>  Exception </throws>
		[Test]
        public virtual void  TestConstructsize()
		{
			DoTestConstructOfsize(8);
			DoTestConstructOfsize(20);
			DoTestConstructOfsize(100);
			DoTestConstructOfsize(1000);
		}
		
		private void  DoTestConstructOfsize(int n)
		{
			BitVector bv = new BitVector(n);
			Assert.AreEqual(n, bv.size());
		}
		
		/// <summary> Test the get() and set() methods on BitVectors of various sizes.</summary>
		/// <throws>  Exception </throws>
		[Test]
        public virtual void  TestGetSet()
		{
			DoTestGetSetVectorOfsize(8);
			DoTestGetSetVectorOfsize(20);
			DoTestGetSetVectorOfsize(100);
			DoTestGetSetVectorOfsize(1000);
		}
		
		private void  DoTestGetSetVectorOfsize(int n)
		{
			BitVector bv = new BitVector(n);
			for (int i = 0; i < bv.size(); i++)
			{
				// ensure a set bit can be git'
				Assert.IsFalse(bv.get(i));
				bv.set(i);
				Assert.IsTrue(bv.get(i));
			}
		}
		
		/// <summary> Test the clear() method on BitVectors of various sizes.</summary>
		/// <throws>  Exception </throws>
		[Test]
        public virtual void  TestClear()
		{
			DoTestClearVectorOfsize(8);
			DoTestClearVectorOfsize(20);
			DoTestClearVectorOfsize(100);
			DoTestClearVectorOfsize(1000);
		}
		
		private void  DoTestClearVectorOfsize(int n)
		{
			BitVector bv = new BitVector(n);
			for (int i = 0; i < bv.size(); i++)
			{
				// ensure a set bit is cleared
				Assert.IsFalse(bv.get(i));
				bv.set(i);
				Assert.IsTrue(bv.get(i));
				bv.clear(i);
				Assert.IsFalse(bv.get(i));
			}
		}
		
		/// <summary> Test the count() method on BitVectors of various sizes.</summary>
		/// <throws>  Exception </throws>
		[Test]
        public virtual void  TestCount()
		{
			DoTestCountVectorOfsize(8);
			DoTestCountVectorOfsize(20);
			DoTestCountVectorOfsize(100);
			DoTestCountVectorOfsize(1000);
		}
		
		private void  DoTestCountVectorOfsize(int n)
		{
			BitVector bv = new BitVector(n);
			// test count when incrementally setting bits
			for (int i = 0; i < bv.size(); i++)
			{
				Assert.IsFalse(bv.get(i));
				Assert.AreEqual(i, bv.count());
				bv.set(i);
				Assert.IsTrue(bv.get(i));
				Assert.AreEqual(i + 1, bv.count());
			}
			
			bv = new BitVector(n);
			// test count when setting then clearing bits
			for (int i = 0; i < bv.size(); i++)
			{
				Assert.IsFalse(bv.get(i));
				Assert.AreEqual(0, bv.count());
				bv.set(i);
				Assert.IsTrue(bv.get(i));
				Assert.AreEqual(1, bv.count());
				bv.clear(i);
				Assert.IsFalse(bv.get(i));
				Assert.AreEqual(0, bv.count());
			}
		}
		
		/// <summary> Test writing and construction to/from Directory.</summary>
		/// <throws>  Exception </throws>
		[Test]
        public virtual void  TestWriteRead()
		{
			DoTestWriteRead(8);
			DoTestWriteRead(20);
			DoTestWriteRead(100);
			DoTestWriteRead(1000);
		}
		
		private void  DoTestWriteRead(int n)
		{
			Directory d = new RAMDirectory();
			
			BitVector bv = new BitVector(n);
			// test count when incrementally setting bits
			for (int i = 0; i < bv.size(); i++)
			{
				Assert.IsFalse(bv.get(i));
				Assert.AreEqual(i, bv.count());
				bv.set(i);
				Assert.IsTrue(bv.get(i));
				Assert.AreEqual(i + 1, bv.count());
				bv.write(d, "TESTBV");
				BitVector compare = new BitVector(d, "TESTBV");
				// compare bit vectors with bits set incrementally
				Assert.IsTrue(DoCompare(bv, compare));
			}
		}
		
		/// <summary> Compare two BitVectors.
		/// This should really be an equals method on the BitVector itself.
		/// </summary>
		/// <param name="bv">One bit vector
		/// </param>
		/// <param name="compare">The second to compare
		/// </param>
		private bool DoCompare(BitVector bv, BitVector compare)
		{
			bool equal = true;
			for (int i = 0; i < bv.size(); i++)
			{
				// bits must be equal
				if (bv.get(i) != compare.get(i))
				{
					equal = false;
					break;
				}
			}
			return equal;
		}
	}
}