////////////////////////////////////////////////////////////////////////////
//
// This file is the tests the implementation of the searching and sorting
// functions.
//
////////////////////////////////////////////////////////////////////////////

#include "search.h"
#include <iostream.h>
#include <iomanip.h>

ItemType List[MAX_LENGTH]; 
int Length;               

void test1(void);
void test2(void);

void testRead(const char filename[]);
void testSort(ItemType list[], int length);
void testLinearSearch(ItemType item);
void testLinearSearchOrd(ItemType item);
void testBinarySearch(ItemType item);
void testDeleteOne(ItemType item);
void testDeleteAll(ItemType item);
void printList(ItemType list[], int length);

int main(void)
{

	cout << endl << endl;

	test1();
	test2();

	return 0;
}

void test1(void)
{
	ItemType item = 10;

	testRead("numbers.dat");

	testLinearSearch(item);

	testSort(List, Length);

	testLinearSearchOrd(item);

	testBinarySearch(item);
}

void test2(void)
{
	Boolean success;
	char filename[] = "numbers2.dat";

	ReadData(filename, List, Length, success);
	Sort(List, Length);
	testDeleteOne(8);

	ReadData(filename, List, Length, success);
	Sort(List, Length);
	testDeleteAll(8);
}

void testRead(const char filename[])
{
	Boolean success;

	ReadData(filename, List, Length, success);
	
	if (success)
	{
		cout << "[testRead] Read " << Length << " items from " << filename << endl;
		printList(List, Length);
	}
	else
		cout << "ReadData failed on file: " << filename << endl;

}

void testLinearSearch(ItemType item)
{
	int index;
	int compares;
	Boolean found;

	cout << endl << "[testLinearSearch] Searching for item " << item << endl;

	LinearSearch(List, item, Length, index, found, compares);

	if (found)
		cout << "The item " << item << " was found at index " << index << endl;
	else
		cout << "The item " << item << " was not found" << endl;

	cout << "The search required " << compares << " compares" << endl;

}

void testSort(ItemType list[], int length)
{

	Sort(list, length);

	cout << endl << "[testSort] Sorted list has " << length << " items" << endl;
	printList(list, length);
}

void testLinearSearchOrd(ItemType item)
{

	int index;
	int compares;
	Boolean found;

	Sort(List, Length);
	LinearSearchOrd(List, item, Length, index, found, compares);

	cout << endl << "[testLinearSearchOrd] Search for item " << item << endl;

	if (found)
		cout << "The item " << item << " was found at index " << index << endl;
	else
		cout << "The item " << item << " was not found" << endl;

	cout << "The search required " << compares << " compares" << endl;

}

void testBinarySearch(ItemType item)
{

	int index;
	int compares;
	Boolean found;

	cout << endl << "[testBinary] Searching for item " << item << endl;
	Sort(List, Length);
	BinarySearch(List, item, Length, index, found, compares);

	if (found)
		cout << "The item " << item << " was found at index " << index << endl;
	else
		cout << "The item " << item << " was not found" << endl;

	cout << "The search required " << compares << " compares" << endl;

}

void testDeleteOne(ItemType item)
{

	cout << endl << "[testDeleteOne] Deleting one occurrence of " << item << endl;

	cout << "List BEFORE delete has " << Length << " items" << endl;
	printList(List, Length);

	DeleteOne(List, Length, item);

	cout << "List AFTER delete has " << Length << " items" << endl;
	printList(List, Length);
}

void testDeleteAll(ItemType item)
{

	cout << endl << "[testDeleteOne] Deleting all occurrences of item " << item << endl;

	cout << "List BEFORE delete has " << Length << " items" << endl;
	printList(List, Length);

	DeleteAll(List, Length, item);

	cout << "List AFTER delete has " << Length << " items" << endl;
	printList(List, Length);
}

void printList(ItemType list[], int length)
{
	for (int i = 0; i < length; i++)
	{
		cout << setw(3) << list[i];
		if ( !((i + 1) % 20))
			cout << endl;
	}

	cout << endl;
}
