#include "AUResultsTest.h" #include #include // for cout //#define DISPLAY_AUS #ifdef DISPLAY_AUS #include // for setprecision( int p ) #endif #include // For auto_ptr #include using namespace std; static const unsigned int auSize = 8; static const string testName = "TestResults"; CPPUNIT_TEST_SUITE_REGISTRATION( AUResultsTest ); AUResultsTest::AUResultsTest() : haveBaseResults(false) { std::string filename( "AUResultsTest." ); filename += mp_AUResults::extensionForFiletype( mp_AUResults::xml ); std::ifstream ifs( filename.c_str() ); if( ifs.good() ) { haveBaseResults = true; baseResults.restore( ifs ); } } bool AUResultsTest::saveResultsToFile( mp_AUResults &res, std::string filename ) { filename += "."; filename += mp_AUResults::extensionForFiletype( mp_AUResults::xml ); std::ofstream ofs( filename.c_str() ); if( !ofs.good() ) return false; res.save( ofs ); return true; } /** */ void AUResultsTest::setUp() { verifyBaseResults(); testResults.clear(); testResults = baseResults; } /** */ void AUResultsTest::tearDown() { } void AUResultsTest::verifyBaseResults() { if( !haveBaseResults ) CPPUNIT_FAIL( "Failed to load base results." ); } /** Rotate an RImage and compare. * Rotate the input RImage and compare with one we know is properly rotated. */ void AUResultsTest::testRanges() { std::vector temp = testResults.getAUResults(); std::vector freq = testResults.frequenciesForAUIndex( 0 ); CPPUNIT_ASSERT( auSize == temp.size() ); CPPUNIT_ASSERT( 72 == freq.size() ); CPPUNIT_ASSERT( testName == testResults.getName() ); // if( !image ) // CPPUNIT_FAIL( "Failed to load the test image from a mat file." ); // CPPUNIT_ASSERT_MESSAGE( "Probable missing weights files", (weights != NULL) ); } /** Test a serialization round-trip. * save testResults to a stringstream * restore a new object from the stringstream * compare the two objects */ void AUResultsTest::testSerialization() { std::ostringstream oss( std::ostringstream::out ); testResults.save( oss ); std::istringstream iss( oss.str() ); mp_AUResults other; other.restore( iss ); CPPUNIT_ASSERT( testResults == other ); std::vector step(8,1.0); other.addFrequency( 1, 1, step ); CPPUNIT_ASSERT( ! (testResults == other) ); } void AUResultsTest::testAddFrequency() { std::vector step(8,1.0); std::vector freq = testResults.frequenciesForAUIndex( 0 ); double before = freq[0]; testResults.addFrequency( 1, 1, step ); freq = testResults.frequenciesForAUIndex( 0 ); before -= freq[0]; // should be 1.0 before = fabs(before); CPPUNIT_ASSERT( (before < 1.000001) && (before > .999999) ); CPPUNIT_ASSERT( step == testResults.getAUResults() ); } void AUResultsTest::testAccumulate() { std::vector step(8,2.0); mp_AUResults other( testResults ); // saveResultsToFile( other, "outOther" ); CPPUNIT_ASSERT_MESSAGE( "Copy constructor failed.", (testResults == other) ); other.addFrequency( 1, 1, step ); // saveResultsToFile( other, "outOtherAdded" ); testResults += other; // saveResultsToFile( testResults, "outResults" ); CPPUNIT_ASSERT( testResults == other ); }