comparison test/ellipsis/main.cc @ 0:3e629dc19168

initial from svn dyncall-1745
author Daniel Adler
date Thu, 19 Mar 2015 22:24:28 +0100
parents
children 9bd3c5219505
comparison
equal deleted inserted replaced
-1:000000000000 0:3e629dc19168
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
9 Copyright (c) 2007-2015 Daniel Adler <dadler@uni-goettingen.de>,
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 {
61 for (int i = 0 ; i < NARGS ; ++i )
62 {
63 valueInt [i] = DCint (i);
64 valueLongLong[i] = DClonglong(i);
65 valueDouble [i] = DCdouble (i);
66 valuePointer [i] = DCpointer (i);
67 }
68 }
69
70 void arg(DCCallVM* pCall, int select, int pos)
71 {
72 switch(select)
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
81 #define assert(x) if (!(x)) return false
82
83 bool test_ellipsis_case(int x)
84 {
85 clearValues();
86
87 DCCallVM* pCall = dcNewCallVM(4096);
88
89 assert( dcGetError(pCall) == DC_ERROR_NONE );
90
91 dcMode(pCall, DC_CALL_C_ELLIPSIS);
92
93 assert( dcGetError(pCall) == DC_ERROR_NONE );
94
95 int y = x;
96 int selects[NARGS] = { 0 };
97 int pos = 0;
98 if (y > 0)
99 {
100 int select = (y-1) % NTYPES;
101 selects[pos] = select;
102 arg(pCall,select,pos);
103 y = (y-1) / NTYPES;
104 ++pos;
105 }
106
107 dcMode(pCall, DC_CALL_C_ELLIPSIS_VARARGS);
108
109 for(; y>0; ++pos)
110 {
111 int select = (y-1) % NTYPES;
112 selects[pos] = select;
113 arg(pCall,select,pos);
114 y = (y-1) / NTYPES;
115 }
116 dcCallVoid(pCall,getFunc(x));
117
118 assert( getId() == x );
119
120 for(int i=0; i<pos; ++i) {
121 assert( equals( selects[i], i, getArg(i) ) );
122 }
123
124 dcFree(pCall);
125 return true;
126 }
127
128 int powerfact(int x, int n)
129 {
130 if (n==0) return 0;
131 return static_cast<int>( pow((double)x,n)+powerfact(x,n-1) );
132 }
133
134
135 bool run_range(int from, int to)
136 {
137 bool tr = true;
138 for (int i = from ; i < to ; ++i ) {
139 printf("%d:", i);
140 bool r = test_ellipsis_case(i);
141 printf("%d\n", r);
142
143 tr &= r;
144 }
145 return tr;
146 }
147
148
149 extern "C" {
150
151 int main(int argc, char* argv[])
152 {
153 dcTest_initPlatform();
154
155 bool success = false;
156 init();
157 if (argc == 2) {
158 int index = atoi(argv[1]);
159 success = run_range( index, index+1 );
160 } else if (argc == 3) {
161 int from = atoi(argv[1]);
162 int to = atoi(argv[2])+1;
163 success = run_range(from,to);
164 } else {
165 int ncalls = powerfact(NTYPES,NARGS)+1;
166 success = run_range(0,ncalls);
167 }
168
169 printf("result: ellipsis: %s\n", success ? "1" : "0");
170
171 dcTest_deInitPlatform();
172
173 return (success) ? 0 : -1;
174 }
175
176 } // extern "C"
177