00001 /****************************************************************************************************************/ 00002 /* */ 00003 /* Flood: An Open Source Neural Networks C++ Library */ 00004 /* www.cimne.com/flood */ 00005 /* */ 00006 /* U N I T T E S T I N G C L A S S */ 00007 /* */ 00008 /* Roberto Lopez */ 00009 /* International Center for Numerical Methods in Engineering (CIMNE) */ 00010 /* Technical University of Catalonia (UPC) */ 00011 /* Barcelona, Spain */ 00012 /* E-mail: rlopez@cimne.upc.edu */ 00013 /* */ 00014 /****************************************************************************************************************/ 00015 00016 #include<iostream> 00017 #include<fstream> 00018 #include<string> 00019 #include<sstream> 00020 #include <time.h> 00021 00022 #include"UnitTesting.h" 00023 00024 namespace Flood 00025 { 00026 00027 // GENERAL CONSTRUCTOR 00028 00037 00038 UnitTesting::UnitTesting(void) 00039 { 00040 message = ""; 00041 00042 tests_count = 0; 00043 tests_passed_count = 0; 00044 tests_failed_count = 0; 00045 00046 display = true; 00047 } 00048 00049 00050 // DESTRUCTOR 00051 00053 00054 UnitTesting::~UnitTesting(void) 00055 { 00056 } 00057 00058 00059 // METHODS 00060 00061 // int get_tests_count(void) method 00062 00064 00065 int UnitTesting::get_tests_count(void) 00066 { 00067 return(tests_count); 00068 } 00069 00070 00071 // int get_tests_passed_count(void) method 00072 00074 00075 int UnitTesting::get_tests_passed_count(void) 00076 { 00077 return(tests_passed_count); 00078 } 00079 00080 00081 // int get_tests_failed_count(void) method 00082 00084 00085 int UnitTesting::get_tests_failed_count(void) 00086 { 00087 return(tests_failed_count); 00088 } 00089 00090 00091 // std::string& get_message(void) method 00092 00094 00095 std::string& UnitTesting::get_message(void) 00096 { 00097 return(message); 00098 } 00099 00100 00101 // bool get_display(void) method 00102 00104 00105 bool UnitTesting::get_display(void) 00106 { 00107 return(display); 00108 } 00109 00110 00111 // void set_tests_count(int) method 00112 00115 00116 void UnitTesting::set_tests_count(int new_tests_count) 00117 { 00118 // Control sentence (if debug) 00119 00120 #ifdef _DEBUG 00121 00122 if(new_tests_count < 0) 00123 { 00124 std::cout << "Flood Warning: UnitTesting class." << std::endl 00125 << "void set_tests_count(int) method." << std::endl 00126 << "Test count must be equal or greater than zero." << std::endl; 00127 } 00128 00129 #endif 00130 00131 tests_count = new_tests_count; 00132 } 00133 00134 00135 // void set_tests_passed_count(int) method 00136 00139 00140 void UnitTesting::set_tests_passed_count(int new_tests_passed_count) 00141 { 00142 // Control sentence (if debug) 00143 00144 #ifdef _DEBUG 00145 00146 if(new_tests_passed_count < 0) 00147 { 00148 std::cout << "Flood Warning: UnitTesting class." << std::endl 00149 << "void set_tests_passed_count(int) method." << std::endl 00150 << "Test pased count must be equal or greater than zero." << std::endl; 00151 } 00152 00153 #endif 00154 00155 tests_passed_count = new_tests_passed_count; 00156 } 00157 00158 00159 // void set_tests_failed_count(int) method 00160 00163 00164 void UnitTesting::set_tests_failed_count(int new_tests_failed_count) 00165 { 00166 // Control sentence (if debug) 00167 00168 #ifdef _DEBUG 00169 00170 if(new_tests_failed_count < 0) 00171 { 00172 std::cout << "Flood Warning: UnitTesting class." << std::endl 00173 << "void set_tests_failed_count(int) method." << std::endl 00174 << "Test failed count must be equal or greater than zero." << std::endl; 00175 } 00176 #endif 00177 00178 tests_failed_count = new_tests_failed_count; 00179 } 00180 00181 00182 // void set_message(const std::string&) method 00183 00186 00187 void UnitTesting::set_message(const std::string& new_message) 00188 { 00189 message = new_message; 00190 } 00191 00192 00193 // void set_display(bool) method 00194 00197 00198 void UnitTesting::set_display(bool new_display) 00199 { 00200 display = new_display; 00201 } 00202 00203 00204 // void assert_true(bool, std::string) method 00205 00214 00215 void UnitTesting::assert_true(bool condition, const std::string& error_message) 00216 { 00217 tests_count++; 00218 00219 if(condition) 00220 { 00221 tests_passed_count++; 00222 } 00223 else 00224 { 00225 message += "void assert_true(bool, const std::string&) method failed\n"; 00226 message += error_message; 00227 tests_failed_count++; 00228 } 00229 } 00230 00231 00232 // void assert_false(bool) method 00233 00242 00243 void UnitTesting::assert_false(bool condition, const std::string& error_message) 00244 { 00245 tests_count++; 00246 00247 if(!condition) 00248 { 00249 tests_passed_count++; 00250 } 00251 else 00252 { 00253 message += "void assert_false(bool, const std::string&) method failed\n"; 00254 message += error_message; 00255 tests_failed_count++; 00256 } 00257 } 00258 00259 00260 // void print_results(void) method 00261 00270 00271 void UnitTesting::print_results(void) 00272 { 00273 run_test_case(); 00274 00275 std::cout << message << std::endl; 00276 00277 std::cout << "Tests run: " << tests_count << std::endl; 00278 std::cout << "Tests passed: " << tests_passed_count << std::endl; 00279 std::cout << "Tests failed: " << tests_failed_count << std::endl; 00280 00281 if(tests_failed_count == 0) 00282 { 00283 std::cout << "Test case OK." << std::endl; 00284 } 00285 else 00286 { 00287 std::cout << "Test case NOT OK: " << tests_failed_count << " tests failed." << std::endl; 00288 } 00289 } 00290 00291 } 00292 00293 // Flood: An Open Source Neural Networks C++ Library. 00294 // Copyright (C) 2005-2010 Roberto Lopez 00295 // 00296 // This library is free software; you can redistribute it and/or 00297 // modify it under the s of the GNU Lesser General Public 00298 // License as published by the Free Software Foundation; either 00299 // version 2.1 of the License, or any later version. 00300 // 00301 // This library is distributed in the hope that it will be useful, 00302 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00303 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00304 // Lesser General Public License for more details. 00305 00306 // You should have received a copy of the GNU Lesser General Public 00307 // License along with this library; if not, write to the Free Software 00308 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA