1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 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 64 65 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
#include <cppunit/extensions/HelperMacros.h>
#include "cppUnitHelper.hxx"
#include "SearchArray.hxx" // CLAM
#include "stl_optbinsearch.h" // CLAM
#include "Array.hxx" // CLAM
#include "Point.hxx" // CLAM
namespace CLAM_Math
{
struct compareX
{
bool operator()( const CLAM::Point& lhs, const CLAM::Point& rhs )
{
return ( lhs.GetX() >= rhs.GetX() );
}
};
class SearchArrayTest;
CPPUNIT_TEST_SUITE_REGISTRATION( SearchArrayTest );
class SearchArrayTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( SearchArrayTest );
CPPUNIT_TEST( test_Search_On_Ordered_Array_Of_Points );
CPPUNIT_TEST( test_optbinsearch );
CPPUNIT_TEST( test_optbinsearch_w_pred );
CPPUNIT_TEST_SUITE_END();
public:
void setUp()
{
}
void tearDown()
{
}
private:
void test_Search_On_Ordered_Array_Of_Points()
{
CLAM::Array< CLAM::Point > array;
CLAM::SearchArray< CLAM::Point > searchArray;
array.AddElem( CLAM::Point( 0.0, 2.0 ) );
array.AddElem( CLAM::Point( 1.5, 1.0 ) );
array.AddElem( CLAM::Point( 3.0, 2.0 ) );
searchArray.Set( array );
CLAM::TIndex nearest = searchArray.Find( CLAM::Point( 0.8, 0.0 ), -1 );
CPPUNIT_ASSERT( nearest == 0 );
nearest = searchArray.Find( CLAM::Point( -3.2, 0.0 ), nearest );
CPPUNIT_ASSERT( nearest == -1 );
nearest = searchArray.Find( CLAM::Point( 3.2, 0.0 ), nearest );
CPPUNIT_ASSERT( nearest == 2 );
}
void test_optbinsearch()
{
CLAM::Array< CLAM::Point > array;
array.AddElem( CLAM::Point( 0.0, 2.0 ) );
array.AddElem( CLAM::Point( 1.5, 1.0 ) );
array.AddElem( CLAM::Point( 3.0, 2.0 ) );
CLAM::Point* nearest = std::hunt( array.GetPtr(), array.GetPtr()+array.Size(),
CLAM::Point( 0.8, 0.0 ) );
CPPUNIT_ASSERT( std::distance( nearest, array.GetPtr() ) == 0 );
nearest = std::hunt( array.GetPtr(), array.GetPtr()+array.Size(),
CLAM::Point( -3.2, 0.0 ), nearest );
CPPUNIT_ASSERT( nearest == array.GetPtr()+array.Size() );
nearest = std::hunt( array.GetPtr(), array.GetPtr()+array.Size(),
CLAM::Point( 3.2, 0.0 ), nearest );
CPPUNIT_ASSERT( nearest == array.GetPtr()+2 );
}
void test_optbinsearch_w_pred()
{
CLAM::Array< CLAM::Point > array;
array.AddElem( CLAM::Point( 0.0, 2.0 ) );
array.AddElem( CLAM::Point( 1.5, 1.0 ) );
array.AddElem( CLAM::Point( 3.0, 2.0 ) );
CLAM::Point* nearest = std::hunt( array.GetPtr(), array.GetPtr()+array.Size(),
CLAM::Point( 0.8, 0.0 ), compareX() );
CPPUNIT_ASSERT( std::distance( nearest, array.GetPtr() ) == 0 );
nearest = std::hunt( array.GetPtr(), array.GetPtr()+array.Size(),
CLAM::Point( -3.2, 0.0 ), nearest, compareX() );
CPPUNIT_ASSERT( nearest == array.GetPtr()+array.Size() );
nearest = std::hunt( array.GetPtr(), array.GetPtr()+array.Size(),
CLAM::Point( 3.2, 0.0 ), nearest, compareX() );
CPPUNIT_ASSERT( nearest == array.GetPtr()+2 );
}
};
}
|