This repository has been archived by the owner on Apr 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathassertion.C
276 lines (235 loc) · 6.46 KB
/
assertion.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// assertion.C -- Managed assertions.
//
// Copyright 2002 Per Abrahamsen and KVL.
//
// This file is part of Daisy.
//
// Daisy is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// Daisy is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser Public License for more details.
//
// You should have received a copy of the GNU Lesser Public License
// along with Daisy; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#define BUILD_DLL
#include "assertion.h"
#include "treelog.h"
#include "mathlib.h"
#include <vector>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <map>
namespace Assertion
{
static std::vector<Treelog*>& logs ()
{
static std::vector<Treelog*> logs;
return logs;
}
// full counter
std::map<std::string, int>* counter = NULL;
}
bool
Assertion::full (const char* file, const int line, bool is_debug)
{
const int max_entries = 5;
// Find entry.
std::ostringstream tmp;
tmp << file << ":" << line;
const std::string entry = tmp.str ();
// Prepare counter.
if (counter == NULL)
counter = new std::map<std::string, int>;
if (counter->find (entry) == counter->end ())
(*counter)[entry] = 0;
// Update count.
(*counter)[entry]++;
if ((*counter)[entry] == max_entries)
{
if (is_debug)
debug (entry + ": last message from this line");
else
warning (entry + ": one last message from this line");
}
return (*counter)[entry] > max_entries;
}
void
Assertion::message (const std::string& msg)
{
static std::ios_base::Init init; // Can be called from static constructor.
if (logs ().size () == 0)
std::cout << msg << "\n";
for (unsigned int i = 0; i < logs ().size (); i++)
{
logs ()[i]->message (msg);
logs ()[i]->flush ();
}
}
void
Assertion::error (const std::string& msg)
{
static std::ios_base::Init init; // Can be called from static constructor.
if (logs ().size () == 0)
std::cerr << msg << "\n";
for (unsigned int i = 0; i < logs ().size (); i++)
{
logs ()[i]->error (msg);
logs ()[i]->flush ();
}
}
void
Assertion::warning (const std::string& msg)
{
static std::ios_base::Init init; // Can be called from static constructor.
if (logs ().size () == 0)
std::cerr << msg << "\n";
for (unsigned int i = 0; i < logs ().size (); i++)
{
logs ()[i]->warning (msg);
logs ()[i]->flush ();
}
}
void
Assertion::debug (const std::string& msg)
{
static std::ios_base::Init init; // Can be called from static constructor.
if (logs ().size () == 0)
std::cerr << msg << "\n";
for (unsigned int i = 0; i < logs ().size (); i++)
{
logs ()[i]->debug (msg);
logs ()[i]->flush ();
}
}
void
Assertion::failure (const char* file, int line, const char* fun,
const char* test)
{
std::ostringstream tmp;
tmp << file << ":" << line << ": assertion '" << test << "' failed";
if (fun)
tmp << " in " << fun;
error (tmp.str ());
if (logs ().size () == 0)
// Windows dislike throw in static constructors.
exit (4);
else
throw 5;
}
void
Assertion::bug (const char* file, int line, const char* fun,
const std::string& msg)
{
if (full (file, line))
return;
std::ostringstream tmp;
tmp << file << ":" << line << ": Daisy bug: '" << msg << "'";
if (fun)
tmp << " in " << fun;
error (tmp.str ());
}
void
Assertion::warning (const char* file, int line, const char* fun,
const std::string& msg)
{
if (full (file, line))
return;
std::ostringstream tmp;
tmp << file << ":" << line << ": ";
if (fun)
tmp << "(" << fun << ") ";
tmp << "warning: " << msg;
warning (tmp.str ());
}
void
Assertion::panic (const char* file, int line, const char* fun,
const std::string& msg)
{
bug (file, line, fun, msg);
if (logs ().size () == 0)
// Windows dislike throw in static constructors.
exit (6);
else
throw 7;
}
void
Assertion::notreached (const char* file, int line, const char* fun)
{ panic (file, line, fun, "This line should never have been reached"); }
void
Assertion::non_negative (const char* file, int line, const char* fun,
const std::vector<double>& v)
{
for (unsigned int i = 0; i < v.size (); i++)
if (v[i] < 0 || !std::isfinite (v[i]))
{
std::ostringstream tmp;
tmp << "v[" << i << "] >= 0";
Assertion::failure (file, line, fun, tmp.str ().c_str ());
}
}
void
Assertion::approximate (const char* file, int line, const char* fun,
double a, double b)
{
if (!::approximate (a, b))
{
std::ostringstream tmp;
tmp << a << " != " << b;
Assertion::warning (file, line, fun, tmp.str ().c_str ());
}
}
void
Assertion::balance (const char* file, int line, const char* fun,
double oldval, double newval, double growth)
{
if (!::approximate (newval, oldval + growth)
&& !::approximate (newval - oldval, growth))
{
std::ostringstream tmp;
tmp << "Internal balance check:\n"
<< "old value = " << oldval << "\n"
<< "new value = " << newval << " (should be "
<< oldval + growth << ")\n"
<< "growth = " << growth << " (should be "
<< newval - oldval << ")";
Assertion::failure (file, line, fun, tmp.str ().c_str ());
}
}
Assertion::Register::Register (Treelog& log)
: treelog (log)
{
for (unsigned int i = 0; i < logs ().size (); i++)
daisy_assert (&log != logs ()[i]);
logs ().push_back (&log);
}
Assertion::Register::~Register ()
{
if (counter)
{
for (auto i: *counter)
{
const std::string entry = i.first;
const int count = i.second;
std::ostringstream tmp;
tmp << entry << ": " << count << " total messages from this line";
warning (tmp.str ());
}
delete counter;
counter = NULL;
}
daisy_assert (find (logs ().begin (),
logs ().end (),
&treelog)
!= logs ().end ());
logs ().erase (find (logs ().begin (), logs ().end (), &treelog));
daisy_safe_assert (find (logs ().begin (), logs ().end (), &treelog)
== logs ().end ());
}
// assertion.C ends here.