Mercurial > pub > dyncall > dyncall
comparison test/callback_suite/main.c @ 509:f3d44195dbdf
callback_suite:
- further, big refactoring to make the test data setup be a lot more in line with call_suite and call_suite_aggrs
- added support to test void return values (was missing in prev version)
author | Tassilo Philipp |
---|---|
date | Sat, 09 Apr 2022 23:00:24 +0200 |
parents | 5a3c07a0f376 |
children | 71c884e610f0 |
comparison
equal
deleted
inserted
replaced
508:1aa2af848e8a | 509:f3d44195dbdf |
---|---|
45 \n\ | 45 \n\ |
46 ", appName); | 46 ", appName); |
47 } | 47 } |
48 | 48 |
49 | 49 |
50 /* -------------- this was do_test.c -------------------------- this was do_test.c ------------> */ | |
51 | |
52 static int cmp_values(char type, DCValue* a, DCValue* b) | |
53 { | |
54 switch(type) | |
55 { | |
56 case DC_SIGCHAR_BOOL: return (a->B == b->B); | |
57 case DC_SIGCHAR_CHAR: return (a->c == b->c); | |
58 case DC_SIGCHAR_UCHAR: return (a->C == b->C); | |
59 case DC_SIGCHAR_SHORT: return (a->s == b->s); | |
60 case DC_SIGCHAR_USHORT: return (a->S == b->S); | |
61 case DC_SIGCHAR_INT: return (a->i == b->i); | |
62 case DC_SIGCHAR_UINT: return (a->I == b->I); | |
63 case DC_SIGCHAR_LONG: return (a->j == b->j); | |
64 case DC_SIGCHAR_ULONG: return (a->J == b->J); | |
65 case DC_SIGCHAR_LONGLONG: return (a->l == b->l); | |
66 case DC_SIGCHAR_ULONGLONG: return (a->L == b->L); | |
67 case DC_SIGCHAR_FLOAT: return (a->f == b->f); | |
68 case DC_SIGCHAR_DOUBLE: return (a->d == b->d); | |
69 case DC_SIGCHAR_POINTER: return (a->p == b->p); | |
70 default: assert(0); | |
71 } | |
72 return 0; | |
73 } | |
74 | |
75 | |
76 static int cmp(const char* signature) | 50 static int cmp(const char* signature) |
77 { | 51 { |
78 DCValue ref; | 52 char atype; |
79 int r = 1; | 53 const char* sig = signature; |
80 int pos; | 54 int pos = 0; |
81 char ch; | 55 int s = 0; |
82 | 56 while ( (atype = *sig++) != '\0') { |
83 /* check arguments */ | 57 switch(atype) { |
84 pos = 0; | 58 case ')': /* skip cconv prefix or ret type separator */ continue; |
85 for(;;) | 59 case 'v': s = (sig > signature+1) && sig[-2] == ')'; /* assure this was the return type */ break; /*TODO:check that no return-arg was touched.*/ |
86 { | 60 case 'B': s = ( V_B[pos] == K_B[pos] ); if (!s) printf("'%c':%d: %d != %d ; ", atype, pos, V_B[pos], K_B[pos]); break; |
87 ch = *signature++; | 61 case 'c': s = ( V_c[pos] == K_c[pos] ); if (!s) printf("'%c':%d: %d != %d ; ", atype, pos, V_c[pos], K_c[pos]); break; |
88 | 62 case 's': s = ( V_s[pos] == K_s[pos] ); if (!s) printf("'%c':%d: %d != %d ; ", atype, pos, V_s[pos], K_s[pos]); break; |
89 if(ch == DC_SIGCHAR_CC_PREFIX) { | 63 case 'i': s = ( V_i[pos] == K_i[pos] ); if (!s) printf("'%c':%d: %d != %d ; ", atype, pos, V_i[pos], K_i[pos]); break; |
90 ++signature; /* skip cconv prefix */ | 64 case 'j': s = ( V_j[pos] == K_j[pos] ); if (!s) printf("'%c':%d: %ld != %ld ; ", atype, pos, V_j[pos], K_j[pos]); break; |
91 continue; | 65 case 'l': s = ( V_l[pos] == K_l[pos] ); if (!s) printf("'%c':%d: %lld != %lld ; ", atype, pos, V_l[pos], K_l[pos]); break; |
92 } | 66 case 'C': s = ( V_C[pos] == K_C[pos] ); if (!s) printf("'%c':%d: %u != %u ; ", atype, pos, V_C[pos], K_C[pos]); break; |
93 | 67 case 'S': s = ( V_S[pos] == K_S[pos] ); if (!s) printf("'%c':%d: %u != %u ; ", atype, pos, V_S[pos], K_S[pos]); break; |
94 if (!ch || ch == DC_SIGCHAR_ENDARG) | 68 case 'I': s = ( V_I[pos] == K_I[pos] ); if (!s) printf("'%c':%d: %u != %u ; ", atype, pos, V_I[pos], K_I[pos]); break; |
95 break; | 69 case 'J': s = ( V_J[pos] == K_J[pos] ); if (!s) printf("'%c':%d: %lu != %lu ; ", atype, pos, V_J[pos], K_J[pos]); break; |
96 | 70 case 'L': s = ( V_L[pos] == K_L[pos] ); if (!s) printf("'%c':%d: %llu != %llu ; ", atype, pos, V_L[pos], K_L[pos]); break; |
97 get_reference_arg(&ref, ch, pos); | 71 case 'p': s = ( V_p[pos] == K_p[pos] ); if (!s) printf("'%c':%d: %p != %p ; ", atype, pos, V_p[pos], K_p[pos]); break; |
98 | 72 case 'f': s = ( V_f[pos] == K_f[pos] ); if (!s) printf("'%c':%d: %f != %f ; ", atype, pos, V_f[pos], K_f[pos]); break; |
99 if ( !cmp_values( ch, &ref, &Args[pos] ) ) { | 73 case 'd': s = ( V_d[pos] == K_d[pos] ); if (!s) printf("'%c':%d: %f != %f ; ", atype, pos, V_d[pos], K_d[pos]); break; |
100 r = 0; | 74 default: printf("unknown atype '%c' ; ", atype); return 0; |
101 printf(" @%d[%c] ", pos, ch); | 75 } |
102 } | 76 if (!s) { |
103 ++pos; | 77 printf("arg mismatch at %d ; ", pos); |
104 } | 78 return 0; |
105 | 79 } |
106 if(ch == DC_SIGCHAR_ENDARG) | 80 pos++; |
107 ch = *signature; | 81 } |
108 | 82 return 1; |
109 /* check result */ | 83 } |
110 get_reference_result(&ref, ch); | 84 |
111 | 85 |
112 if (!cmp_values(ch, &ref, &Result)) { | 86 /* handler just copies all received args as well as return value into V_* */ |
113 r = 0; | |
114 printf(" @-1 "); | |
115 } | |
116 | |
117 return r; | |
118 } | |
119 | |
120 | |
121 static char handler(DCCallback* that, DCArgs* input, DCValue* output, void* userdata) | 87 static char handler(DCCallback* that, DCArgs* input, DCValue* output, void* userdata) |
122 { | 88 { |
123 const char* signature = (const char*) userdata; | 89 const char* signature = (const char*) userdata; |
124 int pos = 0; | 90 int pos = 0; |
125 char ch; | 91 char ch; |
126 | 92 |
127 for(;;) { | 93 for(;;) { |
128 ch = *signature++; | 94 ch = *signature++; |
129 if (!ch || ch == DC_SIGCHAR_ENDARG) break; | 95 if (!ch || ch == DC_SIGCHAR_ENDARG) break; |
130 Args[pos].L = 0xDEADC0DECAFEBABELL; | |
131 switch(ch) { | 96 switch(ch) { |
132 case DC_SIGCHAR_BOOL: Args[pos].B = dcbArgBool (input); break; | 97 case DC_SIGCHAR_BOOL: V_B[pos] = dcbArgBool (input); break; |
133 case DC_SIGCHAR_CHAR: Args[pos].c = dcbArgChar (input); break; | 98 case DC_SIGCHAR_CHAR: V_c[pos] = dcbArgChar (input); break; |
134 case DC_SIGCHAR_UCHAR: Args[pos].C = dcbArgUChar (input); break; | 99 case DC_SIGCHAR_UCHAR: V_C[pos] = dcbArgUChar (input); break; |
135 case DC_SIGCHAR_SHORT: Args[pos].s = dcbArgShort (input); break; | 100 case DC_SIGCHAR_SHORT: V_s[pos] = dcbArgShort (input); break; |
136 case DC_SIGCHAR_USHORT: Args[pos].S = dcbArgUShort (input); break; | 101 case DC_SIGCHAR_USHORT: V_S[pos] = dcbArgUShort (input); break; |
137 case DC_SIGCHAR_INT: Args[pos].i = dcbArgInt (input); break; | 102 case DC_SIGCHAR_INT: V_i[pos] = dcbArgInt (input); break; |
138 case DC_SIGCHAR_UINT: Args[pos].I = dcbArgUInt (input); break; | 103 case DC_SIGCHAR_UINT: V_I[pos] = dcbArgUInt (input); break; |
139 case DC_SIGCHAR_LONG: Args[pos].j = dcbArgLong (input); break; | 104 case DC_SIGCHAR_LONG: V_j[pos] = dcbArgLong (input); break; |
140 case DC_SIGCHAR_ULONG: Args[pos].J = dcbArgULong (input); break; | 105 case DC_SIGCHAR_ULONG: V_J[pos] = dcbArgULong (input); break; |
141 case DC_SIGCHAR_LONGLONG: Args[pos].l = dcbArgLongLong (input); break; | 106 case DC_SIGCHAR_LONGLONG: V_l[pos] = dcbArgLongLong (input); break; |
142 case DC_SIGCHAR_ULONGLONG:Args[pos].L = dcbArgULongLong(input); break; | 107 case DC_SIGCHAR_ULONGLONG:V_L[pos] = dcbArgULongLong(input); break; |
143 case DC_SIGCHAR_FLOAT: Args[pos].f = dcbArgFloat (input); break; | 108 case DC_SIGCHAR_FLOAT: V_f[pos] = dcbArgFloat (input); break; |
144 case DC_SIGCHAR_DOUBLE: Args[pos].d = dcbArgDouble (input); break; | 109 case DC_SIGCHAR_DOUBLE: V_d[pos] = dcbArgDouble (input); break; |
145 case DC_SIGCHAR_STRING: | 110 case DC_SIGCHAR_STRING: |
146 case DC_SIGCHAR_POINTER: Args[pos].p = dcbArgPointer (input); break; | 111 case DC_SIGCHAR_POINTER: V_p[pos] = dcbArgPointer (input); break; |
147 case DC_SIGCHAR_CC_PREFIX: ++signature; /* skip cconv prefix */ continue; | 112 case DC_SIGCHAR_CC_PREFIX: ++signature; /* skip cconv prefix */ continue; |
113 default: assert(0); | |
148 } | 114 } |
149 ++pos; | 115 ++pos; |
150 } | 116 } |
151 | 117 |
152 if(ch == DC_SIGCHAR_ENDARG) | 118 if(ch == DC_SIGCHAR_ENDARG) |
153 ch = *signature; | 119 ch = *signature; |
154 | 120 |
155 /* @@@ unsupported result types or missing retval sig char */ | 121 /* write retval */ |
156 switch(ch) { | 122 switch(ch) { |
157 case DC_SIGCHAR_BOOL: | 123 case DC_SIGCHAR_VOID: /* nothing to set */ break; |
158 case DC_SIGCHAR_CHAR: | 124 case DC_SIGCHAR_BOOL: output->B = K_B[pos]; break; |
159 case DC_SIGCHAR_UCHAR: | 125 case DC_SIGCHAR_CHAR: output->c = K_c[pos]; break; |
160 case DC_SIGCHAR_SHORT: | 126 case DC_SIGCHAR_UCHAR: output->C = K_C[pos]; break; |
161 case DC_SIGCHAR_USHORT: | 127 case DC_SIGCHAR_SHORT: output->s = K_s[pos]; break; |
162 case DC_SIGCHAR_INT: | 128 case DC_SIGCHAR_USHORT: output->S = K_S[pos]; break; |
163 case DC_SIGCHAR_UINT: | 129 case DC_SIGCHAR_INT: output->i = K_i[pos]; break; |
164 case DC_SIGCHAR_LONG: | 130 case DC_SIGCHAR_UINT: output->I = K_I[pos]; break; |
165 case DC_SIGCHAR_ULONG: | 131 case DC_SIGCHAR_LONG: output->j = K_j[pos]; break; |
166 case DC_SIGCHAR_LONGLONG: | 132 case DC_SIGCHAR_ULONG: output->J = K_J[pos]; break; |
167 case DC_SIGCHAR_ULONGLONG: | 133 case DC_SIGCHAR_LONGLONG: output->l = K_l[pos]; break; |
168 case DC_SIGCHAR_FLOAT: | 134 case DC_SIGCHAR_ULONGLONG:output->L = K_L[pos]; break; |
169 case DC_SIGCHAR_DOUBLE: | 135 case DC_SIGCHAR_FLOAT: output->f = K_f[pos]; break; |
136 case DC_SIGCHAR_DOUBLE: output->d = K_d[pos]; break; | |
170 case DC_SIGCHAR_STRING: | 137 case DC_SIGCHAR_STRING: |
171 case DC_SIGCHAR_POINTER: | 138 case DC_SIGCHAR_POINTER: output->p = K_p[pos]; break; |
172 break; | 139 default: assert(0); |
173 case DC_SIGCHAR_VOID: | 140 } |
174 case DC_SIGCHAR_AGGREGATE: | 141 |
175 default: | 142 /* return type info for dyncallback */ |
176 assert(0); return DC_SIGCHAR_VOID; | |
177 } | |
178 | |
179 get_reference_result(output, ch); | |
180 | |
181 return ch; | 143 return ch; |
182 } | 144 } |
183 | 145 |
184 | 146 |
185 | 147 |
195 printf("%d:%s", id, signature); | 157 printf("%d:%s", id, signature); |
196 | 158 |
197 pcb = dcbNewCallback(signature, handler, (void*)signature, NULL); | 159 pcb = dcbNewCallback(signature, handler, (void*)signature, NULL); |
198 assert(pcb != NULL); | 160 assert(pcb != NULL); |
199 | 161 |
162 clear_V(); | |
163 | |
200 /* invoke call */ | 164 /* invoke call */ |
201 G_funtab[id]((void*)pcb); | 165 G_funtab[id]((void*)pcb); |
202 | 166 |
203 result = cmp(signature); | 167 result = cmp(signature); |
168 | |
204 printf(":%d\n", result); | 169 printf(":%d\n", result); |
170 | |
205 dcbFreeCallback(pcb); | 171 dcbFreeCallback(pcb); |
206 | 172 |
207 return result; | 173 return result; |
208 } | 174 } |
209 | 175 |