Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 107 lines (89 sloc) 2.773 kB
2a01379 @arnsholt Preliminary tests for structs and CArray members.
arnsholt authored
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 {
8490818 @arnsholt Some struct test updates.
arnsholt authored
12 long intval;
2a01379 @arnsholt Preliminary tests for structs and CArray members.
arnsholt authored
13 double numval;
8490818 @arnsholt Some struct test updates.
arnsholt authored
14 long *arr;
2a01379 @arnsholt Preliminary tests for structs and CArray members.
arnsholt authored
15 } MyStruct;
16
5560e30 @arnsholt Add tests from CStruct in CStruct.
arnsholt authored
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
affc3eb @arnsholt Add tests for CPointers in CStructs.
arnsholt authored
24 typedef struct {
25 long *p;
26 } PointerStruct;
27
484fb91 @arnsholt Add tests for strings in structs.
arnsholt authored
28 typedef struct {
29 char *first;
30 char *second;
31 } StringStruct;
32
2a01379 @arnsholt Preliminary tests for structs and CArray members.
arnsholt authored
33 DLLEXPORT MyStruct *ReturnAStruct()
34 {
35 MyStruct *obj = (MyStruct *) malloc(sizeof(MyStruct));
36 obj->intval = 17;
37 obj->numval = 4.2;
8490818 @arnsholt Some struct test updates.
arnsholt authored
38 obj->arr = (long *) malloc(3*sizeof(long));
2a01379 @arnsholt Preliminary tests for structs and CArray members.
arnsholt authored
39 obj->arr[0] = 2;
40 obj->arr[1] = 3;
41 obj->arr[2] = 5;
42
43 return obj;
44 }
45
46 DLLEXPORT void TakeAStruct(MyStruct *obj)
47 {
48 if(obj->intval != 42) printf("not ");
49 printf("ok - C-side int value\n");
50 if(obj->numval != -3.7) printf("not ");
51 printf("ok - C-side num value\n");
52 if(!obj->arr || obj->arr[0] != 1) printf("not ");
53 printf("ok - C-side array value, element 1\n");
54 if(!obj->arr || obj->arr[1] != 2) printf("not ");
55 printf("ok - C-side array value, element 2\n");
56 }
5560e30 @arnsholt Add tests from CStruct in CStruct.
arnsholt authored
57
58 DLLEXPORT StructStruct *ReturnAStructStruct() {
59 StructStruct *ss = (StructStruct *) malloc(sizeof(StructStruct));
60 ss->a = (IntStruct *) malloc(sizeof(IntStruct));
61 ss->b = (NumStruct *) malloc(sizeof(NumStruct));
62 ss->a->first = 7;
63 ss->a->second = 11;
64 ss->b->first = 3.7;
65 ss->b->second = 0.1;
66
67 return ss;
68 }
69
70 DLLEXPORT void TakeAStructStruct(StructStruct *obj) {
71 if(!obj->a || obj->a->first != 13) printf("not ");
72 printf("ok - int 1 in struct 1 in struct\n");
73 if(!obj->a || obj->a->second != 17) printf("not ");
74 printf("ok - int 2 in struct 1 in struct\n");
75 if(!obj->b || obj->b->first != 0.9) printf("not ");
76 printf("ok - int 1 in struct 2 in struct\n");
77 if(!obj->b || obj->b->second != 3.14) printf("not ");
78 printf("ok - int 2 in struct 2 in struct\n");
79 }
affc3eb @arnsholt Add tests for CPointers in CStructs.
arnsholt authored
80
81 DLLEXPORT PointerStruct *ReturnAPointerStruct() {
82 PointerStruct *obj = (PointerStruct *) malloc(sizeof(PointerStruct));
83 obj->p = (long *) malloc(sizeof(long));
84 *(obj->p) = 19;
85
86 return obj;
87 }
88
484fb91 @arnsholt Add tests for strings in structs.
arnsholt authored
89 DLLEXPORT StringStruct *ReturnAStringStruct() {
90 StringStruct *obj = (StringStruct *) malloc(sizeof(StringStruct));
91 obj->first = "OMG!";
92 obj->second = "Strings!";
93
94 return obj;
95 }
96
97 DLLEXPORT void TakeAStringStruct(StringStruct *obj) {
98 if(strcmp(obj->first, "Lorem")) printf("not ");
99 printf("ok - C-value of first string in struct\n");
100 if(strcmp(obj->second, "ipsum")) printf("not ");
101 printf("ok - C-value of second string in struct\n");
102 }
103
affc3eb @arnsholt Add tests for CPointers in CStructs.
arnsholt authored
104 DLLEXPORT long _deref(long *ptr) {
105 return *ptr;
106 }
Something went wrong with that request. Please try again.