void TestChunkFileFileRead(void)
{
	CChunkFile		cChunkFile;
	int				iChunkNum;
	char			szZ[] = {"Z"};
	CChunkFileFile	cChunkFileFile;
	char			cz;

	cChunkFile.Init(MemoryFile());
	AssertTrue(cChunkFile.WriteOpen());
	AssertTrue(cChunkFile.WriteChunkBegin());
	AssertLongLongInt(1, cChunkFile.Write(szZ, 1, 1));
	AssertTrue(cChunkFile.WriteChunkEnd("Zynaps"));
	AssertTrue(cChunkFile.WriteClose());

	AssertTrue(cChunkFile.ReadOpen());
	iChunkNum = cChunkFile.FindFirstChunkWithName("Zynaps");
	AssertInt(0, iChunkNum);
	AssertTrue(cChunkFile.ReadChunkBegin(iChunkNum));

	cChunkFileFile.Init(&cChunkFile);
	AssertTrue(cChunkFileFile.Open(EFM_Read));
	AssertLongLongInt(1, cChunkFileFile.Size());
	AssertLongLongInt(0, cChunkFileFile.Tell());
	AssertLongLongInt(1, cChunkFileFile.Read(&cz, 1, 1));
	AssertChar('Z', cz); cz = 'Q';

	AssertTrue(cChunkFileFile.Seek(0, EFSO_SET));
	AssertLongLongInt(0, cChunkFileFile.Tell());
	AssertLongLongInt(1, cChunkFileFile.Read(&cz, 1, 1));
	AssertChar('Z', cz); cz = 'Q';
	AssertLongLongInt(1, cChunkFileFile.Tell());
	AssertFalse(cChunkFileFile.Eof());
	AssertLongLongInt(0, cChunkFileFile.Read(&cz, 1, 1));
	AssertTrue(cChunkFileFile.Eof());
	AssertLongLongInt(1, cChunkFileFile.Tell());

	AssertTrue(cChunkFileFile.Seek(0, EFSO_SET));
	AssertFalse(cChunkFileFile.Eof());
	AssertLongLongInt(1, cChunkFileFile.Read(&cz, 1, 2));
	AssertChar('Z', cz); cz = 'Q';
	AssertLongLongInt(1, cChunkFileFile.Tell());
	AssertTrue(cChunkFileFile.Eof());

	AssertTrue(cChunkFileFile.Close());

	AssertTrue(cChunkFile.ReadChunkEnd());
	AssertTrue(cChunkFile.ReadClose());

	cChunkFile.Kill();
}
void TestSingleChannelAccessor(void)
{
	BeginTests();
	ClassStorageInit();
	TypeConverterInit();
	UnknownsInit();

	
	CChannels					cChannels;
	unsigned char				cData[9] = "ti@YA,\n#";
	CSingleChannelAccessor		cSingle;
	char						c;
	unsigned					s;

	cChannels.Init();
	cChannels.BeginChange();
	cChannels.SetData(cData);
	cChannels.AddChannel(16, 15, 14, 13, PT_nybble);
	cChannels.AddChannel(12, 11, PT_uchar);
	cChannels.AddChannel(10, PT_ushort);
	cChannels.AddChannel(9, 8, 7, 6, PT_bit);
	cChannels.AddChannel(5, 4, PT_crumb);
	cChannels.AddChannel(3, PT_uchar);
	cChannels.SetSize(1);
	cChannels.EndChange();
	cSingle.Init(&cChannels);
	AssertFloat(0.27f, cSingle.GetConvertToFloat(16), 2);
	AssertFloat(0.47f, cSingle.GetConvertToFloat(15), 2);
	AssertFloat(0.60f, cSingle.GetConvertToFloat(14), 2);
	AssertFloat(0.40f, cSingle.GetConvertToFloat(13), 2);
	cSingle.GetNative(16, &c); AssertChar(4, c);
	cSingle.GetNative(15, &c); AssertChar(7, c);
	cSingle.GetNative(14, &c); AssertChar(9, c);
	cSingle.GetNative(13, &c); AssertChar(6, c);

	AssertFloat(0.25f, cSingle.GetConvertToFloat(12), 2);
	AssertFloat(0.35f, cSingle.GetConvertToFloat(11), 2);
	cSingle.GetNative(12, &c); AssertChar('@', c);
	cSingle.GetNative(11, &c); AssertChar('Y', c);

	cSingle.GetNative(10, &s); AssertShortHex(0x2c41, s);

	AssertFloat(0.0f, cSingle.GetConvertToFloat(9), 1);
	AssertFloat(1.0f, cSingle.GetConvertToFloat(8), 1);
	AssertFloat(0.0f, cSingle.GetConvertToFloat(7), 1);
	AssertFloat(1.0f, cSingle.GetConvertToFloat(6), 1);

	AssertFloat(0.14f, cSingle.GetConvertToFloat(3), 2);
	cSingle.GetNative(3, &c); AssertChar('#', c);

	cChannels.Kill();


	UnknownsKill();
	TypeConverterKill();
	ClassStorageKill();
	TestStatistics();
}
void TestArrayPrimitiveTemplate(void)
{
	CArrayChar		aChars;
	CArrayFloat		aFloats;
	CArrayDouble	aDoubles;
	CArrayLong		aLongs;
	int				iIndex;

	BeginTests();

	aChars.Init(1);
	aChars.Add('A');
	AssertChar('A', aChars.GetValue(0));
	aChars.Kill();

	aFloats.Init(2);
	aFloats.InsertAt(5.1f, 0);
	AssertFloat(5.1f, aFloats.GetValue(0), 2);
	AssertInt(2, aFloats.AllocatedElements());
	AssertInt(1, aFloats.NumElements());
	aFloats.Kill();

	aDoubles.Init(3);
	aDoubles.Add(0.0);
	aDoubles.Add(0.1);
	aDoubles.Add(0.2);
	aDoubles.Add(0.3);
	aDoubles.Add(0.4);
	AssertTrue(aDoubles.IsSorted());
	aDoubles.InsertAt(0.5, 2);
	AssertFalse(aDoubles.IsSorted());
	aDoubles.Kill();

	aLongs.Init(4);
	aLongs.AddList(0LL, 4LL, 0x3122345482773411LL, 2LL, 1LL, 0LL);
	iIndex = aLongs.Find(0x3122345482773411LL);
	AssertInt(1, iIndex);
	aLongs.Kill();

	TestStatistics();
}
void TestChannelsAccessorWorstCase(void)
{
	CChannels*					pcChannels;
	CChannelsAccessor*			pcAccessor;
	CChannelsAccessorCreator	cCreator;
	unsigned char*				pucData;
	unsigned char				aucData[5];


	pcChannels = UMalloc(CChannels);
	pcChannels->Init();
	pcChannels->BeginChange();
	pcChannels->SetSize(2);
	pcChannels->AddChannel(CHANNEL_NAME_JACK, PT_bit);
	pcChannels->AddChannel(CHANNEL_NAME_BOB, PT_uint);
	pcChannels->AddChannel(CHANNEL_NAME_ALICE, PT_uchar);
	pcChannels->EndChange();
	pcChannels->Clear();
	AssertInt(2, pcChannels->GetSize());
	AssertInt(11, pcChannels->GetByteSize());
	AssertInt(41, pcChannels->GetBitStride());
	AssertInt(-1, pcChannels->GetByteStride());
	AssertInt(FALSE, pcChannels->IsOnlyBasicTypes());

	memset(aucData, 0, 5);

	cCreator.Init(pcChannels);
	cCreator.AddAccess(CHANNEL_NAME_JACK, PT_uint);
	cCreator.AddAccess(CHANNEL_NAME_BOB, PT_bit);
	cCreator.AddAccess(CHANNEL_NAME_ALICE, PT_nybble);
	pcAccessor = cCreator.CreateAndKill();
	AssertString("CChannelsAccessorWorstCase", pcAccessor->ClassName());
	AssertInt(-1, pcAccessor->GetByteSize());
	AssertInt(37, pcAccessor->GetBitSize());
	AssertInt(5, pcAccessor->GetBufferSize());
	aucData[0] = 0xff;
	aucData[1] = 0xff;
	aucData[2] = 0xff;
	aucData[3] = 0xff;
	aucData[4] = 0xff;
	pcAccessor->Set(0, aucData);
	AssertChar((unsigned char)0xff, pcChannels->GetData()[0]);
	AssertChar((unsigned char)0xff, pcChannels->GetData()[1]);
	AssertChar((unsigned char)0xff, pcChannels->GetData()[2]);
	AssertChar((unsigned char)0xff, pcChannels->GetData()[3]);
	AssertChar((unsigned char)0xff, pcChannels->GetData()[4]);
	AssertChar((unsigned char)0x01, pcChannels->GetData()[5]);  //01 because the next 'pixel' is all zeros.  The 1 is the high bit of the char in ALICE.
	pucData = (unsigned char*)pcAccessor->Get(0);
	AssertChar((unsigned char)0xff, pucData[0]);
	AssertChar((unsigned char)0xff, pucData[1]);
	AssertChar((unsigned char)0xff, pucData[2]);
	AssertChar((unsigned char)0xff, pucData[3]);
	AssertChar((unsigned char)0x1f, pucData[4]);

	aucData[0] = 0xff;
	aucData[1] = 0xff;
	aucData[2] = 0xff;
	aucData[3] = 0xff;
	aucData[4] = 0xfe;
	pcAccessor->Set(1, aucData);
	AssertChar((unsigned char)0x03, pcChannels->GetData()[5]); //01 from 'pixel' 0 and 02 from the bit in JACK in 'pixel' 1.
	AssertChar((unsigned char)0x00, pcChannels->GetData()[6]);
	AssertChar((unsigned char)0x00, pcChannels->GetData()[7]);
	AssertChar((unsigned char)0x00, pcChannels->GetData()[8]);
	AssertChar((unsigned char)0xfc, pcChannels->GetData()[9]);  //fc it the six low bits of the char
	AssertChar((unsigned char)0x03, pcChannels->GetData()[10] & 0x03);  //03 is the two high bits.
	pucData = (unsigned char*)pcAccessor->Get(1);
	AssertChar((unsigned char)0xff, pucData[0]);
	AssertChar((unsigned char)0xff, pucData[1]);
	AssertChar((unsigned char)0xff, pucData[2]);
	AssertChar((unsigned char)0xff, pucData[3]);
	AssertChar((unsigned char)0x1e, pucData[4]);
}
void TestChannelsAccessorChannelBitty(void)
{
	CChannels*					pcChannels;
	CChannelsAccessor*			pcAccessor;
	CChannelsAccessorCreator	cCreator;
	unsigned char*				pucData;
	unsigned char				aucData[3];
	float*						pfData;

	pcChannels = UMalloc(CChannels);
	pcChannels->Init();
	pcChannels->BeginChange();
	pcChannels->SetSize(1);
	pcChannels->AddChannel(CHANNEL_NAME_JACK, PT_bit);
	pcChannels->AddChannel(CHANNEL_NAME_BOB, PT_crumb);
	pcChannels->AddChannel(CHANNEL_NAME_ALICE, PT_bit);
	pcChannels->EndChange();
	pcChannels->Clear();
	AssertInt(1, pcChannels->GetByteSize());
	AssertInt(1, pcChannels->GetSize());

	cCreator.Init(pcChannels);
	cCreator.AddAccess(CHANNEL_NAME_JACK, PT_uchar);
	cCreator.AddAccess(CHANNEL_NAME_BOB, PT_uchar);
	cCreator.AddAccess(CHANNEL_NAME_ALICE, PT_uchar);
	pcAccessor = cCreator.CreateAndKill();
	AssertString("CChannelsAccessorChannelBitty", pcAccessor->ClassName());
	aucData[0] = 0xff;
	aucData[1] = 0x55;
	aucData[2] = 0xff;
	pcAccessor->Set(0, aucData);
	AssertChar(0xb, *pcChannels->GetData());  //Or maybe 0xd.  I'm not sure of the ordering
	pucData = (unsigned char*)pcAccessor->Get(0);
	AssertChar((char)0xff, pucData[0]);
	AssertChar((char)0x55, pucData[1]);
	AssertChar((char)0xff, pucData[2]);
	pcAccessor->Kill();

	pcChannels->BeginChange();
	pcChannels->SetSize(2);
	pcChannels->EndChange();
	AssertInt(1, pcChannels->GetByteSize());
	AssertInt(2, pcChannels->GetSize());

	cCreator.Init(pcChannels);
	cCreator.AddAccess(CHANNEL_NAME_JACK, PT_uchar);
	cCreator.AddAccess(CHANNEL_NAME_BOB, PT_uchar);
	cCreator.AddAccess(CHANNEL_NAME_ALICE, PT_uchar);
	pcAccessor = cCreator.CreateAndKill();
	AssertString("CChannelsAccessorChannelBitty", pcAccessor->ClassName());
	pucData = (unsigned char*)pcAccessor->Get(0);
	AssertChar((char)0xff, pucData[0]);
	AssertChar((char)0x55, pucData[1]);
	AssertChar((char)0xff, pucData[2]);

	aucData[0] = 0x00;
	aucData[1] = 0xaa;
	aucData[2] = 0xff;
	pcAccessor->Set(1, aucData);
	AssertChar(0xc, ((unsigned char)*pcChannels->GetData()) >> 4);  //Or maybe 0xd.  I'm not sure of the ordering
	pucData = (unsigned char*)pcAccessor->Get(1);
	AssertChar((char)0x00, pucData[0]);
	AssertChar((char)0xaa, pucData[1]);
	AssertChar((char)0xff, pucData[2]);
	pcAccessor->Kill();

	cCreator.Init(pcChannels);
	cCreator.AddAccess(CHANNEL_NAME_ALICE, PT_float);
	cCreator.AddAccess(CHANNEL_NAME_BOB, PT_float);
	pcAccessor = cCreator.CreateAndKill();
	AssertString("CChannelsAccessorChannelBitty", pcAccessor->ClassName());
	pfData = (float*)pcAccessor->Get(0);
	AssertFloat(1.0f, pfData[0], 2);
	AssertFloat(0.33f, pfData[1], 2);
	pfData = (float*)pcAccessor->Get(1);
	AssertFloat(1.0f, pfData[0], 2);
	AssertFloat(0.67f, pfData[1], 2);
	pcAccessor->Kill();
}
void TestImageColourAccessorBytes(void)
{
	CImageColourRGB		cRGB; 
	CImageColourOpacity	cAlpha;
	CImageColourCombo2	cColour;
	CChannels*			pcChannels;
	CImageAccessor*		pcAccessor;
	CImage				cImage;
	SImageColour		sDest;
	BOOL				bResult;
	char*				pvData;

	cRGB.Init(1.0f, 0.5f, 0.25f);
	cAlpha.Init(0.333f);
	cColour.Init(&cRGB, &cAlpha);

	cImage.Init(10, 10, PT_uchar,	IMAGE_DIFFUSE_GREEN,	//0x7f
									IMAGE_NORMAL_Y,		
									IMAGE_DIFFUSE_BLUE,	//0x3f
									IMAGE_OPACITY,		//0x54
									IMAGE_DIFFUSE_RED,	//0xff
									IMAGE_MASK, 
									CHANNEL_ZERO);
	cImage.Clear();

	pcAccessor = CImageAccessorCreator::Create(&cImage, PT_uchar, IMAGE_OPACITY, IMAGE_DIFFUSE_RED, IMAGE_DIFFUSE_GREEN, IMAGE_DIFFUSE_BLUE, CHANNEL_ZERO);

	sDest.Full();
	bResult = pcAccessor->MakeColour(&sDest, &cColour);
	AssertBool(TRUE, bResult);

	//Remember ints have reverse endiannes which is why the test number below looks backwarsd.
	AssertIntHex(0x3f7fff54, *(int*)((void*)sDest.c));

	pcAccessor->Set(1, 0, &sDest);
	pcAccessor->Kill();

	pcChannels = &cImage.mcChannels;

	pvData = pcChannels->GetData();

	//First pixel.  Didn't touch it.
	AssertChar(0x00, pvData[0x0]);
	AssertChar(0x00, pvData[0x1]);
	AssertChar(0x00, pvData[0x2]);
	AssertChar(0x00, pvData[0x3]);
	AssertChar(0x00, pvData[0x4]);
	AssertChar(0x00, pvData[0x5]);

	//Second pixel.
	AssertChar(0x7f, pvData[0x6]);
	AssertChar(0x00, pvData[0x7]);
	AssertChar(0x3f, pvData[0x8]);
	AssertChar(0x54, pvData[0x9]);
	AssertChar((char)0xff, pvData[0xa]);
	AssertChar(0x00, pvData[0xb]);
}
void TestBufferedFileRead(void)
{
	CFileUtil	cFileUtil;
	CFileBasic	cFile;
	CTextFile	cText;
	char		sz[20];
	char		c;
	int			iCount;
	int			i;
	char		szExpected[20];

	cFileUtil.Delete("Test.txt");

	cText.Init();
	cText.mcText.Append("abcdefghijk");
	cText.Write("Test.txt");
	cText.Kill();

	cFile.Init(BufferedFile(DiskFile("Test.txt"), 3));

	cFile.Open(EFM_Read);

	for (c = 'a'; c <= 'k'; c++)
	{
		AssertFalse(cFile.IsEndOfFile());
		memset(sz, 0, 20);
		iCount = (int)cFile.Read(sz, 1, 1);
		AssertChar(c, sz[0]);
		AssertChar(0, sz[1]);
		AssertInt(1, iCount);
	}
	AssertTrue(cFile.IsEndOfFile());

	cFile.Seek(0);

	szExpected[2] = 0;
	for (i = 0; i < 5; i++)
	{
		AssertFalse(cFile.IsEndOfFile());
		memset(sz, 0, 20);
		iCount = (int)cFile.Read(sz, 1, 2);
		szExpected[0] = 'a' + (char)i * 2;
		szExpected[1] = 'b' + (char)i * 2;
		AssertString(szExpected, sz);
		AssertInt(2, iCount);
	}
	AssertFalse(cFile.IsEndOfFile());
	memset(sz, 0, 20);
	iCount = (int)cFile.Read(sz, 1, 2);
	AssertString("k", sz);
	AssertInt(1, iCount);
	AssertTrue(cFile.IsEndOfFile());

	cFile.Seek(0);

	szExpected[3] = 0;
	for (i = 0; i < 3; i++)
	{
		AssertFalse(cFile.IsEndOfFile());
		memset(sz, 0, 20);
		iCount = (int)cFile.Read(sz, 1, 3);
		szExpected[0] = 'a' + (char)i * 3;
		szExpected[1] = 'b' + (char)i * 3;
		szExpected[2] = 'c' + (char)i * 3;
		AssertString(szExpected, sz);
		AssertInt(3, iCount);
	}
	AssertFalse(cFile.IsEndOfFile());
	memset(sz, 0, 20);
	iCount = (int)cFile.Read(sz, 1, 3);
	AssertString("jk", sz);
	AssertInt(2, iCount);
	AssertTrue(cFile.IsEndOfFile());

	cFile.Kill();

	cFileUtil.Delete("Test.txt");
}