instrumented_callbacks.hpp
Go to the documentation of this file.
1 #ifndef TEST__UNIT__INSTRUMENTED_CALLBACKS_HPP
2 #define TEST__UNIT__INSTRUMENTED_CALLBACKS_HPP
3 
7 #include <Eigen/Dense>
8 #include <map>
9 #include <string>
10 #include <iostream>
11 #include <exception>
12 
13 namespace stan {
14  namespace test {
15  namespace unit {
16 
17  /**
18  * instrumented_interrupt counts the number of times it is
19  * called and makes the count accessible via a method.
20  */
22  public:
24  counter_(0) {}
25 
26  void operator()() {counter_++;}
27 
28  unsigned int call_count() {return counter_;}
29 
30  private:
31  unsigned int counter_;
32  };
33 
34  /**
35  * instrumented_writer counts the number of times it is called through
36  * each route and makes the count available via methods.
37  * Stores all arguments passed and makes them available via
38  * methods.
39  */
41  public:
43 
44  void operator()(const std::string& key, double value) {
45  counter_["string_double"]++;
46  string_double.push_back(std::make_pair(key, value));
47  }
48 
49  void operator()(const std::string& key, int value) {
50  counter_["string_int"]++;
51  string_int.push_back(std::make_pair(key, value));
52  }
53 
54  void operator()(const std::string& key, const std::string& value) {
55  counter_["string_string"]++;
56  string_string.push_back(std::make_pair(key, value));
57  }
58 
59  void operator()(const std::string& key,
60  const double* values,
61  int n_values) {
62  counter_["string_pdouble_int"]++;
63  if (n_values == 0) return;
64  std::vector<double> value(n_values);
65  for (int i = 0; i < n_values; ++i)
66  value[i] = values[i];
67  string_pdouble_int.push_back(std::make_pair(key, value));
68  }
69 
70  void operator()(const std::string& key,
71  const double* values,
72  int n_rows, int n_cols) {
73  counter_["string_pdouble_int_int"]++;
74  if (n_rows == 0 || n_cols == 0) return;
75  Eigen::MatrixXd value(n_rows, n_cols);
76  for (int i = 0; i < n_rows; ++i)
77  for (int j = 0; j < n_cols; ++j)
78  value(i,j) = values[i*n_cols+j];
79  string_pdouble_int_int.push_back(std::make_pair(key, value));
80  }
81 
82  void operator()(const std::vector<std::string>& names) {
83  counter_["vector_string"]++;
84  vector_string.push_back(names);
85  }
86 
87  void operator()(const std::vector<double>& state) {
88  counter_["vector_double"]++;
89  vector_double.push_back(state);
90  }
91 
92  void operator()() {
93  counter_["empty"]++;
94  }
95 
97  counter_["string"]++;
98  string.push_back(message);
99  }
100 
101  unsigned int call_count() {
102  unsigned int n = 0;
103  for (std::map<std::string, int>::iterator it=counter_.begin();
104  it!=counter_.end(); ++it)
105  n += it->second;
106  return n;
107  }
108 
109  unsigned int call_count(std::string s) {
110  return counter_[s];
111  }
112 
113  std::vector<std::pair<std::string, double> > string_double_values() {
114  return string_double;
115  };
116 
117  std::vector<std::pair<std::string, int> > string_int_values() {
118  return string_int;
119  };
120 
121  std::vector<std::pair<std::string, std::string> > string_string_values() {
122  return string_string;
123  };
124 
125  std::vector<std::pair<std::string, std::vector<double> > > string_pdouble_int_values() {
126  return string_pdouble_int;
127  };
128 
129  std::vector<std::pair<std::string, Eigen::MatrixXd> > string_pdouble_int_int_values() {
130  return string_pdouble_int_int;
131  };
132 
133  std::vector<std::vector<std::string> > vector_string_values() {
134  return vector_string;
135  };
136 
137  std::vector<std::vector<double> > vector_double_values() {
138  return vector_double;
139  };
140 
141  std::vector<std::string> string_values() {
142  return string;
143  };
144 
145 
146  private:
147  std::map<std::string, int> counter_;
148  std::vector<std::pair<std::string, double> > string_double;
149  std::vector<std::pair<std::string, int> > string_int;
150  std::vector<std::pair<std::string, std::string> > string_string;
151  std::vector<std::pair<std::string, std::vector<double> > > string_pdouble_int;
152  std::vector<std::pair<std::string, Eigen::MatrixXd> > string_pdouble_int_int;
153  std::vector<std::vector<std::string> > vector_string;
154  std::vector<std::vector<double> > vector_double;
155  std::vector<std::string> string;
156 
157 
158  };
159 
160  /**
161  * instrumented_logger counts the number of times it is called through
162  * each route and makes the count available via methods.
163  * Stores all arguments passed and makes them available via
164  * methods.
165  */
167  public:
169 
170  void debug(const std::string& message) {
171  debug_.push_back(message);
172  }
173 
174  void debug(const std::stringstream& message) {
175  debug_.push_back(message.str());
176  }
177 
178  void info(const std::string& message) {
179  info_.push_back(message);
180  }
181 
182  void info(const std::stringstream& message) {
183  info_.push_back(message.str());
184  }
185 
186  void warn(const std::string& message) {
187  warn_.push_back(message);
188  }
189 
190  void warn(const std::stringstream& message) {
191  warn_.push_back(message.str());
192  }
193 
194  void error(const std::string& message) {
195  error_.push_back(message);
196  }
197 
198  void error(const std::stringstream& message) {
199  error_.push_back(message.str());
200  }
201 
202  void fatal(const std::string& message) {
203  fatal_.push_back(message);
204  }
205 
206  void fatal(const std::stringstream& message) {
207  fatal_.push_back(message.str());
208  }
209 
210 
211  unsigned int call_count() {
212  return debug_.size() + info_.size() + warn_.size()
213  + error_.size() + fatal_.size();
214  }
215 
216  unsigned int call_count_debug() {
217  return debug_.size();
218  }
219 
220  unsigned int call_count_info() {
221  return info_.size();
222  }
223 
224  unsigned int call_count_warn() {
225  return warn_.size();
226  }
227 
228  unsigned int call_count_error() {
229  return error_.size();
230  }
231 
232  unsigned int call_count_fatal() {
233  return fatal_.size();
234  }
235 
236  unsigned int find_debug(const std::string& msg) {
237  return find_(debug_, msg);
238  }
239 
240  unsigned int find_info(const std::string& msg) {
241  return find_(info_, msg);
242  }
243 
244  unsigned int find_warn(const std::string& msg) {
245  return find_(warn_, msg);
246  }
247 
248  unsigned int find_error(const std::string& msg) {
249  return find_(error_, msg);
250  }
251 
252  unsigned int find_fatal(const std::string& msg) {
253  return find_(fatal_, msg);
254  }
255 
256  unsigned int find(const std::string& msg) {
257  return find_debug(msg) + find_info(msg) + find_warn(msg)
258  + find_error(msg) + find_fatal(msg);
259  }
260 
261  void print_info(std::ostream& o) {
262  for (size_t n = 0; n < info_.size(); ++n)
263  o << info_[n] << std::endl;
264  }
265 
266  private:
267  unsigned int find_(const std::vector<std::string>& vec, const std::string& msg) {
268  unsigned int count = 0;
269  for (size_t n = 0; n < vec.size(); ++n)
270  if (vec[n].find(msg) != std::string::npos)
271  count++;
272  return count;
273  }
274 
275 
276  std::vector<std::string> debug_;
277  std::vector<std::string> info_;
278  std::vector<std::string> warn_;
279  std::vector<std::string> error_;
280  std::vector<std::string> fatal_;
281  };
282 
283 
284  }
285  }
286 }
287 
288 
289 #endif
void warn(const std::string &message)
std::vector< std::vector< std::string > > vector_string_values()
set< int >::iterator it
std::vector< std::pair< std::string, double > > string_double_values()
std::vector< std::pair< std::string, Eigen::MatrixXd > > string_pdouble_int_int
void debug(const std::string &message)
void operator()(const std::vector< double > &state)
void info(const std::string &message)
void operator()(const std::string &key, int value)
std::vector< std::pair< std::string, int > > string_int_values()
std::pair< Spectrum *, CheatDecomp * > make_pair(SpectrumLoaderBase &loader_data, SpectrumLoaderBase &loader_mc, HistAxis *axis, Cut *cut, const SystShifts &shift, const Var &wei)
Definition: DataMCLoad.C:336
unsigned int find_(const std::vector< std::string > &vec, const std::string &msg)
std::vector< std::pair< std::string, double > > string_double
void error(const std::string &message)
void operator()(const std::string &key, const std::string &value)
void operator()(const std::string &message)
const XML_Char * s
Definition: expat.h:262
void operator()(const std::vector< std::string > &names)
std::vector< std::vector< double > > vector_double
void operator()(const std::string &key, const double *values, int n_rows, int n_cols)
const XML_Char int const XML_Char * value
Definition: expat.h:331
void fatal(const std::string &message)
std::vector< std::pair< std::string, std::vector< double > > > string_pdouble_int
std::vector< std::pair< std::string, Eigen::MatrixXd > > string_pdouble_int_int_values()
const double j
Definition: BetheBloch.cxx:29
Eigen::VectorXd vec
unsigned int find_fatal(const std::string &msg)
void fatal(const std::stringstream &message)
std::vector< std::pair< std::string, std::string > > string_string
std::vector< std::pair< std::string, std::string > > string_string_values()
std::vector< std::vector< std::string > > vector_string
std::vector< std::pair< std::string, std::vector< double > > > string_pdouble_int_values()
unsigned int find_warn(const std::string &msg)
unsigned int find_error(const std::string &msg)
std::vector< std::pair< std::string, int > > string_int
void warn(const std::stringstream &message)
std::vector< std::vector< double > > vector_double_values()
void operator()(const std::string &key, double value)
unsigned int find(const std::string &msg)
unsigned int find_info(const std::string &msg)
void operator()(const std::string &key, const double *values, int n_values)
unsigned int find_debug(const std::string &msg)
void debug(const std::stringstream &message)
void error(const std::stringstream &message)
void info(const std::stringstream &message)
enum BeamMode string