utestVector.cpp 4.8 KB
Newer Older
1
// ===================================================================================
2 3 4 5
// Copyright ScalFmm 2016 INRIA, Olivier Coulaud, Bérenger Bramas,
// Matthias Messner olivier.coulaud@inria.fr, berenger.bramas@inria.fr
// This software is a computer program whose purpose is to compute the
// FMM.
6
//
7
// This software is governed by the CeCILL-C and LGPL licenses and
8
// abiding by the rules of distribution of free software.
9 10 11
// An extension to the license is given to allow static linking of scalfmm
// inside a proprietary application (no matter its license).
// See the main license file for more details.
12
//
13 14 15
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 17 18
// GNU General Public and CeCILL-C Licenses for more details.
// "http://www.cecill.info".
// "http://www.gnu.org/licenses".
19
// ===================================================================================
20 21
#include "FUTester.hpp"

BRAMAS Berenger's avatar
BRAMAS Berenger committed
22
#include "Containers/FVector.hpp"
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62


/**
* 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;
63 64 65
                vector.push(TestObject());
                vector.push(TestObject());
                vector.push(TestObject());
66
                uassert(vector.getSize() == 3);
67
		
68
                uassert((TestObject::counter - TestObject::dealloced) == vector.getSize());
69 70

                vector.clear();
71
                uassert(vector.getSize() == 0);
72

73
                uassert(TestObject::counter == TestObject::dealloced);
74 75 76 77 78
	}
	
	// test copy
	void TestCopy(){
                FVector<TestObject> vector;
79 80 81
                vector.push(TestObject());
                vector.push(TestObject());
                vector.push(TestObject());
82 83 84

                {
                    FVector<TestObject> vector2(vector);
85 86
                    uassert(vector.getSize() == vector2.getSize());
                    uassert((TestObject::counter - TestObject::dealloced) == (vector.getSize() + vector2.getSize()));
87 88 89 90
                }
                {
                    FVector<TestObject> vector2(vector.getSize()/2);
                    vector2 = vector;
91 92
                    uassert(vector.getSize() == vector2.getSize());
                    uassert((TestObject::counter - TestObject::dealloced) == (vector.getSize() + vector2.getSize()));
93 94 95 96 97 98 99
                }
	}

	// test iter
	void TestIter(){		
                FVector<TestObject> vector;
		{
100
                        FVector<TestObject>::ConstBasicIterator iter(vector);
101
                        uassert(!iter.hasNotFinished());
102 103
		}
		{
104 105 106
                        vector.push(TestObject());
                        vector.push(TestObject());
                        vector.push(TestObject());
107

108
                        FVector<TestObject>::ConstBasicIterator iter(vector);
109
                        uassert(iter.hasNotFinished());
110 111

			int counter = 0;
berenger-bramas's avatar
berenger-bramas committed
112
			while(iter.hasNotFinished()){ iter.gotoNext(); ++counter; }
113 114
                        uassert(!iter.hasNotFinished());
                        uassert(counter == vector.getSize());
115 116
		}
	}
117 118 119 120 121 122

        // test remove
        void TestRemove(){
                FVector<TestObject> vector;
                {
                        FVector<TestObject>::BasicIterator iter(vector);
123
                        uassert(!iter.hasNotFinished());
124 125 126 127 128 129 130
                }
                {
                        vector.push(TestObject());
                        vector.push(TestObject());
                        vector.push(TestObject());

                        FVector<TestObject>::BasicIterator iter(vector);
131
                        uassert(iter.hasNotFinished());
132 133 134

                        iter.gotoNext();
                        iter.remove();
135
                        uassert(iter.hasNotFinished());
136 137

                        iter.gotoNext();
138
                        uassert(!iter.hasNotFinished());
139

140
                        uassert(2 == vector.getSize());
141 142
                }
        }
143 144 145 146 147 148
		
	// set test
	void SetTests(){
            AddTest(&TestVector::TestSize,"Test Size");
            AddTest(&TestVector::TestCopy,"Test Copy");
            AddTest(&TestVector::TestIter,"Test Iter");
149
            AddTest(&TestVector::TestRemove,"Test Remove");
150 151 152 153 154 155 156
	}
};

// You must do this
TestClass(TestVector)