-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidation.cpp
119 lines (98 loc) · 2.91 KB
/
validation.cpp
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
#include "defs.h"
char ansFilename[FILENAME_LEN];
char resFilename[FILENAME_LEN];
using namespace std;
/* helper */
void usage(int argc, char **argv)
{
printf("%s\n", argv[0]);
printf("Usage:\n");
printf("%s -ans <right answer> -res <result>\n", argv[0]);
printf("Options:\n");
printf(" -ans <right answer> -- right answer filename\n");
printf(" -res <result> -- result filename\n");
exit(1);
}
/* initialization */
void init(int argc, char **argv)
{
int l;
bool no_ansFilename = true;
bool no_resFilename = true;
ansFilename[0] = resFilename[0] = '\0';
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-ans")) {
l = strlen(argv[++i]);
strncpy(ansFilename, argv[i], (l > FILENAME_LEN - 1 ? FILENAME_LEN - 1 : l));
no_ansFilename = false;
}
if (!strcmp(argv[i], "-res")) {
l = strlen(argv[++i]);
strncpy(resFilename, argv[i], (l > FILENAME_LEN - 1 ? FILENAME_LEN - 1 : l));
no_resFilename = false;
}
}
if (no_ansFilename || no_resFilename) {
usage(argc, argv);
}
}
int main(int argc, char **argv)
{
init(argc, argv);
FILE *f;
f = fopen(ansFilename, "rb");
assert(f != NULL);
vector<double> answer;
/* read the right answer */
double val;
while (fread(&val, sizeof(double), 1, f) == 1) {
answer.push_back(val);
}
fclose(f);
vertex_id_t n = answer.size();
f = fopen(resFilename, "rb");
assert(f != NULL);
double *result = new double[n];
assert(result != NULL);
/* read the result */
assert(fread(result, sizeof(double), n, f) == n);
fclose(f);
bool right_answer = true;
/* first check */
for (vertex_id_t i = 0; i < n; i++) {
switch (fpclassify(result[i])) {
case FP_INFINITE:
right_answer = false;
break;
case FP_NAN:
right_answer = false;
break;
case FP_SUBNORMAL:
right_answer = false;
break;
default:
break;
}
}
if (!right_answer) {
/* red color */
cout << "\033[1;31mNan answer\033[0m\n";
delete[] result;
return 1;
}
/* comparison the result with right answer */
for (vertex_id_t i = 0; i < n; i++) {
if (!(fabs(answer[i] - result[i]) < eps || fabs(answer[i] - result[i]) / answer[i] < eps)) {
/* red color */
cout << "\033[1;31mWrong answer i = " << i
<< " answer = " << answer[i]
<< " result = " << result[i] << "\033[0m\n";
delete[] result;
return 2;
}
}
/* green color */
cout << "\033[1;32mAccepted\033[0m\n";
delete[] result;
return 0;
}