Ejemplo n.º 1
0
bool Test::test9()
{
        DoubleLinkedList<int> list;
        bool isPassed = false;

        std::cerr << "Test 9: find returns false when value not in list:  ";

        //add values from 0 to TEST_SIZE-1 inclusively
        for(int i=0; i<TEST_SIZE; i++)
        {
                list.pushFront(i);
        }

        //find value not in list
        if( list.find(-1) == nullptr )
        {
                isPassed = true;
                std::cerr << "PASSED" << std::endl;
        }
        else
        {
                std::cerr << "FAILED" << std::endl;
        }

        return (isPassed);
}
Ejemplo n.º 2
0
bool Test::test5()
{
        bool isPassed = false;
        DoubleLinkedList<int> list;

        std::cerr << "Test 5: size returns correct value after multiple pushBack:\n";


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

        if( (list.size() == TEST_SIZE))
        {
                isPassed = true;
                std::cerr << "PASSED" << std::endl;
        }
        else
        {
                isPassed = false;
                std::cerr << "FAILED" << std::endl;
        }

        return (isPassed);

}
Ejemplo n.º 3
0
bool Test::test25()
{
        DoubleLinkedList<int> list;
        bool isPassed = false;

        std::cerr << "Test 25: insertBehind on empty list throws exception: ";

        try
        {
                list.insertBehind(0, 1);
        }
        catch(std::exception& e)
        {
                isPassed = true;
        }

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

        return(isPassed);
}
Ejemplo n.º 4
0
void TestFirstAndLast(const DoubleLinkedList<int> & alist)
{
	DoubleLinkedList<int> blist;

	std::cout << "\n********** First and Last **********" << std::endl;

	// Full List
	std::cout << alist.First() << std::endl;
	std::cout << alist.Last() << std::endl;

	// Empty List
	try
	{
		std::cout << blist.First() << std::endl;
	}
	catch (char * msg)
	{
		std::cout << msg << std::endl;
	}
	try
	{
		std::cout << blist.Last() << std::endl;
	}
	catch (char * msg)
	{
		std::cout << msg << std::endl;
	}
}
Ejemplo n.º 5
0
void TestFirstAndLast(const DoubleLinkedList<int> & alist)
{
    DoubleLinkedList<int> blist;
    
    std::cout << "\n********** Testing: DoubleLinkedList First and Last **********" << std::endl;
    
    std::cout << "\n********** Testing: First and Last with Full List **********" << std::endl;
    std::cout << alist.First() << std::endl;
    std::cout << alist.Last() << std::endl;
    
    std::cout << "\n********** Testing: First with Empty List **********" << std::endl;
    try
    {
        std::cout << blist.First() << std::endl;
    }
    catch (const char * msg)
    {
        std::cout << msg << std::endl;
    }
    std::cout << "\n********** Testing: First with Empty List **********" << std::endl;
    try
    {
        std::cout << blist.Last() << std::endl;
    }
    catch (const char * msg)
    {
        std::cout << msg << std::endl;
    }
}
Ejemplo n.º 6
0
void TestPurge(DoubleLinkedList<int> alist)
{
    DoubleLinkedList<int> blist;
    
    std::cout << "\n********** Testing: DoubleLinkedList Purge with Full List **********" << std::endl;
    
    std::cout << "\n********** Testing: Before Purge Full List **********" << std::endl;
    std::cout << alist.getHead() << std::endl;
    std::cout << alist.getTail() << std::endl;
    
    alist.Purge();
    
    std::cout << "\n********** Testing: After Purge Full List **********" << std::endl;
    std::cout << alist.getHead() << std::endl;
    std::cout << alist.getTail() << std::endl;
    
    std::cout << "\n********** Testing: DoubleLinkedList Purge with Empty List **********" << std::endl;
    std::cout << "\n********** Testing: Before Purge Empty List **********" << std::endl;
    std::cout << alist.getHead() << std::endl;
    std::cout << alist.getTail() << std::endl;
    
    alist.Purge();
    
    std::cout << "\n********** Testing: After Purge Empty List **********" << std::endl;
    std::cout << alist.getHead() << std::endl;
    std::cout << alist.getTail() << std::endl;
}
Ejemplo n.º 7
0
void main()
{
	// Tests Canonical Functions
	DoubleLinkedList<int> alist;			//ctor
	DoubleLinkedList<int> blist(alist);		//copy ctor
	DoubleLinkedList<int> clist;

	clist = blist;							//= operator

	// Tests Mutators
	std::cout << alist.getHead() << std::endl;
	std::cout << alist.getTail() << std::endl;

	// Tests Methods
	
	TestAppend(alist);
	TestFirstAndLast(alist);
	TestPrepend(alist);
	TestPurge(alist);
	TestInsertBefore();
	TestInsertAfter();
	TestExtract();

	std::cout << "\n********** List Integrity **********" << std::endl;
	alist.PrintForwards();
	alist.PrintBackwards();

	//The memory leak tool detects leaks because alist isn't being destroyed before it is called
	//by calling the destructor or the following line of code, the list is destroyed and
	//there are no leaks:
	//alist.Purge();
	_CrtDumpMemoryLeaks();
}
Ejemplo n.º 8
0
DoubleLinkedList<GraphNode *> Dijkstra(GraphNode *Start, GraphNode * End)
{
	Map<GraphNode *, DijkstraHelperNode *> NodeHelperMap;
	DoubleLinkedList<DijkstraHelperNode *> Visited;
	MinHeap<DijkstraHelperNode *> OpenHeap;
	DijkstraHelperNode *NewHelper = new DijkstraHelperNode();
	NewHelper->m_Node = Start;
	NewHelper->m_Cost = 0;
	NodeHelperMap.Insert(Start, NewHelper);
	OpenHeap.Insert(NewHelper);
	while(!OpenHeap.IsEmpty())
	{
		DijkstraHelperNode *CurrentHelperNode = OpenHeap.PopTop();
		assert(CurrentHelperNode != NULL);
		GraphNode *CurrentGraphNode = CurrentHelperNode->m_Node;
		assert(CurrentGraphNode != NULL);
		DoubleLinkedList<GraphEdge *> *CurrendEdges = CurrentGraphNode->GetEdges();
		DoubleListNode<GraphEdge *> *CurrentEdge = CurrentEdges.GetHead();
		while(CurrentEdge != NULL)
		{
			GraphNode *OtherNode = CurrentEdge->m_End;
			if(OtherNode == CurrentGraphNode)
			{
				OtherNode = CurrentEdge->m_Start;
			}
			assert(OtherNode != CurrentGraphNode);
			DijkstraHelperNode *NodeHelper = NodeHelperMap.GetValue(OtherNode);
			if(NodeHelper == NULL)
			{
 				NodeHelper = new DijkstraHelperNode();
				NodeHelper->m_Node = OtherNode;
				NodeHelperMap.Insert(OtherNode, NodeHelper);		
				OpenHeap.Insert(NodeHelper);
			}
			int CostToNode = CurrentHelperNode->m_Cost + CurrentEdge->m_Cost;
			if(CostToNode < NodeHelper->m_Cost)
			{		
				NodeHelper->m_Cost = CostToNode;
				NodeHelper->m_Previous = CurrentGraphNode;
				OpenHeap.Update(NodeHelper);
			}
			if(OtherNode == End)
			{
				break;
			}
		}
	}

	DoubleLinkedList<GraphNode *> Path;
	DijkstraHelperNode *EndHelper = NodeHelperMap.GetValue(End);
	if(EndHelper != NULL)
	{
		DijkstraHelperNode *CurrentHelper = EndHelper;
		while(CurrentHelper != NULL)
		{
			Path.AddFront(CurrentHelper->m_Node);
			CurrentHelper = CurrentHelper->m_Previous;
		}
	}
}
Ejemplo n.º 9
0
bool Test::test11()
{
        DoubleLinkedList<int> 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);
}
Ejemplo n.º 10
0
void main()
{
	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

	// Tests Canonical Functions
	DoubleLinkedList<int> alist;			//ctor
	DoubleLinkedList<int> blist(alist);		//copy ctor
	DoubleLinkedList<int> clist;

	clist = blist;							//= operator

	// Tests Mutators
	std::cout << alist.getHead() << std::endl;
	std::cout << alist.getTail() << std::endl;

	// Tests Methods

	TestAppend(alist);
	TestFirstAndLast(alist);
	TestPrepend(alist);
	TestPurge(alist);
	TestInsertBefore();
	TestInsertAfter();
	TestExtract();

	std::cout << "\n********** List Integrity **********" << std::endl;
	alist.PrintForwards();
	alist.PrintBackwards();
}
Ejemplo n.º 11
0
bool Test::test27()
{
        DoubleLinkedList<int> list;
        bool isPassed = false;

        std::cerr << "Test 27: insertBehind of value not in list throws exception:\n";

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

        try
        {
                list.insertBehind(-1, 1);
        }
        catch(std::exception& e)
        {
                isPassed = true;
        }

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

        return(isPassed);
}
Ejemplo n.º 12
0
bool Test::test21()
{
        DoubleLinkedList<int> list;
        bool isPassed = false;

        std::cerr << "Test 21: remove(value) where value is not in large list returns false\n";

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

        if( !list.remove(-1) )
        {
                isPassed = true;
        }

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

        return(isPassed);
}
Ejemplo n.º 13
0
bool Test::test17()
{
        DoubleLinkedList<int> list;
        bool isPassed = false;
        int trackedSize = 0;

        std::cerr << "Test 17: size preserved by remove(value) on populated list: ";

        //Remove front on every 3rd iteration, add otherwise.
        for(int i=0; i<TEST_SIZE; i++)
        {
                list.pushBack(i);
                trackedSize++;

        }

        //remove every third value, track the size
        for(int i=0; i<TEST_SIZE; i+=3)
        {
                list.remove(i);
                trackedSize--;
        }

        if(trackedSize == list.size())
        {
                isPassed = true;
                std::cerr << "PASSED" << std::endl;
        }
        else
        {
                std::cerr << "FAILED" << std::endl;
        }

        return (isPassed);
}
Ejemplo n.º 14
0
void main()
{
	// Tests Canonical Functions
	DoubleLinkedList<int> alist;			//ctor
	DoubleLinkedList<int> blist(alist);		//copy ctor
	DoubleLinkedList<int> clist;

	clist = blist;							//= operator

	// Tests Mutators
	std::cout << alist.getHead() << std::endl;
	std::cout << alist.getTail() << std::endl;

	// Tests Methods
	
	TestAppend(alist);
	TestFirstAndLast(alist);
	TestPrepend(alist);
	TestPurge(alist);
	TestInsertBefore();
	TestInsertAfter();
	TestExtract();

	std::cout << "\n********** List Integrity **********" << std::endl;
	alist.PrintForwards();
	alist.PrintBackwards();
	_asm nop;//added as a break point location;
}
Ejemplo n.º 15
0
inline void print(DoubleLinkedList &dll1, DoubleLinkedList &dll2,
                  DoubleLinkedList &dll3, DoubleLinkedList &dll4)
{
    cout << "Size of the list1: " << dll1.getNumberElements() << endl;
    cout << "Size of the list2: " << dll2.getNumberElements() << endl;
    cout << "Size of the modList1: " << dll3.getNumberElements() << endl;
    cout << "Size of the modList2: " << dll4.getNumberElements() << endl << endl;
}
Ejemplo n.º 16
0
void TestAppend(DoubleLinkedList<int> & alist)
{
	std::cout << "\n********** Append **********" << std::endl;

	alist.Append(5);
	alist.Append(15);
	alist.Append(25);
	alist.Append(35);
}
Ejemplo n.º 17
0
void TestPurge(DoubleLinkedList<int> alist)
{
	std::cout << "\n********** Purge **********" << std::endl;

	std::cout << alist.getHead() << std::endl;
	std::cout << alist.getTail() << std::endl;

	alist.Purge();

	std::cout << alist.getHead() << std::endl;
	std::cout << alist.getTail() << std::endl;
}
Ejemplo n.º 18
0
void TestAppend(DoubleLinkedList<int> & alist)
{
    std::cout << "\n********** Testing: DoubleLinkedList Append **********" << std::endl;
    
    alist.Append(5);
    alist.Append(15);
    alist.Append(25);
    alist.Append(35);
    
    std::cout << alist.First() << std::endl;
    std::cout << alist.Last() << std::endl;
}
Ejemplo n.º 19
0
bool Test::test1()
{
        DoubleLinkedList<int> list;
        std::cerr << "Test 1: size of empty list is zero: ";

        if(list.size() == 0)
                std::cerr << "PASSED" << std::endl;
        else
                std::cerr << "FAILED" << std::endl;

        return (list.size() == 0);
}
Ejemplo n.º 20
0
bool Test::test19()
{
        DoubleLinkedList<int> list;
        bool isPassed = true;
        int num=0;


        std::cerr << "Test 19: 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.pushBack(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 << "PASSED" << std::endl;
        }
        else
        {
                std::cerr << "FAILED" << std::endl;
        }

        return (isPassed);
}
Ejemplo n.º 21
0
bool Test::test3()
{
        DoubleLinkedList<int> list;
        std::cerr << "Test 3: size returns correct value after 1 pushBack: ";

        list.pushBack(5);

        if(list.size() == 1)
                std::cerr << "PASSED" << std::endl;
        else
                std::cerr << "FAILED" << std::endl;

        return(list.size() == 1);
}
Ejemplo n.º 22
0
bool Test::test10()
{
        DoubleLinkedList<int> list;
        bool isPassed = true;

        std::cerr << "Test 10: find returns point to node when value is in large 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.  ";

                //Add every other node using pushBack
                if( i%2 == 0)
                {
                        list.pushBack(i);
                }
                else
                {
                        list.pushFront(i);
                }

                std::cerr.flush();
        }

        //find for all added values. Set flag if any value is not found
        for(int i=0; i<TEST_SIZE; i++)
        {
                std::cerr << '\r' << "\tfinding " << (i+1) << "/" << TEST_SIZE << " nodes.  ";
                Node<int>* temp = list.find(i);

                if( temp == nullptr || temp->getValue() != i)
                {
                        isPassed = false;
                }
                std::cerr.flush();
        }

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

        return (isPassed);
}
Ejemplo n.º 23
0
bool Test::test2()
{
        DoubleLinkedList<int> list;

        list.pushFront(1);

        std::cerr << "Test 2: size returns correct value after 1 add : ";

        if(list.size() == 1)
                std::cerr << "PASSED" << std::endl;
        else
                std::cerr << "FAILED" << std::endl;

        return (list.size() == 1);
}
Ejemplo n.º 24
0
 void set(int key, int value) {
     // key found, update the data, and take to the front 
     if (cacheMap.find(key) != cacheMap.end() ){
         Node *p = cacheMap[key];
         p->value = value;
         cacheList.TakeToBegin(cacheMap[key]);
     }else{
         // key not found, new a node to store data
         cacheMap[key] = cacheList.NewAtBegin(key, value);
         // if the capacity exceed, remove the last one.
         if( cacheList.Size() > capacity) {
             int key = cacheList.GetTailNode()->key; 
             cacheMap.erase(key);
             cacheList.DeleteLast();
         }
     }
 }
Ejemplo n.º 25
0
void UIAssetController::onButtonClick(BasicButton* pSrc)
{
    // stop-command for units
    if(pSrc == m_pStopButton) {
        DoubleLinkedList<Unit*>* pUnitList = m_pUnitCommandDispatcher->getUnits();
        ListNode<Unit*>* node = pUnitList->headNode();
        while(node) {
            if(node->item->getMovingLogic())
                node->item->getMovingLogic()->clearTargets();
            node = node->next;
        }
        return;
    }

    if (pSrc->getId() == 666)
    {
        // launch nuke
        m_pSelector->setState(Selector::SELECTOR_NUKE);
        changeState(STATE_NUKE);
        return;
    }

    // build button
    // check for insufficient funds
    /*
    AssetDef* def = (AssetDef*)pSrc->getRandomObject();
    if(!def)
        def = DefManager::getInstance()->getAssetDef(pSrc->getId());
    */
    AssetDef* def = DefManager::getInstance()->getAssetDef(pSrc->getId());
    if(m_pCurrentPlayer->getOre() < def->constructionCostOre) {
        Console::error("Cannot build: not enough ore");
        return;
    }
    if(def->constructionRequires && !m_pCurrentPlayer->hasAsset(def->constructionRequires)) {
        Console::error("Cannot build: requirements not met");
        return;
    }
    // take the ore first to prefent the situation where there is no funds when placed
    m_CurrentBuildOreTaken = def->constructionCostOre;
    m_pCurrentPlayer->modifyOre(-m_CurrentBuildOreTaken);
    // start building
    m_pCurrentBuildAssetDef = def;
    m_pCurrentBuildButton = pSrc;
    changeState(STATE_BUILDING_PLACEMENT);
}
Ejemplo n.º 26
0
bool Test::test14()
{
        DoubleLinkedList<int> list;
        bool isPassed = false;

        std::cerr << "Test 14: removeBack returns false on empty list:  ";

        if( !list.removeBack() )
                isPassed = true;

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

        return (isPassed);
}
Ejemplo n.º 27
0
int main()
{
    // Please put in every lab and assignment this term for ease of testing for memory leaks
    //_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
    
    cout << "\n********** Testing: DoubleLinkedList default ctor  **********\n";
    DoubleLinkedList<int> alist;			//ctor
    cout << "\n********** Testing: DoubleLinkedList copy ctor with empty list **********\n";
    DoubleLinkedList<int> blist(alist);		//copy ctor
    DoubleLinkedList<int> clist;
    
    cout << "\n********** Testing: DoubleLinkedList op = **********\n";
    clist = blist;							//= operator
    
    // Note that these functions are only available for testing.
    // Will be removed from the list before going into production.
    cout << "\n********** Testing: DoubleLinkedList getters  **********\n";
    std::cout << alist.getHead() << std::endl;
    std::cout << alist.getTail() << std::endl;
    
    //// Tests Methods
    TestAppend(alist);
    TestFirstAndLast(alist);
    TestPrepend(alist);
    
    std::cout << "\n********** Testing: DoubleLinkedList Copy ctor with Full List **********" << std::endl;
    TestPurge(alist);
    TestInsertBefore();
    TestInsertAfter();
    TestExtract();
    
    std::cout << "\n********** Testing: List Integrity **********" << std::endl;
    
    // Note these are also only for testing
    alist.PrintForwards();
    alist.PrintBackwards();
    
    // OK NOW MAKE SURE IT WORKS THE SAME FOR COMPLEX DATA TYPES
    // USE THE string CLASS
    
    // Please put in every lab and assignment this term for ease of testing for memory leaks
    system("pause");
    
    return 0;
}
Ejemplo n.º 28
0
        int get(int key) {
            // The accessed node must be up-to-time -- take to the front 
            if (cacheMap.find(key) != cacheMap.end() ){
                cacheList.TakeToBegin(cacheMap[key]);
                return cacheMap[key]->value;
            }
            return -1;

        }
Ejemplo n.º 29
0
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);
}
Ejemplo n.º 30
0
bool Test::test12()
{
        DoubleLinkedList<int> 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.pushBack(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 << "PASSED" << std::endl;
        }
        else
        {
                std::cerr << "FAILED" << std::endl;
        }

        return (isPassed);
}