comparison test/callback_plain/callback_plain.c @ 0:3e629dc19168

initial from svn dyncall-1745
author Daniel Adler
date Thu, 19 Mar 2015 22:24:28 +0100
parents
children 7ca57dbefed4
comparison
equal deleted inserted replaced
-1:000000000000 0:3e629dc19168
1 /*
2
3 Package: dyncall
4 Library: test
5 File: test/callback_plain/callback_plain.c
6 Description:
7 License:
8
9 Copyright (c) 2011-2015 Daniel Adler <dadler@uni-goettingen.de>,
10 Tassilo Philipp <tphilipp@potion-studios.com>
11
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
14 copyright notice and this permission notice appear in all copies.
15
16 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
17 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
18 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
19 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
21 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
22 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23
24 */
25
26 #include "../../dyncallback/dyncall_thunk.h"
27 #include "../../dyncallback/dyncall_callback.h"
28 #include "../common/platformInit.h"
29 #include "../common/platformInit.c" /* Impl. for functions only used in this translation unit */
30
31
32 char cbHandler(DCCallback* cb, DCArgs* args, DCValue* result, void* userdata)
33 {
34 int* ud = (int*)userdata;
35 int arg1 = dcbArgInt (args);
36 float arg2 = dcbArgFloat (args);
37 short arg3 = dcbArgShort (args);
38 double arg4 = dcbArgDouble (args);
39 long long arg5 = dcbArgLongLong(args);
40
41 printf("reached callback\n");
42 printf("userdata (should be 1337): %d\n", *ud);
43 printf("1st argument (should be 123): %d\n", arg1);
44 printf("2nd argument (should be 23.f): %f\n", arg2);
45 printf("3rd argument (should be 3): %d\n", arg3);
46 printf("4th argument (should be 1.82): %f\n", arg4);
47 printf("5th argument (should be 9909): %lld\n", arg5);
48
49 if(*ud == 1337) *ud = 1;
50 if(arg1 == 123) ++*ud;
51 if(arg2 == 23.f) ++*ud;
52 if(arg3 == 3) ++*ud;
53 if(arg4 == 1.82) ++*ud;
54 if(arg5 == 9909) ++*ud;
55 result->s = 1234;
56 return 's';
57 }
58
59
60 int main()
61 {
62 DCCallback* cb;
63 short result = 0;
64 int userdata = 1337;
65
66 dcTest_initPlatform();
67
68 printf("about to callback...\n");
69 cb = dcbNewCallback("ifsdl)s", &cbHandler, &userdata);
70 result = ((short(*)(int, float, short, double, long long))cb)(123, 23.f, 3, 1.82, 9909ull);
71 dcbFreeCallback(cb);
72 printf("successfully returned from callback\n");
73 printf("return value (should be 1234): %d\n", result);
74
75 printf("result: callback_plain: %s\n", (userdata == 6) && (result == 1234) ? "1" : "0");
76
77 dcTest_deInitPlatform();
78
79 return 0;
80 }
81