Ver código fonte

Improved global assert

Thomas Buck 9 anos atrás
pai
commit
533b83eb78
7 arquivos alterados com 57 adições e 59 exclusões
  1. 1
    0
      ChangeLog.md
  2. 53
    7
      include/global.h
  3. 0
    27
      src/main.cpp
  4. 2
    2
      test/CMakeLists.txt
  5. 0
    1
      test/Script.cpp
  6. 1
    2
      test/binary.cpp
  7. 0
    20
      test/test.h

+ 1
- 0
ChangeLog.md Ver arquivo

@@ -5,6 +5,7 @@
5 5
     [ 20140806 ]
6 6
     * Improved Script reader and its Unit Test
7 7
     * Successfully parsing the TR2 Script strings!
8
+    * Now using assertEqual globally
8 9
 
9 10
     [ 20140804 ]
10 11
     * Added preliminary Tomb Raider Script parser

+ 53
- 7
include/global.h Ver arquivo

@@ -107,16 +107,62 @@ KeyboardButton stringToKeyboardButton(const char *key);
107 107
 
108 108
 // If available, use our own assert that prints the call stack
109 109
 #if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE) && defined(HAVE_BACKTRACE_SYMBOLS)
110
+
110 111
 #ifndef NDEBUG
111
-[[noreturn]] void assertImplementation(const char *exp, const char *file, int line);
112
-#define assert(x) (void)((x) || (assertImplementation(#x, __FILE__, __LINE__),0))
113
-#else
112
+
113
+#include <iostream>
114
+#include <execinfo.h>
115
+
116
+template<typename T, typename U>
117
+[[noreturn]] void assertEqualImplementation(const char *exp, T a, U b, const char *file, int line, bool print) {
118
+    const unsigned int maxSize = 128;
119
+    void *callstack[maxSize];
120
+    int frames = backtrace(callstack, maxSize);
121
+    char **strs = backtrace_symbols(callstack, frames);
122
+
123
+    std::cout << std::endl << "assertion failed:" << std::endl;
124
+    std::cout << "\t" << exp << std::endl;
125
+    if (print)
126
+        std::cout << "\t (" << a << " != " << b << ")" << std::endl;
127
+    std::cout << "in " << file << ":" << line << std::endl << std::endl;
128
+
129
+    for (int i = 0; i < frames; i++)
130
+        std::cout << strs[i] << std::endl;
131
+
132
+    delete [] strs;
133
+    abort();
134
+}
135
+
136
+// Evaluating x or y could have side-effects
137
+// So we only do it once!
138
+
139
+#define assert(x) { \
140
+    auto assertEvalTemp = x; \
141
+    if (!assertEvalTemp) \
142
+        assertEqualImplementation(#x, assertEvalTemp, true, __FILE__, __LINE__, false); \
143
+}
144
+
145
+#define assertEqual(x, y) { \
146
+    auto assertEvalTemp = x; \
147
+    auto assertEvalTemp2 = y; \
148
+    if (assertEvalTemp != assertEvalTemp2) \
149
+        assertEqualImplementation(#x " == " #y, assertEvalTemp, assertEvalTemp2, __FILE__, __LINE__, true); \
150
+}
151
+
152
+#else // NDEBUG
153
+
114 154
 #define assert(x)
115
-#endif
116
-#else
155
+#define assertEqual(x, y)
156
+
157
+#endif // NDEBUG
158
+
159
+#else // EXECINFO
160
+
117 161
 // Fall back to the default C assert
118 162
 #include <cassert>
119
-#endif
163
+#define assertEqual(x, y) assert((x) == (y))
120 164
 
121
-#endif
165
+#endif // EXECINFO
166
+
167
+#endif // _GLOBAL_H_
122 168
 

+ 0
- 27
src/main.cpp Ver arquivo

@@ -23,8 +23,6 @@
23 23
 #include "utils/strings.h"
24 24
 #include "utils/time.h"
25 25
 
26
-#ifndef UNIT_TEST
27
-
28 26
 #ifdef USING_AL
29 27
 #include "SoundAL.h"
30 28
 #else
@@ -364,28 +362,3 @@ KeyboardButton stringToKeyboardButton(const char *key) {
364 362
     return unknownKey;
365 363
 }
366 364
 
367
-#endif // UNIT_TEST
368
-
369
-#if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE) && defined(HAVE_BACKTRACE_SYMBOLS) && (!defined(NDEBUG))
370
-
371
-#include <execinfo.h>
372
-
373
-void assertImplementation(const char *exp, const char *file, int line) {
374
-    const unsigned int maxSize = 128;
375
-    void *callstack[maxSize];
376
-    int frames = backtrace(callstack, maxSize);
377
-    char **strs = backtrace_symbols(callstack, frames);
378
-
379
-    std::cout << std::endl << "assertion failed:" << std::endl;
380
-    std::cout << "\t" << exp << std::endl;
381
-    std::cout << "in " << file << ":" << line << std::endl << std::endl;
382
-
383
-    for (int i = 0; i < frames; i++)
384
-        std::cout << strs[i] << std::endl;
385
-
386
-    delete [] strs;
387
-    abort();
388
-}
389
-
390
-#endif
391
-

+ 2
- 2
test/CMakeLists.txt Ver arquivo

@@ -7,7 +7,7 @@ add_custom_target (check COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure)
7 7
 #################################################################
8 8
 
9 9
 add_executable (tester_binary EXCLUDE_FROM_ALL
10
-    "binary.cpp" "../src/utils/binary.cpp" "../src/main.cpp"
10
+    "binary.cpp" "../src/utils/binary.cpp"
11 11
 )
12 12
 add_dependencies (check tester_binary)
13 13
 add_test (NAME test_binary COMMAND tester_binary)
@@ -15,7 +15,7 @@ add_test (NAME test_binary COMMAND tester_binary)
15 15
 #################################################################
16 16
 
17 17
 add_executable (tester_script EXCLUDE_FROM_ALL
18
-    "Script.cpp" "../src/Script.cpp" "../src/main.cpp"
18
+    "Script.cpp" "../src/Script.cpp"
19 19
     "../src/utils/binary.cpp" "../src/utils/strings.cpp"
20 20
 )
21 21
 

+ 0
- 1
test/Script.cpp Ver arquivo

@@ -8,7 +8,6 @@
8 8
 #include <iostream>
9 9
 
10 10
 #include "global.h"
11
-#include "test.h"
12 11
 #include "utils/strings.h"
13 12
 #include "Script.h"
14 13
 

+ 1
- 2
test/binary.cpp Ver arquivo

@@ -10,7 +10,6 @@
10 10
 #include <fstream>
11 11
 
12 12
 #include "global.h"
13
-#include "test.h"
14 13
 #include "utils/binary.h"
15 14
 
16 15
 
@@ -57,7 +56,7 @@ namespace {
57 56
         assertEqual(file.readFloat(), f1);
58 57
         assertEqual(file.readFloat(), f2);
59 58
 
60
-        assert(file.tell() == 38);
59
+        assertEqual(file.tell(), 38);
61 60
 
62 61
         return 0;
63 62
     }

+ 0
- 20
test/test.h Ver arquivo

@@ -1,20 +0,0 @@
1
-/*!
2
- * \file test/test.h
3
- * \brief Common Unit Test helpers
4
- *
5
- * \author xythobuz
6
- */
7
-
8
-#ifndef _TEST_H
9
-#define _TEST_H
10
-
11
-#include <iostream>
12
-
13
-#define assertEqual(x, y) if (x != y) { \
14
-    std::cout << "Assertion failed:" << std::endl; \
15
-    std::cout << #x << " == " << #y << " (" << x << ", " << y << ")" << std::endl; \
16
-    return 1; \
17
-}
18
-
19
-#endif
20
-

Carregando…
Cancelar
Salvar