/*
This program is distributed under the terms of the 'MIT license'. The text
of this licence follows...

Copyright (c) 2007-2009 J.D.Medhurst (a.k.a. Tixy)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

/**
@file
@brief	Test code for class BitVector. Main test function is #TestBitVector()
@test
*/

#include "common.h"
#include "test/test.h"
#include "../bitvector.h"



static const size_t TestSize = 160;
static BitVector<TestSize> Bits;
static BitVector<0> ZeroBits;



//
// Top level test function
//

void TestBitVector()
	{
#ifdef TEST_NOISY
	printf("TestBitVector Set(i) Clear(i) Test(i)\n");
#endif
	TEST((unsigned*)ZeroBits==0);
	unsigned i;
	for(i=0; i<TestSize; i++)
		{
		TEST(!Bits.Test(i));
		Bits.Set(i);
		TEST(Bits.Test(i));
		Bits.Clear(i);
		TEST(!Bits.Test(i));
		}

#ifdef TEST_NOISY
	printf("TestBitVector Set(i,n) Clear(i,n) TestSet(i,n) TestClear(i,n)\n");
#endif
	for(i=0; i<TestSize; i++)
		{
#ifdef TEST_NOISY
		printf("%d\n",i);
#endif
		for(unsigned j=i; j<=TestSize; j++)
			{
			if(j-i)
				TEST(Bits.TestClear(i,j-i));

			// test Set()
			Bits.Set(i,j-i);
			unsigned k;
			for(k=i; k<j; k++)
				TEST(Bits.Test(k)); // test each bit is set
			if(i)
				{
				// bit before range should still be clear...
				TEST(!Bits.Test(i-1));
				Bits.Set(i-1);
				}
			if(j+1<TestSize)
				{
				// bit after range should still be clear...
				TEST(!Bits.Test(j));
				Bits.Set(j);
				}

			// test TestClear()
			if(j-i)
				TEST(!Bits.TestClear(i,j-i)); // test on fully set range
			if(j-i>1)
				for(k=i; k<j; k++)
					{
					// test single bit in range clear...
					Bits.Clear(k);
					TEST(!Bits.TestClear(i,j-i));
					Bits.Set(k);
					}
			// test TestSet()
			if(j-i)
				TEST(Bits.TestSet(i,j-i)); // test on fully set range
			if(j-i>1)
				for(k=i; k<j; k++)
					{
					// test single bit in range clear...
					Bits.Clear(k);
					TEST(!Bits.TestSet(i,j-i));
					Bits.Set(k);
					}

			// test Clear()
			Bits.Clear(i,j-i);
			for(k=i; k<j; k++)
				TEST(!Bits.Test(k)); // test each bit is clear
			if(i)
				{
				// bit before range should still be set...
				TEST(Bits.Test(i-1));
				Bits.Clear(i-1);
				}
			if(j+1<TestSize)
				{
				// bit after range should still be set...
				TEST(Bits.Test(j));
				Bits.Clear(j);
				}

			// test TestClear()
			if(j-i)
				TEST(Bits.TestClear(i,j-i)); // test on cleared range
			if(j-i>1)
				for(k=i; k<j; k++)
					{
					// test single bit in range set...
					Bits.Set(k);
					TEST(!Bits.TestClear(i,j-i));
					Bits.Clear(k);
					}
			// test TestSet()
			if(j-i)
				TEST(!Bits.TestSet(i,j-i)); // test on cleared range
			if(j-i>1)
				for(k=i; k<j; k++)
					{
					// test single bit in range set...
					Bits.Set(k);
					TEST(!Bits.TestSet(i,j-i));
					Bits.Clear(k);
					}

			}
		}

#ifdef TEST_NOISY
	printf("TestBitVector Find(n,0)\n");
#endif
	for(i=0; i<=TestSize/2; i++)
		for(unsigned ss=0; ss<=i; ss++)
			{
#ifdef TEST_NOISY
			printf("%d\n",i);
#endif
			for(unsigned se=ss+1; se<=ss+64; se++)
				for(unsigned b=se+1; b<=se+i && b<TestSize; b++)
					{
					Bits.Reset();
					Bits.Set(ss,se-ss);
					Bits.Set(b);
					int a = Bits.Find(i,0);
					if(i<=ss)
						TEST(a==0);
					else if(i<=b-se)
						TEST(a==(int)se);
					else if(i<=TestSize-(b+1))
						TEST(a==(int)b+1);
					else
						TEST(a==-1);
					}
			}
	}

