Ejemplo n.º 1
0
bool Test::test_mazeReader01()
{
	bool isPassed = false;
	

	m_testNum++;


	printTestMessage(m_testNum, "MazeReader throws MazeCreationException if file doesn't exist");

	try
	{
		MazeReader mr("");
	}
	catch(MazeCreationException& mce)
	{
		isPassed = true;
	}
	catch(...)
	{
		std::cerr << "ERROR: Exception thrown was not MazeCreationException" << std::endl;
	}

	printPassFail(isPassed);

	return(isPassed);
}
Ejemplo n.º 2
0
bool Test::test2()
{
	bool isPassed = true;
	const int SIZE = 1000;
	const int LOW = -100;
	const int HIGH = 100;
	int* arr = Sorts<int>::createTestArray(SIZE, LOW, HIGH);

	printTestMessage(2, "createTestArray creates an array with values in the provided range");
	
	//check each value to ensure it's in range
	for(int i=0; i<SIZE && isPassed; i++)
	{
		if(arr[i] < LOW || arr[i] > HIGH)
		{
			isPassed = false;
			std::cerr << "ERROR! Excepted value from " << LOW << " to " << HIGH << ". Got " << arr[i];
			std::cerr << std::endl;
		}
	}
	
	delete[] arr;
	arr = nullptr;
	printPassFail(isPassed);
	return (isPassed);
}
Ejemplo n.º 3
0
bool Test::test17()
{
	bool isPassed = false;
	const int LOW = 0;
	const int HIGH = 5000;
	int* arr = Sorts<int>::createTestArray(TEST_SIZE, LOW, HIGH);
	double time = 0.0;

	printTestMessage(17, "selection sort completes in reasonable amount of time (0-10secs platform dependent) " + std::to_string(TEST_SIZE));
	
	time = Sorts<int>::sortTimer(Sorts<int>::selectionSort, arr, TEST_SIZE);
	
	std::cerr << "\nTime for selection sort on " + std::to_string(TEST_SIZE) + " elements: " << time << " ";	
	
	if(time<0 || time>10)
	{
		isPassed = false;
	}
	else
	{
		isPassed = true;
	}
	
	delete[] arr;
	arr = nullptr;

	printPassFail(isPassed);
	return (isPassed);
}
Ejemplo n.º 4
0
bool Test::test_order03()
{
	bool isPassed = false;
	BSTI<int>* bst = new BinarySearchTree<int>();
	std::vector<int> input = {50, 25, 75, 10, 30, 65, 100}; 
	std::vector<int> correct = {10, 30, 25, 65, 100, 75, 50};
	std::vector<int> output;

	m_testNum++;

	printTestMessage(m_testNum, "vector returned by treeToVector(IN_ORDER) returns vector: {10, 30, 25, 65, 100, 75, 50}");

	loadVectorIntoTree(input, bst);
	output = bst->treeToVector(POST_ORDER);

	if(output == correct)
	{
		isPassed = true;
	}
	else
	{
		isPassed = false;
		std::cerr << "ERROR: expected ";
		printVector(correct);
		std::cerr << " got ";
		printVector(output);
		std::cerr << std::endl;	
	}	

	printPassFail(isPassed);

	delete bst;
	return(isPassed);
}
Ejemplo n.º 5
0
bool Test::test9()
{
	Stack<int> stack;
	bool isPassed = false;
	int trackedSize = 0;
	printTestMessage(9, "size remains accurate after lots of pushes and pops");
	
	try
	{
		for(int i=0; i<TEST_SIZE; i++)
		{
			if(i%5 == 0 && i > 0)
			{
				stack.pop();
				trackedSize--;
			}
			else
			{
				stack.push(i);
				trackedSize++;			
			}
		}	
	}
	catch(...)
	{
		std::cerr << "Unexpected exception thrown!" << std::endl;
	}	
	
	isPassed = stack.size() == trackedSize;

	std::cerr << "\nExpected size: " << trackedSize << " size() returned: " << stack.size() << " ";
	printPassFail(isPassed);		
	return(isPassed);	
}
Ejemplo n.º 6
0
bool Test::test_search06()
{
	bool isPassed = true;
	BSTI<int>* bst = new BinarySearchTree<int>();
	std::vector<int> vec;
	m_testNum++;

	printTestMessage(m_testNum, "searching for all values in a large tree of random values; all searches return true");

	uniqueRandomFill(vec, TEST_SIZE);
	loadVectorIntoTree(vec, bst);
		
	for(std::size_t i=0; i<vec.size(); i++)
	{
		if( !bst->search(vec[i])  )
		{
			isPassed = false;	
			std::cerr << "ERROR: Search for " << vec[i] << " returned false after being added." << std::endl;
		}
	}	

	printPassFail(isPassed);
	delete bst;
	return(isPassed);
}
Ejemplo n.º 7
0
bool Test::test_clone02()
{
	bool isPassed = true;
	BSTI<int>* original = new BinarySearchTree<int>();
	BSTI<int>* copy = nullptr; 
	std::vector<int> vec;
	m_testNum++;

	printTestMessage(m_testNum, "cloning a large tree, deleting original, searching for all values in copy");
	
	
	uniqueRandomFill(vec, TEST_SIZE);
	loadVectorIntoTree(vec, original);
		
	copy = original->clone();
	delete original;
	original = nullptr;
	
	for(std::size_t i=0; i<vec.size(); i++)
	{
		if( !copy->search(vec[i])  )
		{
			isPassed = false;	
			std::cerr << "ERROR: Search for " << vec[i] << " returned false after being added." << std::endl;
		}
	}

	printPassFail(isPassed);
	delete copy;
	copy = nullptr;
	return(isPassed);
}
Ejemplo n.º 8
0
bool Test::test11()
{
	Stack<int> stack;
	int num = 0;
	bool isPassed = true;
	printTestMessage(11, "print after lots of pushes retains order");

	loadValues(stack);

	redirectOS();
	ss_redirect.clear();
	stack.print();
	restoreOS();

	//sizes will be printed in reverse, since you know, stacks.
	for(int i=TEST_SIZE-1; i>=0; i--)
	{
		ss_redirect >> num;
		
		if( i!=num )
		{
			isPassed = false;
		}
	}

	printPassFail(isPassed);	
	return(isPassed);
}
Ejemplo n.º 9
0
bool Test_LinkedList::test16()
{
	LinkedList<int> list;
	bool isPassed = false;
	int trackedSize = 0;

	printTestMessage("size preserved by removeBack on populated list");

	//Remove front on every 3rd iteration, add otherwise.
	for(int i=0; i<TEST_SIZE; i++)
	{
		if( i>0 && i%3 == 0)
		{

			list.removeBack();
			trackedSize--;
		}
		else
		{

			list.addBack(i);
			trackedSize++;
		}
	}

	isPassed = trackedSize == list.size();
	printPassFail(isPassed);
	return (isPassed); 
}
Ejemplo n.º 10
0
bool Test::testBadFile(std::string testFileName, std::string testMessage)
{
	bool isPassed = false;
	m_testNum++;

	printTestMessage(m_testNum, testMessage);

	if( isFileAccessible(testFileName) )
	{
		try
		{
			MazeReader mr(testFileName);
		}
		catch(MazeCreationException& mce)
		{
			isPassed = true;
		}
		catch(...)
		{
			std::cerr << "ERROR: Exception thrown was not MazeCreationException" << std::endl;
		}
	}
	else
	{
		std::cerr << "ERROR " << testFileName << " not accessible. Place in lab folder." << std::endl;		
	}

	printPassFail(isPassed);
	
	return(isPassed);
}
Ejemplo n.º 11
0
bool Test::test_mazeWalker04()
{
	bool isPassed = false;
	std::string testFileName = "noExit.txt";
	const int correctRows = 6;
	const int correctCols = 6;
	int** correctArray = new int*[correctRows];
	Search searchChoice = Search::BFS;	

	correctArray[0] = new int[correctCols]{0,	  0,	  0,	  0,	  0,	  1};
	correctArray[1] = new int[correctCols]{11,	  9,	  7,	  6,	  4,	  2};
	correctArray[2] = new int[correctCols]{0,	  0,	  8,	  0,	  0,	  3};
	correctArray[3] = new int[correctCols]{14,	  12,	  10,	  0,	  0,	  5};
	correctArray[4] = new int[correctCols]{0,	  13,	  0,	  0,	  0,	  0};
	correctArray[5] = new int[correctCols]{0,	  15,	  16,	  17,	  18,	  19};

	m_testNum++;	
	printTestMessage(m_testNum, "MazeWalker visits all reachable positions in noExit.txt in correct order using bfs");	

	isPassed = testWalker(testFileName, correctArray, correctRows, correctCols, searchChoice);
	
	printPassFail(isPassed);

	for(int i=0; i<correctRows; i++)
	{
		delete[] correctArray[i];
	}

	delete[] correctArray;


	return(isPassed);
}
Ejemplo n.º 12
0
bool Test::test_mazeWalker02()
{
	bool isPassed = false;
	std::string testFileName = "maze.txt";
	const int correctRows = 8;
	const int correctCols = 6;
	int** correctArray = new int*[correctRows];
	Search searchChoice = Search::BFS;	

	correctArray[0] = new int[correctCols]{16,	  0,	  0,	  0,	  0,	  0};
	correctArray[1] = new int[correctCols]{14,	  12,	  10,	  11,	  13,	  15};
	correctArray[2] = new int[correctCols]{0,	  0,	  9,	  0,	  0,	  0};
	correctArray[3] = new int[correctCols]{8,	  6,	  7,	  0,	  0,	  0};
	correctArray[4] = new int[correctCols]{0,	  5,	  0,	  0,	  0,	  0};
	correctArray[5] = new int[correctCols]{0,	  4,	  3,	  2,	  1,	  0};
	correctArray[6] = new int[correctCols]{0,	  0,	  0,	  0,	  0,	  0};
	correctArray[7] = new int[correctCols]{0,	  0,	  0,	  0,	  0,	  0};

	m_testNum++;	
	printTestMessage(m_testNum, "MazeWalker finds exit in maze.txt in correct order using bfs");	

	isPassed = testWalker(testFileName, correctArray, correctRows, correctCols, searchChoice);
	
	printPassFail(isPassed);

	for(int i=0; i<correctRows; i++)
	{
		delete[] correctArray[i];
	}

	delete[] correctArray;


	return(isPassed);
}
Ejemplo n.º 13
0
bool Test_LinkedList::test7()
{
	LinkedList<int> list;
	int trackedSize = 0;
	bool isPassed=false;	

	printTestMessage("size returns correct value after adds and removeBack");

	//Mix of adding to the front, back and removing back
	for(int i = 0; i < TEST_SIZE; i++)
	{
		if(i%2 == 0)
		{
			list.addBack(i);
			trackedSize++;	
		}
		else if(i%3 == 0)
		{
			list.removeBack();
			trackedSize--;
		}
		else
		{
			list.addFront(i);
			trackedSize++;
		}
	}
	
	isPassed = list.size() == trackedSize;
	printPassFail(isPassed);
	return (isPassed); 
}
Ejemplo n.º 14
0
bool Test::test_sort26()
{
        bool isPassed = false;
        const int LOW = 0;
        const int HIGH = 5000;
        int* arr = Sorts<int>::createTestArray(TEST_SIZE, LOW, HIGH);
        double time = 0.0;

        m_testNum++;
        printTestMessage(m_testNum, "quickSortWithMedian sort completes in reasonable amount of time (0-2secs platform dependent) " + std::to_string(TEST_SIZE));

        time = Sorts<int>::sortTimer(Sorts<int>::quickSortWithMedian, arr, TEST_SIZE);

        std::cerr << "\nTime for quickSortWithMedian sort on " + std::to_string(TEST_SIZE) + " elements: " << time << " ";

        if(time<0 || time>2)
        {
                isPassed = false;
        }
        else
        {
                isPassed = true;
        }

        delete[] arr;
        arr = nullptr;

        printPassFail(isPassed);
        return (isPassed);
}
Ejemplo n.º 15
0
bool Test::test1()
{
	Stack<int> stack;
	bool isPassed = stack.size() == 0;

	printTestMessage(1, "size of empty stack is zero");
	printPassFail(isPassed);
	return (isPassed);
}
Ejemplo n.º 16
0
bool Test_LinkedList::test8()
{
	LinkedList<int> list;
	bool isPassed = false;
	
	printTestMessage("search returns false on empty list");

	isPassed = !list.search(42);
	printPassFail(isPassed);
	return (isPassed); 
}
Ejemplo n.º 17
0
bool Test_LinkedList::test14()
{
	LinkedList<int> list;
	bool isPassed = false;

	printTestMessage("removeBack returns false on empty list");

	isPassed = !list.removeBack();
	printPassFail(isPassed);
	return (isPassed); 
}
Ejemplo n.º 18
0
bool Test_LinkedList::test1()
{
	LinkedList<int> list;
	bool isPassed = false;

	printTestMessage("size of empty list is zero"); 

	isPassed = list.size() == 0;
	printPassFail(isPassed);

	return (list.size() == 0);
}
Ejemplo n.º 19
0
bool Test::test2()
{
	Stack<int> stack;
	bool isPassed=false;

	stack.push(1);

	printTestMessage(2, "size returns correct value after 1 push");
	isPassed = stack.size() == 1;
	printPassFail(isPassed);	
	return (isPassed);
}
Ejemplo n.º 20
0
bool Test::test4()
{
	bool isPassed = false;
	int arr[] = {31, 1, 0, 4, 1};

	printTestMessage(4, "isSorted returns false when array is not sorted");
	
	isPassed = Sorts<int>::isSorted(arr, 5) == false;
	
	printPassFail(isPassed);
	return (isPassed);
}
Ejemplo n.º 21
0
bool Test::test5()
{
	bool isPassed = false;
	int arr[] = {31,30,29,28,27};

	printTestMessage(5, "isSorted returns false when array is sorted, but not ascending");
	
	isPassed = Sorts<int>::isSorted(arr, 5) == false;
	
	printPassFail(isPassed);
	return (isPassed);
}
Ejemplo n.º 22
0
bool Test::test6()
{
	bool isPassed = false;
	int arr[] = {27,27,29,64,65};

	printTestMessage(6, "isSorted returns true when array is sorted in ascending order");
	
	isPassed = Sorts<int>::isSorted(arr, 5);
	
	printPassFail(isPassed);
	return (isPassed);
}
Ejemplo n.º 23
0
bool Test_LinkedList::test11()
{
	LinkedList<int> list;
	bool isPassed = false;
	std::vector<int> vec;

	printTestMessage("toVector returns empty vector for empty list");
	
	vec = list.toVector();
	isPassed = vec.empty();
	printPassFail(isPassed);
	return (isPassed); 
}
Ejemplo n.º 24
0
bool Test::test7()
{
	bool isPassed = false;
	int arr[] = {27};

	printTestMessage(7, "bubble sort called with an array of size 1");
	
	Sorts<int>::bubbleSort(arr, 1);
	isPassed = Sorts<int>::isSorted(arr,1);
	
	printPassFail(isPassed);
	return (isPassed);
}
Ejemplo n.º 25
0
bool Test::test3()
{
	bool isPassed = false;
	int* arr = nullptr;

	printTestMessage(3, "isSorted returns true when array size is 1");
	

	isPassed = Sorts<int>::isSorted(arr, 1);
	
	printPassFail(isPassed);
	return (isPassed);
}
Ejemplo n.º 26
0
bool Test::test3()
{
	Stack<int> stack;
	bool isPassed = false;
	printTestMessage(3, "size returns correct value after lots of pushes");
	
	loadValues(stack);
	
	isPassed = stack.size() == TEST_SIZE;

	printPassFail(isPassed);
	return(isPassed);	
}
Ejemplo n.º 27
0
bool Test::test10()
{
	bool isPassed = false;
	int arr[] = {27};

	printTestMessage(10, "bogo sort called with an array of size 1");
	
	Sorts<int>::insertionSort(arr, 1);
	isPassed = Sorts<int>::isSorted(arr,1);
	
	printPassFail(isPassed);
	return (isPassed);
}
Ejemplo n.º 28
0
bool Test::test_isEmpty01()
{
	bool isPassed = false;
	BSTI<int>* bst = new BinarySearchTree<int>();
	m_testNum++;

	printTestMessage(m_testNum, "isEmpty returns true on empty tree");

	isPassed = bst->isEmpty();	

	printPassFail(isPassed);
	delete bst;
	return(isPassed);
}
Ejemplo n.º 29
0
bool Test_LinkedList::test3()
{
	LinkedList<int> list;
	bool isPassed = false;

	printTestMessage("size returns correct value after 1 addBack");

	list.addBack(5);
	
	isPassed = list.size() == 1;
	
	printPassFail(isPassed);
	return(isPassed);	
}
Ejemplo n.º 30
0
bool Test::test_search01()
{
	bool isPassed = false;
	BSTI<int>* bst = new BinarySearchTree<int>();
	m_testNum++;

	printTestMessage(m_testNum, "searching an empty tree returns false");

	isPassed = !bst->search(42);	

	printPassFail(isPassed);
	delete bst;
	return(isPassed);
}