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 | class log_tester : public root |
---|
35 | { |
---|
36 | LOG_LEVEL(log_tester,logr,logth,logpol); |
---|
37 | public: |
---|
38 | RV th, r; |
---|
39 | int pol, i; |
---|
40 | |
---|
41 | log_tester() { |
---|
42 | th = RV ( "{alog blog }" ); |
---|
43 | r = RV ( "{r }", "2" ); |
---|
44 | pol = 11; |
---|
45 | |
---|
46 | log_level[logr] = true; |
---|
47 | log_level[logth] = true; |
---|
48 | log_level[logpol] = true; |
---|
49 | } |
---|
50 | |
---|
51 | void log_register( logger &L, const string prefix ) { |
---|
52 | root::log_register( L, prefix ); |
---|
53 | L.add_vector( log_level, logr, r, prefix ); |
---|
54 | L.add_vector( log_level, logth, th, prefix ); |
---|
55 | L.add_setting( log_level, logpol, prefix, 0 ); |
---|
56 | L.add_setting( log_level, logpol, prefix, 1 ); |
---|
57 | } |
---|
58 | |
---|
59 | void log_write() const { |
---|
60 | root::log_write(); |
---|
61 | log_level.store( logr, vec_2 ( ( double ) i, ( double ) ( i + 1 ) ) ); |
---|
62 | log_level.store( logth, vec_2 ( ( double ) ( 100 - i ), ( double ) ( i - 50 ) ) ); |
---|
63 | log_level.store( logpol, pol, 0 ); |
---|
64 | log_level.store( logpol, pol^2, 1 ); |
---|
65 | } |
---|
66 | |
---|
67 | }; |
---|
68 | |
---|
69 | TEST ( memlog_test ) { |
---|
70 | string ls ( "memlog_test" ); |
---|
71 | remove_all ( ls.c_str() ); |
---|
72 | |
---|
73 | memlog logger ( 100, ls ); |
---|
74 | |
---|
75 | |
---|
76 | log_tester tester; |
---|
77 | tester.log_register( logger, "memlog" ); |
---|
78 | |
---|
79 | logger.init(); |
---|
80 | |
---|
81 | for ( tester.i = 0; tester.i < 10; tester.i++ ) { |
---|
82 | tester.log_write(); |
---|
83 | logger.step(); |
---|
84 | } |
---|
85 | |
---|
86 | logger.finalize(); |
---|
87 | |
---|
88 | CHECK_EQUAL ( get_file_size ( "logger_test.matrix" ), get_file_size ( ( ls + ".it" ).c_str() ) ); |
---|
89 | CHECK_EQUAL ( get_file_size ( ( ls + ".cfg.check" ).c_str() ), get_file_size ( ( ls + ".cfg" ).c_str() ) ); |
---|
90 | } |
---|
91 | |
---|
92 | TEST ( dirfilelog_test ) { |
---|
93 | |
---|
94 | |
---|
95 | string ls ( "exp" ); |
---|
96 | remove_all ( ls.c_str() ); |
---|
97 | makedir ( ls, false ); |
---|
98 | remove_all ( "dirfilelog_files" ); |
---|
99 | |
---|
100 | dirfilelog logger ( "dirfilelog_files", 10 ); |
---|
101 | |
---|
102 | log_tester tester; |
---|
103 | tester.log_register( logger, "dirfilelog" ); |
---|
104 | |
---|
105 | logger.init(); |
---|
106 | |
---|
107 | for ( tester.i = 0; tester.i < 150; tester.i++ ) { |
---|
108 | tester.log_write(); |
---|
109 | logger.step(); |
---|
110 | } |
---|
111 | |
---|
112 | logger.finalize(); |
---|
113 | |
---|
114 | std::string expected ( load_test_file ( "logger_test_dirfile_format.matrix" ) ); |
---|
115 | std::string actual ( load_test_file ( "dirfilelog_files/format" ) ); |
---|
116 | CHECK_EQUAL ( expected, actual ); |
---|
117 | } |
---|