Frobby  0.9.5
asserts.h
Go to the documentation of this file.
1 /* Frobby: Software for monomial ideal computations.
2  Copyright (C) 2009 Bjarke Hammersholt Roune (www.broune.com)
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see http://www.gnu.org/licenses/.
16 */
17 #ifndef ASSERTS_GUARD
18 #define ASSERTS_GUARD
19 
20 #include <stdexcept>
21 #include <sstream>
22 #include <vector>
23 #include <ostream>
24 
25 class AssertException : public logic_error {
26  public:
27  AssertException(const string& str);
29 };
30 
31 namespace TestInternal {
32  struct StdData {
33  StdData(size_t lineParam, const char* fileParam,
34  const char* testNameParam, bool printDotParam):
35  line(lineParam), file(fileParam),
36  testName(testNameParam), printDot(printDotParam) {}
37  size_t line;
38  const char* file;
39  const char* testName;
40  bool printDot;
41  };
42 #define STD_DATA TestInternal::StdData(__LINE__, __FILE__, __nameOfTest, __printDots)
43 #define SILENT(X) \
44  {bool ___printDots = __printDots; __printDots = false; X; __printDots = ___printDots;}
45 
46  template<class T>
47  string toStr(const T& t) {stringstream out;out << t;return out.str();}
48 
49  void assertOK(const StdData&);
50  void assertFail(const char* cond, const char* expected, const StdData&);
51  void assertFail1(const char* cond, const char* expected, const StdData&,
52  const char* exp1, string exp1Value);
53  void assertFail2(const char* cond, const char* expected, const StdData&,
54  const char* exp1, string exp1Value,
55  const char* exp2, string exp2Value);
56 
57  void assertSucceeded(bool printDot);
58 
59  void assertFailed(const char* errorMsg,
60  const char* testName, const char* file, size_t line);
61 
62  void assertTrue(bool value, const char* condition,
63  const char* testName, const char* file, size_t line,
64  bool printDot);
65 
66 #define ASSERT_EXCEPTION(CODE, EXCEPTION_TYPE) \
67  {bool _frobby_sawException = false; \
68  try {CODE;} catch (EXCEPTION_TYPE) {_frobby_sawException = true;} \
69  if (_frobby_sawException)assertOK(STD_DATA); \
70  else assertFail(#CODE,"throw exception of type " #EXCEPTION_TYPE,STD_DATA);}
71 
72 #define ASSERT_TRUE(VALUE) \
73  {if(VALUE)assertOK(STD_DATA);else assertFail(#VALUE,"true",STD_DATA);}
74 #define ASSERT_TRUE_SILENT(VALUE) SILENT(ASSERT_TRUE(VALUE))
75 
76 #define ASSERT_TRUE1(VALUE, X) \
77  {if(VALUE)assertOK(STD_DATA);else assertFail1(#VALUE,"true",STD_DATA,#X,TestInternal::toStr(X));}
78 #define ASSERT_FALSE1(VALUE, X) \
79  {if(!VALUE)assertOK(STD_DATA);else assertFail1(#VALUE,"false",STD_DATA,#X,TestInternal::toStr(X));}
80 
81 #define ASSERT_TRUE2(VALUE, X, Y) \
82  {if(VALUE)assertOK(STD_DATA);else assertFail2(#VALUE,"true",STD_DATA,#X,TestInternal::toStr(X),#Y,TestInternal::toStr(Y));}
83 #define ASSERT_FALSE2(VALUE, X, Y) \
84  {if(!VALUE)assertOK(STD_DATA);else assertFail2(#VALUE,"false",STD_DATA,#X,TestInternal::toStr(X),#Y,TestInternal::toStr(Y));}
85 
86  void assertTrue2Failed(const char* valueString,
87  const char* testName, const char* file, size_t line,
88  const char* expression1, const char* expression1Value,
89  const char* expression2, const char* expression2Value);
90  template<class A, class B>
91  void assertTrue2(bool value, const char* valueString,
92  const char* testName, const char* file, size_t line,
93  const char* expression1, const A& a,
94  const char* expression2, const B& b,
95  bool printDot) {
96  if (value) {
97  assertSucceeded(printDot);
98  return;
99  }
100 
101  stringstream aValue;
102  aValue << a;
103 
104  stringstream bValue;
105  bValue << b;
106 
107  assertTrue2Failed(valueString, testName, file, line,
108  expression1, aValue.str().c_str(),
109  expression2, bValue.str().c_str());
110  }
111 #define ASSERT_TRUE2_SILENT(VALUE, A, B) \
112  TestInternal::assertTrue2(VALUE, #VALUE, __nameOfTest, __FILE__, __LINE__, \
113  #A,A,#B,B, false);
114 
115 
116  void assertFalse(bool value, const char* condition,
117  const char* testName, const char* file, size_t line,
118  bool printDot);
119 #define ASSERT_FALSE(VALUE) \
120  TestInternal::assertFalse(VALUE, #VALUE, __nameOfTest, __FILE__, __LINE__, __printDots)
121 #define ASSERT_FALSE_SILENT(VALUE) \
122  TestInternal::assertFalse(VALUE, #VALUE, __nameOfTest, __FILE__, __LINE__, false)
123 
124  void assertEqualFailed(const char* a, const char* b,
125  const char* aString, const char* bString,
126  const char* testName, const char* file, size_t line);
127  template<class T1, class T2>
128  void assertEqual(const T1& a, const T2& b,
129  const char* aString, const char* bString,
130  const char* testName, const char* file, size_t line,
131  bool printDot) {
132  if (a == b) {
133  assertSucceeded(printDot);
134  return;
135  }
136 
137  stringstream aValue;
138  aValue << a;
139 
140  stringstream bValue;
141  bValue << b;
142 
143  assertEqualFailed(aValue.str().c_str(), bValue.str().c_str(),
144  aString, bString,
145  testName, file, line);
146  }
147 #define ASSERT_EQ(A, B) \
148  TestInternal::assertEqual(A, B, #A, #B, __nameOfTest, __FILE__, __LINE__, __printDots)
149 #define ASSERT_EQ_SILENT(A, B) \
150  TestInternal::assertEqual(A, B, #A, #B, __nameOfTest, __FILE__, __LINE__, false)
151 
152  void assertNotEqualFailed(const char* a, const char* b,
153  const char* aString, const char* bString,
154  const char* testName, const char* file, size_t line);
155  template<class T1, class T2>
156  void assertNotEqual(const T1& a, const T2& b,
157  const char* aString, const char* bString,
158  const char* testName, const char* file, size_t line,
159  bool printDot) {
160  if (a != b) {
161  assertSucceeded(printDot);
162  return;
163  }
164 
165  stringstream aValue;
166  aValue << a;
167 
168  stringstream bValue;
169  bValue << b;
170 
171  assertNotEqualFailed(aValue.str().c_str(), bValue.str().c_str(),
172  aString, bString,
173  testName, file, line);
174  }
175 #define ASSERT_NEQ(A, B) \
176  TestInternal::assertNotEqual(A, B, #A, #B, __nameOfTest, __FILE__, __LINE__, __printDots)
177 #define ASSERT_NEQ_SILENT(A, B) \
178  TestInternal::assertNotEqual(A, B, #A, #B, __nameOfTest, __FILE__, __LINE__, false)
179 
183  template<class T>
184  ostream& operator<<(ostream& out, const vector<T>& v) {
185  out << " std::vector<>: ";
186  for (typename vector<T>::const_iterator it = v.begin(); it != v.end(); ++it)
187  out << (it == v.begin() ? "" : ", ") << *it;
188  out << '\n';
189  return out;
190  }
191 }
192 
193 #endif
AssertException(const string &str)
Definition: asserts.cpp:22
void assertOK(const StdData &data)
Definition: asserts.cpp:31
void assertTrue2Failed(const char *valueString, const char *testName, const char *file, size_t line, const char *expression1, const char *expression1Value, const char *expression2, const char *expression2Value)
Definition: asserts.cpp:167
void assertFail1(const char *cond, const char *expected, const StdData &data, const char *exp1, string exp1Value)
Definition: asserts.cpp:59
void assertTrue2(bool value, const char *valueString, const char *testName, const char *file, size_t line, const char *expression1, const A &a, const char *expression2, const B &b, bool printDot)
Definition: asserts.h:91
void assertEqual(const T1 &a, const T2 &b, const char *aString, const char *bString, const char *testName, const char *file, size_t line, bool printDot)
Definition: asserts.h:128
void assertTrue(bool value, const char *valueString, const char *testName, const char *file, size_t line, bool printDot)
Definition: asserts.cpp:154
void assertFail(const char *cond, const char *expected, const StdData &data)
Definition: asserts.cpp:38
void assertFailed(const char *errorMsg, const char *testName, const char *file, size_t line)
Definition: asserts.cpp:117
void assertSucceeded(bool printDot)
Definition: asserts.cpp:110
void assertFalse(bool value, const char *valueString, const char *testName, const char *file, size_t line, bool printDot)
Definition: asserts.cpp:178
void assertNotEqualFailed(const char *a, const char *b, const char *aString, const char *bString, const char *testName, const char *file, size_t line)
Definition: asserts.cpp:202
string toStr(const T &t)
Definition: asserts.h:47
ostream & operator<<(ostream &out, const vector< T > &v)
Prints out a vector.
Definition: asserts.h:184
void assertEqualFailed(const char *a, const char *b, const char *aString, const char *bString, const char *testName, const char *file, size_t line)
Definition: asserts.cpp:191
void assertNotEqual(const T1 &a, const T2 &b, const char *aString, const char *bString, const char *testName, const char *file, size_t line, bool printDot)
Definition: asserts.h:156
void assertFail2(const char *cond, const char *expected, const StdData &data, const char *exp1, string exp1Value, const char *exp2, string exp2Value)
Definition: asserts.cpp:83
const char * file
Definition: asserts.h:38
const char * testName
Definition: asserts.h:39
StdData(size_t lineParam, const char *fileParam, const char *testNameParam, bool printDotParam)
Definition: asserts.h:33