FUTester.hpp 5.67 KB
Newer Older
1
// ===================================================================================
COULAUD Olivier's avatar
COULAUD Olivier committed
2
// Copyright ScalFmm 2011 INRIA, Olivier Coulaud, Berenger Bramas, Matthias Messner
3 4 5 6 7 8 9 10 11 12 13 14
// olivier.coulaud@inria.fr, berenger.bramas@inria.fr
// This software is a computer program whose purpose is to compute the FMM.
//
// This software is governed by the CeCILL-C and LGPL licenses and
// abiding by the rules of distribution of free software.  
// 
// 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
// GNU General Public and CeCILL-C Licenses for more details.
// "http://www.cecill.info". 
// "http://www.gnu.org/licenses".
15
// ===================================================================================
16 17
#ifndef UTESTER_HPP
#define UTESTER_HPP
18

19 20 21 22

#include <iostream>
#include <list>
#include <string>
23
#include <cstdio>
24 25 26 27 28


#define TestClass(X)\
int main(void){\
	X Controller;\
29
        return Controller.Run();\
30 31 32 33 34 35 36 37 38 39 40
}\


/**
* @author Berenger Bramas (berenger.bramas@inria.fr)
* Please read the license
*
* This class is used to make simple unit test cases
*
* Please refer to testUTest.cpp to see an example
* @warning Create a derived class that implement SetTests() and use TestClass() macro
BRAMAS Berenger's avatar
BRAMAS Berenger committed
41
*
COULAUD Olivier's avatar
COULAUD Olivier committed
42
* We recommend to have a look to a unit test to better understand how it works,
BRAMAS Berenger's avatar
BRAMAS Berenger committed
43
* as for example @example TestList
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
*/
template <class TestClass>
class FUTester{
	// Test function pointer
	typedef void (TestClass::*TestFunc)(void);
	
	/** Test descriptor */
	struct TestFuncDescriptor{
		TestFunc func;		//< Test adress
		std::string name;	//< Test name
	};
	

	std::list<TestFuncDescriptor> tests;	//< all tests
	
	int totalTests;				//< number of tests
	
	int currentTest;			//< current processing test in the run
	int currentStep;			//< current processing step in the run

PIACIBELLO Cyrille's avatar
Typos  
PIACIBELLO Cyrille committed
64 65
	int failedSteps;			//< number of failed step in the current test
	int failedTests;			//< number of failed tests
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	
protected:
	/** Constructor */
	FUTester(){
		totalTests = 0;
	}

	/** Callback before processing test */
	virtual void Before(){}
	
	/** Callback after processing test */
	virtual void After(){}
	
	/** Callback before each unit test */
	virtual void PreTest(){}
	
	/** Callback after each unit test */
	virtual void PostTest(){}
	
	/**
	* This function has to add tests
        * <code> AddTest(&MyTest::TestOne); </code>
	*/
        virtual void SetTests() = 0;

	/**
	* Add a test without giving a name
	* @param inFunc test function address
	*/
        void AddTest(TestFunc inFunc){
		char buff[256];
COULAUD Olivier's avatar
COULAUD Olivier committed
97
		sprintf(buff,"Unnamed Test number %d",totalTests+1);
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
		AddTest(inFunc,buff);
	}
	
	/**
	* Add a test with a name
	* @param inFunc test function address
	* @param inFuncName function name
	*/
        void AddTest(TestFunc inFunc, const std::string& inFuncName){
		++totalTests;
		TestFuncDescriptor desc;
		desc.func = inFunc;
                desc.name = inFuncName;
		tests.push_back(desc);
	}
	
	/**
	* To print a message manually in the test
	* @param value a object that ostream can work on
	*/
        template <class Output>
	void Print(const Output& value){
		std::cout<< "--- Output from program : " << value << "\n";
	}
COULAUD Olivier's avatar
COULAUD Olivier committed
122 123

		/**
124 125
	* To test
	* @param result the test result
COULAUD Olivier's avatar
COULAUD Olivier committed
126
	* if result is false test failed
127
	*/
128
        void uassert(const bool result){
129 130
		++currentStep;
		if(!result){
PIACIBELLO Cyrille's avatar
Typos  
PIACIBELLO Cyrille committed
131 132
			std::cout << ">> Step " << currentStep << " Failed\n";
			++failedSteps;
133 134 135 136 137 138 139
		}
	}
	
	/**
	* To test equality
	* @param v1 value one
	* @param v2 value 2
PIACIBELLO Cyrille's avatar
Typos  
PIACIBELLO Cyrille committed
140
	* if v1 is not equal v2 test failed
141 142 143
	*/
	template <class T>
        void equal(const T& v1, const T& v2){
144
                uassert(v1 == v2);
145 146 147 148 149 150
	}
	
	/**
	* To test equality
	* @param v1 value one
	* @param v2 value 2
PIACIBELLO Cyrille's avatar
Typos  
PIACIBELLO Cyrille committed
151
	* if v1 is equal v2 test failed
152 153 154
	*/
	template <class T>
        void different(const T& v1, const T& v2){
155
                uassert(v1 != v2);
156 157 158 159 160
	}
	
public :
	/**
	* Processing the test
161
        * return application exit code (= nb of errors)
162
	*/
163
        int Run(){
164 165 166 167 168 169
		tests.clear();
		// register tests
		SetTests();
	
		TestClass* const toTest = static_cast<TestClass*>(this);
		currentTest = 0;
PIACIBELLO Cyrille's avatar
Typos  
PIACIBELLO Cyrille committed
170
		failedTests = 0;
171 172 173 174 175 176 177

		Before();
		
		// for each tests
                const typename std::list<TestFuncDescriptor>::const_iterator end = tests.end();
                for(typename std::list<TestFuncDescriptor>::iterator iter = tests.begin() ; iter != end ; ++iter){
			currentStep = 0;
PIACIBELLO Cyrille's avatar
Typos  
PIACIBELLO Cyrille committed
178
			failedSteps = 0;
179 180 181 182 183 184 185 186
			
			std::cout << "[Start] " << (*iter).name << "\n";
			
			PreTest();
			TestFunc ff = (*iter).func;
			(toTest->*ff)();
			PostTest();
			
PIACIBELLO Cyrille's avatar
Typos  
PIACIBELLO Cyrille committed
187 188 189
			if(failedSteps){
				std::cout << "[Finished] FAILED (" << failedSteps << "/" << currentStep<< " steps failed)\n";
				++failedTests;
190 191 192 193 194 195 196 197 198 199 200
			}
                        else{
				std::cout << "[Finished] PASSED (" << currentStep << " steps)\n";
			}
			
			++currentTest;
		}
		
		
		After();

PIACIBELLO Cyrille's avatar
Typos  
PIACIBELLO Cyrille committed
201
		std::cout <<"Test is over, " << (totalTests-failedTests) << " Passed, " << failedTests << " Failed\n";
202

PIACIBELLO Cyrille's avatar
Typos  
PIACIBELLO Cyrille committed
203
                return failedTests;
204 205 206 207
	}

};

208
#ifdef ScalFMM_USE_MPI
209

210
#include "../Src/Utils/FMpi.hpp"
211

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
#define TestClassMpi(X)						\
  int main(int argc, char** argv){				\
    X Controller(argc,argv);					\
    return Controller.Run();					\
  }								\
  
template <class TestClass>
class FUTesterMpi : public FUTester<TestClass>{
protected:
  FMpi app;
  
  //Constructor with params to initialize FMpi
  FUTesterMpi(int argc, char ** argv) : app(argc,argv){
  }

  /**
   * To print a message manually in the test
   * @param value a object that ostream can work on
   */
  template <class Output>
  void Print(const Output& value){
    if(app.global().processId()==0){
      std::cout<< "--- Output from program : " << value << "\n";
    }
  }

  
};
 
#endif  
242
#endif