1 | #include "base/loggers.h" |
---|
2 | #include "../test_util.h" |
---|
3 | #include "osutils.h" |
---|
4 | #include "UnitTest++.h" |
---|
5 | #include <string> |
---|
6 | #include <stdexcept> |
---|
7 | #include <sys/stat.h> |
---|
8 | #include <sys/types.h> |
---|
9 | |
---|
10 | #ifndef WIN32 |
---|
11 | #include <unistd.h> |
---|
12 | #endif |
---|
13 | |
---|
14 | using namespace bdm; |
---|
15 | |
---|
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 | |
---|
24 | int sz = static_cast<int> ( st.st_size ); |
---|
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 | |
---|
34 | TEST ( memlog_test ) { |
---|
35 | RV th = RV ( "{alog blog }" ); |
---|
36 | RV r = RV ( "{r }", "2" ); |
---|
37 | |
---|
38 | string ls ( "memlog_test" ); |
---|
39 | remove_all ( ls.c_str() ); |
---|
40 | |
---|
41 | memlog logger ( 100, ls ); |
---|
42 | |
---|
43 | int pol = 11; |
---|
44 | |
---|
45 | int rid = logger.add_vector ( r, "" ); |
---|
46 | int thid = logger.add_vector ( th, "th" ); |
---|
47 | int polid = logger.add_setting ( "pols" ); |
---|
48 | |
---|
49 | logger.init(); |
---|
50 | |
---|
51 | for ( int i = 0; i < 10; i++ ) { |
---|
52 | logger.log_vector ( rid, vec_2 ( ( double ) i, ( double ) ( i + 1 ) ) ); |
---|
53 | logger.log_vector ( thid, vec_2 ( ( double ) ( 100 - i ), ( double ) ( i - 50 ) ) ); |
---|
54 | UI::save ( pol, logger.log_to_setting ( polid ), "polozka" ); |
---|
55 | logger.step(); |
---|
56 | } |
---|
57 | |
---|
58 | logger.finalize(); |
---|
59 | |
---|
60 | CHECK_EQUAL ( get_file_size ( "logger_test.matrix" ), get_file_size ( ( ls + ".it" ).c_str() ) ); |
---|
61 | CHECK_EQUAL ( get_file_size ( ( ls + ".cfg.check" ).c_str() ), get_file_size ( ( ls + ".cfg" ).c_str() ) ); |
---|
62 | } |
---|
63 | |
---|
64 | TEST ( dirfilelog_test ) { |
---|
65 | RV th = RV ( "{alog blog }" ); |
---|
66 | RV r = RV ( "{r }", "2" ); |
---|
67 | |
---|
68 | string ls ( "exp" ); |
---|
69 | remove_all ( ls.c_str() ); |
---|
70 | makedir ( ls, false ); |
---|
71 | remove_all ( "dirfilelog_files" ); |
---|
72 | |
---|
73 | dirfilelog L ( "dirfilelog_files", 10 ); |
---|
74 | |
---|
75 | int rid = L.add_vector ( r, "" ); |
---|
76 | int thid = L.add_vector ( th, "th" ); |
---|
77 | |
---|
78 | L.init(); |
---|
79 | |
---|
80 | for ( int i = 0; i < 150; i++ ) { |
---|
81 | L.log_vector ( rid, vec_2 ( ( double ) i, ( double ) ( i + 1 ) ) ); |
---|
82 | L.log_vector ( thid, vec_2 ( ( double ) ( 100 - i ), ( double ) ( i - 50 ) ) ); |
---|
83 | L.step(); |
---|
84 | } |
---|
85 | |
---|
86 | L.finalize(); |
---|
87 | |
---|
88 | std::string expected ( load_test_file ( "logger_test_dirfile_format.matrix" ) ); |
---|
89 | std::string actual ( load_test_file ( "dirfilelog_files/format" ) ); |
---|
90 | CHECK_EQUAL ( expected, actual ); |
---|
91 | } |
---|