Changeset 456 for library/tests
- Timestamp:
- 07/31/09 08:38:18 (15 years ago)
- Location:
- library/tests
- Files:
-
- 22 modified
Legend:
- Unmodified
- Added
- Removed
-
library/tests/egamma_test.cpp
r446 r456 1 1 #define BDMLIB // not an ideal way to prevent double registration of UI factories... 2 #include "base/bdmbase.h"3 2 #include "base/user_info.h" 4 3 #include "stat/exp_family.h" 5 #include "itpp_ext.h"6 4 #include "epdf_harness.h" 7 #include "mat_checks.h"8 5 #include "UnitTest++.h" 9 6 … … 15 12 16 13 TEST(test_egamma) { 17 RV::clear_all(); 18 UIFile in("egamma.cfg"); 19 Array<epdf_harness *> input; 20 UI::get(input, in, "data"); 21 int sz = input.size(); 22 CHECK(sz > 0); 23 for (int i = 0; i < sz; ++i) { 24 input(i)->test(); 25 } 14 epdf_harness::test_config("egamma.cfg"); 26 15 } -
library/tests/egiw.cfg
r454 r456 13 13 }; 14 14 mean = [ 1.1, 0.1 ]; 15 lognc = 11.45368;15 lognc = 3.39463; 16 16 variance = [ 0.01, 8e-05 ]; 17 17 support = ( "matrix", 2, 2, [ -2.0, 4.0, 0.01, 2.0 ] ); 18 18 nbins = [ 100, 200 ]; 19 19 integral = 1.0; 20 tolerance = 0.2; 20 21 }, 21 22 { -
library/tests/egiw_harness.cpp
r440 r456 4 4 #include "test_util.h" 5 5 #include "UnitTest++.h" 6 7 const double epsilon = 0.00001;8 9 namespace UnitTest10 {11 12 inline void CheckClose(TestResults &results, const itpp::vec &expected,13 const itpp::vec &actual, double tolerance,14 TestDetails const &details) {15 if (!AreClose(expected, actual, tolerance)) {16 MemoryOutStream stream;17 stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual;18 19 results.OnTestFailure(details, stream.GetText());20 }21 }22 23 inline void CheckClose(TestResults &results, const itpp::mat &expected,24 const itpp::mat &actual, double tolerance,25 TestDetails const &details) {26 if (!AreClose(expected, actual, tolerance)) {27 MemoryOutStream stream;28 stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual;29 30 results.OnTestFailure(details, stream.GetText());31 }32 }33 34 }35 6 36 7 namespace bdm { … … 41 12 } 42 13 43 void egiw_harness::test( )14 void egiw_harness::test(const char *config_name, int idx) 44 15 { 45 epdf_harness::test(); 16 epdf_harness::test(config_name, idx); 17 18 CurrentContext cc(config_name, idx); 46 19 47 20 egiw *wide = dynamic_cast<egiw *>(get_epdf()); 48 21 CHECK(wide); 49 22 50 CHECK_CLOSE (lognc, wide->lognc(), epsilon);23 CHECK_CLOSE_EX(lognc, wide->lognc(), get_tolerance()); 51 24 } 52 25 -
library/tests/egiw_harness.h
r439 r456 27 27 egiw_harness():lognc(0) { } 28 28 29 void test( );29 void test(const char *config_name, int idx); 30 30 31 31 void from_setting(const Setting &set); -
library/tests/egiw_test.cpp
r440 r456 11 11 const double epsilon = 0.00001; 12 12 13 namespace UnitTest14 {15 16 inline void CheckClose(TestResults &results, const itpp::vec &expected,17 const itpp::vec &actual, double tolerance,18 TestDetails const &details) {19 if (!AreClose(expected, actual, tolerance)) {20 MemoryOutStream stream;21 stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual;22 23 results.OnTestFailure(details, stream.GetText());24 }25 }26 27 }28 29 13 using namespace bdm; 30 14 … … 38 22 39 23 TEST(test_egiw) { 40 RV::clear_all(); 41 UIFile in("egiw.cfg"); 42 Array<epdf_harness *> input; 43 UI::get(input, in, "data"); 44 int sz = input.size(); 45 CHECK(sz > 0); 46 for (int i = 0; i < sz; ++i) { 47 input(i)->test(); 48 } 24 epdf_harness::test_config("egiw.cfg"); 49 25 } 50 26 -
library/tests/enorm.cfg
r454 r456 21 21 names = ( "x" ); 22 22 }; 23 23 tolerance = 0.2; 24 24 }, 25 25 { … … 41 41 nbins = [ 200, 200 ]; 42 42 integral = 1.0; 43 tolerance = 0.5; 43 44 }, 44 45 { … … 58 59 variance = [ 1 ]; 59 60 R = 1.0; 61 tolerance = 0.5; 60 62 }, 61 63 { … … 105 107 variance = [ 2.0, 0.5 ]; 106 108 R = ( "matrix", 2, 2, [ 2.0, 0.0, 0.0, 0.5 ] ); 109 tolerance = 0.5; 107 110 } ); 108 111 -
library/tests/enorm_test.cpp
r454 r456 2 2 #include "stat/exp_family.h" 3 3 #include "epdf_harness.h" 4 #include "mat_checks.h"5 #include "test_util.h"6 4 #include "UnitTest++.h" 7 5 … … 9 7 10 8 const double epsilon = 0.00001; 11 12 namespace UnitTest13 {14 15 inline void CheckClose(TestResults &results, const itpp::vec &expected,16 const itpp::vec &actual, double tolerance,17 TestDetails const& details) {18 if (!AreClose(expected, actual, tolerance)) {19 MemoryOutStream stream;20 stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual;21 22 results.OnTestFailure(details, stream.GetText());23 }24 }25 26 inline void CheckClose(TestResults &results, const itpp::mat &expected,27 const itpp::mat &actual, double tolerance,28 TestDetails const& details) {29 if (!AreClose(expected, actual, tolerance)) {30 MemoryOutStream stream;31 stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual;32 33 results.OnTestFailure(details, stream.GetText());34 }35 }36 37 }38 9 39 10 template<> … … 50 21 51 22 TEST(test_enorm) { 52 RV::clear_all(); 53 UIFile in("enorm.cfg"); 54 Array<epdf_harness *> input; 55 UI::get(input, in, "data"); 56 int sz = input.size(); 57 CHECK(sz > 0); 58 for (int i = 0; i < sz; ++i) { 59 input(i)->test(); 60 } 23 epdf_harness::test_config("enorm.cfg"); 61 24 } -
library/tests/epdf_harness.cpp
r447 r456 1 1 #include "epdf_harness.h" 2 #include "base/bdmbase.h" 3 #include "base/user_info.h" 2 4 #include "stat/exp_family.h" 3 5 #include "mat_checks.h" 4 6 #include "test_util.h" 5 7 #include "UnitTest++.h" 6 7 const double epsilon = 0.00001;8 9 namespace UnitTest10 {11 12 inline void CheckClose(TestResults &results, const itpp::vec &expected,13 const itpp::vec &actual, double tolerance,14 TestDetails const &details) {15 if (!AreClose(expected, actual, tolerance)) {16 MemoryOutStream stream;17 stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual;18 19 results.OnTestFailure(details, stream.GetText());20 }21 }22 23 inline void CheckClose(TestResults &results, const itpp::mat &expected,24 const itpp::mat &actual, double tolerance,25 TestDetails const &details) {26 if (!AreClose(expected, actual, tolerance)) {27 MemoryOutStream stream;28 stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual;29 30 results.OnTestFailure(details, stream.GetText());31 }32 }33 34 }35 8 36 9 namespace bdm { … … 39 12 const ParticularUI<epdf_harness> &ParticularUI<epdf_harness>::factory( 40 13 ParticularUI<epdf_harness>("epdf_harness")); 14 15 void epdf_harness::test_config(const char *config_file_name) { 16 RV::clear_all(); 17 18 UIFile in(config_file_name); 19 Array<epdf_harness *> input; 20 UI::get(input, in, "data"); 21 int sz = input.size(); 22 CHECK(sz > 0); 23 for (int i = 0; i < sz; ++i) { 24 input(i)->test(config_file_name, i); 25 } 26 } 41 27 42 28 void epdf_harness::from_setting(const Setting &set) { … … 64 50 UI::get(R, set, "R"); 65 51 } 52 53 if (set.exists("tolerance")) { 54 UI::get(tolerance, set, "tolerance"); 55 } 66 56 } 67 57 68 void epdf_harness::test( )58 void epdf_harness::test(const char *config_name, int idx) 69 59 { 70 CHECK_CLOSE(mean, hepdf->mean(), epsilon); 71 CHECK_CLOSE(variance, hepdf->variance(), epsilon); 60 CurrentContext cc(config_name, idx); 61 62 CHECK_CLOSE_EX(mean, hepdf->mean(), tolerance); 63 CHECK_CLOSE_EX(variance, hepdf->variance(), tolerance); 72 64 73 65 if (support.rows() == 2) { … … 85 77 } 86 78 87 CHECK_CLOSE (mean, num_mean2(hepdf.get(), xb, yb, nbins(0), nbins(1)), 0.1);88 CHECK_CLOSE (integral, normcoef(hepdf.get(), xb, yb, nbins(0), nbins(1)), 0.1);79 CHECK_CLOSE_EX(mean, num_mean2(hepdf.get(), xb, yb, nbins(0), nbins(1)), 0.1); 80 CHECK_CLOSE_EX(integral, normcoef(hepdf.get(), xb, yb, nbins(0), nbins(1)), 0.1); 89 81 } 90 82 … … 94 86 vec Emu = smp * ones(n) / n; 95 87 mat Er = (smp*smp.transpose())/n - outer_product(Emu,Emu); 96 CHECK_CLOSE (mean, Emu, 0.1);97 CHECK_CLOSE (R, Er, 0.1);88 CHECK_CLOSE_EX(mean, Emu, tolerance); 89 CHECK_CLOSE_EX(R, Er, tolerance); 98 90 } 99 91 } -
library/tests/epdf_harness.h
r442 r456 36 36 double integral; 37 37 mat R; 38 double tolerance; 38 39 39 40 public: 40 epdf_harness():nsamples(1000), integral(0) { }41 static void test_config(const char *config_file_name); 41 42 42 virtual void test(); 43 epdf_harness():nsamples(1000), integral(0), tolerance(0.1) { } 44 45 virtual void test(const char *config_name, int idx); 43 46 44 47 void from_setting(const Setting &set); … … 46 49 protected: 47 50 epdf *get_epdf() { return hepdf.get(); } 51 52 double get_tolerance() const { return tolerance; } 48 53 }; 49 54 -
library/tests/mat_checks.cpp
r428 r456 37 37 38 38 } 39 40 const char *CurrentContext::config_name = "???"; 41 42 int CurrentContext::index = -1; 43 44 CurrentContext::CurrentContext(const char *name, int idx) 45 { 46 config_name = name; 47 index = idx; 48 } 49 50 CurrentContext::~CurrentContext() 51 { 52 config_name = "???"; 53 index = -1; 54 } -
library/tests/mat_checks.h
r428 r456 19 19 namespace UnitTest 20 20 { 21 bool AreClose(const itpp::vec &expected, const itpp::vec &actual,22 double tolerance);23 21 24 bool AreClose(const itpp::mat &expected, const itpp::mat &actual, 25 double tolerance); 22 bool AreClose(const itpp::vec &expected, const itpp::vec &actual, 23 double tolerance); 24 25 bool AreClose(const itpp::mat &expected, const itpp::mat &actual, 26 double tolerance); 27 28 inline void CheckClose(TestResults &results, const itpp::vec &expected, 29 const itpp::vec &actual, double tolerance, 30 TestDetails const &details) { 31 if (!AreClose(expected, actual, tolerance)) { 32 MemoryOutStream stream; 33 stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual; 34 35 results.OnTestFailure(details, stream.GetText()); 36 } 26 37 } 27 38 39 inline void CheckClose(TestResults &results, const itpp::mat &expected, 40 const itpp::mat &actual, double tolerance, 41 TestDetails const &details) { 42 if (!AreClose(expected, actual, tolerance)) { 43 MemoryOutStream stream; 44 stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual; 45 46 results.OnTestFailure(details, stream.GetText()); 47 } 48 } 49 50 } 51 52 /*! CHECK_CLOSE_EX macro should be used only in blocks having an 53 instance of this class (which sets the globals for error 54 reporting). */ 55 class CurrentContext 56 { 57 private: 58 static const char *config_name; 59 static int index; 60 61 public: 62 // the pointer must stay valid for the lifetime of the object 63 CurrentContext(const char *name, int idx); 64 ~CurrentContext(); 65 66 template< typename Expected, typename Actual, typename Tolerance > 67 static void CheckCloseEx(UnitTest::TestResults& results, 68 Expected const& expected, 69 Actual const& actual, 70 Tolerance const& tolerance, 71 UnitTest::TestDetails const& details) { 72 if (!UnitTest::AreClose(expected, actual, tolerance)) { 73 UnitTest::MemoryOutStream stream; 74 stream << "error at " << config_name << '[' << index << "]: expected " << expected << " +/- " << tolerance << " but was " << actual; 75 76 results.OnTestFailure(details, stream.GetText()); 77 } 78 } 79 }; 80 81 #define CHECK_CLOSE_EX(expected, actual, tolerance) \ 82 do \ 83 { \ 84 try { \ 85 CurrentContext::CheckCloseEx(*UnitTest::CurrentTest::Results(), expected, actual, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), 0, false)); \ 86 } \ 87 catch (...) { \ 88 UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ 89 "Unhandled exception in CHECK_CLOSE(" #expected ", " #actual ")"); \ 90 } \ 91 } while (0) 92 28 93 #endif -
library/tests/mepdf.cfg
r454 r456 18 18 mean = [ 1.1, -1.0 ]; 19 19 R = ( "matrix", 2, 2, [ 1.0, -0.5, -0.5, 2.0 ] ); 20 tolerance = 0.3; 20 21 } ); 21 22 -
library/tests/mepdf_test.cpp
r448 r456 13 13 const double epsilon = 0.00001; 14 14 15 namespace UnitTest16 {17 18 inline void CheckClose(TestResults &results, const itpp::vec &expected,19 const itpp::vec &actual, double tolerance,20 TestDetails const& details) {21 if (!AreClose(expected, actual, tolerance)) {22 MemoryOutStream stream;23 stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual;24 25 results.OnTestFailure(details, stream.GetText());26 }27 }28 29 inline void CheckClose(TestResults &results, const itpp::mat &expected,30 const itpp::mat &actual, double tolerance,31 TestDetails const& details) {32 if (!AreClose(expected, actual, tolerance)) {33 MemoryOutStream stream;34 stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual;35 36 results.OnTestFailure(details, stream.GetText());37 }38 }39 40 }41 42 15 TEST(test_mepdf) { 43 RV::clear_all(); 44 UIFile in("mepdf.cfg"); 45 Array<mpdf_harness *> input; 46 UI::get(input, in, "data"); 47 int sz = input.size(); 48 CHECK(sz > 0); 49 for (int i = 0; i < sz; ++i) { 50 input(i)->test(); 51 } 16 mpdf_harness::test_config("mepdf.cfg"); 52 17 } 53 18 -
library/tests/mgamma.cfg
r454 r456 20 20 mean = [ 1.5, 1.7 ]; 21 21 R = ( "matrix", 2, 2, [ 2.25, 0.0, 0.0, 2.89 ] ); 22 tolerance = 0.5; 22 23 } ); 23 24 -
library/tests/mgamma_test.cpp
r447 r456 15 15 16 16 TEST(test_mgamma) { 17 RV::clear_all(); 18 UIFile in("mgamma.cfg"); 19 Array<mpdf_harness *> input; 20 UI::get(input, in, "data"); 21 int sz = input.size(); 22 CHECK(sz > 0); 23 for (int i = 0; i < sz; ++i) { 24 input(i)->test(); 25 } 17 mpdf_harness::test_config("mgamma.cfg"); 26 18 } -
library/tests/mpdf_harness.cpp
r447 r456 1 1 #include "mpdf_harness.h" 2 #include "base/bdmbase.h" 3 #include "base/user_info.h" 2 4 #include "stat/exp_family.h" 3 5 #include "mat_checks.h" 4 6 #include "test_util.h" 5 7 #include "UnitTest++.h" 6 7 const double epsilon = 0.00001;8 9 namespace UnitTest10 {11 12 inline void CheckClose(TestResults &results, const itpp::vec &expected,13 const itpp::vec &actual, double tolerance,14 TestDetails const &details) {15 if (!AreClose(expected, actual, tolerance)) {16 MemoryOutStream stream;17 stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual;18 19 results.OnTestFailure(details, stream.GetText());20 }21 }22 23 inline void CheckClose(TestResults &results, const itpp::mat &expected,24 const itpp::mat &actual, double tolerance,25 TestDetails const &details) {26 if (!AreClose(expected, actual, tolerance)) {27 MemoryOutStream stream;28 stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual;29 30 results.OnTestFailure(details, stream.GetText());31 }32 }33 34 }35 8 36 9 namespace bdm { … … 39 12 const ParticularUI<mpdf_harness> &ParticularUI<mpdf_harness>::factory( 40 13 ParticularUI<mpdf_harness>("mpdf_harness")); 14 15 void mpdf_harness::test_config(const char *config_file_name) { 16 RV::clear_all(); 17 18 UIFile in(config_file_name); 19 Array<mpdf_harness *> input; 20 UI::get(input, in, "data"); 21 int sz = input.size(); 22 CHECK(sz > 0); 23 for (int i = 0; i < sz; ++i) { 24 input(i)->test(config_file_name, i); 25 } 26 } 41 27 42 28 void mpdf_harness::from_setting(const Setting &set) { … … 50 36 51 37 UI::get(R, set, "R"); 38 39 if (set.exists("tolerance")) { 40 UI::get(tolerance, set, "tolerance"); 41 } 52 42 } 53 43 54 void mpdf_harness::test( )44 void mpdf_harness::test(const char *config_name, int idx) 55 45 { 46 CurrentContext cc(config_name, idx); 47 56 48 mat smp = hmpdf->samplecond_m(cond, nsamples); 57 49 int n = smp.cols(); 58 vec Emu = smp * ones(n) / n;59 mat Er = (smp*smp.transpose())/n - outer_product(Emu,Emu);60 CHECK_CLOSE (mean, Emu, 0.1);61 CHECK_CLOSE (R, Er, 0.1);50 vec emu = smp * ones(n) / n; 51 mat er = (smp * smp.T()) / n - outer_product(emu, emu); 52 CHECK_CLOSE_EX(mean, emu, tolerance); 53 CHECK_CLOSE_EX(R, er, tolerance); 62 54 } 63 55 -
library/tests/mpdf_harness.h
r447 r456 33 33 int nsamples; 34 34 mat R; 35 double tolerance; 35 36 36 37 public: 37 mpdf_harness():nsamples(1000) { }38 static void test_config(const char *config_file_name); 38 39 39 virtual void test(); 40 mpdf_harness():nsamples(1000), tolerance(0.1) { } 41 42 virtual void test(const char *config_name, int idx); 40 43 41 44 void from_setting(const Setting &set); -
library/tests/square_mat_stress.cpp
r438 r456 1 1 #include "../bdm/math/square_mat.h" 2 2 #include "../bdm/math/chmat.h" 3 #include "mat_checks.h"4 3 #include "UnitTest++.h" 5 4 #include "TestReporterStdout.h" … … 19 18 namespace UnitTest 20 19 { 20 21 // can't include mat_checks.h because CheckClose is different in this file 22 extern bool AreClose(const itpp::vec &expected, const itpp::vec &actual, 23 double tolerance); 24 25 extern bool AreClose(const itpp::mat &expected, const itpp::mat &actual, 26 double tolerance); 21 27 22 28 void CheckClose(TestResults &results, const itpp::mat &expected, -
library/tests/square_mat_test.cpp
r455 r456 6 6 7 7 const double epsilon = 0.00001; 8 9 bool fast = false;10 11 namespace UnitTest12 {13 14 inline void CheckClose(TestResults &results, const itpp::mat &expected,15 const itpp::mat &actual, double tolerance,16 TestDetails const& details) {17 if (!AreClose(expected, actual, tolerance)) {18 MemoryOutStream stream;19 stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual;20 21 results.OnTestFailure(details, stream.GetText());22 }23 }24 25 }26 8 27 9 template<typename TMatrix> -
library/tests/unittest-cpp/TestDetails.cpp
r418 r456 3 3 namespace UnitTest { 4 4 5 TestDetails::TestDetails(char const* testName_, char const* suiteName_, char const* filename_, int lineNumber_ )5 TestDetails::TestDetails(char const* testName_, char const* suiteName_, char const* filename_, int lineNumber_, bool useFilename_) 6 6 : suiteName(suiteName_) 7 7 , testName(testName_) 8 8 , filename(filename_) 9 9 , lineNumber(lineNumber_) 10 , useFilename(useFilename_) 10 11 { 11 12 } 12 13 13 TestDetails::TestDetails(const TestDetails& details, int lineNumber_ )14 TestDetails::TestDetails(const TestDetails& details, int lineNumber_, bool useFilename_) 14 15 : suiteName(details.suiteName) 15 16 , testName(details.testName) 16 17 , filename(details.filename) 17 18 , lineNumber(lineNumber_) 19 , useFilename(useFilename_) 18 20 { 19 21 } -
library/tests/unittest-cpp/TestDetails.h
r418 r456 7 7 { 8 8 public: 9 TestDetails(char const* testName, char const* suiteName, char const* filename, int lineNumber );10 TestDetails(const TestDetails& details, int lineNumber );9 TestDetails(char const* testName, char const* suiteName, char const* filename, int lineNumber, bool useFilename = true); 10 TestDetails(const TestDetails& details, int lineNumber, bool useFilename = true); 11 11 12 12 char const* const suiteName; … … 14 14 char const* const filename; 15 15 int const lineNumber; 16 bool const useFilename; 16 17 17 18 TestDetails(TestDetails const&); // Why is it public? --> http://gcc.gnu.org/bugs.html#cxx_rvalbind -
library/tests/unittest-cpp/TestReporterStdout.cpp
r418 r456 19 19 #endif 20 20 21 using namespace std; 22 printf(errorFormat, details.filename, details.lineNumber, details.testName, failure); 21 using namespace std; 22 23 if (details.useFilename) { 24 // standard way 25 printf(errorFormat, details.filename, details.lineNumber, details.testName, failure); 26 } else { 27 // extended for BDM - the failure string already includes 28 // (custom) test location info 29 printf("%s\n", failure); 30 } 23 31 } 24 32