#include "stdio.h"
#include "pthread.h"

#include "dfixer_lib.h"

extern set<Pair> preAcqSet;
extern set<Pair> hwCondSet;


/**
* g++ -g -pthread -I./ -c -o  test.o test.cpp
* g++ -g -pthread dfixer_lib.o test.o -o test
* or
* g++ -g -pthread -I./ -c -o  test.o test.cpp;g++ -g -pthread dfixer_lib.o test.o -o test
*/

pthread_mutex_t lock1, lock2, lock3, lock4;
bool takenLock1;
bool takenLock2;

bool hwCond;


void testPreTaken()
{
	takenLock1 = preAcqTaken(&lock1);
	takenLock2 = preAcqTaken(&lock2);

	printf("Taken lock1: %s, lock2: %s\n", takenLock1? "Yes": "No", takenLock2? "Yes":"No");
	
	
	printf("Set taken lock1\n");
	setPreAcqTaken(&lock1);
	printf("Size: %d\n", preAcqSet.size());
	
	takenLock1 = preAcqTaken(&lock1);
	takenLock2 = preAcqTaken(&lock2);

	printf("Taken lock1: %s, lock2: %s\n", takenLock1? "Yes": "No", takenLock2? "Yes":"No");

	printf("Set taken lock2\n");
	setPreAcqTaken(&lock2);
	printf("Size: %d\n", preAcqSet.size());
	
	takenLock1 = preAcqTaken(&lock1);
	takenLock2 = preAcqTaken(&lock2);
	printf("Taken lock1: %s, lock2: %s\n", takenLock1? "Yes": "No", takenLock2? "Yes":"No");


	clearPreAcq(&lock1);
	printf("Clear lock1\n");

	printf("Size: %d\n", preAcqSet.size());
	
	takenLock1 = preAcqTaken(&lock1);
	takenLock2 = preAcqTaken(&lock2);
	printf("Taken lock1: %s, lock2: %s\n", takenLock1? "Yes": "No", takenLock2? "Yes":"No");


	clearPreAcq(&lock2);
	printf("Clear lock2\n");
	
	printf("Size: %d\n", preAcqSet.size());

	takenLock1 = preAcqTaken(&lock1);
	takenLock2 = preAcqTaken(&lock2);
	printf("Taken lock1: %s, lock2: %s\n", takenLock1? "Yes": "No", takenLock2? "Yes":"No");

}

void testHWCond()
{
	hwCond = getHWCond(&lock1, &lock2);
	printf("HW on? %s\n", hwCond? "Yes":"No");
	
	printf("Size: %d\n", hwCondSet.size());
		
	printf("Set on\n");
	setHWCondOn(&lock1, &lock2);
	printf("Size: %d\n", hwCondSet.size());
		
	hwCond = getHWCond(&lock1, &lock2);
	printf("HW on? %s\n", hwCond? "Yes":"No");
	
	//lock3 and lock4
	hwCond = getHWCond(&lock3, &lock4);
	printf("HW on(3,4)? %s\n", hwCond? "Yes":"No");
	
	printf("Size: %d\n", hwCondSet.size());
	
	printf("clear\n");
	setHWCondOff(&lock1, &lock2);
	hwCond = getHWCond(&lock1, &lock2);
	printf("HW on? %s\n", hwCond? "Yes":"No");

	printf("Size: %d\n", hwCondSet.size());
	
	hwCond = getHWCond(&lock3, &lock4);
	printf("HW on(3,4)? %s\n", hwCond? "Yes":"No");
}


void testPreAcq()
{
	preAcq_pthread_mutex_lock(&lock1, &lock2);
	
	printf("acquired two locks\n");
	
	pthread_mutex_unlock(&lock1);
	pthread_mutex_unlock(&lock2);
}

int main(int arg, char** args)
{

	testPreTaken();
	testHWCond();
	testPreAcq();

	return 0;
}
