[386] | 1 | #include "base/loggers.h" |
---|
[717] | 2 | #include "../test_util.h" |
---|
[469] | 3 | #include "osutils.h" |
---|
[425] | 4 | #include "UnitTest++.h" |
---|
| 5 | #include <string> |
---|
[548] | 6 | #include <stdexcept> |
---|
[425] | 7 | #include <sys/stat.h> |
---|
| 8 | #include <sys/types.h> |
---|
[553] | 9 | |
---|
| 10 | #ifndef WIN32 |
---|
[548] | 11 | #include <unistd.h> |
---|
[553] | 12 | #endif |
---|
[87] | 13 | |
---|
[254] | 14 | using namespace bdm; |
---|
[87] | 15 | |
---|
[548] | 16 | int get_file_size ( const char *fname ) { |
---|
| 17 | struct stat st; |
---|
| 18 | if ( stat ( fname, &st ) ) { |
---|
| 19 | std::string msg = "can't stat "; |
---|
| 20 | msg += fname; |
---|
| 21 | throw std::runtime_error ( msg ); |
---|
| 22 | } |
---|
| 23 | |
---|
[722] | 24 | int sz = static_cast<int> ( st.st_size ); |
---|
[548] | 25 | if ( sz != st.st_size ) { |
---|
| 26 | std::string msg = fname; |
---|
| 27 | msg += " too big"; |
---|
| 28 | throw std::runtime_error ( msg ); |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | return sz; |
---|
| 32 | } |
---|
| 33 | |
---|
[689] | 34 | TEST ( memlog_test ) { |
---|
[548] | 35 | RV th = RV ( "{alog blog }" ); |
---|
| 36 | RV r = RV ( "{r }", "2" ); |
---|
| 37 | |
---|
| 38 | string ls ( "test_log.bin" ); |
---|
| 39 | remove_all ( ls.c_str() ); |
---|
| 40 | |
---|
| 41 | memlog logger ( 100, ls ); |
---|
| 42 | |
---|
| 43 | int rid = logger.add ( r, "" ); |
---|
| 44 | int thid = logger.add ( th, "th" ); |
---|
| 45 | |
---|
| 46 | logger.init(); |
---|
| 47 | |
---|
| 48 | for ( int i = 0; i < 10; i++ ) { |
---|
| 49 | logger.logit ( rid, vec_2 ( ( double ) i, ( double ) ( i + 1 ) ) ); |
---|
| 50 | logger.logit ( thid, vec_2 ( ( double ) ( 100 - i ), ( double ) ( i - 50 ) ) ); |
---|
| 51 | logger.step(); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | logger.finalize(); |
---|
| 55 | |
---|
[721] | 56 | CHECK_EQUAL ( get_file_size ( "logger_test.matrix" ), get_file_size ( ls.c_str() ) ); |
---|
[548] | 57 | } |
---|
| 58 | |
---|
[689] | 59 | TEST ( dirfilelog_test ) { |
---|
[477] | 60 | RV th = RV ( "{alog blog }" ); |
---|
| 61 | RV r = RV ( "{r }", "2" ); |
---|
[87] | 62 | |
---|
[477] | 63 | string ls ( "exp" ); |
---|
| 64 | remove_all ( ls.c_str() ); |
---|
| 65 | makedir ( ls, false ); |
---|
[723] | 66 | remove_all ( "dirfilelog_files" ); |
---|
[87] | 67 | |
---|
[723] | 68 | dirfilelog L ( "dirfilelog_files", 10 ); |
---|
[425] | 69 | |
---|
[548] | 70 | int rid = L.add ( r, "" ); |
---|
| 71 | int thid = L.add ( th, "th" ); |
---|
[425] | 72 | |
---|
[477] | 73 | L.init(); |
---|
[425] | 74 | |
---|
[477] | 75 | for ( int i = 0; i < 150; i++ ) { |
---|
| 76 | L.logit ( rid, vec_2 ( ( double ) i, ( double ) ( i + 1 ) ) ); |
---|
| 77 | L.logit ( thid, vec_2 ( ( double ) ( 100 - i ), ( double ) ( i - 50 ) ) ); |
---|
| 78 | L.step(); |
---|
| 79 | } |
---|
[425] | 80 | |
---|
[477] | 81 | L.finalize(); |
---|
[425] | 82 | |
---|
[721] | 83 | std::string expected ( load_test_file ( "logger_test_dirfile_format.matrix" ) ); |
---|
[723] | 84 | std::string actual ( load_test_file ( "dirfilelog_files/format" ) ); |
---|
[477] | 85 | CHECK_EQUAL ( expected, actual ); |
---|
[87] | 86 | } |
---|