-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassert.cpp
86 lines (68 loc) · 1.58 KB
/
assert.cpp
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
#include "assert.h"
#if defined(_MSC_VER)
//#include <Windows.h>
#endif
void assertFrom(const char* cond, const char* function, const char* file, int line, const char* msg){
printf("\nASSERTION!\n");
if (cond != nullptr){
printf("Condition: %s\n", cond);
}
if(msg != nullptr){
printf("Message: %s\n", msg);
}
printf("File: %s\nLine: %d\nFunction: %s\n", file, line, function);
#if defined(EXIT_ON_ASSERT)
exit(-1);
#else
#if defined(_MSC_VER)
DEBUG_BREAK();
#else
printf("Type b to break, or s to skip.\n");
while(true){
char response[256] = {0};
char* retVal = fgets(response, sizeof(response) - 1, stdin);
ASSERT(retVal != nullptr);
if(response[0] == 'b'){
DEBUG_BREAK();
break;
}
else if(response[0] == 's'){
break;
}
else{
printf("Please enter b or s.\n");
}
}
#endif
#endif
}
// We skip this on MSVC, since it has no signal handlers...
// we should probably get a better system in place
#if defined(ASSERT_TEST_MAIN) && !defined(_MSC_VER)
int assertCount = 0;
void CheckAssertCount(int expected){
if(assertCount != expected){
printf("Expected assertions: %d, actually: %d\n", expected, assertCount);
exit(-1);
}
}
void TrapHandler(int sigNum){
assertCount++;
BNS_UNUSED(sigNum);
}
int main(){
signal(SIGTRAP, TrapHandler);
CheckAssertCount(0);
ASSERT(1 == 1);
CheckAssertCount(0);
ASSERT(1 == 3);
CheckAssertCount(1);
ASSERT_MSG(1 == 5, "Hi.%s\n", "Bye.");
CheckAssertCount(2);
ASSERT_WARN("%s", "This is a warning.");
CheckAssertCount(3);
ASSERT_MSG(1 == 1, "%s", "Should noy go off.");
CheckAssertCount(3);
return 0;
}
#endif