utestVector.cpp 3.83 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/FVector.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


/**
* This file is a unit test for the FVector 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 vector container */
class TestVector : public FUTester<TestVector> {
	// Called before each test : simply set counter to 0
	void PreTest(){
		TestObject::counter = 0;
		TestObject::dealloced = 0;
	}

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

                vector.clear();
53
                uassert(vector.getSize() == 0);
54

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

                {
                    FVector<TestObject> vector2(vector);
67 68
                    uassert(vector.getSize() == vector2.getSize());
                    uassert((TestObject::counter - TestObject::dealloced) == (vector.getSize() + vector2.getSize()));
69 70 71 72
                }
                {
                    FVector<TestObject> vector2(vector.getSize()/2);
                    vector2 = vector;
73 74
                    uassert(vector.getSize() == vector2.getSize());
                    uassert((TestObject::counter - TestObject::dealloced) == (vector.getSize() + vector2.getSize()));
75 76 77 78 79 80 81
                }
	}

	// test iter
	void TestIter(){		
                FVector<TestObject> vector;
		{
82
                        FVector<TestObject>::ConstBasicIterator iter(vector);
83
                        uassert(!iter.hasNotFinished());
84 85
		}
		{
86 87 88
                        vector.push(TestObject());
                        vector.push(TestObject());
                        vector.push(TestObject());
89

90
                        FVector<TestObject>::ConstBasicIterator iter(vector);
91
                        uassert(iter.hasNotFinished());
92 93

			int counter = 0;
94
			while(iter.hasNotFinished()){ iter.gotoNext(); ++counter; }
95 96
                        uassert(!iter.hasNotFinished());
                        uassert(counter == vector.getSize());
97 98
		}
	}
99 100 101 102 103 104

        // test remove
        void TestRemove(){
                FVector<TestObject> vector;
                {
                        FVector<TestObject>::BasicIterator iter(vector);
105
                        uassert(!iter.hasNotFinished());
106 107 108 109 110 111 112
                }
                {
                        vector.push(TestObject());
                        vector.push(TestObject());
                        vector.push(TestObject());

                        FVector<TestObject>::BasicIterator iter(vector);
113
                        uassert(iter.hasNotFinished());
114 115 116

                        iter.gotoNext();
                        iter.remove();
117
                        uassert(iter.hasNotFinished());
118 119

                        iter.gotoNext();
120
                        uassert(!iter.hasNotFinished());
121

122
                        uassert(2 == vector.getSize());
123 124
                }
        }
125 126 127 128 129 130
		
	// set test
	void SetTests(){
            AddTest(&TestVector::TestSize,"Test Size");
            AddTest(&TestVector::TestCopy,"Test Copy");
            AddTest(&TestVector::TestIter,"Test Iter");
131
            AddTest(&TestVector::TestRemove,"Test Remove");
132 133 134 135 136 137 138
	}
};

// You must do this
TestClass(TestVector)