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) : do_print(print) {
18 if (do_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
35 void set_do_print(bool print) {
37 }
38
41 std::cout << logger.str() << std::endl;
42 logger.clear();
43 }
44
45 void checkpoint(double error, double tol,
46 std::string message, double time=-1.0) {
47 bool use_logger=cout_set_to_logger;
49 bool success=error<tol;
50 final_success = success and final_success;
51 if (not have_checkpoints) if (do_print) print(""); // first checkpoint
53 if (do_print) std::cout << " " << ltrim_to_length(message,66);
54 double time1=cpu_time()-time_last_checkpoint;
56 print_success_fail(std::cout,success,time1,error);
57 if (not success) {
59 }
60 if (use_logger) set_cout_to_logger();
61 }
62
63 void checkpoint(double value, double reference, double tol,
64 std::string message, double time=-1.0) {
65 bool use_logger=cout_set_to_logger;
67 double error=fabs(value-reference);
68 bool success=error<tol;
69 final_success = success and final_success;
70 if (not have_checkpoints and do_print) print(""); // first checkpoint
72 if (do_print) std::cout << " " << ltrim_to_length(message,66);
73 double time1=cpu_time()-time_last_checkpoint;
75 print_success_fail(std::cout,success,time1,error);
76 if (not success) {
78 }
79 if (use_logger) set_cout_to_logger();
80 }
81
82 void checkpoint(bool success, std::string message, double time=-1.0) {
83 bool use_logger=cout_set_to_logger;
85 final_success = success and final_success;
86 if (not have_checkpoints and do_print) print(""); // first checkpoint
88 if (do_print) std::cout << " " << ltrim_to_length(message,66);
89 double time1=cpu_time()-time_last_checkpoint;
91 print_success_fail(std::cout,success,time1,-1.0);
92 if (not success) {
94 }
95 if (use_logger) set_cout_to_logger();
96 }
97
98 void print_success_fail(std::ostream& os, bool success, double time, double error) const {
99
100 if (do_print) {
101 if (success) os << "\033[32m" << "passed " << "\033[0m";
102 else os << "\033[31m" << "failed " << "\033[0m";
103 if (time>0) {
104 std::stringstream ss;
105 ss<< " in " << std::fixed << std::setprecision(1) << time << "s";
106 os << ss.str();
107 }
108 if (error>=0.0) os << " error " << error;
109 os << std::endl;
110 }
111 }
112
113 int end(bool success=true) {
115 if (do_print and have_checkpoints) std::cout << ltrim_to_length("--> final result -->",70);
116 success = success and final_success;
117 double time_end=cpu_time();
118 print_success_fail(std::cout,success,time_end-time_begin,-1.0);
119 if (not success) print_and_clear_log();
120 return (success) ? 0 : 1;
121 }
122
124 if (cout_set_to_logger) return;
126 stream_buffer_cout = std::cout.rdbuf();
127 std::streambuf* stream_buffer_file = logger.rdbuf();
128 std::cout.rdbuf(stream_buffer_file);
129 }
130
131 /// newline for use by user, not for internal use (e.g. checkpoint())
132 void set_cout_to_terminal(bool newline=true) {
133 if (not cout_set_to_logger) return;
134 if (cout_set_to_logger) {
135 std::cout.rdbuf(stream_buffer_cout);
136 }
137 cout_set_to_logger=false;
138 if (newline) std::cout << std::endl;
139 }
140
141 std::stringstream logger;
142 bool get_final_success() const {return final_success;}
143private:
144
145 bool final_success=true;
146 bool cout_set_to_logger=false; // do not change this directly!
148 std::streambuf* stream_buffer_cout;
149 double time_begin=0.0;
151 bool do_print=true; // if run on several ranks
152};
153
154
155}
156
157#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:150
int end(bool success=true)
Definition test_utilities.h:113
void checkpoint(double value, double reference, double tol, std::string message, double time=-1.0)
Definition test_utilities.h:63
double time_begin
Definition test_utilities.h:149
static std::string ltrim_to_length(std::string line, long length=70)
Definition test_utilities.h:25
bool do_print
Definition test_utilities.h:151
void checkpoint(bool success, std::string message, double time=-1.0)
Definition test_utilities.h:82
std::stringstream logger
Definition test_utilities.h:141
void set_cout_to_terminal(bool newline=true)
newline for use by user, not for internal use (e.g. checkpoint())
Definition test_utilities.h:132
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:142
std::streambuf * stream_buffer_cout
Definition test_utilities.h:148
void print_and_clear_log()
Definition test_utilities.h:39
void checkpoint(double error, double tol, std::string message, double time=-1.0)
Definition test_utilities.h:45
bool have_checkpoints
Definition test_utilities.h:147
void print_success_fail(std::ostream &os, bool success, double time, double error) const
Definition test_utilities.h:98
bool cout_set_to_logger
Definition test_utilities.h:146
bool final_success
Definition test_utilities.h:145
void set_cout_to_logger()
Definition test_utilities.h:123
void set_do_print(bool print)
Definition test_utilities.h:35