Mercurial > pub > dyncall > dyncall
annotate test/ellipsis/main.cc @ 410:7608e34098b0
- cleanups, simplifications, some api clarification, ...
- test cases consistency:
* return status code depending on test results (for actual conformance tests, not stuff that is not an example or hack to check something, ..)
* platform init helper added for some
author | Tassilo Philipp |
---|---|
date | Tue, 05 Oct 2021 21:53:04 +0200 |
parents | f5577f6bf97a |
children | 85b7a117b807 |
rev | line source |
---|---|
0 | 1 /* |
2 | |
3 Package: dyncall | |
4 Library: test | |
5 File: test/ellipsis/main.cc | |
6 Description: call (...) functions via dyncall library, targets are auto-generated | |
7 License: | |
8 | |
410
7608e34098b0
- cleanups, simplifications, some api clarification, ...
Tassilo Philipp
parents:
281
diff
changeset
|
9 Copyright (c) 2007-2021 Daniel Adler <dadler@uni-goettingen.de>, |
0 | 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 | |
27 | |
28 #include "config.h" | |
29 #include <math.h> | |
30 #include "../../dyncall/dyncall.h" | |
31 #include "../../dyncall/dyncall_value.h" | |
32 #include <stdlib.h> | |
33 #include "../common/platformInit.h" | |
34 #include "../common/platformInit.c" /* Impl. for functions only used in this translation unit */ | |
35 | |
36 | |
37 int getId(); | |
38 DCpointer getFunc(int x); | |
39 DCValue* getArg(int pos); | |
40 | |
41 DCint valueInt [NARGS]; | |
42 DClonglong valueLongLong[NARGS]; | |
43 DCdouble valueDouble [NARGS]; | |
44 DCpointer valuePointer [NARGS]; | |
45 | |
46 bool equals(int select, int pos, void* data) | |
47 { | |
48 switch(select) | |
49 { | |
50 case 0: return ( getArg(pos)->i == valueInt [pos] ); break; | |
51 case 1: return ( getArg(pos)->l == valueLongLong[pos] ); break; | |
52 case 2: return ( getArg(pos)->d == valueDouble [pos] ); break; | |
53 case 3: return ( getArg(pos)->p == valuePointer [pos] ); break; | |
54 } | |
55 return false; | |
56 } | |
57 void clearValues(); | |
58 | |
59 void init() | |
60 { | |
410
7608e34098b0
- cleanups, simplifications, some api clarification, ...
Tassilo Philipp
parents:
281
diff
changeset
|
61 for (int i = 0 ; i < NARGS ; ++i ) |
0 | 62 { |
63 valueInt [i] = DCint (i); | |
64 valueLongLong[i] = DClonglong(i); | |
65 valueDouble [i] = DCdouble (i); | |
66 valuePointer [i] = DCpointer (i); | |
410
7608e34098b0
- cleanups, simplifications, some api clarification, ...
Tassilo Philipp
parents:
281
diff
changeset
|
67 } |
0 | 68 } |
69 | |
70 void arg(DCCallVM* pCall, int select, int pos) | |
71 { | |
410
7608e34098b0
- cleanups, simplifications, some api clarification, ...
Tassilo Philipp
parents:
281
diff
changeset
|
72 switch(select) |
0 | 73 { |
74 case 0: dcArgInt ( pCall, valueInt [pos] ); break; | |
75 case 1: dcArgLongLong( pCall, valueLongLong[pos] ); break; | |
76 case 2: dcArgDouble ( pCall, valueDouble [pos] ); break; | |
77 case 3: dcArgPointer ( pCall, valuePointer [pos] ); break; | |
78 } | |
79 } | |
80 | |
410
7608e34098b0
- cleanups, simplifications, some api clarification, ...
Tassilo Philipp
parents:
281
diff
changeset
|
81 #define test(x) if (!(x)) return false |
0 | 82 |
83 bool test_ellipsis_case(int x) | |
84 { | |
85 clearValues(); | |
86 | |
87 DCCallVM* pCall = dcNewCallVM(4096); | |
88 | |
410
7608e34098b0
- cleanups, simplifications, some api clarification, ...
Tassilo Philipp
parents:
281
diff
changeset
|
89 test( dcGetError(pCall) == DC_ERROR_NONE ); |
7608e34098b0
- cleanups, simplifications, some api clarification, ...
Tassilo Philipp
parents:
281
diff
changeset
|
90 |
0 | 91 dcMode(pCall, DC_CALL_C_ELLIPSIS); |
50 | 92 dcReset(pCall); |
410
7608e34098b0
- cleanups, simplifications, some api clarification, ...
Tassilo Philipp
parents:
281
diff
changeset
|
93 |
7608e34098b0
- cleanups, simplifications, some api clarification, ...
Tassilo Philipp
parents:
281
diff
changeset
|
94 test( dcGetError(pCall) == DC_ERROR_NONE ); |
7608e34098b0
- cleanups, simplifications, some api clarification, ...
Tassilo Philipp
parents:
281
diff
changeset
|
95 |
0 | 96 int y = x; |
97 int selects[NARGS] = { 0 }; | |
98 int pos = 0; | |
410
7608e34098b0
- cleanups, simplifications, some api clarification, ...
Tassilo Philipp
parents:
281
diff
changeset
|
99 if (y > 0) |
0 | 100 { |
410
7608e34098b0
- cleanups, simplifications, some api clarification, ...
Tassilo Philipp
parents:
281
diff
changeset
|
101 int select = (y-1) % NTYPES; |
0 | 102 selects[pos] = select; |
103 arg(pCall,select,pos); | |
104 y = (y-1) / NTYPES; | |
105 ++pos; | |
106 } | |
107 | |
108 dcMode(pCall, DC_CALL_C_ELLIPSIS_VARARGS); | |
109 | |
410
7608e34098b0
- cleanups, simplifications, some api clarification, ...
Tassilo Philipp
parents:
281
diff
changeset
|
110 for(; y>0; ++pos) |
0 | 111 { |
410
7608e34098b0
- cleanups, simplifications, some api clarification, ...
Tassilo Philipp
parents:
281
diff
changeset
|
112 int select = (y-1) % NTYPES; |
0 | 113 selects[pos] = select; |
114 arg(pCall,select,pos); | |
115 y = (y-1) / NTYPES; | |
116 } | |
117 dcCallVoid(pCall,getFunc(x)); | |
410
7608e34098b0
- cleanups, simplifications, some api clarification, ...
Tassilo Philipp
parents:
281
diff
changeset
|
118 |
7608e34098b0
- cleanups, simplifications, some api clarification, ...
Tassilo Philipp
parents:
281
diff
changeset
|
119 test( getId() == x ); |
7608e34098b0
- cleanups, simplifications, some api clarification, ...
Tassilo Philipp
parents:
281
diff
changeset
|
120 |
0 | 121 for(int i=0; i<pos; ++i) { |
410
7608e34098b0
- cleanups, simplifications, some api clarification, ...
Tassilo Philipp
parents:
281
diff
changeset
|
122 test( equals( selects[i], i, getArg(i) ) ); |
0 | 123 } |
410
7608e34098b0
- cleanups, simplifications, some api clarification, ...
Tassilo Philipp
parents:
281
diff
changeset
|
124 |
0 | 125 dcFree(pCall); |
126 return true; | |
127 } | |
128 | |
129 int powerfact(int x, int n) | |
130 { | |
131 if (n==0) return 0; | |
132 return static_cast<int>( pow((double)x,n)+powerfact(x,n-1) ); | |
133 } | |
134 | |
135 | |
136 bool run_range(int from, int to) | |
137 { | |
138 bool tr = true; | |
139 for (int i = from ; i < to ; ++i ) { | |
140 printf("%d:", i); | |
141 bool r = test_ellipsis_case(i); | |
142 printf("%d\n", r); | |
143 | |
144 tr &= r; | |
145 } | |
146 return tr; | |
147 } | |
148 | |
149 | |
150 extern "C" { | |
151 | |
152 int main(int argc, char* argv[]) | |
153 { | |
154 dcTest_initPlatform(); | |
155 | |
156 bool success = false; | |
157 init(); | |
158 if (argc == 2) { | |
159 int index = atoi(argv[1]); | |
410
7608e34098b0
- cleanups, simplifications, some api clarification, ...
Tassilo Philipp
parents:
281
diff
changeset
|
160 success = run_range( index, index+1 ); |
0 | 161 } else if (argc == 3) { |
162 int from = atoi(argv[1]); | |
163 int to = atoi(argv[2])+1; | |
164 success = run_range(from,to); | |
165 } else { | |
166 int ncalls = powerfact(NTYPES,NARGS)+1; | |
167 success = run_range(0,ncalls); | |
168 } | |
169 | |
410
7608e34098b0
- cleanups, simplifications, some api clarification, ...
Tassilo Philipp
parents:
281
diff
changeset
|
170 printf("result: ellipsis: %d\n", success); |
0 | 171 |
172 dcTest_deInitPlatform(); | |
173 | |
410
7608e34098b0
- cleanups, simplifications, some api clarification, ...
Tassilo Philipp
parents:
281
diff
changeset
|
174 return !success; |
0 | 175 } |
176 | |
177 } // extern "C" | |
178 |