utestVector.cpp 2.77 KB
Newer Older
1 2 3 4 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
#include "FUTester.hpp"

#include "../Src/Containers/FVector.hpp"


/**
* 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;
44 45 46
                vector.push(TestObject());
                vector.push(TestObject());
                vector.push(TestObject());
47 48 49 50 51 52 53 54 55 56 57 58 59
                assert(vector.getSize() == 3);
		
                assert((TestObject::counter - TestObject::dealloced) == vector.getSize());

                vector.clear();
                assert(vector.getSize() == 0);

		assert(TestObject::counter == TestObject::dealloced);
	}
	
	// test copy
	void TestCopy(){
                FVector<TestObject> vector;
60 61 62
                vector.push(TestObject());
                vector.push(TestObject());
                vector.push(TestObject());
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

                {
                    FVector<TestObject> vector2(vector);
                    assert(vector.getSize() == vector2.getSize());
                    assert((TestObject::counter - TestObject::dealloced) == (vector.getSize() + vector2.getSize()));
                }
                {
                    FVector<TestObject> vector2(vector.getSize()/2);
                    vector2 = vector;
                    assert(vector.getSize() == vector2.getSize());
                    assert((TestObject::counter - TestObject::dealloced) == (vector.getSize() + vector2.getSize()));
                }
	}

	// test iter
	void TestIter(){		
                FVector<TestObject> vector;
		{
                        FVector<TestObject>::BasicIterator iter(vector);
			assert(!iter.isValide());
		}
		{
85 86 87
                        vector.push(TestObject());
                        vector.push(TestObject());
                        vector.push(TestObject());
88 89 90 91 92

                        FVector<TestObject>::BasicIterator iter(vector);
			assert(iter.isValide());

			int counter = 0;
93
			while(iter.isValide()){ iter.gotoNext(); ++counter; }
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
			assert(!iter.isValide());
                        assert(counter == vector.getSize());
		}
	}
		
	// set test
	void SetTests(){
            AddTest(&TestVector::TestSize,"Test Size");
            AddTest(&TestVector::TestCopy,"Test Copy");
            AddTest(&TestVector::TestIter,"Test Iter");
	}
};

// You must do this
TestClass(TestVector)