00001 #define TOON_TEST_INTERNALS 00002 #include <TooN/TooN.h> 00003 #include <string> 00004 00005 using namespace TooN; 00006 using namespace std; 00007 00008 int lineno; 00009 00010 template<class A, class B> struct no_duplicates 00011 { 00012 typedef B type; 00013 }; 00014 00015 00016 template<class A> struct no_duplicates<A, A> 00017 { 00018 typedef class IgnoreMe{} type; 00019 }; 00020 00021 namespace TooN{namespace Internal{ 00022 struct NoError{}; 00023 }} 00024 00025 #define TRY lineno = __LINE__; try{ 00026 00027 #define EXPECT_CATCH(X, Y) \ 00028 catch(no_duplicates<TooN::Internal::X, TooN::Internal::Y>::type e)\ 00029 {\ 00030 cerr << "Test FAILED on line " << lineno << " from " << func_lineno << ". Expected " << #X << ", got " << #Y << "." << endl;\ 00031 }\ 00032 00033 #define EXPECT(X) \ 00034 throw TooN::Internal::NoError();\ 00035 }\ 00036 catch(TooN::Internal::X e)\ 00037 {\ 00038 cerr << "Test OK on line " << lineno << " from " << func_lineno << endl;\ 00039 }\ 00040 EXPECT_CATCH(X, BadIndex)\ 00041 EXPECT_CATCH(X, SliceError)\ 00042 EXPECT_CATCH(X, StaticSliceError)\ 00043 EXPECT_CATCH(X, SizeMismatch)\ 00044 EXPECT_CATCH(X, StaticSizeMismatch)\ 00045 EXPECT_CATCH(X, NoError) 00046 00047 #define test_static_static_slices(...) test_static_static_slices_(__LINE__ , __VA_ARGS__) 00048 template<class C> void test_static_static_slices_(int func_lineno, C v) 00049 { 00050 TRY{ 00051 v.template slice<0, 3>(); 00052 } 00053 EXPECT(StaticSliceError); 00054 00055 TRY{ 00056 v.template slice<2, 2>(); 00057 } 00058 EXPECT(StaticSliceError); 00059 00060 TRY{ 00061 v.template slice<-1, 1>(); 00062 } 00063 EXPECT(StaticSliceError); 00064 00065 TRY{ 00066 v.template slice<0, 2>(); 00067 } 00068 EXPECT(NoError) 00069 00070 } 00071 00072 #define test_static_dynamic_slices(...) test_static_dynamic_slices_(__LINE__ , __VA_ARGS__) 00073 template<class C> void test_static_dynamic_slices_(int func_lineno, C v) 00074 { 00075 TRY{ 00076 v.template slice<0, 3>(); 00077 } 00078 EXPECT(SliceError); 00079 00080 TRY{ 00081 v.template slice<2, 2>(); 00082 } 00083 EXPECT(SliceError); 00084 00085 TRY{ 00086 v.template slice<-1, 1>(); 00087 } 00088 EXPECT(StaticSliceError); 00089 00090 TRY{ 00091 v.template slice<0, 2>(); 00092 } 00093 EXPECT(NoError); 00094 00095 } 00096 00097 #define test_dynamic_slices(...) test_dynamic_slices_(__LINE__, __VA_ARGS__) 00098 template<class C> void test_dynamic_slices_(int func_lineno, C v) 00099 { 00100 TRY{ 00101 v.slice(0,3); 00102 } 00103 EXPECT(SliceError); 00104 00105 TRY{ 00106 v.slice(2,2); 00107 } 00108 EXPECT(SliceError); 00109 00110 TRY{ 00111 v.slice(-1,1); 00112 } 00113 EXPECT(SliceError); 00114 00115 TRY{ 00116 v.template slice(0, 2); 00117 } 00118 EXPECT(NoError); 00119 } 00120 00121 00122 #define test_index(...) test_index_(__LINE__, __VA_ARGS__) 00123 template<class C> void test_index_(int func_lineno, C v) 00124 { 00125 TRY{ 00126 v[-2]; 00127 } 00128 EXPECT(BadIndex); 00129 00130 TRY{ 00131 v[-1]; 00132 } 00133 EXPECT(BadIndex); 00134 00135 TRY{ 00136 v[0]; 00137 } 00138 EXPECT(NoError); 00139 00140 TRY{ 00141 v[1]; 00142 } 00143 EXPECT(NoError); 00144 00145 TRY{ 00146 v[2]; 00147 } 00148 EXPECT(BadIndex); 00149 } 00150 00151 00152 00153 #define test_comma(...) test_comma_(__LINE__, __VA_ARGS__) 00154 template<class C> void test_comma_(int func_lineno) 00155 { 00156 TRY{ 00157 Vector<4> v; 00158 Fill(v) = 1,2,3,4; 00159 } 00160 EXPECT(NoError); 00161 00162 TRY{ 00163 Vector<4> v(4); 00164 Fill(v) = 1,2,3,4; 00165 } 00166 EXPECT(NoError); 00167 00168 TRY{ 00169 Vector<4> v; 00170 Fill(v) = 1,2,3,4,5; 00171 } 00172 EXPECT(StaticVectorOverfill); 00173 00174 TRY{ 00175 Vector<> v(4); 00176 Fill(v) = 1,2,3,4,5; 00177 } 00178 EXPECT(VectorOverfill); 00179 00180 TRY{ 00181 Vector<4> v; 00182 Fill(v) = 1,2,3; 00183 } 00184 EXPECT(Underfill); 00185 00186 TRY{ 00187 Vector<> v(4); 00188 Fill(v) = 1,2,3; 00189 } 00190 EXPECT(Underfill); 00191 } 00192 00193 00194 00195 int main() 00196 { 00197 test_static_static_slices(Vector<2>()); 00198 test_dynamic_slices(Vector<2>()); 00199 00200 test_static_static_slices(Vector<4>().slice<0,2>()); 00201 test_dynamic_slices(Vector<4>().slice<0,2>()); 00202 00203 test_static_dynamic_slices(Vector<4>().slice(0,2)); 00204 test_dynamic_slices(Vector<4>().slice(0,2)); 00205 00206 test_static_dynamic_slices(Vector<>(2)); 00207 test_dynamic_slices(Vector<>(2)); 00208 00209 test_static_static_slices(Vector<>(4).slice<0,2>()); 00210 test_dynamic_slices(Vector<>(4).slice<0,2>()); 00211 00212 test_static_dynamic_slices(Vector<>(4).slice(0,2)); 00213 test_dynamic_slices(Vector<>(4).slice(0,2)); 00214 00215 test_index(Vector<2>()); 00216 test_index(Vector<>(2)); 00217 }