Changeset 1240
- Timestamp:
- Mar 16, 2008, 6:21:39 AM (15 years ago)
- Location:
- trunk/test
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/matrix_lookup_test.cc
r1170 r1240 22 22 */ 23 23 24 #include "Suite.h" 25 24 26 #include "yat/utility/Matrix.h" 25 27 #include "yat/classifier/MatrixLookup.h" … … 33 35 utility::Matrix matrix(size_t n); 34 36 35 int main( const int argc,constchar* argv[])37 int main(int argc, char* argv[]) 36 38 { 37 39 using namespace theplu::yat::classifier; 38 40 39 std::ostream* error; 40 if (argc>1 && argv[1]==std::string("-v")) 41 error = &std::cerr; 42 else { 43 error = new std::ofstream("/dev/null"); 44 if (argc>1) 45 std::cout << "lookup_test -v : for printing extra information\n"; 46 } 47 bool ok = true; 41 test::Suite suite(argc, argv); 48 42 49 *error<< "\nTesting MatrixLookup" << std::endl;50 *error<< "MatrixLookup::MatrixLookup(const utility::Matrix& data)...";43 suite.err() << "\nTesting MatrixLookup" << std::endl; 44 suite.err() << "MatrixLookup::MatrixLookup(const utility::Matrix& data)..."; 51 45 utility::Matrix gsl_m1(matrix(2)); 52 46 classifier::MatrixLookup m1(gsl_m1); … … 54 48 m1(0,0)!=gsl_m1(0,0) || m1(0,1)!=gsl_m1(0,1) || 55 49 m1(1,0)!=gsl_m1(1,0) || m1(1,1)!=gsl_m1(1,1) ) { 56 ok =false;57 *error<< "ERROR:" << std::endl;50 suite.add(false); 51 suite.err() << "ERROR:" << std::endl; 58 52 } 59 53 else 60 *error<< "Ok" << std::endl;54 suite.err() << "Ok" << std::endl; 61 55 62 56 63 *error<< "MatrixLookup::MatrixLookup(const utility::Matrix&,\n"57 suite.err() << "MatrixLookup::MatrixLookup(const utility::Matrix&,\n" 64 58 << " const std::vector<size_t>&,\n" 65 59 << " const std::vector<size_t>&)..."; … … 77 71 m2(0,0)!=gsl_m2(1,2) || m2(0,1)!=gsl_m2(1,0) || 78 72 m2(1,0)!=gsl_m2(3,2) || m2(1,1)!=gsl_m2(3,0) ) { 79 ok =false;80 *error<< "ERROR:" << std::endl;81 *error<< "rows: " << m2.rows() << " expected 2\n"73 suite.add(false); 74 suite.err() << "ERROR:" << std::endl; 75 suite.err() << "rows: " << m2.rows() << " expected 2\n" 82 76 << "columns: " << m2.columns() << " expected 2\n" 83 77 << "(0,0): " << m2(0,0) << " expected " << gsl_m2(1,2) << "\n" … … 87 81 } 88 82 else 89 *error<< "Ok" << std::endl;83 suite.err() << "Ok" << std::endl; 90 84 91 *error<< "MatrixLookup::MatrixLookup(const utility::Matrix&,\n"85 suite.err() << "MatrixLookup::MatrixLookup(const utility::Matrix&,\n" 92 86 << " const std::vector<size_t>&,\n" 93 87 << " const bool)..."; … … 96 90 if (m3.rows()!=1 || m3.columns()!=gsl_m2.columns() || m3(0,0)!=gsl_m2(1,0) || 97 91 m3(0,1)!=gsl_m2(1,1) || m3(0,2)!=gsl_m2(1,2) || m3(0,3)!=gsl_m2(1,3)) { 98 ok =false;99 *error<< "ERROR:" << std::endl;100 *error<< "m3.rows(): " << m3.rows() << " expected 1" << std::endl;101 *error<< "m3.columns(): " << m3.columns() << " expected "92 suite.add(false); 93 suite.err() << "ERROR:" << std::endl; 94 suite.err() << "m3.rows(): " << m3.rows() << " expected 1" << std::endl; 95 suite.err() << "m3.columns(): " << m3.columns() << " expected " 102 96 << gsl_m2.columns() << std::endl; 103 97 } 104 98 else 105 *error<< "Ok" << std::endl;99 suite.err() << "Ok" << std::endl; 106 100 107 *error<< "MatrixLookup::MatrixLookup(const MatrixLookup&)...";101 suite.err() << "MatrixLookup::MatrixLookup(const MatrixLookup&)..."; 108 102 classifier::MatrixLookup m4(m2); 109 103 if (m4.rows()!=m2.rows() || m4.columns()!=m2.rows() || m4(0,0)!=m2(0,0) || 110 104 m4(0,1)!=m2(0,1) || m4(1,0)!=m2(1,0) || m4(1,1)!=m2(1,1) ) { 111 ok =false;112 *error<< "ERROR:" << std::endl;105 suite.add(false); 106 suite.err() << "ERROR:" << std::endl; 113 107 } 114 108 else 115 *error<< "Ok" << std::endl;109 suite.err() << "Ok" << std::endl; 116 110 117 *error<< "MatrixLookup::MatrixLookup(const MatrixLookup& data\n"111 suite.err() << "MatrixLookup::MatrixLookup(const MatrixLookup& data\n" 118 112 << " const std::vector<size_t>&,\n" 119 113 << " const std::vector<size_t>&)..."; 120 114 classifier::MatrixLookup m5(m2,utility::Index(one),utility::Index(one)); 121 115 if (m5.rows()!=1 || m5.columns()!=1 || m5(0,0)!=m2(1,1) ) { 122 ok =false;123 *error<< "ERROR:" << std::endl;124 *error<< "MatrixLookup is " << m5(0,0) << " expected " << m2(1,1)116 suite.add(false); 117 suite.err() << "ERROR:" << std::endl; 118 suite.err() << "MatrixLookup is " << m5(0,0) << " expected " << m2(1,1) 125 119 << std::endl; 126 120 } 127 121 else 128 *error<< "Ok" << std::endl;122 suite.err() << "Ok" << std::endl; 129 123 130 *error<< "MatrixLookup::MatrixLookup(const MatrixLookup&,\n"124 suite.err() << "MatrixLookup::MatrixLookup(const MatrixLookup&,\n" 131 125 << " const std::vector<size_t>&,\n" 132 126 << " const bool)..."; … … 134 128 if (m6.rows()!=1 || m6.columns()!=m2.columns() || m6(0,0)!=m2(1,0) || 135 129 m6(0,1)!=m2(1,1)) { 136 ok =false;137 *error<< "ERROR:" << std::endl;130 suite.add(false); 131 suite.err() << "ERROR:" << std::endl; 138 132 } 139 133 else 140 *error<< "Ok" << std::endl;134 suite.err() << "Ok" << std::endl; 141 135 142 *error<< "MatrixLookup::MatrixLookup(const size_t,const size_t,\n"136 suite.err() << "MatrixLookup::MatrixLookup(const size_t,const size_t,\n" 143 137 << " const double)..."; 144 138 classifier::MatrixLookup m7(103,112,12); 145 139 if (m7.rows()!=103 || m7.columns()!=112 || m7(0,0)!=12) { 146 ok =false;147 *error<< "ERROR:" << std::endl;140 suite.add(false); 141 suite.err() << "ERROR:" << std::endl; 148 142 } 149 143 else 150 *error<< "Ok" << std::endl;144 suite.err() << "Ok" << std::endl; 151 145 152 146 153 *error<< "MatrixLookup::training_data(const std::vector<size_t>)...";147 suite.err() << "MatrixLookup::training_data(const std::vector<size_t>)..."; 154 148 const classifier::MatrixLookup* TrnData = 155 149 new MatrixLookup(m2, utility::Index(one), false); 156 150 if (TrnData->rows() != m2.rows() || TrnData->columns()!=one.size()){ 157 ok =false;158 *error<< "ERROR:" << std::endl;151 suite.add(false); 152 suite.err() << "ERROR:" << std::endl; 159 153 } 160 154 else 161 *error<< "Ok" << std::endl;155 suite.err() << "Ok" << std::endl; 162 156 delete TrnData; 163 157 164 *error<< "MatrixLookup::validation_data(const std::vector<size_t>,\n"158 suite.err() << "MatrixLookup::validation_data(const std::vector<size_t>,\n" 165 159 << " const std::vector<size_t>)..."; 166 160 std::vector<size_t> val(23,2); … … 168 162 new MatrixLookup(m2, utility::Index(val), false); 169 163 if (ValData->rows() != m2.rows() || TrnData->columns()!=val.size()){ 170 ok =false;171 *error<< "ERROR:" << std::endl;164 suite.add(false); 165 suite.err() << "ERROR:" << std::endl; 172 166 } 173 167 else 174 *error<< "Ok" << std::endl;168 suite.err() << "Ok" << std::endl; 175 169 delete ValData; 176 170 177 if (ok) 178 *error << "Test Ok." << std::endl; 179 if (error!=&std::cerr) 180 delete error; 181 return (ok ? 0 : -1); 171 return suite.return_value(); 182 172 } 183 173 -
trunk/test/matrix_test.cc
r1151 r1240 24 24 */ 25 25 26 #include "Suite.h" 27 26 28 #include "yat/utility/Matrix.h" 27 29 … … 46 48 47 49 48 int main( const int argc,constchar* argv[])50 int main(int argc, char* argv[]) 49 51 { 50 52 using namespace theplu::yat; 51 std::ostream* error; 52 if (argc>1 && argv[1]==std::string("-v")) 53 error = &std::cerr; 54 else { 55 error = new std::ofstream("/dev/null"); 56 if (argc>1) 57 std::cout << "matrix_test -v : for printing extra information\n"; 58 } 59 60 *error << "Testing matrix class" << std::endl; 61 bool ok = true; 53 test::Suite suite(argc, argv); 54 55 suite.err() << "Testing matrix class" << std::endl; 62 56 utility::Matrix unit3x3(3,3); 63 57 for (size_t i=0; i<unit3x3.rows(); ++i) 64 58 unit3x3(i,i)=1; 65 59 66 *error<< "\tcopy constructor and operator!=" << std::endl;60 suite.err() << "\tcopy constructor and operator!=" << std::endl; 67 61 utility::Matrix m(3,3,9); 68 62 utility::Matrix m2(m); 69 63 if (m2!=m) 70 ok=false;71 72 *error<< "\toutput operator and istream constructor" << std::endl;64 suite.add(false); 65 66 suite.err() << "\toutput operator and istream constructor" << std::endl; 73 67 // Checking that the matrix output operator writes a file that the 74 68 // input operator can read. … … 80 74 is.close(); 81 75 if (m3!=m2) 82 ok=false;76 suite.add(false); 83 77 std::remove("data/tmp_test_matrix.txt"); 84 78 85 *error<< "\toperator*=(double)" << std::endl;79 suite.err() << "\toperator*=(double)" << std::endl; 86 80 utility::Matrix m4(3,3,1); 87 81 m4 *= 9; 88 82 if (m4!=m) { 89 ok=false;90 *error<< "error operator*=(double)" << std::endl;83 suite.add(false); 84 suite.err() << "error operator*=(double)" << std::endl; 91 85 } 92 86 … … 105 99 // a non-view matrix is copied 106 100 { 107 *error<< "\tcopy constructor" << std::endl;101 suite.err() << "\tcopy constructor" << std::endl; 108 102 utility::Matrix m2(m5); 109 ok &=(m2.rows()==m5.rows());110 ok &=(m2.columns()==m5.columns());111 ok &=(m2==m5);112 ok &=(&m2 != &m5);113 } 114 115 *error<< "\tsub-(row)vector" << std::endl;103 suite.add(m2.rows()==m5.rows()); 104 suite.add(m2.columns()==m5.columns()); 105 suite.add(m2==m5); 106 suite.add(&m2 != &m5); 107 } 108 109 suite.err() << "\tsub-(row)vector" << std::endl; 116 110 // Checking that the row view works, i.e. mutation to the view are 117 111 // reflected in the viewed object. … … 131 125 m5_sum3+=m5(i,j); 132 126 if (m5_sum3-m5_sum2+v5subrow_sum>1e-13) { 133 ok=false;134 *error<< "error sub-vector test 1" << std::endl;135 } 136 137 *error<< "\tsub-(column)vector" << std::endl;127 suite.add(false); 128 suite.err() << "error sub-vector test 1" << std::endl; 129 } 130 131 suite.err() << "\tsub-(column)vector" << std::endl; 138 132 // Checking that the column view works, i.e. mutation to the view 139 133 // are reflected in the viewed object. … … 153 147 m5_sum4+=m5(i,j); 154 148 if (m5_sum4-m5_sum3-v5subcolumn.size()+v5subcolumn_sum>1e-13) { 155 ok=false;156 *error<< "error sub-vector test 2" << std::endl;149 suite.add(false); 150 suite.err() << "error sub-vector test 2" << std::endl; 157 151 } 158 152 … … 163 157 v5subrow_sum2+=v5subrow(i); 164 158 if (v5subrow_sum2-v5subcolumn(3)>1e-13) { 165 ok=false;166 *error<< "error sub-vector test 3" << std::endl;167 } 168 169 *error<< "\tsub-vector and vector copying" << std::endl;159 suite.add(false); 160 suite.err() << "error sub-vector test 3" << std::endl; 161 } 162 163 suite.err() << "\tsub-vector and vector copying" << std::endl; 170 164 // Checking that a view is not inherited through the copy 171 165 // contructor. … … 176 170 v5subrow_sum3+=v5subrow(i); 177 171 if (v5subrow_sum3-v5subrow_sum2>1e-13) { 178 ok=false;179 *error<< "error sub-vector test 4" << std::endl;172 suite.add(false); 173 suite.err() << "error sub-vector test 4" << std::endl; 180 174 } 181 175 // Checking that values in a vector is copied into a viewed matrix. … … 186 180 m5_sum5+=m5(i,j); 187 181 if (m5_sum5-m5_sum4-v5subrow.size()*2+v5subrow_sum3>1e-13) { 188 ok=false;189 *error<< "error sub-vector test 5" << std::endl;182 suite.add(false); 183 suite.err() << "error sub-vector test 5" << std::endl; 190 184 } 191 185 192 186 // Checking that the memberfunction matrixwrapper::row() returns a 193 187 // view 194 *error<< "\tthat class member returns a view" << std::endl;188 suite.err() << "\tthat class member returns a view" << std::endl; 195 189 matrixwrapper mw(5,2); 196 190 utility::VectorView mwrow=mw.row(2); 197 191 if (mwrow.gsl_vector_p()->data != &(mw.matrix()(2,0))) { 198 ok=false;199 *error<< "error sub-vector test 7" << std::endl;200 } 201 202 *error<< "\tmatrix::nan()" << std::endl;192 suite.add(false); 193 suite.err() << "error sub-vector test 7" << std::endl; 194 } 195 196 suite.err() << "\tmatrix::nan()" << std::endl; 203 197 is.open("data/sorlie_centroid_data.txt"); 204 198 utility::Matrix* m_nan = new utility::Matrix(is,'\t'); … … 207 201 is.close(); 208 202 if (m_weight(0,0)!=1){ 209 *error<< "error in matrix::nan(): element(0,0) is " << m_weight(0,0)203 suite.err() << "error in matrix::nan(): element(0,0) is " << m_weight(0,0) 210 204 << " expected 1." << std::endl; 211 ok=false;205 suite.add(false); 212 206 } 213 207 if (m_weight(3,2)!=0){ 214 *error<< "error in matrix::nan(): element(3,2) is " << m_weight(3,2)208 suite.err() << "error in matrix::nan(): element(3,2) is " << m_weight(3,2) 215 209 << " expected 0." << std::endl; 216 ok=false;210 suite.add(false); 217 211 } 218 212 delete m_nan; 219 213 220 *error<< "\toperator*=(matrix&)" << std::endl;214 suite.err() << "\toperator*=(matrix&)" << std::endl; 221 215 utility::Matrix m6(unit3x3); 222 216 m6 *= m; 223 217 if (m6!=m) { 224 ok=false;225 *error<< "error operator*=(matrix) 1" << std::endl;218 suite.add(false); 219 suite.err() << "error operator*=(matrix) 1" << std::endl; 226 220 } 227 221 m6 *= unit3x3; 228 222 if (m6!=m) { 229 ok=false;230 *error<< "error operator*=(matrix) 2" << std::endl;223 suite.add(false); 224 suite.err() << "error operator*=(matrix) 2" << std::endl; 231 225 } 232 226 m6*= utility::Matrix(3,4,1.0); … … 237 231 m6*= unit3x3; 238 232 239 if (error!=&std::cerr) 240 delete error; 241 242 return (ok ? 0 : -1); 233 suite.return_value(); 243 234 }
Note: See TracChangeset
for help on using the changeset viewer.