comparison 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
comparison
equal deleted inserted replaced
409:15698dc0cba3 410:7608e34098b0
4 Library: test 4 Library: test
5 File: test/ellipsis/main.cc 5 File: test/ellipsis/main.cc
6 Description: call (...) functions via dyncall library, targets are auto-generated 6 Description: call (...) functions via dyncall library, targets are auto-generated
7 License: 7 License:
8 8
9 Copyright (c) 2007-2018 Daniel Adler <dadler@uni-goettingen.de>, 9 Copyright (c) 2007-2021 Daniel Adler <dadler@uni-goettingen.de>,
10 Tassilo Philipp <tphilipp@potion-studios.com> 10 Tassilo Philipp <tphilipp@potion-studios.com>
11 11
12 Permission to use, copy, modify, and distribute this software for any 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 13 purpose with or without fee is hereby granted, provided that the above
14 copyright notice and this permission notice appear in all copies. 14 copyright notice and this permission notice appear in all copies.
56 } 56 }
57 void clearValues(); 57 void clearValues();
58 58
59 void init() 59 void init()
60 { 60 {
61 for (int i = 0 ; i < NARGS ; ++i ) 61 for (int i = 0 ; i < NARGS ; ++i )
62 { 62 {
63 valueInt [i] = DCint (i); 63 valueInt [i] = DCint (i);
64 valueLongLong[i] = DClonglong(i); 64 valueLongLong[i] = DClonglong(i);
65 valueDouble [i] = DCdouble (i); 65 valueDouble [i] = DCdouble (i);
66 valuePointer [i] = DCpointer (i); 66 valuePointer [i] = DCpointer (i);
67 } 67 }
68 } 68 }
69 69
70 void arg(DCCallVM* pCall, int select, int pos) 70 void arg(DCCallVM* pCall, int select, int pos)
71 { 71 {
72 switch(select) 72 switch(select)
73 { 73 {
74 case 0: dcArgInt ( pCall, valueInt [pos] ); break; 74 case 0: dcArgInt ( pCall, valueInt [pos] ); break;
75 case 1: dcArgLongLong( pCall, valueLongLong[pos] ); break; 75 case 1: dcArgLongLong( pCall, valueLongLong[pos] ); break;
76 case 2: dcArgDouble ( pCall, valueDouble [pos] ); break; 76 case 2: dcArgDouble ( pCall, valueDouble [pos] ); break;
77 case 3: dcArgPointer ( pCall, valuePointer [pos] ); break; 77 case 3: dcArgPointer ( pCall, valuePointer [pos] ); break;
78 } 78 }
79 } 79 }
80 80
81 #define assert(x) if (!(x)) return false 81 #define test(x) if (!(x)) return false
82 82
83 bool test_ellipsis_case(int x) 83 bool test_ellipsis_case(int x)
84 { 84 {
85 clearValues(); 85 clearValues();
86 86
87 DCCallVM* pCall = dcNewCallVM(4096); 87 DCCallVM* pCall = dcNewCallVM(4096);
88 88
89 assert( dcGetError(pCall) == DC_ERROR_NONE ); 89 test( dcGetError(pCall) == DC_ERROR_NONE );
90 90
91 dcMode(pCall, DC_CALL_C_ELLIPSIS); 91 dcMode(pCall, DC_CALL_C_ELLIPSIS);
92 dcReset(pCall); 92 dcReset(pCall);
93 93
94 assert( dcGetError(pCall) == DC_ERROR_NONE ); 94 test( dcGetError(pCall) == DC_ERROR_NONE );
95 95
96 int y = x; 96 int y = x;
97 int selects[NARGS] = { 0 }; 97 int selects[NARGS] = { 0 };
98 int pos = 0; 98 int pos = 0;
99 if (y > 0) 99 if (y > 0)
100 { 100 {
101 int select = (y-1) % NTYPES; 101 int select = (y-1) % NTYPES;
102 selects[pos] = select; 102 selects[pos] = select;
103 arg(pCall,select,pos); 103 arg(pCall,select,pos);
104 y = (y-1) / NTYPES; 104 y = (y-1) / NTYPES;
105 ++pos; 105 ++pos;
106 } 106 }
107 107
108 dcMode(pCall, DC_CALL_C_ELLIPSIS_VARARGS); 108 dcMode(pCall, DC_CALL_C_ELLIPSIS_VARARGS);
109 109
110 for(; y>0; ++pos) 110 for(; y>0; ++pos)
111 { 111 {
112 int select = (y-1) % NTYPES; 112 int select = (y-1) % NTYPES;
113 selects[pos] = select; 113 selects[pos] = select;
114 arg(pCall,select,pos); 114 arg(pCall,select,pos);
115 y = (y-1) / NTYPES; 115 y = (y-1) / NTYPES;
116 } 116 }
117 dcCallVoid(pCall,getFunc(x)); 117 dcCallVoid(pCall,getFunc(x));
118 118
119 assert( getId() == x ); 119 test( getId() == x );
120 120
121 for(int i=0; i<pos; ++i) { 121 for(int i=0; i<pos; ++i) {
122 assert( equals( selects[i], i, getArg(i) ) ); 122 test( equals( selects[i], i, getArg(i) ) );
123 } 123 }
124 124
125 dcFree(pCall); 125 dcFree(pCall);
126 return true; 126 return true;
127 } 127 }
128 128
129 int powerfact(int x, int n) 129 int powerfact(int x, int n)
155 155
156 bool success = false; 156 bool success = false;
157 init(); 157 init();
158 if (argc == 2) { 158 if (argc == 2) {
159 int index = atoi(argv[1]); 159 int index = atoi(argv[1]);
160 success = run_range( index, index+1 ); 160 success = run_range( index, index+1 );
161 } else if (argc == 3) { 161 } else if (argc == 3) {
162 int from = atoi(argv[1]); 162 int from = atoi(argv[1]);
163 int to = atoi(argv[2])+1; 163 int to = atoi(argv[2])+1;
164 success = run_range(from,to); 164 success = run_range(from,to);
165 } else { 165 } else {
166 int ncalls = powerfact(NTYPES,NARGS)+1; 166 int ncalls = powerfact(NTYPES,NARGS)+1;
167 success = run_range(0,ncalls); 167 success = run_range(0,ncalls);
168 } 168 }
169 169
170 printf("result: ellipsis: %s\n", success ? "1" : "0"); 170 printf("result: ellipsis: %d\n", success);
171 171
172 dcTest_deInitPlatform(); 172 dcTest_deInitPlatform();
173 173
174 return (success) ? 0 : -1; 174 return !success;
175 } 175 }
176 176
177 } // extern "C" 177 } // extern "C"
178 178