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 ( test_memlog ) { |
---|
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 | |
---|
56 | CHECK_EQUAL ( get_file_size ( "test_log.matrix" ), get_file_size ( ls.c_str() ) ); |
---|
57 | } |
---|
58 | |
---|
59 | TEST ( test_dirfilelog ) { |
---|
60 | RV th = RV ( "{alog blog }" ); |
---|
61 | RV r = RV ( "{r }", "2" ); |
---|
62 | |
---|
63 | string ls ( "exp" ); |
---|
64 | remove_all ( ls.c_str() ); |
---|
65 | makedir ( ls, false ); |
---|
66 | remove_all ( "exp/dirfile" ); |
---|
67 | |
---|
68 | dirfilelog L ( "exp/dirfile", 10 ); |
---|
69 | |
---|
70 | int rid = L.add ( r, "" ); |
---|
71 | int thid = L.add ( th, "th" ); |
---|
72 | |
---|
73 | L.init(); |
---|
74 | |
---|
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 | } |
---|
80 | |
---|
81 | L.finalize(); |
---|
82 | |
---|
83 | std::string expected ( load_test_file ( "dirfile-format.matrix" ) ); |
---|
84 | std::string actual ( load_test_file ( "exp/dirfile/format" ) ); |
---|
85 | CHECK_EQUAL ( expected, actual ); |
---|
86 | } |
---|