//
//  UnitTests.m
//  omino_fitting_2006
//
//  Created by david van brink on 3/30/06.
//  Copyright 2006 __MyCompanyName__. All rights reserved.
//

#import "UnitTests.h"

#include "fittings.h"
#include "Memory.h"



#define assertX(_m,_n) if(!(_n)) printf("ERROR %d [%s:%d] %s\n",++gErrorCount,__FILE__,__LINE__,_m);

#define assertNotZero(_m,_n) assertX(_m,(_n)!=0)
#define assertZero(_m,_n) assertEquals(_m,(_n),0)
#define assertEquals(_m,_n1,_n2) if((_n1) != (_n2)) printf("ERROR %d [%s:%d] %d!=%d %s\n",++gErrorCount,__FILE__,__LINE__,_n1,_n2,_m);

static int gErrorCount = 0;



@implementation UnitTests

- (void) testMemory
{
	char *n = Malloc(20);
	assertNotZero("Malloc",(int)n);
	
	n[19] = 99;
	n[200]=100;
	FreeAt((void **)&n);
	assertZero("FreeAt",(int)n);
}

- (void) testNewOfo
{
	OFOmino *ofo = newOFOmino("123",100,0);//3,0);
	assertNotZero("newOFOmino",ofo);
	assertEquals("fill order",ofo->fillOrder,0);
	assertEquals("field row",ofo->fieldRow,3);
	assertEquals("orientation count",ofo->orientationCount,8);
	
	assertNotZero("orientation 0",ofo->orientations[0]);
	
	// "123" on row 3 should make 0,3,4,6,7,8
	assertEquals("123",ofo->orientations[0]->cellOffsets[0],0);
	assertEquals("123",ofo->orientations[0]->cellOffsets[1],3);
	assertEquals("123",ofo->orientations[0]->cellOffsets[2],4);
	assertEquals("123",ofo->orientations[0]->cellOffsets[3],6);
	assertEquals("123",ofo->orientations[0]->cellOffsets[4],7);
	assertEquals("123",ofo->orientations[0]->cellOffsets[5],8);

	// first ccw rotation
	assertEquals("123",ofo->orientations[1]->cellOffsets[0],2);
	assertEquals("123",ofo->orientations[1]->cellOffsets[1],1);
	assertEquals("123",ofo->orientations[1]->cellOffsets[2],4);
	assertEquals("123",ofo->orientations[1]->cellOffsets[3],0);
	assertEquals("123",ofo->orientations[1]->cellOffsets[4],3);
	assertEquals("123",ofo->orientations[1]->cellOffsets[5],6);

	// first ccw rotation
	assertEquals("123",ofo->orientations[3]->cellOffsets[0],4);
	assertEquals("123",ofo->orientations[3]->cellOffsets[1],5);
	assertEquals("123",ofo->orientations[3]->cellOffsets[2],2);
	assertEquals("123",ofo->orientations[3]->cellOffsets[3],6);
	assertEquals("123",ofo->orientations[3]->cellOffsets[4],3);
	assertEquals("123",ofo->orientations[3]->cellOffsets[5],0);

	// reflected
	assertEquals("-123",ofo->orientations[4]->cellOffsets[0],0);
	assertEquals("-123",ofo->orientations[4]->cellOffsets[1],3);
	assertEquals("-123",ofo->orientations[4]->cellOffsets[2],2);
	assertEquals("-123",ofo->orientations[4]->cellOffsets[3],6);
	assertEquals("-123",ofo->orientations[4]->cellOffsets[4],5);
	assertEquals("-123",ofo->orientations[4]->cellOffsets[5],4);
}
@end
