00001 #include <cppunit/Portability.h>
00002 #include <cppunit/TestAssert.h>
00003 #include <cppunit/extensions/HelperMacros.h>
00004
00005
00006 #include <list>
00007 #include <string>
00008
00009 #include <CGAL/circulator.h>
00010 #include <CGAL/Object.h>
00011 #include <CEP/intersection/Object.h>
00012 #include <CGAL/Point_2.h>
00013 #include <CGAL/Point_3.h>
00014 #include <CGAL/Segment_3.h>
00015 #include <CGAL/Triangle_3.h>
00016
00017 #include <CGAL/IO/Verbose_ostream.h>
00018
00019
00020 extern CGAL::Verbose_ostream verr;
00021
00022
00023 using std::string;
00024
00025
00026 namespace CppUnit {
00027
00028 template <class T>
00029 struct cgal_assertion_traits
00030 {
00031 static bool equal( const T& x,
00032 const T& y )
00033 { return x == y; }
00034
00035 static string toString( const T& x )
00036 {
00037 CppUnit::OStringStream ost;
00038 CGAL::set_pretty_mode(ost);
00039 ost << x;
00040 return ost.str();
00041 }
00042 };
00043
00044
00045 template <class R>
00046 struct assertion_traits<CGAL::Point_3<R> >
00047 : public cgal_assertion_traits<CGAL::Point_3<R> >
00048 {};
00049
00050
00051 template <class R>
00052 struct assertion_traits<CGAL::Plane_3<R> >
00053 : public cgal_assertion_traits<CGAL::Plane_3<R> >
00054 {};
00055
00056
00057 template <class R>
00058 struct assertion_traits<CGAL::Segment_3<R> >
00059 : public cgal_assertion_traits<CGAL::Segment_3<R> >
00060 {
00061 static bool equal( const CGAL::Segment_3<R>& s,
00062 const CGAL::Segment_3<R>& t )
00063 {
00064 return s == t ||
00065 ( s.source() == t.target() && s.target() == t.source() );
00066 }
00067 };
00068
00069
00070 template <class R>
00071 struct assertion_traits<CGAL::Triangle_3<R> >
00072 : public cgal_assertion_traits<CGAL::Triangle_3<R> >
00073 {
00074 static bool equal( const CGAL::Triangle_3<R>& T,
00075 const CGAL::Point_3<R>& a,
00076 const CGAL::Point_3<R>& b,
00077 const CGAL::Point_3<R>& c )
00078 {
00079 return a == T[0] && b == T[1] && c == T[2];
00080 }
00081
00082 static bool equal( const CGAL::Triangle_3<R>& T1,
00083 const CGAL::Triangle_3<R>& T2 )
00084 {
00085 return equal( T1, T2[0], T2[1], T2[2] )
00086 || equal( T1, T2[0], T2[2], T2[1] )
00087 || equal( T1, T2[1], T2[0], T2[2] )
00088 || equal( T1, T2[1], T2[2], T2[0] )
00089 || equal( T1, T2[2], T2[0], T2[1] )
00090 || equal( T1, T2[2], T2[1], T2[0] );
00091 }
00092 };
00093
00094
00095
00096 template <class R>
00097 struct assertion_traits<std::list<CGAL::Point_2<R> > >
00098 {
00099 typedef CGAL::Point_2<R> Point;
00100 typedef std::list<Point> Point_list;
00101 typedef Point_list::const_iterator Point_iterator;
00102 typedef CGAL::Circulator_from_iterator<Point_iterator>
00103 Point_circulator;
00104
00105
00106 static bool same_sequence( Point_circulator begin1,
00107 Point_circulator begin2 )
00108 {
00109 Point_circulator p1 = begin1;
00110 Point_circulator p2 = begin2;
00111
00112 do {
00113 if ( *p1++ != *p2++ )
00114 return false;
00115 } while ( p1 != begin1 && p2 != begin2 );
00116
00117 return p1 == begin1 && p2 == begin2;
00118 }
00119
00120 static bool equal( const Point_list& poly1,
00121 const Point_list& poly2 )
00122 {
00123 Point_circulator begin1( poly1.begin(), poly1.end() );
00124 Point_circulator begin2( poly2.begin(), poly2.end() );
00125
00126 Point_circulator c1 = begin1;
00127 do {
00128 if ( same_sequence( c1, begin2 ) )
00129 return true;
00130 } while ( ++c1 != begin1 );
00131 return false;
00132 }
00133
00134 static string toString( const Point_list& poly )
00135 {
00136 return "[polygon]";
00137 }
00138 };
00139
00140
00141
00142 }
00143
00144
00145 template <class R>
00146 std::string toString( const CGAL::Object& obj )
00147 {
00148 CppUnit::OStringStream ost;
00149 CGAL::set_pretty_mode(ost);
00150
00151 CGAL::Point_3<R> p;
00152 CGAL::Segment_3<R> s;
00153 CGAL::Triangle_3<R> t;
00154 CGAL::Plane_3<R> P;
00155
00156 if ( obj.is_empty() )
00157 ost << "[empty object]";
00158 else if ( assign(p,obj) )
00159 ost << p;
00160 else if ( assign(s,obj) )
00161 ost << s;
00162 else if ( assign(t,obj) )
00163 ost << t;
00164 else if ( assign(P,obj) )
00165 ost << P;
00166 else
00167 ost << "[unknown object]";
00168
00169 return ost.str();
00170 }
00171
00172
00173 template <class T>
00174 inline void assert_equals_object( const T& expected,
00175 const CGAL::Object& actual_obj,
00176 long line,
00177 string file )
00178 {
00179 typedef typename T::R R;
00180 CGAL::Object act_nondegen =
00181 CEP::intersection::make_object_nondegenerate<R>( actual_obj );
00182 T actual;
00183
00184 if (assign( actual, act_nondegen ))
00185 CppUnit::TestAssert::assertEquals( expected, actual, line, file );
00186 else {
00187 std::string diag
00188 = "expected: "
00189 + CppUnit::assertion_traits<T>::toString(expected)
00190 + " but was: "
00191 + toString<R>(actual_obj);
00192
00193 CppUnit::TestAssert::assertImplementation( false,
00194 diag,
00195 line,
00196 file );
00197 }
00198 }
00199
00200
00201 #define ASSERT_EQUAL_OBJECT(expected,actual)\
00202 (assert_equals_object( (expected),(actual),__LINE__,__FILE__ ))
00203
00204 #define ASSERT_NULL_OBJECT(actual) CPPUNIT_ASSERT( (actual).is_empty() )
00205