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 
12 namespace madness {
13 
14 /// small class for pretty printing of test output
15 struct test_output {
16  test_output(std::string line) {
17  std::cout << ltrim_to_length(line,70);
18  logger << std::scientific << std::setprecision(8) ;
22  }
23 
24  static std::string ltrim_to_length(std::string line, long length=70) {
25  int ncharacter=line.size();
26  if (line.size()<size_t(length)) line+= std::string(length-ncharacter, ' ' );
27  return line;
28  }
29 
31  set_cout_to_terminal(false);
32  }
33 
36  std::cout << logger.str() << std::endl;
37  logger.clear();
38  }
39 
40  void checkpoint(double error, double tol,
41  std::string message, double time=-1.0) {
42  bool use_logger=cout_set_to_logger;
43  set_cout_to_terminal(false);
44  bool success=error<tol;
45  final_success = success and final_success;
46  if (not have_checkpoints) print(""); // first checkpoint
47  have_checkpoints=true;
48  std::cout << " " << ltrim_to_length(message,66);
49  double time1=cpu_time()-time_last_checkpoint;
51  print_success_fail(std::cout,success,time1,error);
52  if (not success) {
54  }
55  if (use_logger) set_cout_to_logger();
56  }
57 
58  void checkpoint(double value, double reference, double tol,
59  std::string message, double time=-1.0) {
60  bool use_logger=cout_set_to_logger;
61  set_cout_to_terminal(false);
62  double error=fabs(value-reference);
63  bool success=error<tol;
64  final_success = success and final_success;
65  if (not have_checkpoints) print(""); // first checkpoint
66  have_checkpoints=true;
67  std::cout << " " << ltrim_to_length(message,66);
68  double time1=cpu_time()-time_last_checkpoint;
70  print_success_fail(std::cout,success,time1,error);
71  if (not success) {
73  }
74  if (use_logger) set_cout_to_logger();
75  }
76 
77  void checkpoint(bool success, std::string message, double time=-1.0) {
78  bool use_logger=cout_set_to_logger;
79  set_cout_to_terminal(false);
80  final_success = success and final_success;
81  if (not have_checkpoints) print(""); // first checkpoint
82  have_checkpoints=true;
83  std::cout << " " << ltrim_to_length(message,66);
84  double time1=cpu_time()-time_last_checkpoint;
86  print_success_fail(std::cout,success,time1,-1.0);
87  if (not success) {
89  }
90  if (use_logger) set_cout_to_logger();
91  }
92 
93  void print_success_fail(std::ostream& os, bool success, double time, double error) {
94 
95  if (success) os << "\033[32m" << "passed " << "\033[0m";
96  else os << "\033[31m" << "failed " << "\033[0m";
97  if (time>0) {
98  std::stringstream ss;
99  ss<< " in " << std::fixed << std::setprecision(1) << time << "s";
100  os << ss.str();
101  }
102  if (error>=0.0) os << " error " << error;
103  os << std::endl;
104  }
105 
106  int end(bool success=true) {
107  set_cout_to_terminal(false);
108  if (have_checkpoints) std::cout << ltrim_to_length("--> final result -->",70);
109  success = success and final_success;
110  double time_end=cpu_time();
111  print_success_fail(std::cout,success,time_end-time_begin,-1.0);
112  if (not success) print_and_clear_log();
113  return (success) ? 0 : 1;
114  }
115 
117  if (cout_set_to_logger) return;
118  cout_set_to_logger=true;
119  stream_buffer_cout = std::cout.rdbuf();
120  std::streambuf* stream_buffer_file = logger.rdbuf();
121  std::cout.rdbuf(stream_buffer_file);
122  }
123 
124  /// newline for use by user, not for internal use (e.g. checkpoint())
125  void set_cout_to_terminal(bool newline=true) {
126  if (not cout_set_to_logger) return;
127  if (cout_set_to_logger) {
128  std::cout.rdbuf(stream_buffer_cout);
129  }
130  cout_set_to_logger=false;
131  if (newline) std::cout << std::endl;
132  }
133 
134  std::stringstream logger;
135  bool get_final_success() const {return final_success;}
136 private:
137 
138  bool final_success=true;
139  bool cout_set_to_logger=false; // do not change this directly!
140  bool have_checkpoints=false;
141  std::streambuf* stream_buffer_cout;
142  double time_begin=0.0;
144 };
145 
146 
147 }
148 
149 #endif /* SRC_APPS_CHEM_TEST_UTILITIES_H_ */
static const double length
Definition: hedft.cc:48
File holds all helper structures necessary for the CC_Operator and CC2 class.
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:225
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:143
int end(bool success=true)
Definition: test_utilities.h:106
void checkpoint(double value, double reference, double tol, std::string message, double time=-1.0)
Definition: test_utilities.h:58
double time_begin
Definition: test_utilities.h:142
static std::string ltrim_to_length(std::string line, long length=70)
Definition: test_utilities.h:24
void checkpoint(bool success, std::string message, double time=-1.0)
Definition: test_utilities.h:77
std::stringstream logger
Definition: test_utilities.h:134
void set_cout_to_terminal(bool newline=true)
newline for use by user, not for internal use (e.g. checkpoint())
Definition: test_utilities.h:125
test_output(std::string line)
Definition: test_utilities.h:16
~test_output()
Definition: test_utilities.h:30
bool get_final_success() const
Definition: test_utilities.h:135
std::streambuf * stream_buffer_cout
Definition: test_utilities.h:141
void print_and_clear_log()
Definition: test_utilities.h:34
void checkpoint(double error, double tol, std::string message, double time=-1.0)
Definition: test_utilities.h:40
bool have_checkpoints
Definition: test_utilities.h:140
void print_success_fail(std::ostream &os, bool success, double time, double error)
Definition: test_utilities.h:93
bool cout_set_to_logger
Definition: test_utilities.h:139
bool final_success
Definition: test_utilities.h:138
void set_cout_to_logger()
Definition: test_utilities.h:116