ProteoWizard
unit.hpp
Go to the documentation of this file.
1 //
2 // $Id: unit.hpp 6141 2014-05-05 21:03:47Z chambm $
3 //
4 //
5 // Original author: Darren Kessner <darren@proteowizard.org>
6 //
7 // Copyright 2006 Louis Warschaw Prostate Cancer Center
8 // Cedars Sinai Medical Center, Los Angeles, California 90048
9 //
10 // Licensed under the Apache License, Version 2.0 (the "License");
11 // you may not use this file except in compliance with the License.
12 // You may obtain a copy of the License at
13 //
14 // http://www.apache.org/licenses/LICENSE-2.0
15 //
16 // Unless required by applicable law or agreed to in writing, software
17 // distributed under the License is distributed on an "AS IS" BASIS,
18 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 // See the License for the specific language governing permissions and
20 // limitations under the License.
21 //
22 
23 
24 #ifndef _UNIT_HPP_
25 #define _UNIT_HPP_
26 
27 
28 #include "Exception.hpp"
29 #include "DateTime.hpp"
30 #include "Filesystem.hpp"
32 #include <string>
33 #include <sstream>
34 #include <cmath>
35 #include <boost/filesystem/detail/utf8_codecvt_facet.hpp>
36 
37 
38 namespace pwiz {
39 namespace util {
40 
41 
42 //
43 // These are assertion macros for unit testing. They throw a runtime_error
44 // exception on failure, instead of calling abort(), allowing the application
45 // to recover and return an appropriate error value to the shell.
46 //
47 // unit_assert(x): asserts x is true
48 // unit_assert_equal(x, y, epsilon): asserts x==y, within epsilon
49 // unit_assert_matrices_equal(A, B, epsilon): asserts A==B, within epsilon
50 //
51 
52 
53 inline std::string unit_assert_message(const char* filename, int line, const char* expression)
54 {
55  std::ostringstream oss;
56  oss << "[" << filename << ":" << line << "] Assertion failed: " << expression;
57  return oss.str();
58 }
59 
60 inline std::string unit_assert_equal_message(const char* filename, int line, const std::string& x, const std::string& y, const char* expression)
61 {
62  std::ostringstream oss;
63  oss << "[" << filename << ":" << line << "] Assertion failed: expected \"" << x << "\" but got \"" << y << "\" (" << expression << ")";
64  return oss.str();
65 }
66 
67 inline std::string unit_assert_numeric_equal_message(const char* filename, int line, double x, double y, double epsilon)
68 {
69  std::ostringstream oss;
70  oss.precision(10);
71  oss << "[" << filename << ":" << line << "] Assertion failed: |" << x << " - " << y << "| < " << epsilon;
72  return oss.str();
73 }
74 
75 inline std::string unit_assert_exception_message(const char* filename, int line, const char* expression, const std::string& exception)
76 {
77  std::ostringstream oss;
78  oss << "[" << filename << ":" << line << "] Assertion \"" << expression << "\" failed to throw " << exception;
79  return oss.str();
80 }
81 
82 inline std::string quote_string(const string& str) {return "\"" + str + "\"";}
83 
84 
85 #define unit_assert(x) \
86  (!(x) ? throw std::runtime_error(unit_assert_message(__FILE__, __LINE__, #x)) : 0)
87 
88 
89 #define unit_assert_operator_equal(expected, actual) \
90  (!(expected == actual) ? throw std::runtime_error(unit_assert_equal_message(__FILE__, __LINE__, lexical_cast<string>(expected), lexical_cast<string>(actual), #actual)) : 0)
91 
92 
93 #define unit_assert_equal(x, y, epsilon) \
94  (!(fabs((x)-(y)) <= (epsilon)) ? throw std::runtime_error(unit_assert_numeric_equal_message(__FILE__, __LINE__, (x), (y), (epsilon))) : 0)
95 
96 
97 #define unit_assert_throws(x, exception) \
98  { \
99  bool threw = false; \
100  try { (x); } \
101  catch (exception&) \
102  { \
103  threw = true; \
104  } \
105  if (!threw) \
106  throw std::runtime_error(unit_assert_exception_message(__FILE__, __LINE__, #x, #exception)); \
107  }
108 
109 
110 #define unit_assert_throws_what(x, exception, whatStr) \
111  { \
112  bool threw = false; \
113  try { (x); } \
114  catch (exception& e) \
115  { \
116  if (e.what() == std::string(whatStr)) \
117  threw = true; \
118  else \
119  throw std::runtime_error(unit_assert_exception_message(__FILE__, __LINE__, #x, std::string(#exception)+" "+quote_string(whatStr)+"\nBut a different exception was thrown: ")+quote_string(e.what())); \
120  } \
121  if (!threw) \
122  throw std::runtime_error(unit_assert_exception_message(__FILE__, __LINE__, #x, std::string(#exception)+" "+quote_string(whatStr))); \
123  }
124 
125 
126 #define unit_assert_matrices_equal(A, B, epsilon) \
127  unit_assert(boost::numeric::ublas::norm_frobenius((A)-(B)) < (epsilon))
128 
129 
130 #define unit_assert_vectors_equal(A, B, epsilon) \
131  unit_assert(boost::numeric::ublas::norm_2((A)-(B)) < (epsilon))
132 
133 
134 // the following macros are used by the ProteoWizard tests to report test status and duration to TeamCity
135 
136 inline std::string escape_teamcity_string(const std::string& str)
137 {
138  string result = str;
139  bal::replace_all(result, "'", "|'");
140  bal::replace_all(result, "\n", "|n");
141  bal::replace_all(result, "\r", "|r");
142  bal::replace_all(result, "|", "||");
143  bal::replace_all(result, "[", "|[");
144  bal::replace_all(result, "]", "|]");
145  return result;
146 }
147 
148 #define TEST_PROLOG_EX(argc, argv, suffix) \
149  bnw::args args(argc, argv); \
150  std::locale global_loc = std::locale(); \
151  std::locale loc(global_loc, new boost::filesystem::detail::utf8_codecvt_facet); \
152  bfs::path::imbue(loc); \
153  bfs::path testName = bfs::change_extension(bfs::basename(argv[0]), (suffix)); \
154  string teamcityTestName = pwiz::util::escape_teamcity_string(testName.string()); \
155  bpt::ptime testStartTime; \
156  vector<string> testArgs(argv, argv+argc); \
157  bool teamcityTestDecoration = find(testArgs.begin(), testArgs.end(), "--teamcity-test-decoration") != testArgs.end(); \
158  if (teamcityTestDecoration) \
159  { \
160  testStartTime = bpt::microsec_clock::local_time(); \
161  cout << "##teamcity[testStarted name='" << teamcityTestName << "']" << endl; \
162  } \
163  int testExitStatus = 0;
164 
165 #define TEST_PROLOG(argc, argv) TEST_PROLOG_EX(argc, argv, "")
166 
167 #define TEST_FAILED(x) \
168  if (teamcityTestDecoration) \
169  cout << "##teamcity[testFailed name='" << teamcityTestName << "' message='" << pwiz::util::escape_teamcity_string((x)) << "']\n"; \
170  cerr << (x) << endl; \
171  testExitStatus = 1;
172 
173 #define TEST_EPILOG \
174  if (teamcityTestDecoration) \
175  cout << "##teamcity[testFinished name='" << teamcityTestName << \
176  "' duration='" << round((bpt::microsec_clock::local_time() - testStartTime).total_microseconds() / 1000.0) << "']" << endl; \
177  return testExitStatus;
178 
179 
180 } // namespace util
181 } // namespace pwiz
182 
183 
184 #endif // _UNIT_HPP_
185 
std::string escape_teamcity_string(const std::string &str)
Definition: unit.hpp:136
std::string unit_assert_numeric_equal_message(const char *filename, int line, double x, double y, double epsilon)
Definition: unit.hpp:67
const double epsilon
Definition: DiffTest.cpp:41
std::string quote_string(const string &str)
Definition: unit.hpp:82
std::string unit_assert_message(const char *filename, int line, const char *expression)
Definition: unit.hpp:53
std::string unit_assert_exception_message(const char *filename, int line, const char *expression, const std::string &exception)
Definition: unit.hpp:75
std::string unit_assert_equal_message(const char *filename, int line, const std::string &x, const std::string &y, const char *expression)
Definition: unit.hpp:60
KernelTraitsBase< Kernel >::space_type::abscissa_type x
KernelTraitsBase< Kernel >::space_type::ordinate_type y