-
Notifications
You must be signed in to change notification settings - Fork 3
/
tmc-check.c
229 lines (198 loc) · 5.2 KB
/
tmc-check.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
#include "tmc-check.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
typedef struct SuitePoints
{
Suite *s;
const char *s_name;
const char *points;
struct SuitePoints *next;
} SuitePoints;
typedef struct PointsAssoc
{
TCase *tc;
const char *tc_name;
const char *points;
struct PointsAssoc *next;
} PointsAssoc;
typedef struct PointsList
{
char *point;
struct PointsList *next;
} PointsList;
static PointsAssoc *points_assocs = NULL;
static SuitePoints *suite_points = NULL;
static PointsList *all_points = NULL;
static void parse_points(const char *points, PointsList **target_list);
static void add_to_point_set(const char *point, ssize_t len, PointsList **target_list);
static int points_list_contains(const PointsList *list, const char *point, ssize_t len);
static void delete_points_assoc();
static void delete_suite_points();
static void delete_all_points();
void tmc_set_tcase_points(TCase *tc, const char *tc_name, const char *points)
{
PointsAssoc *pa = (PointsAssoc*)malloc(sizeof(PointsAssoc));
pa->tc = tc;
pa->tc_name = tc_name;
pa->points = points;
pa->next = points_assocs;
points_assocs = pa;
parse_points(points, &all_points);
}
void _tmc_register_test(Suite *s, TFun tf, const char *fname, const char *points)
{
TCase *tc = tcase_create(fname);
tmc_set_tcase_points(tc, fname, points);
_tcase_add_test(tc, tf, fname, 0, 0, 0, 1);
suite_add_tcase(s, tc);
}
void tmc_set_suite_points(Suite *s, const char *s_name, const char *points)
{
SuitePoints *sp = (SuitePoints*) malloc(sizeof(SuitePoints));
sp->s = s;
sp->points = points;
sp->s_name = s_name;
sp->next = suite_points;
suite_points = sp;
parse_points(points, &all_points);
}
Suite* tmc_suite_create(const char *name, const char *points)
{
Suite *s = suite_create(name);
tmc_set_suite_points(s, name, points);
return s;
}
int tmc_run_tests(int argc, const char **argv, Suite *s)
{
int i;
for (i = 1; i < argc; ++i) {
if (strcmp(argv[i], "--print-available-points") == 0) {
return tmc_print_available_points(stdout, '\n');
}
}
FILE *points_file = fopen("tmc_available_points.txt", "wb");
if (tmc_print_suite_points(points_file) != 0) {
fclose(points_file);
return EXIT_FAILURE;
}
if (tmc_print_test_points(points_file) != 0) {
fclose(points_file);
return EXIT_FAILURE;
}
fclose(points_file);
SRunner *sr = srunner_create(s);
srunner_set_xml(sr, "tmc_test_results.xml");
srunner_run_all(sr, CK_VERBOSE);
srunner_free(sr);
delete_points_assoc();
delete_all_points();
delete_suite_points();
return EXIT_SUCCESS;
}
int tmc_print_available_points(FILE *f, char delimiter)
{
const PointsList *pl = all_points;
while (pl != NULL) {
fputs(pl->point, f);
fputc(delimiter, f);
pl = pl->next;
}
fflush(f);
return 0;
}
int tmc_print_test_points(FILE *f)
{
const PointsAssoc *pa = points_assocs;
while (pa != NULL) {
fprintf(f, "[test] [%s] %s\n", pa->tc_name, pa->points);
pa = pa->next;
}
fflush(f);
return 0;
}
int tmc_print_suite_points(FILE *f)
{
const SuitePoints *sp = suite_points;
while (sp != NULL) {
fprintf(f, "[suite] [%s] %s\n", sp->s_name, sp->points);
sp = sp->next;
}
fflush(f);
return 0;
}
static void parse_points(const char *points, PointsList **target_list)
{
const char *p = points;
const char *q = p;
while (*q != '\0') {
if (isspace((int)*q)) {
const ssize_t len = q - p;
if (!isspace((int)*p)) {
add_to_point_set(p, len, target_list);
}
p = q + 1;
q = p;
} else {
q++;
}
}
if (!isspace((int)*p) && q > p) {
const ssize_t len = q - p;
add_to_point_set(p, len, target_list);
}
}
static void add_to_point_set(const char *point, ssize_t len, PointsList **target_list)
{
if (!points_list_contains(*target_list, point, len)) {
PointsList *pl = (PointsList*)malloc(sizeof(PointsList));
pl->point = malloc(len + 1);
memcpy(pl->point, point, len);
pl->point[len] = '\0';
pl->next = *target_list;
*target_list = pl;
}
}
static int points_list_contains(const PointsList *list, const char *point, ssize_t len)
{
const PointsList *pl = all_points;
while (pl != NULL) {
if (strncmp(pl->point, point, len) == 0) {
return 1;
}
pl = pl->next;
}
return 0;
}
static void delete_points_assoc()
{
PointsAssoc *pa = points_assocs;
while(pa) {
PointsAssoc *next = pa->next;
free(pa);
pa = next;
}
points_assocs = NULL;
}
static void delete_suite_points()
{
SuitePoints *sp = suite_points;
while(sp) {
SuitePoints *next = sp->next;
free(sp);
sp = next;
}
suite_points = NULL;
}
static void delete_all_points()
{
PointsList *pl = all_points;
while(pl) {
PointsList *next = pl->next;
free(pl->point);
free(pl);
pl = next;
}
all_points = NULL;
}