Beispiel #1
0
bool Test::test5()
{
	Stack<int> s;
	bool printResult = true;	


	std::cout << "Test 5: printStack on populated stack : ";
	
	s.push(3);	
	s.push(2);
	
	redirectOS();
	ss_redirect.clear();
	s.printStack();
	restoreOS();
	
	for(int i = 2; i <= 3; ++i){
		int num;
		ss_redirect >> num;
		if( num != i)
			printResult = false;
	}
	
	//Check to make sure a print leaves the stack in a valid state.
	//Or in other words, can we still use the stack after printing?
	s.push(1);
	s.push(0);

	redirectOS();
	ss_redirect.clear();
	s.printStack();
	restoreOS();

	for(int i = 0; i <= 3; ++i){
		int num;
		ss_redirect >> num;
		if( num != i)
			printResult = false;
	}


	if(printResult)
		std::cout << "PASS" << std::endl;
	else
		std::cout << "FAIL" << std::endl;

	return printResult;
}
Beispiel #2
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);
}
Beispiel #3
0
bool Test::test11()
{
	LinkedList list;
	bool isPassed = false;

	redirectOS();
	ss_redirect.clear();
	list.printList();
	restoreOS();

	std::cerr << "Test 10: printList prints empty string for empty list: ";

	//Convert captured output to string and check length
	if(ss_redirect.str().length() == 0)
	{
		isPassed = true;
		std::cerr << "PASSED" << std::endl;
	}
	else
	{
		std::cerr << "FAILED" << std::endl;
	}

	return(isPassed);
}
Beispiel #4
0
bool Test::test18()
{
	LinkedList list;
	bool isPassed = true;
	int num=0;


	std::cerr << "Test 18: order preserved by removeBack on populated list\n ";

	//Add values 0-(TEST_SIZE-1)
	for(int i=0; i<TEST_SIZE; i++)
	{
		std::cerr << '\r' << "\tAdding " << (i+1) << "/" << TEST_SIZE << " nodes ";
		list.addBack(i);
		std::cerr.flush();
	}

	//Remove half of the node
	for(int i=0; i<TEST_SIZE/2; i++)
	{
		list.removeBack();
	}


	redirectOS();
	ss_redirect.clear();
	list.printList();
	restoreOS();

	//Check the order of the printed list
	for(int i=0; i<TEST_SIZE/2; i++)
	{
		ss_redirect >> num;

		if( i!=num )
		{
			isPassed = false;
		}
	}

	if(isPassed)
	{
		std::cerr << "PASS" << std::endl;
	}
	else
	{
		std::cerr << "FAIL" << std::endl;
	}

	return (isPassed);
}
Beispiel #5
0
bool Test::test4()
{
	Stack<int> s;

	std::cout << "Test 4: printStack an empty list : ";

	// this test only tries to trigger a seg fault

    	redirectOS();
	ss_redirect.clear();
	s.printStack();
	restoreOS();

	std::cout << "PASS" << std::endl;

	return true;
}
Beispiel #6
0
bool Test::test12()
{
	LinkedList list;
	bool isPassed = true;
	int num=0;


	std::cerr << "Test 12: printList prints the contents of large list correctly \n";

	//Add values 0-(TEST_SIZE-1)
	for(int i=0; i<TEST_SIZE; i++)
	{
		std::cerr << '\r' << "\tAdding " << (i+1) << "/" << TEST_SIZE << " nodes ";
		list.addBack(i);
		std::cerr.flush();
	}

	redirectOS();
	ss_redirect.clear();
	list.printList();
	restoreOS();

	//Check the order of the numbers produced by the printList call
	for(int i=0; i<TEST_SIZE; i++)
	{
		ss_redirect >> num;

		if( i!=num )
		{
			isPassed = false;
		}
	}

	if(isPassed)
	{
		std::cerr << "PASS" << std::endl;
	}
	else
	{
		std::cerr << "FAIL" << std::endl;
	}

	return (isPassed);
}
bool Test::test28()
{
        DoubleLinkedList<int> list;
        bool isPassed = false;
        int num1=0, num2=0;

        std::cerr << "Test 28: insertAhead of list size 1 retains list order:";
        list.pushBack(1);


        try
        {
                list.insertAhead(1,0);

                redirectOS();
                ss_redirect.clear();
                list.printList();
                restoreOS();

                ss_redirect >> num1;
                ss_redirect >> num2;

                if(num1 == 0 && num2 == 1)
                {
                        isPassed = true;
                }
        }
        catch(std::exception& e)
        {
                std::cerr << "\nException thrown! ";
                isPassed = false;
        }

        if(isPassed)
        {
                std::cerr << "PASSED" << std::endl;
        }
        else
        {
                std::cerr << "FAILED" << std::endl;
        }

        return(isPassed);
}
Beispiel #8
0
bool Test::test10()
{
	Stack<int> stack;
	bool isPassed = false;
	printTestMessage(10, "print on empty list print empty string");

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

	if(ss_redirect.str().length() == 0)
	{
		isPassed = true;
	}

	printPassFail(isPassed);	
	return(isPassed);
}
Beispiel #9
0
bool Test::test12()
{
	Stack<int> stack;
	int num = 0;
	bool isPassed = true;
	printTestMessage(12, "print after lots of pushes, pops, and peeks retains order");

	loadValues(stack);
	
	std::cerr << std::endl;	
	//Remove half the stack
	for(int i=0; i<TEST_SIZE/2; i++)
	{
		
		//call peek just to ensure peek does not alter order
		std::cerr << '\r' << "\tpeeking at and popping " << (i+1) << "/" << TEST_SIZE/2 << " nodes.  "; 
		stack.peek();
		stack.pop();
		std::cerr.flush();
	}

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

	//sizes will be printed in reverse, since you know, stacks.
	for(int i=(TEST_SIZE/2)-1; i>=0; i--)
	{
		ss_redirect >> num;
		
		if( i!=num )
		{
			isPassed = false;
		}
	}
	
	printPassFail(isPassed);	
	return(isPassed);
}
bool Test::test23()
{
        DoubleLinkedList<int> list;
        bool isPassed = true;
        int num = 0;

        std::cerr << "Test 23: order preserved by remove(value) on populated list\n";

        for(int i=0; i<TEST_SIZE; i++)
        {
                std::cerr << '\r' << "\tAdding " << (i+1) << "/" << TEST_SIZE << " nodes. ";
                list.pushBack(i);
                std::cerr.flush();
        }

        std::cerr << std::endl;

        for(int i=0; i<TEST_SIZE; i++)
        {
                std::cerr << '\r' << "\tRemoving " << (i+1)/2 << "/" << TEST_SIZE/2 << " nodes. ";
                if(i%2 == 0)
                {
                        list.remove(i);
                }
                std::cerr.flush();
        }
        std::cerr << std::endl;

        redirectOS();
        ss_redirect.clear();
        list.printList();
        restoreOS();

        int count=1;
        //Check the order of the printed list
        for(int i=1; i<TEST_SIZE; i+=2)
        {
                ss_redirect >> num;

                if( i!=num )
                {
                        isPassed = false;
                }

                std::cerr << '\r' << "\tChecking " << count << "/" << TEST_SIZE/2 << " nodes. ";
                std::cerr.flush();
                count++;
        }

        if(isPassed)
        {
                std::cerr << "PASSED" << std::endl;
        }
        else
        {
                std::cerr << "FAILED" << std::endl;
        }

        return(isPassed);

}
bool Test::test31()
{
        DoubleLinkedList<int> list;
        bool isPassed = true;
        bool isExceptionThrown = false;
        int num=0;
        int count=0;

        std::cerr << "Test 31: insertBehind on large list retains list order \n";

        count=1;//keep track of how many nodes are added
        //Add even numbers from 0 to TEST_SIZE to list
        for(int i=0; i<TEST_SIZE; i+=2)
        {
                std::cerr << '\r' << "\tAdding " << count << "/" << TEST_SIZE/2 << " nodes.";
                list.pushBack(i);
                std::cerr.flush();
                count++;
        }
        std::cerr << std::endl;

        count=1;//keep track of how many nodes are added
        //insert odd numbers from 1 to TEST_SIZE to list
        for(int i=1; i<TEST_SIZE; i+=2)
        {
                std::cerr << '\r' << "\tInserting " << count << "/" << TEST_SIZE/2 << " nodes.";
                try
                {
                        list.insertBehind(i-1, i);
                }
                catch(std::exception& e)
                {
                        isExceptionThrown = true;
                }
                std::cerr.flush();
                count++;
        }
        std::cerr << std::endl;

        if(isExceptionThrown)
        {
                std::cerr << "Exception was thrown during insertion!\n";
        }

        redirectOS();
        ss_redirect.clear();
        list.printList();
        restoreOS();

        //Check the order of the numbers produced by the printList call
        for(int i=0; i<TEST_SIZE; i++)
        {
                ss_redirect >> num;

                if( i!=num )
                {
                        isPassed = false;
                }

                std::cerr << '\r' << "\tChecking " << (i+1) << "/" << TEST_SIZE << " nodes. ";
                std::cerr.flush();
        }

        isPassed = isPassed && !isExceptionThrown;

        if(isPassed)
        {
                std::cerr << "PASSED" << std::endl;
        }
        else
        {
                std::cerr << "FAILED" << std::endl;
        }

        return (isPassed);
}