comparison test/callback_suite/do_test.c @ 487:d8f0e6cecdab

- simplified test/callback_suite
author Tassilo Philipp
date Fri, 18 Mar 2022 09:47:18 +0100
parents 7608e34098b0
children
comparison
equal deleted inserted replaced
486:d160046da104 487:d8f0e6cecdab
22 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 22 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 23
24 */ 24 */
25 25
26 #include <assert.h> 26 #include <assert.h>
27 #include "_auto_config.h" 27 #include <stdio.h>
28 #include "invokers.h"
29 #include "dyncall_callback.h" 28 #include "dyncall_callback.h"
30 #include "sigstrings.h"
31 #include "signature_utils.h"
32 #include "env.h" 29 #include "env.h"
33 #include "../common/platformInit.h" 30
34 31
35 int CompareValues(char type, DCValue* a, DCValue* b) 32 /* arguments filled-in by callback handlers, and return value */
36 { 33 DCValue Args[CONFIG_MAXARGS];
37 int isequal = 0; 34 DCValue Result;
35
36
37 static int CompareValues(char type, DCValue* a, DCValue* b)
38 {
38 switch(type) 39 switch(type)
39 { 40 {
40 case DC_SIGCHAR_BOOL: isequal = (a->B == b->B) ? 1 : 0 ; break; 41 case DC_SIGCHAR_BOOL: return (a->B == b->B);
41 case DC_SIGCHAR_CHAR: isequal = (a->c == b->c) ? 1 : 0 ; break; 42 case DC_SIGCHAR_CHAR: return (a->c == b->c);
42 case DC_SIGCHAR_UCHAR: isequal = (a->C == b->C) ? 1 : 0 ; break; 43 case DC_SIGCHAR_UCHAR: return (a->C == b->C);
43 case DC_SIGCHAR_SHORT: isequal = (a->s == b->s) ? 1 : 0 ; break; 44 case DC_SIGCHAR_SHORT: return (a->s == b->s);
44 case DC_SIGCHAR_USHORT: isequal = (a->S == b->S) ? 1 : 0 ; break; 45 case DC_SIGCHAR_USHORT: return (a->S == b->S);
45 case DC_SIGCHAR_INT: isequal = (a->i == b->i) ? 1 : 0 ; break; 46 case DC_SIGCHAR_INT: return (a->i == b->i);
46 case DC_SIGCHAR_UINT: isequal = (a->I == b->I) ? 1 : 0 ; break; 47 case DC_SIGCHAR_UINT: return (a->I == b->I);
47 case DC_SIGCHAR_LONG: isequal = (a->j == b->j) ? 1 : 0 ; break; 48 case DC_SIGCHAR_LONG: return (a->j == b->j);
48 case DC_SIGCHAR_ULONG: isequal = (a->J == b->J) ? 1 : 0 ; break; 49 case DC_SIGCHAR_ULONG: return (a->J == b->J);
49 case DC_SIGCHAR_LONGLONG: isequal = (a->l == b->l) ? 1 : 0 ; break; 50 case DC_SIGCHAR_LONGLONG: return (a->l == b->l);
50 case DC_SIGCHAR_ULONGLONG: isequal = (a->L == b->L) ? 1 : 0 ; break; 51 case DC_SIGCHAR_ULONGLONG: return (a->L == b->L);
51 case DC_SIGCHAR_FLOAT: isequal = (a->f == b->f) ? 1 : 0 ; break; 52 case DC_SIGCHAR_FLOAT: return (a->f == b->f);
52 case DC_SIGCHAR_DOUBLE: isequal = (a->d == b->d) ? 1 : 0 ; break; 53 case DC_SIGCHAR_DOUBLE: return (a->d == b->d);
53 case DC_SIGCHAR_POINTER: isequal = (a->p == b->p) ? 1 : 0 ; break; 54 case DC_SIGCHAR_POINTER: return (a->p == b->p);
54 default: assert(0); 55 default: assert(0);
55 } 56 }
56 return isequal; 57 return 0;
57 } 58 }
58 59
59 int Compare(const char* signature) 60
61 static int Compare(const char* signature)
60 { 62 {
61 DCValue ref; 63 DCValue ref;
62 int total = 1; 64 int total = 1;
63 int pos; 65 int pos;
64 int isequal; 66 int isequal;
65 char ch; 67 char ch;
66 68
67 /* skip prefix */
68
69 signature = SignatureSkipCallPrefix(signature);
70
71 /* check arguments */ 69 /* check arguments */
72
73 pos = 0; 70 pos = 0;
74 71 for(;;)
75 for(;;) { 72 {
76 73 ch = *signature++;
77 ch = *signature++;
78 74
79 if (ch == DC_SIGCHAR_ENDARG) break; 75 if(ch == DC_SIGCHAR_CC_PREFIX) {
76 ++signature; /* skip cconv prefix */
77 continue;
78 }
79
80 if (!ch || ch == DC_SIGCHAR_ENDARG) break;
80 GetReferenceArg(&ref, ch, pos); 81 GetReferenceArg(&ref, ch, pos);
81 isequal = CompareValues( ch, &ref, &Args[pos] ); 82 isequal = CompareValues( ch, &ref, &Args[pos] );
82 if ( !isequal ) { 83 if ( !isequal ) {
83 if (OptionVerbose) { total = 0; fprintf(stdout, " @%d[%c] ", pos, ch); } 84 if (OptionVerbose) { total = 0; fprintf(stdout, " @%d[%c] ", pos, ch); }
84 else return 0; 85 else return 0;
85 } 86 }
86 ++ pos; 87 ++pos;
87 } 88 }
88 89
89 ch = *signature++; 90 if(ch == DC_SIGCHAR_ENDARG)
91 ch = *signature;
90 92
91 /* check result */ 93 /* check result */
92 94
93 GetReferenceResult(&ref, ch); 95 GetReferenceResult(&ref, ch);
94 96
100 102
101 return total; 103 return total;
102 } 104 }
103 105
104 106
105 extern DCCallbackHandler handler; /* see handler.c for implementation */ 107 static char handler(DCCallback* that, DCArgs* input, DCValue* output, void* userdata)
108 {
109 const char* signature = (const char*) userdata;
110 int pos = 0;
111 char ch;
112
113 for(;;) {
114 ch = *signature++;
115 if (!ch || ch == DC_SIGCHAR_ENDARG) break;
116 Args[pos].L = 0xDEADC0DECAFEBABELL;
117 switch(ch) {
118 case DC_SIGCHAR_BOOL: Args[pos].B = dcbArgBool (input); break;
119 case DC_SIGCHAR_CHAR: Args[pos].c = dcbArgChar (input); break;
120 case DC_SIGCHAR_UCHAR: Args[pos].C = dcbArgUChar (input); break;
121 case DC_SIGCHAR_SHORT: Args[pos].s = dcbArgShort (input); break;
122 case DC_SIGCHAR_USHORT: Args[pos].S = dcbArgUShort (input); break;
123 case DC_SIGCHAR_INT: Args[pos].i = dcbArgInt (input); break;
124 case DC_SIGCHAR_UINT: Args[pos].I = dcbArgUInt (input); break;
125 case DC_SIGCHAR_LONG: Args[pos].j = dcbArgLong (input); break;
126 case DC_SIGCHAR_ULONG: Args[pos].J = dcbArgULong (input); break;
127 case DC_SIGCHAR_LONGLONG: Args[pos].l = dcbArgLongLong (input); break;
128 case DC_SIGCHAR_ULONGLONG:Args[pos].L = dcbArgULongLong(input); break;
129 case DC_SIGCHAR_FLOAT: Args[pos].f = dcbArgFloat (input); break;
130 case DC_SIGCHAR_DOUBLE: Args[pos].d = dcbArgDouble (input); break;
131 case DC_SIGCHAR_STRING:
132 case DC_SIGCHAR_POINTER: Args[pos].p = dcbArgPointer (input); break;
133 case DC_SIGCHAR_CC_PREFIX: ++signature; /* skip cconv prefix */ continue;
134 }
135 ++pos;
136 }
137
138 if(ch == DC_SIGCHAR_ENDARG)
139 ch = *signature;
140
141 /* @@@ unsupported result types or missing retval sig char */
142 switch(ch) {
143 case DC_SIGCHAR_BOOL:
144 case DC_SIGCHAR_CHAR:
145 case DC_SIGCHAR_UCHAR:
146 case DC_SIGCHAR_SHORT:
147 case DC_SIGCHAR_USHORT:
148 case DC_SIGCHAR_INT:
149 case DC_SIGCHAR_UINT:
150 case DC_SIGCHAR_LONG:
151 case DC_SIGCHAR_ULONG:
152 case DC_SIGCHAR_LONGLONG:
153 case DC_SIGCHAR_ULONGLONG:
154 case DC_SIGCHAR_FLOAT:
155 case DC_SIGCHAR_DOUBLE:
156 case DC_SIGCHAR_STRING:
157 case DC_SIGCHAR_POINTER:
158 break;
159 case DC_SIGCHAR_VOID:
160 case DC_SIGCHAR_AGGREGATE:
161 default:
162 assert(0); return 'v';
163 }
164
165 GetReferenceResult(output, ch);
166
167 return ch;
168 }
169
170
171 /* index range: [0,CONFIG_NSIGS[ */
172 static const char* GetSignature(int index)
173 {
174 static const char* sigstrings[CONFIG_NSIGS] = {
175 #include "_auto_sigstrings.h"
176 };
177
178 return sigstrings[index];
179 }
180
181
182 #define X(CH,T) typedef T CH;
183 DEF_TYPES
184 #undef X
185
186 #define M ValueMatrix
187 #include "_auto_invoke_macros.h"
188 #include "_auto_invokers.h"
189 #undef M
190
191 void (*invokers[CONFIG_NSIGS])(void*) = {
192 #include "_auto_invoke_table.h"
193 };
194
195
106 196
107 int DoTest(int id) 197 int DoTest(int id)
108 { 198 {
109 int index, result;
110 const char* signature; 199 const char* signature;
111 DCCallback* pcb; 200 DCCallback* pcb;
112 201 int result, index = id - 1;
113 assert( id > 0 && id <= CONFIG_NSIGS ); 202
114 index = id - 1;
115
116 signature = GetSignature(index); 203 signature = GetSignature(index);
117 printf("f%d(\t%s", id, signature); 204 printf("f%d(\t%s", id, signature);
118 205
119 pcb = dcbNewCallback( signature, handler, (void*) signature ); 206 pcb = dcbNewCallback(signature, handler, (void*)signature, NULL);
120 assert(pcb != NULL); 207 assert(pcb != NULL);
121 DoInvoke(index, (void*) pcb); 208
209 /* invoke call */
210 invokers[index]((void*)pcb);
211
122 result = Compare(signature); 212 result = Compare(signature);
123 printf(" :%d\n", result); 213 printf(" :%d\n", result);
124 dcbFreeCallback(pcb); 214 dcbFreeCallback(pcb);
215
125 return result; 216 return result;
126 } 217 }
127 218