Bläddra i källkod

Script Unit Test should run everywhere

Thomas Buck 9 år sedan
förälder
incheckning
b085b79791
5 ändrade filer med 1185 tillägg och 30 borttagningar
  1. 3
    0
      ChangeLog.md
  2. 5
    1
      test/CMakeLists.txt
  3. 126
    29
      test/Script.cpp
  4. 1015
    0
      test/ScriptPayload.h
  5. 36
    0
      test/ScriptTest.h

+ 3
- 0
ChangeLog.md Visa fil

@@ -2,6 +2,9 @@
2 2
 
3 3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
4 4
 
5
+    [ 20140809 ]
6
+    * Script Unit Test brings it’s own scripts to test
7
+
5 8
     [ 20140808 ]
6 9
     * Added unit test for file-system utils
7 10
     * Moved Menu to Folder/File API

+ 5
- 1
test/CMakeLists.txt Visa fil

@@ -32,8 +32,12 @@ add_executable (tester_script EXCLUDE_FROM_ALL
32 32
     "../src/Exception.cpp" "../src/main.cpp"
33 33
 )
34 34
 
35
+find_package (ZLIB REQUIRED)
36
+include_directories (SYSTEM ${ZLIB_INCLUDE_DIRS})
37
+target_link_libraries (tester_script ${ZLIB_LIBRARIES})
38
+
35 39
 add_dependencies (check tester_script)
36
-#add_test (NAME test_script COMMAND tester_script)
40
+add_test (NAME test_script COMMAND tester_script)
37 41
 
38 42
 #################################################################
39 43
 

+ 126
- 29
test/Script.cpp Visa fil

@@ -6,10 +6,14 @@
6 6
  */
7 7
 
8 8
 #include <iostream>
9
+#include <cstring>
10
+#include <cstdlib>
11
+#include <zlib.h>
9 12
 
10 13
 #include "global.h"
11 14
 #include "utils/strings.h"
12 15
 #include "Script.h"
16
+#include "ScriptTest.h"
13 17
 
14 18
 #define printStrings(cnt, acc, name) { \
15 19
     std::cout << name << " (" << cnt << ")" << std::endl; \
@@ -44,10 +48,7 @@
44 48
 }
45 49
 
46 50
 namespace {
47
-    int test(const char *f, bool strings) {
48
-        Script s;
49
-        assertEqual(s.load(f), 0);
50
-
51
+    int printDataScript(Script &s, bool strings) {
51 52
         if (strings) {
52 53
             printStrings(s.levelCount(), s.getLevelName, "Level Names");
53 54
             printStrings(s.levelCount(), s.getLevelFilename, "Level Filenames");
@@ -92,46 +93,142 @@ namespace {
92 93
                 else
93 94
                     std::cout << "Script for \"" << s.getLevelName(i - 1) << "\" (" << i - 1 << "):" << std::endl;
94 95
                 int error = s.runScript(i);
95
-                if (error != 0)
96
+                if (error != 0) {
96 97
                     std::cout << "Returned " << error << "..." << std::endl;
98
+                    return error;
99
+                }
97 100
                 std::cout << std::endl;
98 101
             }
99 102
         }
100 103
 
101 104
         return 0;
102 105
     }
103
-}
104 106
 
105
-int main(int argc, char *argv[]) {
106
-    std::cout << "Tomb Raider 2:" << std::endl;
107
-    char *f = fullPath("~/.OpenRaider/paks/tr2/TOMBPC.DAT", 0);
108
-    int error = test(f, !((argc > 1) && (argv[1][0] == 's')));
109
-    delete [] f;
107
+    int test(const char *file, unsigned int n) {
108
+        Script s;
110 109
 
111
-    if (error != 0)
112
-        return error;
110
+        std::cout << "Testing " << testDescription[n] << std::endl;
113 111
 
114
-    std::cout << std::endl << "Tomb Raider 3:" << std::endl;
115
-    f = fullPath("~/.OpenRaider/paks/tr3/TOMBPC.DAT", 0);
116
-    error = test(f, !((argc > 1) && (argv[1][0] == 's')));
117
-    delete [] f;
112
+        if (s.load(file) != 0) {
113
+            std::cout << "Could not open file " << file << std::endl;
114
+            return 1;
115
+        }
118 116
 
119
-    if (error != 0)
120
-        return error;
117
+        if (s.gameStringCount() != testExpectedGameStringCount[n]) {
118
+            std::cout << "Game String Count " << s.gameStringCount() << " != " << testExpectedGameStringCount[n] << std::endl;
119
+            return 2;
120
+        }
121
+
122
+        if (s.pcStringCount() != testExpectedPlatformStringCount[n]) {
123
+            std::cout << "Platform String Count " << s.pcStringCount() << " != " << testExpectedPlatformStringCount[n] << std::endl;
124
+            return 3;
125
+        }
126
+
127
+        std::cout << "Success!" << std::endl << std::endl;
128
+        return 0;
129
+    }
130
+
131
+    void readPayloadChunk(const unsigned char *data, unsigned int size, const char *file) {
132
+        static const unsigned int bufferSize = 16384; // 16K should be enough for everybody :)
133
+        unsigned char buffer[bufferSize];
134
+
135
+        z_stream stream;
136
+        stream.zalloc = Z_NULL;
137
+        stream.zfree =  Z_NULL;
138
+        stream.opaque = Z_NULL;
139
+        assertEqual(inflateInit2(&stream, 16), Z_OK); // 16 -> gzip header
140
+
141
+        // Inflate data in one go
142
+        stream.avail_in = size;
143
+        stream.next_in = const_cast<unsigned char *>(data);
144
+        stream.avail_out = bufferSize;
145
+        stream.next_out = buffer;
146
+        assertEqual(inflate(&stream, Z_FINISH), Z_STREAM_END);
147
+        inflateEnd(&stream);
148
+
149
+        // Write buffer to file
150
+        std::ofstream s(file, std::ios_base::out | std::ios_base::binary);
151
+        s.write(reinterpret_cast<const char *>(buffer), bufferSize - stream.avail_out);
152
+    }
153
+
154
+    int runForPayload(unsigned int n, bool print, bool printData) {
155
+        assert(n < testPayloadCount);
156
+        // Get temp file name
157
+        char tmpFile[] = "/tmp/openraider_unit_test_0";
158
+        FILE *f;
159
+        while ((f = fopen(tmpFile, "r")) != NULL) {
160
+            fclose(f);
161
+            tmpFile[25]++;
162
+        }
163
+
164
+        std::cout << "Temporary test file: " << tmpFile << std::endl;
121 165
 
122
-    std::cout << std::endl << "Tomb Raider 2 PSX:" << std::endl;
123
-    f = fullPath("~/.OpenRaider/paks/tr2_psx/TOMBPSX.DAT", 0);
124
-    error = test(f, !((argc > 1) && (argv[1][0] == 's')));
125
-    delete [] f;
166
+        readPayloadChunk(testPayloads[n], testSizes[n], tmpFile);
126 167
 
127
-    if (error != 0)
168
+        int error = 0;
169
+        if (print) {
170
+            Script s;
171
+            error = s.load(tmpFile);
172
+            if (error == 0)
173
+                error = printDataScript(s, printData);
174
+        } else {
175
+            error = test(tmpFile, n);
176
+        }
177
+
178
+        remove(tmpFile);
128 179
         return error;
180
+    }
181
+}
182
+
183
+int main(int argc, char *argv[]) {
184
+    bool printHelp = false;
185
+    bool print = false;
186
+    bool printData = true;
187
+    int whichFile = -1;
188
+
189
+    if (argc == 3) {
190
+        if ((strcmp(argv[1], "--printData") == 0)
191
+                || (strcmp(argv[1], "--printScript") == 0)) {
192
+            print = true;
193
+            if (strcmp(argv[1], "--printScript") == 0) {
194
+                printData = false;
195
+            }
196
+            assert(testPayloadCount < 10);
197
+            if ((argv[2][0] >= '0') && ((unsigned int)argv[2][0] <= (testPayloadCount + '0'))) {
198
+                whichFile = argv[2][0] - '0';
199
+            }
200
+        } else {
201
+            printHelp = true;
202
+        }
203
+    } else if (argc != 1) {
204
+        printHelp = true;
205
+    }
129 206
 
130
-    std::cout << std::endl << "Tomb Raider 3 PSX:" << std::endl;
131
-    f = fullPath("~/.OpenRaider/paks/tr3_psx/TOMBPSX.DAT", 0);
132
-    error = test(f, !((argc > 1) && (argv[1][0] == 's')));
133
-    delete [] f;
207
+    if (printHelp) {
208
+        std::cout << "Usage:" << std::endl;
209
+        std::cout << "\t" << argv[0] << " [--printData | --printScript] [N | /path]" << std::endl;
210
+        return 1;
211
+    }
134 212
 
135
-    return error;
213
+    if (print) {
214
+        // Print single script
215
+        if (whichFile == -1) {
216
+            // From given path
217
+            Script s;
218
+            assertEqual(s.load(argv[2]), 0);
219
+            return printDataScript(s, printData);
220
+        } else {
221
+            // From payload
222
+            return runForPayload((unsigned int)whichFile, true, printData);
223
+        }
224
+    } else {
225
+        // Run test on all scripts in payload
226
+        for (unsigned int i = 0; i < testPayloadCount; i++) {
227
+            int error = runForPayload(i, false, false);
228
+            if (error != 0)
229
+                return error;
230
+        }
231
+        return 0;
232
+    }
136 233
 }
137 234
 

+ 1015
- 0
test/ScriptPayload.h
Filskillnaden har hållits tillbaka eftersom den är för stor
Visa fil


+ 36
- 0
test/ScriptTest.h Visa fil

@@ -0,0 +1,36 @@
1
+/*!
2
+ * \file test/ScriptTest.h
3
+ * \brief Game script loader unit test payload
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#ifndef _SCRIPT_TEST_H
9
+#define _SCRIPT_TEST_H
10
+
11
+#include "ScriptPayload.h"
12
+
13
+static const unsigned int testPayloadCount = 4; // 4 gzipped files in data block
14
+
15
+static const char *testDescription[testPayloadCount] = {
16
+    "TR2 PC", "TR2 PSX", "TR3 PC", "TR3 PSX"
17
+};
18
+
19
+static const unsigned char *testPayloads[testPayloadCount] = {
20
+    tr2_pc_dat_gz, tr2_psx_dat_gz, tr3_pc_dat_gz, tr3_psx_dat_gz
21
+};
22
+
23
+static const unsigned int testSizes[testPayloadCount] = {
24
+    sizeof(tr2_pc_dat_gz), sizeof(tr2_psx_dat_gz), sizeof(tr3_pc_dat_gz), sizeof(tr3_psx_dat_gz)
25
+};
26
+
27
+static const unsigned int testExpectedGameStringCount[testPayloadCount] = {
28
+    89, 89, 92, 92
29
+};
30
+
31
+static const unsigned int testExpectedPlatformStringCount[testPayloadCount] = {
32
+    41, 80, 41, 80
33
+};
34
+
35
+#endif
36
+

Laddar…
Avbryt
Spara