Mercurial > pub > dyncall > dyncall
comparison test/plain_c++/test_main.cc @ 324:dd78bd0152af
- removal of never-adopted mini-test framework stub
- test/plain: better output and cleanup
- test/plain_c++: better output and re-enabled this calls for other platforms than windows (was disabled by mistake)
author | Tassilo Philipp |
---|---|
date | Thu, 21 Nov 2019 12:50:37 +0100 |
parents | f5577f6bf97a |
children | da4b267c10bf |
comparison
equal
deleted
inserted
replaced
323:6ffb6a00cf55 | 324:dd78bd0152af |
---|---|
1 /* | 1 /* |
2 | 2 |
3 Package: dyncall | 3 Package: dyncall |
4 Library: test | 4 Library: test |
5 File: test/plain_c++/test_main.cc | 5 File: test/plain_c++/test_main.cc |
6 Description: | 6 Description: |
7 License: | 7 License: |
8 | 8 |
9 Copyright (c) 2007-2018 Daniel Adler <dadler@uni-goettingen.de>, | 9 Copyright (c) 2007-2019 Daniel Adler <dadler@uni-goettingen.de>, |
10 Tassilo Philipp <tphilipp@potion-studios.com> | 10 Tassilo Philipp <tphilipp@potion-studios.com> |
11 | 11 |
12 Permission to use, copy, modify, and distribute this software for any | 12 Permission to use, copy, modify, and distribute this software for any |
13 purpose with or without fee is hereby granted, provided that the above | 13 purpose with or without fee is hereby granted, provided that the above |
14 copyright notice and this permission notice appear in all copies. | 14 copyright notice and this permission notice appear in all copies. |
24 */ | 24 */ |
25 | 25 |
26 | 26 |
27 | 27 |
28 | 28 |
29 #include "../common/test_framework.h" | |
30 #include "../../dyncall/dyncall.h" | 29 #include "../../dyncall/dyncall.h" |
31 #include "../common/platformInit.h" | 30 #include "../common/platformInit.h" |
32 #include "../common/platformInit.c" /* Impl. for functions only used in this translation unit */ | 31 #include "../common/platformInit.c" /* Impl. for functions only used in this translation unit */ |
33 | 32 |
34 | 33 |
35 /* ------------------------------------------------------------------------- | 34 #include <signal.h> |
36 * test: identity function calls | 35 #include <setjmp.h> |
36 | |
37 jmp_buf jbuf; | |
38 | |
39 | |
40 void segv_handler(int sig) | |
41 { | |
42 longjmp(jbuf, 1); | |
43 } | |
44 | |
45 | |
46 /* ------------------------------------------------------------------------- | |
47 * test: identity function calls | |
37 * ------------------------------------------------------------------------- */ | 48 * ------------------------------------------------------------------------- */ |
38 | 49 |
39 #define DEF_FUNCS(API,NAME) \ | 50 #define DEF_FUNCS(API,NAME) \ |
40 void API fun_##NAME##_v() { } \ | 51 void API fun_##NAME##_v() { } \ |
41 DCbool API fun_##NAME##_b(DCbool x) { return x; } \ | 52 DCbool API fun_##NAME##_b(DCbool x) { return x; } \ |
47 DCpointer API fun_##NAME##_p(DCpointer x) { return x; } | 58 DCpointer API fun_##NAME##_p(DCpointer x) { return x; } |
48 | 59 |
49 /* __cdecl */ | 60 /* __cdecl */ |
50 | 61 |
51 #if !defined(DC__OS_Win32) | 62 #if !defined(DC__OS_Win32) |
52 # define __declspec(X) | |
53 # define __cdecl | 63 # define __cdecl |
54 #endif | 64 #endif |
55 | 65 |
56 | 66 |
57 /* ------------------------------------------------------------------------- | 67 /* ------------------------------------------------------------------------- |
58 * test: identity this calls | 68 * test: identity this calls |
59 * ------------------------------------------------------------------------- */ | 69 * ------------------------------------------------------------------------- */ |
60 | 70 |
61 union ValueUnion | 71 union ValueUnion |
62 { | 72 { |
63 DCbool B; | 73 DCbool B; |
73 | 83 |
74 // #define VTBI_DESTRUCTOR 0 | 84 // #define VTBI_DESTRUCTOR 0 |
75 | 85 |
76 /* | 86 /* |
77 * the layout of the VTable is non-standard and it is not clear what is the initial real first method index. | 87 * the layout of the VTable is non-standard and it is not clear what is the initial real first method index. |
78 * so for it turns out that: | 88 * so far it turns out that: |
79 * on vc/x86 : 1 | 89 * on vc/x86 : 1 |
80 * on GCC/x86 : 2 | 90 * on GCC/x86 : 2 |
81 */ | 91 */ |
82 | 92 |
83 #if defined DC__C_MSVC | 93 #if defined DC__C_MSVC |
148 private: | 158 private: |
149 ValueUnion mValue; | 159 ValueUnion mValue; |
150 }; | 160 }; |
151 | 161 |
152 template<typename T> | 162 template<typename T> |
153 void testCallValue(DCCallVM* pc) | 163 bool testCallValue(DCCallVM* pc, const char* name) |
154 { | 164 { |
165 bool r = true, b; | |
155 T o; | 166 T o; |
156 T* pThis = &o; | 167 T* pThis = &o; |
157 DCpointer* vtbl = *( (DCpointer**) pThis ); /* vtbl is located at beginning of class */ | 168 DCpointer* vtbl = *( (DCpointer**) pThis ); /* vtbl is located at beginning of class */ |
158 | 169 |
159 /* set/get bool (TRUE) */ | 170 /* set/get bool (TRUE) */ |
162 dcArgPointer(pc, pThis); | 173 dcArgPointer(pc, pThis); |
163 dcArgBool(pc,DC_TRUE); | 174 dcArgBool(pc,DC_TRUE); |
164 dcCallVoid(pc, vtbl[VTBI_SET_BOOL] ); | 175 dcCallVoid(pc, vtbl[VTBI_SET_BOOL] ); |
165 dcReset(pc); | 176 dcReset(pc); |
166 dcArgPointer(pc, pThis); | 177 dcArgPointer(pc, pThis); |
167 DC_TEST( dcCallBool(pc, vtbl[VTBI_GET_BOOL] ) == DC_TRUE ); | 178 b = ( dcCallBool(pc, vtbl[VTBI_GET_BOOL] ) == DC_TRUE ); |
179 printf("bt (%s): %d\n", name, b); | |
180 r = r && b; | |
181 | |
168 /* set/get bool (FALSE) */ | 182 /* set/get bool (FALSE) */ |
169 | 183 |
170 dcReset(pc); | 184 dcReset(pc); |
171 dcArgPointer(pc, pThis); | 185 dcArgPointer(pc, pThis); |
172 dcArgBool(pc,DC_FALSE); | 186 dcArgBool(pc,DC_FALSE); |
173 dcCallVoid(pc, vtbl[VTBI_SET_BOOL] ); | 187 dcCallVoid(pc, vtbl[VTBI_SET_BOOL] ); |
174 dcReset(pc); | 188 dcReset(pc); |
175 dcArgPointer(pc, pThis); | 189 dcArgPointer(pc, pThis); |
176 DC_TEST( dcCallBool(pc, vtbl[VTBI_GET_BOOL] ) == DC_FALSE ); | 190 b = ( dcCallBool(pc, vtbl[VTBI_GET_BOOL] ) == DC_FALSE ); |
191 printf("bf (%s): %d\n", name, b); | |
192 r = r && b; | |
177 | 193 |
178 /* set/get int */ | 194 /* set/get int */ |
179 | 195 |
180 dcReset(pc); | 196 dcReset(pc); |
181 dcArgPointer(pc, pThis); | 197 dcArgPointer(pc, pThis); |
182 dcArgInt(pc,1234); | 198 dcArgInt(pc,1234); |
183 dcCallVoid(pc, vtbl[VTBI_SET_INT] ); | 199 dcCallVoid(pc, vtbl[VTBI_SET_INT] ); |
184 dcReset(pc); | 200 dcReset(pc); |
185 dcArgPointer(pc, pThis); | 201 dcArgPointer(pc, pThis); |
186 DC_TEST( dcCallInt(pc, vtbl[VTBI_GET_INT] ) == 1234 ); | 202 b = ( dcCallInt(pc, vtbl[VTBI_GET_INT] ) == 1234 ); |
203 printf("i (%s): %d\n", name, b); | |
204 r = r && b; | |
187 | 205 |
188 /* set/get long */ | 206 /* set/get long */ |
189 | 207 |
190 dcReset(pc); | 208 dcReset(pc); |
191 dcArgPointer(pc, pThis); | 209 dcArgPointer(pc, pThis); |
192 dcArgLong(pc,0xCAFEBABEUL); | 210 dcArgLong(pc,0xCAFEBABEUL); |
193 dcCallVoid(pc, vtbl[VTBI_SET_LONG] ); | 211 dcCallVoid(pc, vtbl[VTBI_SET_LONG] ); |
194 dcReset(pc); | 212 dcReset(pc); |
195 dcArgPointer(pc, pThis); | 213 dcArgPointer(pc, pThis); |
196 DC_TEST( dcCallLong(pc, vtbl[VTBI_GET_LONG] ) == (DClong)0xCAFEBABEUL ); | 214 b = ( dcCallLong(pc, vtbl[VTBI_GET_LONG] ) == (DClong)0xCAFEBABEUL ); |
215 printf("l (%s): %d\n", name, b); | |
216 r = r && b; | |
197 | 217 |
198 /* set/get long long */ | 218 /* set/get long long */ |
199 | 219 |
200 dcReset(pc); | 220 dcReset(pc); |
201 dcArgPointer(pc, pThis); | 221 dcArgPointer(pc, pThis); |
202 dcArgLongLong(pc,0xCAFEBABEDEADC0DELL); | 222 dcArgLongLong(pc,0xCAFEBABEDEADC0DELL); |
203 dcCallVoid(pc, vtbl[VTBI_SET_LONG_LONG] ); | 223 dcCallVoid(pc, vtbl[VTBI_SET_LONG_LONG] ); |
204 dcReset(pc); | 224 dcReset(pc); |
205 dcArgPointer(pc, pThis); | 225 dcArgPointer(pc, pThis); |
206 DC_TEST( dcCallLongLong(pc, vtbl[VTBI_GET_LONG_LONG] ) == (DClonglong)0xCAFEBABEDEADC0DELL ); | 226 b = ( dcCallLongLong(pc, vtbl[VTBI_GET_LONG_LONG] ) == (DClonglong)0xCAFEBABEDEADC0DELL ); |
227 printf("ll (%s): %d\n", name, b); | |
228 r = r && b; | |
207 | 229 |
208 /* set/get float */ | 230 /* set/get float */ |
209 | 231 |
210 dcReset(pc); | 232 dcReset(pc); |
211 dcArgPointer(pc, pThis); | 233 dcArgPointer(pc, pThis); |
212 dcArgFloat(pc,1.2345f); | 234 dcArgFloat(pc,1.2345f); |
213 dcCallVoid(pc, vtbl[VTBI_SET_FLOAT] ); | 235 dcCallVoid(pc, vtbl[VTBI_SET_FLOAT] ); |
214 dcReset(pc); | 236 dcReset(pc); |
215 dcArgPointer(pc, pThis); | 237 dcArgPointer(pc, pThis); |
216 DC_TEST( dcCallFloat(pc, vtbl[VTBI_GET_FLOAT] ) == 1.2345f ); | 238 b = ( dcCallFloat(pc, vtbl[VTBI_GET_FLOAT] ) == 1.2345f ); |
239 printf("f (%s): %d\n", name, b); | |
240 r = r && b; | |
217 | 241 |
218 /* set/get double */ | 242 /* set/get double */ |
219 | 243 |
220 dcReset(pc); | 244 dcReset(pc); |
221 dcArgPointer(pc, pThis); | 245 dcArgPointer(pc, pThis); |
222 dcArgDouble(pc,1.23456789); | 246 dcArgDouble(pc,1.23456789); |
223 dcCallVoid(pc, vtbl[VTBI_SET_DOUBLE] ); | 247 dcCallVoid(pc, vtbl[VTBI_SET_DOUBLE] ); |
224 dcReset(pc); | 248 dcReset(pc); |
225 dcArgPointer(pc, pThis); | 249 dcArgPointer(pc, pThis); |
226 DC_TEST( dcCallDouble(pc, vtbl[VTBI_GET_DOUBLE] ) == 1.23456789 ); | 250 b = ( dcCallDouble(pc, vtbl[VTBI_GET_DOUBLE] ) == 1.23456789 ); |
251 printf("d (%s): %d\n", name, b); | |
252 r = r && b; | |
227 | 253 |
228 /* set/get pointer */ | 254 /* set/get pointer */ |
229 | 255 |
230 dcReset(pc); | 256 dcReset(pc); |
231 dcArgPointer(pc, pThis); | 257 dcArgPointer(pc, pThis); |
232 dcArgPointer(pc, (DCpointer) 0xCAFEBABE ); | 258 dcArgPointer(pc, (DCpointer) 0xCAFEBABE ); |
233 dcCallVoid(pc, vtbl[VTBI_SET_POINTER] ); | 259 dcCallVoid(pc, vtbl[VTBI_SET_POINTER] ); |
234 dcReset(pc); | 260 dcReset(pc); |
235 dcArgPointer(pc, pThis); | 261 dcArgPointer(pc, pThis); |
236 DC_TEST( dcCallPointer(pc, vtbl[VTBI_GET_POINTER] ) == ( (DCpointer) 0xCAFEBABE ) ); | 262 b = ( dcCallPointer(pc, vtbl[VTBI_GET_POINTER] ) == ( (DCpointer) 0xCAFEBABE ) ); |
263 printf("p (%s): %d\n", name, b); | |
264 r = r && b; | |
265 | |
266 return r; | |
237 } | 267 } |
238 | 268 |
239 | 269 |
240 #ifdef DC__OS_Win32 | 270 #ifdef DC__OS_Win32 |
241 | 271 |
242 DC_DEFINE_TEST_FUNC_BEGIN(testCallThisMS) | 272 int testCallThisMS() |
243 | 273 { |
274 bool r = false; | |
244 DCCallVM* pc = dcNewCallVM(4096); | 275 DCCallVM* pc = dcNewCallVM(4096); |
245 dcMode(pc,DC_CALL_C_X86_WIN32_THIS_MS); | 276 dcMode(pc, DC_CALL_C_X86_WIN32_THIS_MS); |
246 dcReset(pc); | 277 dcReset(pc); |
247 testCallValue<ValueMS>(pc); | 278 if(setjmp(jbuf) != 0) |
279 printf("sigsegv\n"); | |
280 else | |
281 r = testCallValue<ValueMS>(pc, "MS"); | |
248 dcFree(pc); | 282 dcFree(pc); |
249 | 283 return r; |
250 DC_DEFINE_TEST_FUNC_END | 284 } |
251 | 285 |
252 #endif | 286 #endif |
253 | 287 |
254 | 288 |
255 DC_DEFINE_TEST_FUNC_BEGIN(testCallThisC) | 289 int testCallThisC() |
256 | 290 { |
291 bool r = false; | |
257 DCCallVM* pc = dcNewCallVM(4096); | 292 DCCallVM* pc = dcNewCallVM(4096); |
258 dcReset(pc); | 293 dcReset(pc); |
259 testCallValue<Value>(pc); | 294 if(setjmp(jbuf) != 0) |
295 printf("sigsegv\n"); | |
296 else | |
297 r = testCallValue<Value>(pc, "c"); | |
260 dcFree(pc); | 298 dcFree(pc); |
261 | 299 return r; |
262 DC_DEFINE_TEST_FUNC_END | 300 } |
263 | 301 |
264 | 302 |
265 extern "C" { | 303 extern "C" { |
266 | 304 |
267 int main(int argc, char* argv[]) | 305 int main(int argc, char* argv[]) |
268 { | 306 { |
269 dcTest_initPlatform(); | 307 dcTest_initPlatform(); |
270 | 308 |
271 int b = TRUE; | 309 signal(SIGSEGV, segv_handler); |
272 | 310 |
273 #if defined(DC__OS_Win32) // ThisCall temporarily only for win 32 @@@ | 311 bool r = true; |
274 | 312 |
275 b = b && testCallThisC(); | 313 r = testCallThisC() && r; |
276 printf("ThisC:%d\n",b); | |
277 | |
278 #if defined(DC__C_MSVC) | 314 #if defined(DC__C_MSVC) |
279 b = b && testCallThisMS(); | 315 r = testCallThisMS() && r; |
280 printf("ThisMS:%d\n",b); | |
281 #endif | 316 #endif |
282 | 317 |
283 #endif | 318 printf("result: plain_cpp: %d\n", r); |
284 | |
285 printf("result: plain_cpp: %d\n", b); | |
286 | 319 |
287 dcTest_deInitPlatform(); | 320 dcTest_deInitPlatform(); |
288 | 321 |
289 return !b; | 322 return !r; |
290 } | 323 } |
291 | 324 |
292 } // extern "C" | 325 } // extern "C" |
293 | 326 |