Frobby  0.9.5
asserts.cpp
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 #include "stdinc.h"
18 #include "asserts.h"
19 
20 #include <sstream>
21 
23  logic_error(str) {
24 }
25 
27 logic_error(e) {
28 }
29 
30 namespace TestInternal {
31  void assertOK(const StdData& data) {
32  if (data.printDot) {
33  fputc('.', stdout);
34  fflush(stdout);
35  }
36  }
37 
38  void assertFail(const char* cond, const char* expected, const StdData& data) {
39  ASSERT(false);
40  stringstream msg;
41  msg << "Unit test " << data.testName
42  << " failed in file " << data.file
43  << " on line " << data.line << ".\n"
44  << "Expected \n " << cond << "\nto be\n " << expected
45  << "\nbut it was not.";
46  if (!msg) {
47  // This means msg has run out of memory, and so no message will be
48  // printed. In this case it is better to indicate running out of
49  // memory. As it happens, this also avoids the need for some
50  // special cases for tests when being run as a test for recovery
51  // from running out of memory. E.g. when precisely this thing
52  // happens with stringstream just ignoring its input without an
53  // exception causes tests to fail.
54  throw bad_alloc();
55  }
56  throw AssertException(msg.str());
57  }
58 
59  void assertFail1(const char* cond, const char* expected, const StdData& data,
60  const char* exp1, string exp1Value) {
61  ASSERT(false);
62  stringstream msg;
63  msg << "Unit test " << data.testName
64  << " failed in file " << data.file
65  << " on line " << data.line << ".\n"
66  << "Expected \n " << cond << "\nto equal\n " << expected
67  << "\nbut it did not.\n"
68  << "The value of the expression\n " << exp1
69  << "\nprints as\n " << exp1Value << '\n';
70  if (!msg) {
71  // This means msg has run out of memory, and so no message will be
72  // printed. In this case it is better to indicate running out of
73  // memory. As it happens, this also avoids the need for some
74  // special cases for tests when being run as a test for recovery
75  // from running out of memory. E.g. when precisely this thing
76  // happens with stringstream just ignoring its input without an
77  // exception causes tests to fail.
78  throw bad_alloc();
79  }
80  throw AssertException(msg.str());
81  }
82 
83  void assertFail2(const char* cond, const char* expected, const StdData& data,
84  const char* exp1, string exp1Value,
85  const char* exp2, string exp2Value) {
86  ASSERT(false);
87  stringstream msg;
88  msg << "Unit test " << data.testName
89  << " failed in file " << data.file
90  << " on line " << data.line << ".\n"
91  << "Expected \n " << cond << "\nto equal\n " << expected
92  << "\nbut it did not.\n"
93  << "The value of the expression\n " << exp1
94  << "\nprints as\n " << exp1Value << '\n'
95  << "The value of the expression\n " << exp2
96  << "\nprints as\n " << exp2Value << '\n';
97  if (!msg) {
98  // This means msg has run out of memory, and so no message will be
99  // printed. In this case it is better to indicate running out of
100  // memory. As it happens, this also avoids the need for some
101  // special cases for tests when being run as a test for recovery
102  // from running out of memory. E.g. when precisely this thing
103  // happens with stringstream just ignoring its input without an
104  // exception causes tests to fail.
105  throw bad_alloc();
106  }
107  throw AssertException(msg.str());
108  }
109 
110  void assertSucceeded(bool printDot) {
111  if (printDot) {
112  fputc('.', stdout);
113  fflush(stdout);
114  }
115  }
116 
117  void assertFailed(const char* errorMsg,
118  const char* testName, const char* file, size_t line) {
119  if (testName == 0)
120  testName = "";
121 
122  ASSERT(false);
123  stringstream msg;
124  msg << "Unit test " << testName
125  << " failed in file " << file
126  << " on line " << line << ".\n"
127  << errorMsg;
128  if (!msg) {
129  // This means msg has run out of memory, and so no message will be
130  // printed. In this case it is better to indicate running out of
131  // memory. As it happens, this also avoids the need for some
132  // special cases for tests when being run as a test for recovery
133  // from running out of memory. E.g. when precisely this thing
134  // happens with stringstream just ignoring its input without an
135  // exception causes tests to fail.
136  throw bad_alloc();
137  }
138  throw AssertException(msg.str());
139  }
140 
141  void assertFailed2(const char* errorMsg,
142  const char* testName, const char* file, size_t line,
143  const char* expression1, const char* expression1Value,
144  const char* expression2, const char* expression2Value) {
145  stringstream msg;
146  msg << errorMsg
147  << "The value of the expression\n " << expression1
148  << "\nprints as\n " << expression1Value << '\n'
149  << "and the value of the expression\n " << expression2
150  << "\nprints as\n " << expression2Value << '\n';
151  assertFailed(msg.str().c_str(), testName, file, line);
152  }
153 
154  void assertTrue(bool value, const char* valueString,
155  const char* testName, const char* file, size_t line,
156  bool printDot) {
157  if (value) {
158  assertSucceeded(printDot);
159  return;
160  }
161 
162  stringstream msg;
163  msg << "Expected \n " << valueString << "\nto be true, but it was not.\n";
164  assertFailed(msg.str().c_str(), testName, file, line);
165  }
166 
167  void assertTrue2Failed(const char* valueString,
168  const char* testName, const char* file, size_t line,
169  const char* expression1, const char* expression1Value,
170  const char* expression2, const char* expression2Value) {
171  stringstream msg;
172  msg << "Expected \n " << valueString << "\nto be true, but it was not.\n";
173  assertFailed2(msg.str().c_str(), testName, file, line,
174  expression1, expression1Value,
175  expression2, expression2Value);
176  }
177 
178  void assertFalse(bool value, const char* valueString,
179  const char* testName, const char* file, size_t line,
180  bool printDot) {
181  if (!value) {
182  assertSucceeded(printDot);
183  return;
184  }
185 
186  stringstream msg;
187  msg << "Expected \n " << valueString << "\nto be false, but it was not.\n";
188  assertFailed(msg.str().c_str(), testName, file, line);
189  }
190 
191  void assertEqualFailed(const char* a, const char* b,
192  const char* aString, const char* bString,
193  const char* testName, const char* file, size_t line) {
194  stringstream msg;
195  msg << "Expected " << aString << " == " << bString << ",\n"
196  << "but operator== returned false. "
197  << "The left hand side prints as\n" << a << "\nwhile "
198  << "the right hand side prints as\n" << b << ".\n";
199  assertFailed(msg.str().c_str(), testName, file, line);
200  }
201 
202  void assertNotEqualFailed(const char* a, const char* b,
203  const char* aString, const char* bString,
204  const char* testName, const char* file, size_t line) {
205  stringstream msg;
206  msg << "Expected " << aString << " != " << bString << ",\n"
207  << "but operator!= returned false. "
208  << "The left hand side prints as\n" << a << "\nwhile "
209  << "the right hand side prints as\n" << b << ".\n";
210  assertFailed(msg.str().c_str(), testName, file, line);
211  }
212 }
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 assertFailed2(const char *errorMsg, const char *testName, const char *file, size_t line, const char *expression1, const char *expression1Value, const char *expression2, const char *expression2Value)
Definition: asserts.cpp:141
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
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 assertFail2(const char *cond, const char *expected, const StdData &data, const char *exp1, string exp1Value, const char *exp2, string exp2Value)
Definition: asserts.cpp:83
#define ASSERT(X)
Definition: stdinc.h:86
const char * file
Definition: asserts.h:38
const char * testName
Definition: asserts.h:39