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 checkpoint(error<tol,message,time);
48 }
49
50 void checkpoint(double value, double reference, double tol,
51 std::string message, double time=-1.0) {
52 double error=fabs(value-reference);
53 checkpoint(error,tol,message,time);
54 }
55
56 void checkpoint(int value, int reference, std::string message, double time=-1.0) {
57 bool success=(value==reference);
58 checkpoint(success,message,time);
59 }
60
61 void checkpoint(bool success, std::string message, double time=-1.0) {
62 bool use_logger=cout_set_to_logger;
64 final_success = success and final_success;
65 if (not have_checkpoints and do_print) print(""); // first checkpoint
67 if (do_print) std::cout << " " << ltrim_to_length(message,66);
68 double time1=cpu_time()-time_last_checkpoint;
70 print_success_fail(std::cout,success,time1,-1.0);
71 if (not success) {
73 }
74 if (use_logger) set_cout_to_logger();
75 }
76
77 void print_success_fail(std::ostream& os, bool success, double time, double error) const {
78
79 if (do_print) {
80 if (success) os << "\033[32m" << "passed " << "\033[0m";
81 else os << "\033[31m" << "failed " << "\033[0m";
82 if (time>0) {
83 std::stringstream ss;
84 ss<< " in " << std::fixed << std::setprecision(1) << time << "s";
85 os << ss.str();
86 }
87 if (error>=0.0) os << " error " << error;
88 os << std::endl;
89 }
90 }
91
92 int end(bool success=true) {
94 if (do_print and have_checkpoints) std::cout << ltrim_to_length("--> final result -->",70);
95 success = success and final_success;
96 double time_end=cpu_time();
97 print_success_fail(std::cout,success,time_end-time_begin,-1.0);
98 if (not success) print_and_clear_log();
99 return (success) ? 0 : 1;
100 }
101
103 if (cout_set_to_logger) return;
105 stream_buffer_cout = std::cout.rdbuf();
106 std::streambuf* stream_buffer_file = logger.rdbuf();
107 std::cout.rdbuf(stream_buffer_file);
108 }
109
110 /// newline for use by user, not for internal use (e.g. checkpoint())
111 void set_cout_to_terminal(bool newline=true) {
112 if (not cout_set_to_logger) return;
113 if (cout_set_to_logger) {
114 std::cout.rdbuf(stream_buffer_cout);
115 }
116 cout_set_to_logger=false;
117 if (newline) std::cout << std::endl;
118 }
119
120 std::stringstream logger;
121 bool get_final_success() const {return final_success;}
122private:
123
124 bool final_success=true;
125 bool cout_set_to_logger=false; // do not change this directly!
127 std::streambuf* stream_buffer_cout;
128 double time_begin=0.0;
130 bool do_print=true; // if run on several ranks
131};
132
133
134}
135
136#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:142
small class for pretty printing of test output
Definition test_utilities.h:15
double time_last_checkpoint
Definition test_utilities.h:129
int end(bool success=true)
Definition test_utilities.h:92
void checkpoint(double value, double reference, double tol, std::string message, double time=-1.0)
Definition test_utilities.h:50
double time_begin
Definition test_utilities.h:128
static std::string ltrim_to_length(std::string line, long length=70)
Definition test_utilities.h:25
bool do_print
Definition test_utilities.h:130
void checkpoint(bool success, std::string message, double time=-1.0)
Definition test_utilities.h:61
std::stringstream logger
Definition test_utilities.h:120
void set_cout_to_terminal(bool newline=true)
newline for use by user, not for internal use (e.g. checkpoint())
Definition test_utilities.h:111
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:121
std::streambuf * stream_buffer_cout
Definition test_utilities.h:127
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
void checkpoint(int value, int reference, std::string message, double time=-1.0)
Definition test_utilities.h:56
bool have_checkpoints
Definition test_utilities.h:126
void print_success_fail(std::ostream &os, bool success, double time, double error) const
Definition test_utilities.h:77
bool cout_set_to_logger
Definition test_utilities.h:125
bool final_success
Definition test_utilities.h:124
void set_cout_to_logger()
Definition test_utilities.h:102
void set_do_print(bool print)
Definition test_utilities.h:35