Skip to content
This repository
Newer
Older
100644 86 lines (73 sloc) 2.235 kb
2a013792 » arnsholt
2012-04-20 Preliminary tests for structs and CArray members.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #ifdef WIN32
6 #define DLLEXPORT __declspec(dllexport)
7 #else
8 #define DLLEXPORT extern
9 #endif
10
11 typedef struct {
84908186 » arnsholt
2012-04-21 Some struct test updates.
12 long intval;
2a013792 » arnsholt
2012-04-20 Preliminary tests for structs and CArray members.
13 double numval;
84908186 » arnsholt
2012-04-21 Some struct test updates.
14 long *arr;
2a013792 » arnsholt
2012-04-20 Preliminary tests for structs and CArray members.
15 } MyStruct;
16
5560e305 » arnsholt
2012-04-21 Add tests from CStruct in CStruct.
17 typedef struct { long first, second; } IntStruct;
18 typedef struct { double first, second; } NumStruct;
19 typedef struct {
20 IntStruct *a;
21 NumStruct *b;
22 } StructStruct;
23
affc3ebd » arnsholt
2012-04-21 Add tests for CPointers in CStructs.
24 typedef struct {
25 long *p;
26 } PointerStruct;
27
2a013792 » arnsholt
2012-04-20 Preliminary tests for structs and CArray members.
28 DLLEXPORT MyStruct *ReturnAStruct()
29 {
30 MyStruct *obj = (MyStruct *) malloc(sizeof(MyStruct));
31 obj->intval = 17;
32 obj->numval = 4.2;
84908186 » arnsholt
2012-04-21 Some struct test updates.
33 obj->arr = (long *) malloc(3*sizeof(long));
2a013792 » arnsholt
2012-04-20 Preliminary tests for structs and CArray members.
34 obj->arr[0] = 2;
35 obj->arr[1] = 3;
36 obj->arr[2] = 5;
37
38 return obj;
39 }
40
41 DLLEXPORT void TakeAStruct(MyStruct *obj)
42 {
43 if(obj->intval != 42) printf("not ");
44 printf("ok - C-side int value\n");
45 if(obj->numval != -3.7) printf("not ");
46 printf("ok - C-side num value\n");
47 if(!obj->arr || obj->arr[0] != 1) printf("not ");
48 printf("ok - C-side array value, element 1\n");
49 if(!obj->arr || obj->arr[1] != 2) printf("not ");
50 printf("ok - C-side array value, element 2\n");
51 }
5560e305 » arnsholt
2012-04-21 Add tests from CStruct in CStruct.
52
53 DLLEXPORT StructStruct *ReturnAStructStruct() {
54 StructStruct *ss = (StructStruct *) malloc(sizeof(StructStruct));
55 ss->a = (IntStruct *) malloc(sizeof(IntStruct));
56 ss->b = (NumStruct *) malloc(sizeof(NumStruct));
57 ss->a->first = 7;
58 ss->a->second = 11;
59 ss->b->first = 3.7;
60 ss->b->second = 0.1;
61
62 return ss;
63 }
64
65 DLLEXPORT void TakeAStructStruct(StructStruct *obj) {
66 if(!obj->a || obj->a->first != 13) printf("not ");
67 printf("ok - int 1 in struct 1 in struct\n");
68 if(!obj->a || obj->a->second != 17) printf("not ");
69 printf("ok - int 2 in struct 1 in struct\n");
70 if(!obj->b || obj->b->first != 0.9) printf("not ");
71 printf("ok - int 1 in struct 2 in struct\n");
72 if(!obj->b || obj->b->second != 3.14) printf("not ");
73 printf("ok - int 2 in struct 2 in struct\n");
74 }
affc3ebd » arnsholt
2012-04-21 Add tests for CPointers in CStructs.
75
76 DLLEXPORT PointerStruct *ReturnAPointerStruct() {
77 PointerStruct *obj = (PointerStruct *) malloc(sizeof(PointerStruct));
78 obj->p = (long *) malloc(sizeof(long));
79 *(obj->p) = 19;
80
81 return obj;
82 }
83
84 DLLEXPORT long _deref(long *ptr) {
85 return *ptr;
86 }
Something went wrong with that request. Please try again.