MADNESS 0.10.1
test_utilities.h
Go to the documentation of this file.
1/*
2 * test_utilities.h
3 *
4 * Created on: 15 May 2019
5 * Author: fbischoff
6 */
7
8#ifndef SRC_APPS_CHEM_TEST_UTILITIES_H_
9#define SRC_APPS_CHEM_TEST_UTILITIES_H_
10
11
12namespace madness {
13
14/// small class for pretty printing of test output
16 /// @param[in] use as if (world.rank()==0) to avoid printing on all ranks
17 test_output(std::string line, bool print=true) {
18 if (print) std::cout << ltrim_to_length(line,70);
19 logger << std::scientific << std::setprecision(8) ;
23 }
24
25 static std::string ltrim_to_length(std::string line, long length=70) {
26 int ncharacter=line.size();
27 if (line.size()<size_t(length)) line+= std::string(length-ncharacter, ' ' );
28 return line;
29 }
30
34
37 std::cout << logger.str() << std::endl;
38 logger.clear();
39 }
40
41 void checkpoint(double error, double tol,
42 std::string message, double time=-1.0) {
43 bool use_logger=cout_set_to_logger;
45 bool success=error<tol;
46 final_success = success and final_success;
47 if (not have_checkpoints) print(""); // first checkpoint
49 std::cout << " " << ltrim_to_length(message,66);
50 double time1=cpu_time()-time_last_checkpoint;
52 print_success_fail(std::cout,success,time1,error);
53 if (not success) {
55 }
56 if (use_logger) set_cout_to_logger();
57 }
58
59 void checkpoint(double value, double reference, double tol,
60 std::string message, double time=-1.0) {
61 bool use_logger=cout_set_to_logger;
63 double error=fabs(value-reference);
64 bool success=error<tol;
65 final_success = success and final_success;
66 if (not have_checkpoints) print(""); // first checkpoint
68 std::cout << " " << ltrim_to_length(message,66);
69 double time1=cpu_time()-time_last_checkpoint;
71 print_success_fail(std::cout,success,time1,error);
72 if (not success) {
74 }
75 if (use_logger) set_cout_to_logger();
76 }
77
78 void checkpoint(bool success, std::string message, double time=-1.0) {
79 bool use_logger=cout_set_to_logger;
81 final_success = success and final_success;
82 if (not have_checkpoints) print(""); // first checkpoint
84 std::cout << " " << ltrim_to_length(message,66);
85 double time1=cpu_time()-time_last_checkpoint;
87 print_success_fail(std::cout,success,time1,-1.0);
88 if (not success) {
90 }
91 if (use_logger) set_cout_to_logger();
92 }
93
94 void print_success_fail(std::ostream& os, bool success, double time, double error) {
95
96 if (success) os << "\033[32m" << "passed " << "\033[0m";
97 else os << "\033[31m" << "failed " << "\033[0m";
98 if (time>0) {
99 std::stringstream ss;
100 ss<< " in " << std::fixed << std::setprecision(1) << time << "s";
101 os << ss.str();
102 }
103 if (error>=0.0) os << " error " << error;
104 os << std::endl;
105 }
106
107 int end(bool success=true) {
109 if (have_checkpoints) std::cout << ltrim_to_length("--> final result -->",70);
110 success = success and final_success;
111 double time_end=cpu_time();
112 print_success_fail(std::cout,success,time_end-time_begin,-1.0);
113 if (not success) print_and_clear_log();
114 return (success) ? 0 : 1;
115 }
116
118 if (cout_set_to_logger) return;
120 stream_buffer_cout = std::cout.rdbuf();
121 std::streambuf* stream_buffer_file = logger.rdbuf();
122 std::cout.rdbuf(stream_buffer_file);
123 }
124
125 /// newline for use by user, not for internal use (e.g. checkpoint())
126 void set_cout_to_terminal(bool newline=true) {
127 if (not cout_set_to_logger) return;
128 if (cout_set_to_logger) {
129 std::cout.rdbuf(stream_buffer_cout);
130 }
131 cout_set_to_logger=false;
132 if (newline) std::cout << std::endl;
133 }
134
135 std::stringstream logger;
136 bool get_final_success() const {return final_success;}
137private:
138
139 bool final_success=true;
140 bool cout_set_to_logger=false; // do not change this directly!
142 std::streambuf* stream_buffer_cout;
143 double time_begin=0.0;
145};
146
147
148}
149
150#endif /* SRC_APPS_CHEM_TEST_UTILITIES_H_ */
static const double length
Definition hedft.cc:48
Namespace for all elements and tools of MADNESS.
Definition DFParameters.h:10
static double cpu_time()
Returns the cpu time in seconds relative to an arbitrary origin.
Definition timers.h:127
void print(const T &t, const Ts &... ts)
Print items to std::cout (items separated by spaces) and terminate with a new line.
Definition print.h:226
void error(const char *msg)
Definition world.cc:139
small class for pretty printing of test output
Definition test_utilities.h:15
double time_last_checkpoint
Definition test_utilities.h:144
int end(bool success=true)
Definition test_utilities.h:107
void checkpoint(double value, double reference, double tol, std::string message, double time=-1.0)
Definition test_utilities.h:59
double time_begin
Definition test_utilities.h:143
static std::string ltrim_to_length(std::string line, long length=70)
Definition test_utilities.h:25
void checkpoint(bool success, std::string message, double time=-1.0)
Definition test_utilities.h:78
std::stringstream logger
Definition test_utilities.h:135
void set_cout_to_terminal(bool newline=true)
newline for use by user, not for internal use (e.g. checkpoint())
Definition test_utilities.h:126
test_output(std::string line, bool print=true)
Definition test_utilities.h:17
~test_output()
Definition test_utilities.h:31
bool get_final_success() const
Definition test_utilities.h:136
std::streambuf * stream_buffer_cout
Definition test_utilities.h:142
void print_and_clear_log()
Definition test_utilities.h:35
void checkpoint(double error, double tol, std::string message, double time=-1.0)
Definition test_utilities.h:41
bool have_checkpoints
Definition test_utilities.h:141
void print_success_fail(std::ostream &os, bool success, double time, double error)
Definition test_utilities.h:94
bool cout_set_to_logger
Definition test_utilities.h:140
bool final_success
Definition test_utilities.h:139
void set_cout_to_logger()
Definition test_utilities.h:117