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 | #include <unistd.h> |
---|
10 | |
---|
11 | using namespace bdm; |
---|
12 | |
---|
13 | int get_file_size ( const char *fname ) { |
---|
14 | struct stat st; |
---|
15 | if ( stat ( fname, &st ) ) { |
---|
16 | std::string msg = "can't stat "; |
---|
17 | msg += fname; |
---|
18 | throw std::runtime_error ( msg ); |
---|
19 | } |
---|
20 | |
---|
21 | int sz = static_cast<int>(st.st_size); |
---|
22 | if ( sz != st.st_size ) { |
---|
23 | std::string msg = fname; |
---|
24 | msg += " too big"; |
---|
25 | throw std::runtime_error ( msg ); |
---|
26 | } |
---|
27 | |
---|
28 | return sz; |
---|
29 | } |
---|
30 | |
---|
31 | TEST ( test_memlog ) { |
---|
32 | RV th = RV ( "{alog blog }" ); |
---|
33 | RV r = RV ( "{r }", "2" ); |
---|
34 | |
---|
35 | string ls ( "test_log.bin" ); |
---|
36 | remove_all ( ls.c_str() ); |
---|
37 | |
---|
38 | memlog logger ( 100, ls ); |
---|
39 | |
---|
40 | int rid = logger.add ( r, "" ); |
---|
41 | int thid = logger.add ( th, "th" ); |
---|
42 | |
---|
43 | logger.init(); |
---|
44 | |
---|
45 | for ( int i = 0; i < 10; i++ ) { |
---|
46 | logger.logit ( rid, vec_2 ( ( double ) i, ( double ) ( i + 1 ) ) ); |
---|
47 | logger.logit ( thid, vec_2 ( ( double ) ( 100 - i ), ( double ) ( i - 50 ) ) ); |
---|
48 | logger.step(); |
---|
49 | } |
---|
50 | |
---|
51 | logger.finalize(); |
---|
52 | |
---|
53 | CHECK_EQUAL ( get_file_size ( "test_log.matrix" ), get_file_size ( ls.c_str() ) ); |
---|
54 | } |
---|
55 | |
---|
56 | TEST ( test_dirfilelog ) { |
---|
57 | RV th = RV ( "{alog blog }" ); |
---|
58 | RV r = RV ( "{r }", "2" ); |
---|
59 | |
---|
60 | string ls ( "exp" ); |
---|
61 | remove_all ( ls.c_str() ); |
---|
62 | makedir ( ls, false ); |
---|
63 | remove_all ( "exp/dirfile" ); |
---|
64 | |
---|
65 | dirfilelog L ( "exp/dirfile", 10 ); |
---|
66 | |
---|
67 | int rid = L.add ( r, "" ); |
---|
68 | int thid = L.add ( th, "th" ); |
---|
69 | |
---|
70 | L.init(); |
---|
71 | |
---|
72 | for ( int i = 0; i < 150; i++ ) { |
---|
73 | L.logit ( rid, vec_2 ( ( double ) i, ( double ) ( i + 1 ) ) ); |
---|
74 | L.logit ( thid, vec_2 ( ( double ) ( 100 - i ), ( double ) ( i - 50 ) ) ); |
---|
75 | L.step(); |
---|
76 | } |
---|
77 | |
---|
78 | L.finalize(); |
---|
79 | |
---|
80 | std::string expected ( load_test_file ( "dirfile-format.matrix" ) ); |
---|
81 | std::string actual ( load_test_file ( "exp/dirfile/format" ) ); |
---|
82 | CHECK_EQUAL ( expected, actual ); |
---|
83 | } |
---|