Browse Source

Unit Tests are back!

Thomas Buck 9 years ago
parent
commit
ca11cfe73a

+ 7
- 3
CMakeLists.txt View File

@@ -72,6 +72,10 @@ endif (NOT ${GIT_SHA1} STREQUAL "GITDIR-NOTFOUND")
72 72
 
73 73
 #################################################################
74 74
 
75
+enable_testing ()
76
+
77
+#################################################################
78
+
75 79
 # Add subdirectories
76 80
 add_subdirectory (src)
77 81
 
@@ -101,13 +105,13 @@ if (${CMAKE_GENERATOR} STREQUAL "Unix Makefiles")
101 105
     set (CHECK_NORMAL "--enable=information,warning,performance,portability")
102 106
     set (CHECK_FULL "--enable=all")
103 107
     set (CHECK_CONFIG "--check-config")
104
-    add_custom_target (check echo "Running cppcheck..."
108
+    add_custom_target (cppcheck echo "Running cppcheck..."
105 109
         COMMAND cppcheck ${CHECK_FLAGS} "-I${PROJECT_SOURCE_DIR}/include" ${CHECK_STD} ${CHECK_NORMAL} ${PROJECT_SOURCE_DIR}
106 110
     )
107
-    add_custom_target (checkFull echo "Running full cppcheck..."
111
+    add_custom_target (cppcheckFull echo "Running full cppcheck..."
108 112
         COMMAND cppcheck ${CHECK_FLAGS} "-I${PROJECT_SOURCE_DIR}/include" ${CHECK_STD} ${CHECK_FULL} ${PROJECT_SOURCE_DIR}
109 113
     )
110
-    add_custom_target (checkConfig echo "Checking cppcheck config..."
114
+    add_custom_target (cppcheckConfig echo "Checking cppcheck config..."
111 115
         COMMAND cppcheck ${CHECK_FLAGS} "-I${PROJECT_SOURCE_DIR}/include" ${CHECK_CONFIG} ${PROJECT_SOURCE_DIR}
112 116
     )
113 117
 

+ 6
- 0
ChangeLog.md View File

@@ -2,6 +2,12 @@
2 2
 
3 3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
4 4
 
5
+    [ 20140729 ]
6
+    * Unit Tests are back, currently only for the binary reader
7
+    * Some cmake build system improvements:
8
+      - deps/math/utils libs are now so-called object libraries
9
+      - added ctest unit testing
10
+
5 11
     [ 20140728 ]
6 12
     * Implemented binary file reading utility in preparation of
7 13
       TombRaider.cpp/h rewrite

+ 3
- 1
README.md View File

@@ -71,7 +71,9 @@ Do a standard cmake out-of-source build to get a debug configuration and then ru
71 71
 
72 72
 On Mac OS X, running `make install` will produce a (more or less) distributable OpenRaider.app bundle.
73 73
 
74
-You can run a static analysis with cppcheck using `make check`, `make checkFull` or `make checkConfig`.
74
+Run `make check` to build and run the unit tests.
75
+
76
+You can run a static analysis with cppcheck using `make cppcheck`, `make cppcheckFull` or `make cppcheckConfig`.
75 77
 
76 78
 A Doxygen API documentation can be created with `make doc`.
77 79
 

+ 1
- 0
TODO.md View File

@@ -17,6 +17,7 @@
17 17
 
18 18
 * Support SSE with other compilers than Clang (src/CMakeLists.txt)
19 19
 * Visual C++ compiler flags? (CMakeLists.txt)
20
+* Better test integration
20 21
 
21 22
 ## Future Features
22 23
 

+ 2
- 0
include/utils/binary.h View File

@@ -31,6 +31,8 @@ public:
31 31
     int64_t read64();
32 32
     uint64_t readU64();
33 33
 
34
+    float readFloat();
35
+
34 36
 private:
35 37
     std::ifstream file;
36 38
 };

+ 8
- 8
src/CMakeLists.txt View File

@@ -156,12 +156,12 @@ if (APPLE)
156 156
     # Copy Icon
157 157
     set (MAC_ICON "../cmake/AppIcon.icns")
158 158
     set_source_files_properties (${MAC_ICON} PROPERTIES MACOSX_PACKAGE_LOCATION Resources)
159
-    set (SRCS ${SRCS} ${MAC_ICON})
159
+    set (RESRCS ${RESRCS} ${MAC_ICON})
160 160
 
161 161
     # Copy Data
162 162
     set (MAC_DATA "../data/font.pc" "../data/OpenRaider.ini" "../data/test.ttf" "../data/splash.tga")
163 163
     set_source_files_properties (${MAC_DATA} PROPERTIES MACOSX_PACKAGE_LOCATION Resources/defaults)
164
-    set (SRCS ${SRCS} ${MAC_DATA})
164
+    set (RESRCS ${RESRCS} ${MAC_DATA})
165 165
 endif (APPLE)
166 166
 
167 167
 #################################################################
@@ -204,10 +204,15 @@ set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${OpenRaider_CXX_FLAGS}
204 204
 # Add subdirectories
205 205
 add_subdirectory ("deps")
206 206
 add_subdirectory ("math")
207
+add_subdirectory ("test")
207 208
 add_subdirectory ("utils")
208 209
 
209 210
 # Add Executable
210
-add_executable (OpenRaider MACOSX_BUNDLE ${SRCS})
211
+add_library (OpenRaider_all OBJECT ${SRCS})
212
+add_executable (OpenRaider MACOSX_BUNDLE ${RESRCS}
213
+    $<TARGET_OBJECTS:OpenRaider_all> $<TARGET_OBJECTS:OpenRaider_deps>
214
+    $<TARGET_OBJECTS:OpenRaider_math> $<TARGET_OBJECTS:OpenRaider_utils>
215
+)
211 216
 
212 217
 #################################################################
213 218
 
@@ -257,11 +262,6 @@ if (UNIX)
257 262
     set (LIBS ${LIBS} m)
258 263
 endif (UNIX)
259 264
 
260
-# Add utils Library
261
-set (LIBS ${LIBS} OpenRaider_deps)
262
-set (LIBS ${LIBS} OpenRaider_math)
263
-set (LIBS ${LIBS} OpenRaider_utils)
264
-
265 265
 # Link to all found libs
266 266
 target_link_libraries (OpenRaider ${LIBS})
267 267
 

+ 1
- 1
src/deps/CMakeLists.txt View File

@@ -2,5 +2,5 @@
2 2
 set (DEPS_SRCS ${DEPS_SRCS} "commander/commander.c")
3 3
 
4 4
 # Add library
5
-add_library (OpenRaider_deps ${DEPS_SRCS})
5
+add_library (OpenRaider_deps OBJECT ${DEPS_SRCS})
6 6
 

+ 1
- 1
src/math/CMakeLists.txt View File

@@ -5,5 +5,5 @@ set (MATH_SRCS ${MATH_SRCS} "Quaternion.cpp")
5 5
 set (MATH_SRCS ${MATH_SRCS} "Vector3d.cpp")
6 6
 
7 7
 # Add library
8
-add_library (OpenRaider_math ${MATH_SRCS})
8
+add_library (OpenRaider_math OBJECT ${MATH_SRCS})
9 9
 

+ 16
- 0
src/test/CMakeLists.txt View File

@@ -0,0 +1,16 @@
1
+set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenRaider_CXX_FLAGS}")
2
+set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${OpenRaider_CXX_FLAGS} ${OpenRaider_CXX_FLAGS_DEBUG}")
3
+set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${OpenRaider_CXX_FLAGS} ${OpenRaider_CXX_FLAGS_RELEASE}")
4
+
5
+add_custom_target (check COMMAND ${CMAKE_CTEST_COMMAND} -V)
6
+
7
+#################################################################
8
+
9
+add_executable (tester_binary EXCLUDE_FROM_ALL
10
+    "binary.cpp" "../utils/binary.cpp"
11
+)
12
+add_dependencies (check tester_binary)
13
+add_test (NAME test_binary COMMAND tester_binary)
14
+
15
+#################################################################
16
+

+ 91
- 0
src/test/binary.cpp View File

@@ -0,0 +1,91 @@
1
+/*!
2
+ * \file test/binary.cpp
3
+ * \brief Binary Unit Test
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#include <cstdio>
9
+#include <iostream>
10
+#include <fstream>
11
+
12
+#include "utils/binary.h"
13
+
14
+
15
+namespace {
16
+    const char testData[] = {
17
+        // Unsigned Integers
18
+        -1,                     // u 8 -> 255
19
+        -1, -1,                 // u16 -> 65535
20
+        42, 0, 0, 0,            // u32 -> 42
21
+        1, 0, 0, 0, 0, 0, 0, 0, // u64 -> 1
22
+
23
+        // Signed Integers
24
+        -1,                     //  8 -> -1
25
+        92, -2,                 // 16 -> -420
26
+        102, -3, -1, -1,        // 32 -> -666
27
+        -5, -1, -1, -1,
28
+        -1, -1, -1, -1          // 64 -> -5
29
+    };
30
+
31
+    float f1 = 3.1415926f;
32
+    float f2 = 42.23f;
33
+
34
+    void fillFile(const char *name) {
35
+        std::ofstream file(name, std::ios_base::out | std::ios_base::binary);
36
+        file.write(testData, sizeof(testData) / sizeof(testData[0]));
37
+        file.write(reinterpret_cast<char *>(&f1), sizeof(f1));
38
+        file.write(reinterpret_cast<char *>(&f2), sizeof(f2));
39
+    }
40
+
41
+    int assertImplementation(const char *exp, const char *file, int line) {
42
+        std::cout << "Failed: \"" << exp << "\" in " << file << " at " << line << std::endl;
43
+        return 1;
44
+    }
45
+
46
+#define assert(x) if (!(x)) { return assertImplementation(#x, __FILE__, __LINE__); }
47
+
48
+    int test(const char *name) {
49
+        BinaryFile file(name);
50
+
51
+        uint8_t a = file.readU8();
52
+        uint16_t b = file.readU16();
53
+        uint32_t c = file.readU32();
54
+        uint64_t d = file.readU64();
55
+
56
+        int8_t e = file.read8();
57
+        int16_t f = file.read16();
58
+        int32_t g = file.read32();
59
+        int64_t h = file.read64();
60
+
61
+        float i = file.readFloat();
62
+        float j = file.readFloat();
63
+
64
+        assert(a == 255);
65
+        assert(b == 65535);
66
+        assert(c == 42);
67
+        assert(d == 1);
68
+
69
+        assert(e == -1);
70
+        assert(f == -420);
71
+        assert(g == -666);
72
+        assert(h == -5);
73
+
74
+        assert(i == f1);
75
+        assert(j == f2);
76
+
77
+        return 0;
78
+    }
79
+}
80
+
81
+int main(int argc, char *argv[]) {
82
+    char *tmpFile = tmpnam(NULL);
83
+    std::cout << "Temporary test-file: " << tmpFile << std::endl;
84
+
85
+    fillFile(tmpFile);
86
+    int error = test(tmpFile);
87
+    remove(tmpFile);
88
+
89
+    return error;
90
+}
91
+

+ 1
- 1
src/utils/CMakeLists.txt View File

@@ -11,5 +11,5 @@ if (USING_PNG)
11 11
 endif (USING_PNG)
12 12
 
13 13
 # Add library
14
-add_library (OpenRaider_utils ${UTIL_SRCS})
14
+add_library (OpenRaider_utils OBJECT ${UTIL_SRCS})
15 15
 

+ 67
- 74
src/utils/binary.cpp View File

@@ -5,16 +5,8 @@
5 5
  * \author xythobuz
6 6
  */
7 7
 
8
-#include "global.h"
9 8
 #include "utils/binary.h"
10 9
 
11
-/*! \fixme Left-Shift with signed integer is undefined!
12
- *  Is there a portable way to read multi-byte signed integers?
13
- *  Without having to detect the endianness at run-time?
14
- */
15
-const int bigendiandetection = 1;
16
-#define ISBIGENDIAN() ((*(char *)&bigendiandetection) == 0)
17
-
18 10
 BinaryFile::BinaryFile(const char *f) {
19 11
     file.open(f, std::ios_base::in | std::ios_base:: binary);
20 12
 }
@@ -28,94 +20,95 @@ long long BinaryFile::tell() {
28 20
 }
29 21
 
30 22
 void BinaryFile::seek(long long pos) {
31
-    assert(pos >= 0);
32 23
     file.seekg(pos);
33 24
 }
34 25
 
35
-int8_t BinaryFile::read8() {
36
-    int8_t ret;
37
-    file.read(reinterpret_cast<char *>(&ret), sizeof(ret));
26
+uint8_t BinaryFile::readU8() {
27
+    uint8_t ret;
28
+    char *c = reinterpret_cast<char *>(&ret);
29
+    file.read(c, 1);
38 30
     return ret;
39 31
 }
40 32
 
41
-uint8_t BinaryFile::readU8() {
42
-    uint8_t ret;
43
-    file.read(reinterpret_cast<char *>(&ret), sizeof(ret));
33
+uint16_t BinaryFile::readU16() {
34
+    uint8_t a = readU8();
35
+    uint8_t b = readU8();
36
+    return ((uint16_t)a | (uint16_t)(b << 8));
37
+}
38
+
39
+uint32_t BinaryFile::readU32() {
40
+    uint16_t a = readU16();
41
+    uint16_t b = readU16();
42
+    return ((uint32_t)a | (uint32_t)(b << 16));
43
+}
44
+
45
+uint64_t BinaryFile::readU64() {
46
+    uint32_t a = readU32();
47
+    uint32_t b = readU32();
48
+    return ((uint64_t)a | ((uint64_t)b << 32));
49
+}
50
+
51
+float BinaryFile::readFloat() {
52
+    uint32_t val = readU32();
53
+    char *a = reinterpret_cast<char *>(&val);
54
+
55
+    float ret;
56
+    char *b = reinterpret_cast<char *>(&ret);
57
+
58
+    for (int i = 0; i < 4; i++)
59
+        b[i] = a[i];
60
+
44 61
     return ret;
45 62
 }
46 63
 
47
-int16_t BinaryFile::read16() {
48
-    int16_t ret;
49
-    file.read(reinterpret_cast<char *>(&ret), sizeof(ret));
50
-    if (ISBIGENDIAN()) {
51
-        char *p = reinterpret_cast<char *>(&ret);
52
-        char tmp = p[0];
53
-        p[0] = p[1];
54
-        p[1] = tmp;
64
+namespace {
65
+    /*! \fixme Left-Shift with signed integer is undefined!
66
+     *  So we can't use the same method as for unsigned integers.
67
+     *  Is there a portable way to read multi-byte signed integers,
68
+     *  without having to detect the endianness at run-time?
69
+     */
70
+    const int bigendiandetection = 1;
71
+#define ISBIGENDIAN() ((*(char *)&bigendiandetection) == 0)
72
+
73
+    void swapByteOrder(char *d, unsigned int n) {
74
+        if (ISBIGENDIAN()) {
75
+            for (unsigned int i = 0; i < (n / 2); i++) {
76
+                char tmp = d[i];
77
+                d[i] = d[n - i - 1];
78
+                d[n - i - 1] = tmp;
79
+            }
80
+        }
55 81
     }
82
+}
83
+
84
+int8_t BinaryFile::read8() {
85
+    int8_t ret;
86
+    char *p = reinterpret_cast<char *>(&ret);
87
+    file.read(p, sizeof(ret));
56 88
     return ret;
57 89
 }
58 90
 
59
-uint16_t BinaryFile::readU16() {
60
-    uint16_t a = readU8();
61
-    uint16_t b = readU8();
62
-    return (uint16_t)(a | (b << 8));
91
+int16_t BinaryFile::read16() {
92
+    int16_t ret;
93
+    char *p = reinterpret_cast<char *>(&ret);
94
+    file.read(p, sizeof(ret));
95
+    swapByteOrder(p, 2);
96
+    return ret;
63 97
 }
64 98
 
65 99
 int32_t BinaryFile::read32() {
66 100
     int32_t ret;
67
-    file.read(reinterpret_cast<char *>(&ret), sizeof(ret));
68
-    if (ISBIGENDIAN()) {
69
-        char *p = reinterpret_cast<char *>(&ret);
70
-        char tmp = p[0];
71
-        p[0] = p[3];
72
-        p[3] = tmp;
73
-        tmp = p[1];
74
-        p[1] = p[2];
75
-        p[2] = tmp;
76
-    }
101
+    char *p = reinterpret_cast<char *>(&ret);
102
+    file.read(p, sizeof(ret));
103
+    swapByteOrder(p, 4);
77 104
     return ret;
78 105
 }
79 106
 
80
-uint32_t BinaryFile::readU32() {
81
-    uint32_t a = readU8();
82
-    uint32_t b = readU8();
83
-    uint32_t c = readU8();
84
-    uint32_t d = readU8();
85
-    return (uint32_t)(a | (b << 8) | (c << 16) | (d << 24));
86
-}
87
-
88 107
 int64_t BinaryFile::read64() {
89 108
     int64_t ret;
90
-    file.read(reinterpret_cast<char *>(&ret), sizeof(ret));
91
-    if (ISBIGENDIAN()) {
92
-        char *p = reinterpret_cast<char *>(&ret);
93
-        char tmp = p[0];
94
-        p[0] = p[7];
95
-        p[7] = tmp;
96
-        tmp = p[1];
97
-        p[1] = p[6];
98
-        p[6] = tmp;
99
-        tmp = p[2];
100
-        p[2] = p[5];
101
-        p[5] = tmp;
102
-        tmp = p[3];
103
-        p[3] = p[4];
104
-        p[4] = tmp;
105
-    }
109
+    char *p = reinterpret_cast<char *>(&ret);
110
+    file.read(p, sizeof(ret));
111
+    swapByteOrder(p, 8);
106 112
     return ret;
107 113
 }
108 114
 
109
-uint64_t BinaryFile::readU64() {
110
-    uint64_t a = readU8();
111
-    uint64_t b = readU8();
112
-    uint64_t c = readU8();
113
-    uint64_t d = readU8();
114
-    uint64_t e = readU8();
115
-    uint64_t f = readU8();
116
-    uint64_t g = readU8();
117
-    uint64_t h = readU8();
118
-    return (uint64_t)(a | (b << 8) | (c << 16) | (d << 24)
119
-            | (e << 32) | (f << 40) | (g << 48) | (h << 56));
120
-}
121
-

Loading…
Cancel
Save