//-*- mode: c++ -*- #ifndef __AVERAGE_TEST_H__ #define __AVERAGE_TEST_H__ // Custom #include "Average.h" // 3rd-party #include #include // Standard #include using namespace std; static const float TEST_EPSILON = 1e-6; /* * FIXME: This unit test only exercises the A++ version of the code. * I don't know of a good way to use cxxTest on P++ codes because the * initialization (Optimization_Manager::Initialize_Virtual_Machine) * requires argc and argv. However, CxxTest doesn't appear to provide * access to these. * * If we can find a way to get argc/argv in CxxTest, you would use the * MpiSingleton below to initialize & clean up P++. * * Thanks again, P++ developers! */ #if 0 class MpiSingleton : CxxTest::GlobalFixture { public: virtual bool setUpWorld() { int num_procs = 1; char *localName = __FILE__; int argc; char ** argv; // Startup P++ Optimization_Manager::Initialize_Virtual_Machine(localName,num_procs,argc,argv); return true; } virtual bool tearDownWorld() { Communication_Manager::Sync(); Optimization_Manager::Exit_Virtual_Machine(); return true; } }; static MpiSingleton mpiSetup;; #endif class TestAverage : public CxxTest::TestSuite { public: vector variables; Average accumulate; void setUp( ) { variables.insert( variables.end(), AverageVariables::BI ); // 3 variables.insert( variables.end(), AverageVariables::BX ); // 0 variables.insert( variables.end(), AverageVariables::BY ); // 1 variables.insert( variables.end(), AverageVariables::BJ ); // 4 variables.insert( variables.end(), AverageVariables::BY ); // 1 variables.insert( variables.end(), AverageVariables::BK ); // 5 accumulate = Average(53, 24, 32, NULL, variables); } void tearDown() { accumulate.reset(); } /* * Without the overloaded Average::operator=, the * following code would result in an error when integrated with the * LFM: * * NODE 0: ERROR in floatArray::Test_Conformability ( const floatArray & X ) * NODE 0: Operands have different width ghost boundaries -- sorry -- not yet supported! * NODE 0: Internal Ghost Boundary widths: Lhs (0, 0, 0, 0) Rhs (4, 4, 4, 0) * NODE 0: Exiting ... * Exiting P++ program from inside of APP_ABORT()! (Calling system abort function ...) * Exiting P++ Virtual Machine! * * If this code executes in parallel, the bug was fixed. */ void testEqualOperatorOverload( void ) { Average accum; accum = Average(53, 24, 32, NULL, variables); } /* * This code should (and always has) worked. It doesn't involk the * default (empty) constructor. Compare to * testEqualOperatorOverload. */ void testOverloadedConstructor( void ) { Average accum(53, 24, 32, NULL, variables); } void testGetVariableIds( void ) { map vars = accumulate.getVariableIds(); // loop over all keys in the map like so: //typedef map mapType; //for (mapType::const_iterator it = vars.begin(); it != vars.end(); ++it){ // cout << it->first << ": " << it->second << endl; //} TS_ASSERT_EQUALS( vars.find(AverageVariables::BX)->second, "avgBx" ); TS_ASSERT_EQUALS( vars.find(AverageVariables::BY)->second, "avgBy" ); TS_ASSERT_EQUALS( vars.find(AverageVariables::BI)->second, "avgBi" ); TS_ASSERT_EQUALS( vars.find(AverageVariables::BJ)->second, "avgBj" ); TS_ASSERT_EQUALS( vars.find(AverageVariables::BK)->second, "avgBk" ); } void testIncrement( void ) { floatArray var(53,24,32); var = 1.0; accumulate.setDeltaTime(0.5); accumulate.increment(var, AverageVariables::BI); var = 2.0; accumulate.setDeltaTime(0.5); accumulate.increment(var, AverageVariables::BI); floatArray varTest = accumulate.getVariable(AverageVariables::BI); // Minimize the signal-to-noise ratio by running three simple tests TS_ASSERT_DELTA( sum(varTest), 1.5*53*24*32, TEST_EPSILON ); TS_ASSERT_DELTA( min(varTest), 1.5, TEST_EPSILON ); TS_ASSERT_DELTA( max(varTest), 1.5, TEST_EPSILON ); // Another way of validating BI is to element-by-element compare // the values. However, this may be slower & outputs WAY too much // to stderr on failure. // //for (int k=1; k < varTest.getLength(2)+1; k++){ // for (int j=1; j < varTest.getLength(1)+1; j++){ // for (int i=1; i < varTest.getLength(0)+1; i++){ // TS_ASSERT_DELTA(varTest(i,j,k), 1.5, TEST_EPSILON); // } // } //} } void testReset( void ) { floatArray var(53,24,32); var = 2.0; accumulate.setDeltaTime(1.0); accumulate.increment(var, AverageVariables::BI); floatArray varTest = accumulate.getVariable(AverageVariables::BI); TS_ASSERT_DELTA( sum(varTest), 2.0*53*24*32, TEST_EPSILON ); TS_ASSERT_DELTA( min(varTest), 2.0, TEST_EPSILON ); TS_ASSERT_DELTA( max(varTest), 2.0, TEST_EPSILON ); accumulate.reset(); varTest = accumulate.getVariable(AverageVariables::BI); TS_ASSERT_DELTA( sum(varTest), 0.0, TEST_EPSILON ); TS_ASSERT_DELTA( min(varTest), 0.0, TEST_EPSILON ); TS_ASSERT_DELTA( max(varTest), 0.0, TEST_EPSILON ); } }; #endif