-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_test_calc_reduce.c
125 lines (97 loc) · 3.08 KB
/
test_test_calc_reduce.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdbool.h>
#include "testdata.h"
#include "test_test_calc_reduce.h"
bool test(char *filename) {
FILE *testdata = fopen( filename,"rb" );
if (!testdata) {
char err[255];
sprintf( err, "Can't open %s", filename);
fail( err );
}
testHeader header;
bool ok = true;
while (hasMoreTestSets(testdata, &header)) {
result r = executeTestSet(testdata);
printf("%s: ", header.description );
if (r.ok) printf("%d OK ", r.ok);
if (r.not_ok) printf("%d NOT OK", r.not_ok);
printf("\n");
ok = ok && (r.not_ok == 0);
}
fclose( testdata );
return ok;
}
static int hasMoreTestSets(FILE* testdata, testHeader* header) {
fread( header, sizeof(testHeader), 1, testdata );
return !feof(testdata);
}
static result executeTestSet(FILE* testdata) {
result r = {0,0};
swissCalcHeader scHeader;
fread( &scHeader, sizeof(scHeader), 1, testdata );
swissCalcData scData[scHeader.numberOfRecords];
int numberOfRecordsRead = fread( &scData, sizeof(swissCalcData), scHeader.numberOfRecords, testdata );
if (numberOfRecordsRead != scHeader.numberOfRecords) {
printf( "Wrong number %d of records read - expected %d\n", numberOfRecordsRead, scHeader.numberOfRecords );
exit( EXIT_FAILURE );
}
double precisions[6];
computePrecisions( precisions, scHeader.precisions );
for (int i=0;i<scHeader.numberOfRecords;i++) {
bool test_ok = calcsAsExpected(
scData[i].jd,
scData[i].planet,
scData[i].flags,
scData[i].flags_ret,
scData[i].result,
precisions,
scData[i].msg
);
if (!test_ok) {
printf( "\nDifferences from test case %d", i );
printf( "\nJuldat %15.10f, planet %d, iflags %d\n",scData[i].jd, scData[i].planet, scData[i].flags);
}
if (test_ok) r.ok++;
else r.not_ok++;
}
return r;
}
static bool calcsAsExpected(
const double juldate,
const int planet,
const int flags,
const int flags_ret,
const double expectedResult[],
const double precisions[],
const char* expectedMessage
) {
double actualResult[6];
char actualMessage[255];
int return_flags = swe_calc(
juldate,
planet,
flags,
actualResult,
actualMessage );
bool ok =
diff_check_degree( "xx[0]", actualResult[0],expectedResult[0],precisions[0]);
for (int i=1;i<6;i++) {
char var[15];
sprintf(var,"xx[%d]",i);
ok = ok && diff_check(var,actualResult[i],expectedResult[i],precisions[i]);
}
ok = ok
&& diff_check_char( "serr", actualMessage, expectedMessage)
&& diff_check_int( "iflags", return_flags, flags_ret );
return ok;
}
static void computePrecisions( double precisions[6], int precisionsExp[6] ) {
for (int i=0;i<6;i++) {
precisions[i] = defaultPrecision;
if (precisionsExp[i] != 0) precisions[i] *= pow(10,precisionsExp[i]);
}
}