utestList.cpp 3.38 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/FList.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 63

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

		list.clear();
72
                uassert(list.getSize() == 0);
73

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

		FList<TestObject> list2 = list;
85
                uassert(list.getSize() == list2.getSize());
86
		
87
                uassert((TestObject::counter - TestObject::dealloced) == (list.getSize() + list2.getSize()));
88 89 90 91 92 93
	}

	// test iter
	void TestIter(){		
		FList<TestObject> list;
		{
94
                        FList<TestObject>::ConstBasicIterator iter(list);
95
                        uassert(!iter.hasNotFinished());
96 97
		}
		{
98 99 100
			list.push(TestObject());
			list.push(TestObject());
			list.push(TestObject());
101

102
                        FList<TestObject>::ConstBasicIterator iter(list);
103
                        uassert(iter.hasNotFinished());
104 105

			int counter = 0;
106 107 108 109 110
                        while(iter.hasNotFinished()){
                            iter.gotoNext();
                            ++counter;
                        }

111 112
                        uassert(!iter.hasNotFinished());
                        uassert(counter == list.getSize());
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
		}
	}
		
	// 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)