forked from firepick1/FireSight
-
Notifications
You must be signed in to change notification settings - Fork 2
/
FireUtils.hpp
150 lines (128 loc) · 4.61 KB
/
FireUtils.hpp
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
/*
The MIT License (MIT)
Copyright (c) 2014 Karl Lew https://github.com/firepick1
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef FIREUTILS_HPP
#define FIREUTILS_HPP
#include <assert.h>
#include <iostream>
#include "FireLog.h"
#include "string.h"
#include <errno.h>
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
#ifndef bool
#define bool int
#endif
inline int fail(int rc) {
std::cout << "***ASSERT FAILED*** expected:0 actual:" << rc << std::endl;
return FALSE;
}
#define ASSERTFAIL(msg) {LOGERROR3("***ASSERTION FAILED*** %s in %s:%d",msg,__FILE__,__LINE__); throw "***ASSERTION FAILED*** " msg;}
#define ASSERT(e) ASSERTNONZERO(e)
#define ASSERTNOERRNO(exp) assertnoerrno((long) (exp), __FILE__,__LINE__)
#define ASSERTNONZERO(exp) assertnonzero((long) (exp), __FILE__, __LINE__)
#define ASSERTZERO(exp) assertzero((long) (exp), __FILE__, __LINE__)
#define ASSERTEQUAL(e,a) assertEqual((double)(e),(double)(a),0,__FILE__,__LINE__)
#define ASSERTEQUALT(e,a,t) assertEqual((e),(a),t,__FILE__,__LINE__)
inline void
assertnoerrno(long actual, const char* fname, long line) {
if (actual>=0) {
return;
}
const char *errstr;
switch (errno) {
case EACCES: errstr = "EACCESS"; break;
case EEXIST: errstr = "EEXIST"; break;
case EINVAL: errstr = "EINVAL"; break;
case ENFILE: errstr = "ENFILE"; break;
case ENOENT: errstr = "ENOENT"; break;
case ENOMEM: errstr = "ENOMEM"; break;
case ENOSPC: errstr = "ENOSPC"; break;
case EPERM: errstr = "EPERM"; break;
default: errstr = ""; break;
}
char buf[255];
snprintf(buf, sizeof(buf), "%s@%ld return:%ld errno:%s (%d) ",
fname, line, actual, errstr, errno);
LOGERROR(buf);
std::cerr << "***ASSERT FAILED*** " << buf << std::endl;
ASSERTFAIL("system errno");
}
inline void
assertzero(long actual, const char* fname, long line) {
if (actual==0) {
return;
}
char buf[255];
snprintf(buf, sizeof(buf), "%s@%ld expected zero", fname, line);
LOGERROR(buf);
std::cerr << "***ASSERT FAILED*** " << buf << std::endl;
ASSERTFAIL("expected zero");
}
inline void
assertnonzero(long actual, const char* fname, long line) {
if (actual) {
return;
}
char buf[255];
snprintf(buf, sizeof(buf), "%s@%ld expected non-zero", fname, line);
LOGERROR(buf);
std::cerr << "***ASSERT FAILED*** " << buf << std::endl;
ASSERTFAIL("expected nonzero");
}
inline void
assertEqual(double expected, double actual, double tolerance, const char* context, long line)
{
double diff = expected - actual;
if (-tolerance <= diff && diff <= tolerance) {
return;
}
char buf[255];
if (tolerance == 0) {
snprintf(buf, sizeof(buf), "%s expected:%ld actual:%ld line:%ld",
context, (long) expected, (long) actual, line);
} else {
snprintf(buf, sizeof(buf), "%s expected:%g actual:%g tolerance:%g line:%ld",
context, expected, actual, tolerance, line);
}
LOGERROR(buf);
std::cerr << "***ASSERT FAILED*** " << buf << std::endl;
ASSERTFAIL("expected equal");
}
#define ASSERTEQUALS(e,a) assertEqual(e,a,__FILE__,__LINE__)
inline void
assertEqual(const char* expected, const char* actual, const char* context, int line) {
if (actual && strcmp(expected, actual)==0) {
return;
}
char buf[255];
if (actual) {
snprintf(buf, sizeof(buf), "%s@%d expected:\"%s\" actual:\"%s\"", context, line, expected, actual);
} else {
snprintf(buf, sizeof(buf), "%s@%d expected:\"%s\" actual:NULL", context, line, expected);
}
LOGERROR(buf);
std::cerr << "***ASSERT FAILED*** " << buf << std::endl;
ASSERTFAIL("expected equal strings");
}
#endif