random_racer/unittests/ControlPointContainerTests.cpp

Go to the documentation of this file.
00001 #include <cppunit/BriefTestProgressListener.h>
00002 #include <cppunit/CompilerOutputter.h>
00003 #include <cppunit/extensions/TestFactoryRegistry.h>
00004 #include <cppunit/TestResult.h>
00005 #include <cppunit/TestResultCollector.h>
00006 #include <cppunit/TestRunner.h>
00007 #include <cppunit/config/SourcePrefix.h>
00008 
00009 #include "ControlPointContainerTests.h"
00010 
00011 
00012 namespace random_racer
00013 {
00014     
00015 CPPUNIT_TEST_SUITE_REGISTRATION(ControlPointContainerTests);
00016 
00017 void 
00018 ControlPointContainerTests::setUp()
00019 {
00020     m_controlPointGenerator = new ControlPointTestGenerator(0);
00021     m_controlPoints = new ControlPointContainer(10, 10,
00022                         m_controlPointGenerator->getCallback(), -1, 3);
00023     m_controlPointsFive = new ControlPointContainer(5, 10,
00024                         m_controlPointGenerator->getCallback(), -5, 35);
00025 }
00026 
00027 void 
00028 ControlPointContainerTests::testMapsValueToKey()
00029 {
00030     // test a ControlPointContainer with blocksize 10
00031     // test positive values
00032     CPPUNIT_ASSERT_EQUAL(0, m_controlPoints->mapToKey(0));
00033     CPPUNIT_ASSERT_EQUAL(0, m_controlPoints->mapToKey(9));
00034     CPPUNIT_ASSERT_EQUAL(1, m_controlPoints->mapToKey(10));
00035     CPPUNIT_ASSERT_EQUAL(2, m_controlPoints->mapToKey(20));
00036     
00037     // test negative values
00038     CPPUNIT_ASSERT_EQUAL(-1, m_controlPoints->mapToKey(-1));
00039     CPPUNIT_ASSERT_EQUAL(-1, m_controlPoints->mapToKey(-10));
00040     CPPUNIT_ASSERT_EQUAL(-2, m_controlPoints->mapToKey(-11));
00041     CPPUNIT_ASSERT_EQUAL(-2, m_controlPoints->mapToKey(-20));
00042     
00043     // test floating point values
00044     CPPUNIT_ASSERT_EQUAL(0, m_controlPoints->mapToKey(0.1));
00045     CPPUNIT_ASSERT_EQUAL(0, m_controlPoints->mapToKey(9.99));
00046     CPPUNIT_ASSERT_EQUAL(1, m_controlPoints->mapToKey(10.1));
00047     
00048     // test negative floating point values
00049     CPPUNIT_ASSERT_EQUAL(-1, m_controlPoints->mapToKey(-0.1));
00050     
00051     // test a ControlPointContainer with blocksize 5
00052     CPPUNIT_ASSERT_EQUAL(0, m_controlPointsFive->mapToKey(0));
00053     CPPUNIT_ASSERT_EQUAL(0, m_controlPointsFive->mapToKey(5));
00054     CPPUNIT_ASSERT_EQUAL(1, m_controlPointsFive->mapToKey(10));
00055     
00056     // negative values
00057     CPPUNIT_ASSERT_EQUAL(-1, m_controlPointsFive->mapToKey(-1));
00058     CPPUNIT_ASSERT_EQUAL(-1, m_controlPointsFive->mapToKey(-5));
00059     CPPUNIT_ASSERT_EQUAL(-2, m_controlPointsFive->mapToKey(-11));
00060 }
00061 
00062 void 
00063 ControlPointContainerTests::testGetOffsetToKey()
00064 {
00065     // test a ControlPointContainer with blocksize 10
00066     // test positive values
00067     CPPUNIT_ASSERT_EQUAL(0, m_controlPoints->getOffset(0));
00068     CPPUNIT_ASSERT_EQUAL(10, m_controlPoints->getOffset(1));
00069     CPPUNIT_ASSERT_EQUAL(20, m_controlPoints->getOffset(2));
00070     
00071     // test negative values
00072     CPPUNIT_ASSERT_EQUAL(-10, m_controlPoints->getOffset(-1));
00073     CPPUNIT_ASSERT_EQUAL(-20, m_controlPoints->getOffset(-2));
00074     CPPUNIT_ASSERT_EQUAL(-30, m_controlPoints->getOffset(-3));
00075     
00076     // test a ControlPointContainer with blocksize 5
00077     CPPUNIT_ASSERT_EQUAL(0, m_controlPointsFive->getOffset(0));
00078     CPPUNIT_ASSERT_EQUAL(10, m_controlPointsFive->getOffset(1));
00079     CPPUNIT_ASSERT_EQUAL(-10, m_controlPointsFive->getOffset(-1));        
00080 }
00081 
00082 void 
00083 ControlPointContainerTests::testGetTheFourBlocks()
00084 {
00085     // test a ControlPointContainer with blocksize 10
00086     m_controlPoints->getFourBlocks(4, 5, m_blockList);
00087     CPPUNIT_ASSERT_EQUAL(0, m_blockList[0]);
00088     CPPUNIT_ASSERT_EQUAL(1, m_blockList[1]);
00089     CPPUNIT_ASSERT_EQUAL(-1, m_blockList[2]);
00090     CPPUNIT_ASSERT_EQUAL(0, m_blockList[3]);
00091     CPPUNIT_ASSERT_EQUAL(-1, m_blockList[4]);
00092     CPPUNIT_ASSERT_EQUAL(1, m_blockList[5]);
00093     CPPUNIT_ASSERT_EQUAL(0, m_blockList[6]);
00094     CPPUNIT_ASSERT_EQUAL(0, m_blockList[7]);
00095     
00096     m_controlPoints->getFourBlocks(5, 5, m_blockList);
00097     CPPUNIT_ASSERT_EQUAL(1, m_blockList[0]);
00098     CPPUNIT_ASSERT_EQUAL(1, m_blockList[1]);
00099     CPPUNIT_ASSERT_EQUAL(0, m_blockList[2]);
00100     CPPUNIT_ASSERT_EQUAL(0, m_blockList[3]);
00101     CPPUNIT_ASSERT_EQUAL(0, m_blockList[4]);
00102     CPPUNIT_ASSERT_EQUAL(1, m_blockList[5]);
00103     CPPUNIT_ASSERT_EQUAL(1, m_blockList[6]);
00104     CPPUNIT_ASSERT_EQUAL(0, m_blockList[7]);
00105     
00106     m_controlPoints->getFourBlocks(4, 4, m_blockList);
00107     CPPUNIT_ASSERT_EQUAL(0, m_blockList[0]);
00108     CPPUNIT_ASSERT_EQUAL(0, m_blockList[1]);
00109     CPPUNIT_ASSERT_EQUAL(-1, m_blockList[2]);
00110     CPPUNIT_ASSERT_EQUAL(-1, m_blockList[3]);
00111     CPPUNIT_ASSERT_EQUAL(-1, m_blockList[4]);
00112     CPPUNIT_ASSERT_EQUAL(0, m_blockList[5]);
00113     CPPUNIT_ASSERT_EQUAL(0, m_blockList[6]);
00114     CPPUNIT_ASSERT_EQUAL(-1, m_blockList[7]);
00115     
00116     m_controlPoints->getFourBlocks(5, 4, m_blockList);
00117     CPPUNIT_ASSERT_EQUAL(1, m_blockList[0]);
00118     CPPUNIT_ASSERT_EQUAL(0, m_blockList[1]);
00119     CPPUNIT_ASSERT_EQUAL(0, m_blockList[2]);
00120     CPPUNIT_ASSERT_EQUAL(-1, m_blockList[3]);
00121     CPPUNIT_ASSERT_EQUAL(0, m_blockList[4]);
00122     CPPUNIT_ASSERT_EQUAL(0, m_blockList[5]);
00123     CPPUNIT_ASSERT_EQUAL(1, m_blockList[6]);
00124     CPPUNIT_ASSERT_EQUAL(-1, m_blockList[7]);
00125     
00126     // test a ControlPointContainer with blocksize 5
00127     m_controlPointsFive->getFourBlocks(4, 4, m_blockList);
00128     CPPUNIT_ASSERT_EQUAL(0, m_blockList[0]);
00129     CPPUNIT_ASSERT_EQUAL(0, m_blockList[1]);
00130     CPPUNIT_ASSERT_EQUAL(-1, m_blockList[2]);
00131     CPPUNIT_ASSERT_EQUAL(-1, m_blockList[3]);
00132     CPPUNIT_ASSERT_EQUAL(-1, m_blockList[4]);
00133     CPPUNIT_ASSERT_EQUAL(0, m_blockList[5]);
00134     CPPUNIT_ASSERT_EQUAL(0, m_blockList[6]);
00135     CPPUNIT_ASSERT_EQUAL(-1, m_blockList[7]);
00136 }
00137 
00138 void 
00139 ControlPointContainerTests::testHashMapValues()
00140 {
00141     // test a ControlPointContainer with blocksize 5 and viewsize 10
00142     m_controlPointsFive->getPoints(0, 0, &m_target);
00143     CPPUNIT_ASSERT(4 == m_target.size());
00144     
00145     unsigned int position = 0;
00146     
00147     for(  std::vector<VRS::Vector>::iterator i = m_target.begin(); i < 
00148             m_target.end(); ++i , position++)
00149     {             
00150         if(position == 0)
00151         {
00152             CPPUNIT_ASSERT(0 == (*i)[0]);
00153             CPPUNIT_ASSERT(35 == (*i)[1]);
00154             CPPUNIT_ASSERT(2 == (*i)[2]);
00155         }
00156         else if(position == 1)
00157         {
00158             CPPUNIT_ASSERT(-10 == (*i)[0]);
00159             CPPUNIT_ASSERT(35 == (*i)[1]);
00160             CPPUNIT_ASSERT(-8 == (*i)[2]);
00161         }
00162         else if(position == 2)
00163         {
00164             CPPUNIT_ASSERT(-10 == (*i)[0]);
00165             CPPUNIT_ASSERT(35 == (*i)[1]);
00166             CPPUNIT_ASSERT(2 == (*i)[2]);
00167         }
00168         else
00169         {
00170             CPPUNIT_ASSERT(0 == (*i)[0]);
00171             CPPUNIT_ASSERT(35 == (*i)[1]);
00172             CPPUNIT_ASSERT(-8 == (*i)[2]);
00173         }
00174     }
00175     m_target.clear();
00176     
00177     // test a ControlPointContainer with blocksize 10
00178     m_controlPoints->getPoints(0, 0, &m_target);
00179     CPPUNIT_ASSERT(4 == m_target.size());
00180     
00181     position = 0;
00182     
00183     for(  std::vector<VRS::Vector>::iterator i = m_target.begin(); i < 
00184             m_target.end(); ++i , position++)
00185     {
00186         if(position == 0)
00187         {
00188             CPPUNIT_ASSERT(0 == (*i)[0]);
00189             CPPUNIT_ASSERT(3 == (*i)[1]);
00190             CPPUNIT_ASSERT(1 == (*i)[2]);
00191         }
00192         else if(position == 1)
00193         {
00194             CPPUNIT_ASSERT(-10 == (*i)[0]);
00195             CPPUNIT_ASSERT(3 == (*i)[1]);
00196             CPPUNIT_ASSERT(-9 == (*i)[2]);
00197         }
00198         else if(position == 2)
00199         {
00200             CPPUNIT_ASSERT(-10 == (*i)[0]);
00201             CPPUNIT_ASSERT(3 == (*i)[1]);
00202             CPPUNIT_ASSERT(1 == (*i)[2]);
00203         }
00204         else
00205         {
00206             CPPUNIT_ASSERT(0 == (*i)[0]);
00207             CPPUNIT_ASSERT(3 == (*i)[1]);
00208             CPPUNIT_ASSERT(-9 == (*i)[2]);
00209         }
00210     }
00211     m_target.clear();
00212     
00213     m_controlPoints->getPoints(9, 10, &m_target);
00214     CPPUNIT_ASSERT(4 == m_target.size());
00215     
00216     position = 0;
00217     
00218     for(  std::vector<VRS::Vector>::iterator i = m_target.begin(); i < 
00219             m_target.end(); ++i, position++ )
00220     {        
00221         if(position == 0)
00222         {
00223             CPPUNIT_ASSERT(10 == (*i)[0]);
00224             CPPUNIT_ASSERT(3  == (*i)[1]);
00225             CPPUNIT_ASSERT(11 == (*i)[2]);
00226         }
00227         else if(position == 1)
00228         {
00229             CPPUNIT_ASSERT(0 == (*i)[0]);
00230             CPPUNIT_ASSERT(3 == (*i)[1]);
00231             CPPUNIT_ASSERT(1 == (*i)[2]);
00232         }
00233         else if(position == 2)
00234         {
00235             CPPUNIT_ASSERT(0  == (*i)[0]);
00236             CPPUNIT_ASSERT(3  == (*i)[1]);
00237             CPPUNIT_ASSERT(11 == (*i)[2]);
00238         }
00239         else
00240         {
00241             CPPUNIT_ASSERT(10 == (*i)[0]);
00242             CPPUNIT_ASSERT(3  == (*i)[1]);
00243             CPPUNIT_ASSERT(1  == (*i)[2]);
00244         }
00245     }
00246     m_target.clear();
00247     
00248     // test a ControlPointContainer with blocksize 10
00249     m_controlPoints->getPoints(0, 0, 9, 9, &m_target);
00250     CPPUNIT_ASSERT(1 == m_target.size());
00251     
00252     position = 0;
00253     
00254     for(  std::vector<VRS::Vector>::iterator i = m_target.begin(); i < 
00255             m_target.end(); ++i, position++ )
00256     {                
00257         if(position == 0)
00258         {
00259             CPPUNIT_ASSERT(0 == (*i)[0]);
00260             CPPUNIT_ASSERT(3 == (*i)[1]);
00261             CPPUNIT_ASSERT(1 == (*i)[2]);
00262         }
00263     }
00264     m_target.clear();
00265     
00266     // test a ControlPointContainer with blocksize 10
00267     m_controlPoints->getPoints(0, 0, 10, 9, &m_target);
00268     CPPUNIT_ASSERT(2 == m_target.size());
00269     
00270     position = 0;
00271     
00272     for(  std::vector<VRS::Vector>::iterator i = m_target.begin(); i < 
00273             m_target.end(); ++i, position++ )
00274     {                
00275         if(position == 0)
00276         {
00277             CPPUNIT_ASSERT(0 == (*i)[0]);
00278             CPPUNIT_ASSERT(3 == (*i)[1]);
00279             CPPUNIT_ASSERT(1 == (*i)[2]);
00280         }
00281         if(position == 1)
00282         {
00283             CPPUNIT_ASSERT(10 == (*i)[0]);
00284             CPPUNIT_ASSERT(3  == (*i)[1]);
00285             CPPUNIT_ASSERT(1  == (*i)[2]);
00286         }
00287     }
00288     m_target.clear();
00289     
00290     // test a ControlPointContainer with blocksize 10
00291    m_controlPoints->getPoints(0, 0, -5, 5, &m_target);
00292    CPPUNIT_ASSERT(2 == m_target.size());
00293    
00294    position = 0;
00295    
00296    for(  std::vector<VRS::Vector>::iterator i = m_target.begin(); i < 
00297            m_target.end(); ++i, position++ )
00298    {                
00299        if(position == 0)
00300        {
00301            CPPUNIT_ASSERT(0 == (*i)[0]);
00302            CPPUNIT_ASSERT(3 == (*i)[1]);
00303            CPPUNIT_ASSERT(1 == (*i)[2]);
00304        }
00305        if(position == 1)
00306        {
00307            CPPUNIT_ASSERT(-10 == (*i)[0]);
00308            CPPUNIT_ASSERT(3  == (*i)[1]);
00309            CPPUNIT_ASSERT(1  == (*i)[2]);
00310        }
00311    }
00312    m_target.clear();
00313    
00314    // test a ControlPointContainer with blocksize 10
00315    m_controlPoints->getPoints(0, 0, 19, 19, &m_target);
00316    CPPUNIT_ASSERT(4 == m_target.size());
00317    
00318    position = 0;
00319    
00320    for(  std::vector<VRS::Vector>::iterator i = m_target.begin(); i < 
00321            m_target.end(); ++i , position++)
00322    {        
00323        if(position == 0)
00324        {
00325            CPPUNIT_ASSERT(0 == (*i)[0]);
00326            CPPUNIT_ASSERT(3 == (*i)[1]);
00327            CPPUNIT_ASSERT(1 == (*i)[2]);
00328        }
00329        else if(position == 1)
00330        {
00331            CPPUNIT_ASSERT(-10 == (*i)[0]);
00332            CPPUNIT_ASSERT(3 == (*i)[1]);
00333            CPPUNIT_ASSERT(-9 == (*i)[2]);
00334        }
00335        else if(position == 2)
00336        {
00337            CPPUNIT_ASSERT(-10 == (*i)[0]);
00338            CPPUNIT_ASSERT(3 == (*i)[1]);
00339            CPPUNIT_ASSERT(1 == (*i)[2]);
00340        }
00341        else
00342        {
00343            CPPUNIT_ASSERT(0 == (*i)[0]);
00344            CPPUNIT_ASSERT(3 == (*i)[1]);
00345            CPPUNIT_ASSERT(-9 == (*i)[2]);
00346        }
00347    }
00348    m_target.clear();
00349    
00350    // test a ControlPointContainer with blocksize 10
00351    m_controlPoints->getPoints(0, 0, -10, -10, &m_target);
00352    CPPUNIT_ASSERT(4 == m_target.size());
00353    
00354    position = 0;
00355    
00356    for(  std::vector<VRS::Vector>::iterator i = m_target.begin(); i < 
00357            m_target.end(); ++i , position++)
00358    {        
00359        if(position == 0)
00360        {
00361            CPPUNIT_ASSERT(0 == (*i)[0]);
00362            CPPUNIT_ASSERT(3 == (*i)[1]);
00363            CPPUNIT_ASSERT(1 == (*i)[2]);
00364        }
00365        else if(position == 1)
00366        {
00367            CPPUNIT_ASSERT(-10 == (*i)[0]);
00368            CPPUNIT_ASSERT(3 == (*i)[1]);
00369            CPPUNIT_ASSERT(-9 == (*i)[2]);
00370        }
00371        else if(position == 2)
00372        {
00373            CPPUNIT_ASSERT(-10 == (*i)[0]);
00374            CPPUNIT_ASSERT(3 == (*i)[1]);
00375            CPPUNIT_ASSERT(1 == (*i)[2]);
00376        }
00377        else
00378        {
00379            CPPUNIT_ASSERT(0 == (*i)[0]);
00380            CPPUNIT_ASSERT(3 == (*i)[1]);
00381            CPPUNIT_ASSERT(-9 == (*i)[2]);
00382        }
00383    }
00384    m_target.clear();  
00385 }
00386 
00387 }
00388 
00389 int
00390 main( int argc, char* argv[] )
00391 {
00392     // Create the event manager and test controller
00393     CPPUNIT_NS::TestResult controller;
00394 
00395     // Add a listener that colllects test result
00396     CPPUNIT_NS::TestResultCollector result;
00397     controller.addListener( &result );        
00398 
00399     // Add a listener that print dots as test run.
00400     CPPUNIT_NS::BriefTestProgressListener progress;
00401     controller.addListener( &progress );      
00402 
00403     // Add the top suite to the test runner
00404     CPPUNIT_NS::TestRunner runner;
00405     runner.addTest( CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest() );
00406     runner.run( controller );
00407 
00408     // Print test in a compiler compatible format.
00409     CPPUNIT_NS::CompilerOutputter outputter( &result, CPPUNIT_NS::stdCOut() );
00410     outputter.write(); 
00411 
00412     return result.wasSuccessful() ? 0 : 1;
00413 }

Generated on Fri May 11 21:01:58 2007 for Random Racer by  doxygen 1.5.1