utestList.cpp 2.41 KB
Newer Older
1
// See LICENCE file at project root
2 3
#include "FUTester.hpp"

BRAMAS Berenger's avatar
BRAMAS Berenger committed
4
#include "Containers/FList.hpp"
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

// compile by g++ utestList.cpp -o utestList.exe

/**
* This file is a unit test for the FList class
*/

/**
* This class is simply used to count alloc dealloc
*/
class TestObject{
public:
	static int counter;
	static int dealloced;

	TestObject(){
		++counter;
	}
	TestObject(const TestObject&){
		++counter;
	}
	~TestObject(){
		++dealloced;
	}
};

int TestObject::counter(0);
int TestObject::dealloced(0);


/** this class test the list container */
class TestList : public FUTester<TestList> {
	// Called before each test : simply set counter to 0
	void PreTest(){
		TestObject::counter = 0;
		TestObject::dealloced = 0;
	}

	// test size
	void TestSize(){
		FList<TestObject> list;
46 47 48
		list.push(TestObject());
		list.push(TestObject());
		list.push(TestObject());
49
                uassert(list.getSize() == 3);
50
		
51
                uassert((TestObject::counter - TestObject::dealloced) == list.getSize());
52 53

		list.clear();
54
                uassert(list.getSize() == 0);
55

56
                uassert(TestObject::counter == TestObject::dealloced);
57 58 59 60 61
	}
	
	// test copy
	void TestCopy(){
		FList<TestObject> list;
62 63 64
		list.push(TestObject());
		list.push(TestObject());
		list.push(TestObject());
65 66

		FList<TestObject> list2 = list;
67
                uassert(list.getSize() == list2.getSize());
68
		
69
                uassert((TestObject::counter - TestObject::dealloced) == (list.getSize() + list2.getSize()));
70 71 72 73 74 75
	}

	// test iter
	void TestIter(){		
		FList<TestObject> list;
		{
76
                        FList<TestObject>::ConstBasicIterator iter(list);
77
                        uassert(!iter.hasNotFinished());
78 79
		}
		{
80 81 82
			list.push(TestObject());
			list.push(TestObject());
			list.push(TestObject());
83

84
                        FList<TestObject>::ConstBasicIterator iter(list);
85
                        uassert(iter.hasNotFinished());
86 87

			int counter = 0;
88 89 90 91 92
                        while(iter.hasNotFinished()){
                            iter.gotoNext();
                            ++counter;
                        }

93 94
                        uassert(!iter.hasNotFinished());
                        uassert(counter == list.getSize());
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
		}
	}
		
	// set test
	void SetTests(){
            AddTest(&TestList::TestSize,"Test Size");
            AddTest(&TestList::TestCopy,"Test Copy");
            AddTest(&TestList::TestIter,"Test Iter");
	}
};

// You must do this
TestClass(TestList)