-
Notifications
You must be signed in to change notification settings - Fork 1
/
testgen.c
317 lines (262 loc) · 8.32 KB
/
testgen.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include "testgen.h"
// --- An ephemeris test data generator, configurable with a fixture file
int main( int argc, char** argv) {
doInitializations();
if (argc == 3)
generateTestsetFromFixture(argv[1],argv[2]);
else {
printf("Wrong arg number\nSyntax: testgen [fixture file] [test data file]");
exit(EXIT_FAILURE);
}
}
static int generateTestsetFromFixture(char* fixtureFile, char* dataFile) {
FILE* data = openFileBinary(dataFile);
testFixture tests[MAX_FIXNUM];
int size;
if (parseFixtureFile(fixtureFile, tests, &size) == ERR) {
printf( "Error while reading fixture file\n" );
return ERR;
}
for (int i=0; i<size;i++) {
if (generateTest( data, & tests[i] )==ERR) {
printf("Error in generation of test %d\n",i+1);
return ERR;
}
}
fclose(data);
return OK;
}
static int generateTest( FILE* out, testFixture* test) {
long int afterHeaderPos = doGeneralHeader( out, SWISS_CALC_TEST, test->description );
doSwissCalcHeader( out, afterHeaderPos, test, 0 );
int numberOfRecords = doSwissCalcTestData( out, test, false );
// Correct SwissCalcHeader block - set number of records
doSwissCalcHeader( out, afterHeaderPos, test, numberOfRecords );
// Goto end of file afterwards
fseek( out, 0, SEEK_END );
printf("%s : %d records generated\n", test->description, numberOfRecords );
return OK;
}
static long int doGeneralHeader( FILE* out, int type, char* description ) {
testHeader header;
header.type = type ; // there may be other test types than SwissCalc
strcpy(header.description,description);
fwrite( &header, sizeof(header), 1, out);
return ftell(out);
}
static void doSwissCalcHeader( FILE* out, long int pos, testFixture* test, int numberOfRecords) {
swissCalcHeader scHeader;
for (int i=0;i<6;i++) scHeader.precisions[i] = test->precisions[i];
scHeader.numberOfRecords = numberOfRecords;
fseek( out, pos, SEEK_SET);
fwrite( &scHeader, sizeof(scHeader), 1, out); // will be corrected later
}
static int doSwissCalcTestData( FILE* out, testFixture* test, bool warn_on_flag_change) {
int numberOfRecords = 0;
double dates[test->n_dates];
test->method(dates,test->n_dates,test->jd_from,test->jd_to,test->incr);
range_iterator
planet_range = { &test->planets, 0, -1 },
flags_range = { &test->flags, 0, -1 };
swissCalcData scData;
while( (scData.planet = get_next(&planet_range)) != END_OF_RANGE) {
while( (scData.flags = get_next(&flags_range)) != END_OF_RANGE) {
for (int i=0;i<test->n_dates;i++) {
scData.jd = dates[i];
numberOfRecords++;
scData.msg[0] = '\0';
memset( scData.result, 0, 6*sizeof( double ) ) ;
scData.flags_ret = swe_calc( dates[i], scData.planet, scData.flags, scData.result, scData.msg );
if (warn_on_flag_change) {
warn_if_flag_changed( &scData );
}
fwrite( & scData, sizeof( scData) , 1, out );
}
}
}
return numberOfRecords;
}
static void warn_if_flag_changed(const swissCalcData *scData) {
if (scData->flags_ret != scData->flags) {
char bits1[33],bits2[33];
int_to_binary(bits1,scData->flags_ret);
int_to_binary(bits2,scData->flags);
printf(
"(Warning:) Flags changed! %15.10f %d %f %s : \n%32s (actual)\n%32s (requested)\n",
scData->jd, scData->planet,
scData->result[0], scData->msg,
bits1,
bits2
);
}
}
void get_random_dates(double dates[], int n, double from, double to, double dummy) {
for (int i=0;i<n;i++) {
dates[i] = from + rand()*(to-from)/RAND_MAX; // Suffices for out purpose
}
}
void get_date_series(double dates[], int n, double from, double to, double incr) {
for (int i=0;i<n;i++) {
dates[i] = from + i*incr;
}
}
static int parseFixtureFile(char* fixtureFile, testFixture* tests, int *size) {
FILE* fixture = fopen( fixtureFile, "r");
if (fixture==NULL) {
printf("Can't open %s\n",fixtureFile);
return ERR;
}
char line[255], name[50], value[205];
*size = 0;
testFixture* data;
for (;fgets(line,255,fixture);) {
if (sscanf(line," %[T]ESTCASE",value)==1) {
data = & tests[*size];
(*size)++;
clear( data );
continue;
}
// Skip everything before the first TESTCASE tag
if (*size == 0) continue;
// Skip comments
if (sscanf(line," %[#]",name)) continue;
// From here on: Only name:value pairs are scanned
if (sscanf(line,"%[^: \t] : %[^\n#] \n",name,value)==2) {
if (parseNameValuePair( name, value, data )==ERR) {
printf("Error when parsing '%s:%s'\n",name,value);
return ERR;
}
}
}
return OK;
}
static int parseNameValuePair( char* name, char* value, testFixture* data) {
if (equals(name,"description")) {
strcpy(data->description,trim(value));
}
else if (equals(name,"flags")) {
return parseRange(value, &data->flags);
}
else if (equals(name,"planets")) {
return parseRange(value, &data->planets);
}
else if (equals(name,"dates")) {
return parseDates( value, data);
}
else if (equals(name,"jd")) {
if (!sscanf(value,"%lf",&data->jd_from)) return ERR;
data->n_dates = 1;
data->jd_to = data->jd_from + 1;
data->incr = 1;
data->method = get_date_series;
}
else if (equals(name,"precisions")) {
return parsePrecisions(value,data->precisions);
}
else {
printf("Couldn't understand '%s':'%s'\n",name,value);
return ERR;
}
return OK;
}
static int parseRange( char* value, rangetab *r) {
char *p0 = value,
*p1,
*end = p0 + strlen(value);
range line;
do {
int n = sscanf(p0,"%d - %d", &line.low, &line.high);
line.between = (n==2);
r->line[(r->size)++]=line;
} while(
(p1 = strchr(p0,',')) != NULL &&
(p0 = p1+1) < end &&
r->size < MAXRANGE );
return OK;
}
static int get_next( range_iterator *iter) {
rangetab *r = iter->r;
int value;
if (iter->i >= r->size) {
iter->i = 0;
return END_OF_RANGE;
}
if (r->line[iter->i].between) {
if (iter->j == -1) {
iter->j = r->line[iter->i].low;
}
else {
(iter->j)++;
if (iter->j > r->line[iter->i].high) {
(iter->i)++;
(iter->j) = -1;
return get_next(iter);
}
}
value = iter->j;
}
else {
value = r->line[iter->i].low;
(iter->i)++;
(iter->j) = 0;
}
return value;
}
static int parseDates( char* value, testFixture* data) {
int d_from, d_to, m_from, m_to, y_from, y_to;
char method[50];
int args = sscanf(value,"%d . %d . %d - %d . %d . %d , %d , %s ",
&d_from, &m_from, &y_from, &d_to, &m_to, &y_to, &(data->n_dates), method );
if (args < 7) {
printf("Not enough parameters in '%s'\n",value);
return ERR;
}
if (strcmp(method,"randomly")==0) {
data->method = get_random_dates;
} else {
data->method = get_date_series;
sscanf(method, "%lf",&data->incr);
}
data->jd_from = swe_julday( y_from, m_from, d_from, (y_from > 1582) ? 1 : 0, 0 );
data->jd_to = swe_julday( y_to, m_to, d_to, (y_to > 1582) ? 1 : 0, 0 );
return OK;
}
static int parsePrecisions( char* value, int* precisions ) {
char *vp = value,
*vend = value + strlen(value);
int i=0;
while ( sscanf( vp, "%d", &precisions[i])
&& ++i < 6
&& (vp = strchr(vp,',')) != NULL
&& ++vp < vend );
return OK;
}
static FILE* openFileBinary( const char* file ) {
FILE* f = fopen( file,"wb");
if (!f) {
printf("Can't open %s\n",file);
exit(EXIT_FAILURE);
}
return f;
}
static void clear( testFixture* data) {
* data->description = '\0';
data->planet = data->iflag = data->n_dates = 0;
for (int i=0;i<6;i++) data->precisions[i] = i < 3 ? 0 : 1;
data->jd_from = data->jd_to = data->incr = 0;
data->planets.size = 0;
data->flags.size = 0;
}
static char *int_to_binary(char* b, int x) {
char *p = b;
for (unsigned int z = 1<<31; z ; z >>= 1) {
*p++ = x & z ? '1' : '0';
}
*p = '\0';
return b;
}
static void doInitializations() {
// initialize pseudo random number generator
const int RAND_SEED = 2621411;
srand(RAND_SEED);
}