0
|
1 /*
|
|
2
|
|
3 Package: dyncall
|
|
4 Library: test
|
|
5 File: test/suite_floats/main.cc
|
|
6 Description:
|
|
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 "../../dyncall/dyncall.h"
|
|
29 #include "config.h"
|
|
30 #include "../../dyncall/dyncall_value.h"
|
|
31 #include <math.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 DCdouble valueDouble [NARGS];
|
|
42 DCfloat valueFloat [NARGS];
|
|
43
|
|
44
|
|
45 bool equals(int select, int pos, void* data)
|
|
46 {
|
|
47 switch(select)
|
|
48 {
|
|
49 case 0: return ( getArg(pos)->d == valueDouble [pos] ); break;
|
|
50 case 1: return ( getArg(pos)->f == valueFloat [pos] ); break;
|
|
51 }
|
|
52 return false;
|
|
53 }
|
|
54
|
|
55
|
|
56 void clearValues();
|
|
57
|
|
58
|
|
59 void init()
|
|
60 {
|
|
61 for (int i = 0 ; i < NARGS ; ++i ) {
|
|
62 valueDouble[i] = DCdouble(i);
|
|
63 valueFloat[i] = DCfloat(i);
|
|
64 }
|
|
65 }
|
|
66
|
|
67
|
|
68 void push(DCCallVM* pCall, int select, int pos)
|
|
69 {
|
|
70 switch(select)
|
|
71 {
|
|
72 case 0: dcArgDouble ( pCall, valueDouble [pos] ); break;
|
|
73 case 1: dcArgFloat ( pCall, valueFloat [pos] ); break;
|
|
74 }
|
|
75 }
|
|
76
|
|
77
|
|
78 #define assert(x) if (!(x)) return false
|
|
79
|
|
80
|
|
81 bool test(int x)
|
|
82 {
|
|
83 clearValues();
|
|
84
|
|
85 DCCallVM* pCall = dcNewCallVM(4096);
|
|
86 dcReset(pCall);
|
|
87 int y = x;
|
|
88 int selects[NARGS] = { 0, };
|
|
89 int pos = 0;
|
|
90 for(pos = 0;y>0;++pos)
|
|
91 {
|
|
92 int select = (y-1) % NTYPES;
|
|
93 selects[pos] = select;
|
|
94 push(pCall,select,pos);
|
|
95 y = (y-1) / NTYPES;
|
|
96 }
|
|
97 dcCallVoid(pCall,getFunc(x));
|
|
98
|
|
99 assert( getId() == x );
|
|
100
|
|
101 for(int i = 0;i<pos;++i) {
|
|
102 assert( equals( selects[i], i, getArg(i) ) );
|
|
103 }
|
|
104
|
|
105 dcFree(pCall);
|
|
106 return true;
|
|
107 }
|
|
108
|
|
109
|
|
110 int powerfact(int x, int n)
|
|
111 {
|
|
112 if (n==0) return 0;
|
|
113 return static_cast<int>( pow((double)x,n)+powerfact(x,n-1) );
|
|
114 }
|
|
115
|
|
116
|
|
117 bool run_range(int from, int to)
|
|
118 {
|
|
119 bool tr = true;
|
|
120 for (int i = from ; i < to ; ++i ) {
|
|
121 printf("%d:",i);
|
|
122 bool r = test(i);
|
|
123 printf("%d\n", r);
|
|
124 tr &= r;
|
|
125 }
|
|
126 return tr;
|
|
127 }
|
|
128
|
|
129 extern "C" {
|
|
130
|
|
131 int main(int argc, char* argv[])
|
|
132 {
|
|
133 dcTest_initPlatform();
|
|
134
|
|
135 bool success = false;
|
|
136 init();
|
|
137 if (argc == 2) {
|
|
138 int index = atoi(argv[1]);
|
|
139 success = run_range( index, index+1 );
|
|
140 } else if (argc == 3) {
|
|
141 int from = atoi(argv[1]);
|
|
142 int to = atoi(argv[2])+1;
|
|
143 success = run_range(from,to);
|
|
144 } else {
|
|
145 int ncalls = powerfact(NTYPES,NARGS)+1;
|
|
146 success = run_range(0,ncalls);
|
|
147 }
|
|
148
|
|
149 printf("result: suite_floats: %s\n", success ? "1" : "0");
|
|
150
|
|
151 dcTest_deInitPlatform();
|
|
152
|
|
153 return (success) ? 0 : -1;
|
|
154 }
|
|
155
|
|
156 } // extern "C"
|
|
157
|