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
|
#include "wmap_parser.h"
#include <time.h>
void print_component(const wmap_component_t* comp) {
const char* shape_names[] = {"", "X", "Square", "Triangle", "Circle"};
printf("Component: %s (%.2f, %.2f)", comp->name, comp->x, comp->y);
if (comp->shape != WMAP_SHAPE_NONE) {
printf(" [%s]", shape_names[comp->shape]);
}
printf("\n");
}
void print_dependency(const wmap_dependency_t* dep) {
printf("Dependency: %s %s %s\n", dep->from, dep->is_arrow ? "->" : "--", dep->to);
}
void print_note(const wmap_note_t* note) {
printf("Note: (%.2f, %.2f) %s\n", note->x, note->y, note->text);
}
void print_stage(const wmap_stage_data_t* stage) {
const char* stage_names[] = {"", "I", "II", "III", "IV"};
printf("Stage: [%s] %.2f\n", stage_names[stage->stage], stage->value);
}
void print_group(const wmap_group_t* group) {
int i;
printf("Group: ");
for (i = 0; i < group->member_count; i++) {
printf("%s", group->members[i]);
if (i < group->member_count - 1) printf(", ");
}
printf("\n");
}
void print_inertia(const wmap_inertia_t* inertia) {
printf("Inertia: %s\n", inertia->name);
}
void print_evolution(const wmap_evolution_t* evolution) {
printf("Evolution: %s %+.2f\n", evolution->name, evolution->change);
}
void print_map_stats(const wmap_map_t* map) {
printf("\n=== Map Statistics ===\n");
printf("Components: %d\n", map->component_count);
printf("Dependencies: %d\n", map->dependency_count);
printf("Notes: %d\n", map->note_count);
printf("Stages: %d\n", map->stage_count);
printf("Groups: %d\n", map->group_count);
printf("Inertias: %d\n", map->inertia_count);
printf("Evolutions: %d\n", map->evolution_count);
printf("\n");
}
void print_map_contents(const wmap_map_t* map) {
int i;
printf("=== Components ===\n");
for (i = 0; i < map->component_count; i++) {
print_component(&map->components[i]);
}
printf("\n=== Dependencies ===\n");
for (i = 0; i < map->dependency_count; i++) {
print_dependency(&map->dependencies[i]);
}
printf("\n=== Notes ===\n");
for (i = 0; i < map->note_count; i++) {
print_note(&map->notes[i]);
}
printf("\n=== Stages ===\n");
for (i = 0; i < map->stage_count; i++) {
print_stage(&map->stages[i]);
}
printf("\n=== Groups ===\n");
for (i = 0; i < map->group_count; i++) {
print_group(&map->groups[i]);
}
printf("\n=== Inertias ===\n");
for (i = 0; i < map->inertia_count; i++) {
print_inertia(&map->inertias[i]);
}
printf("\n=== Evolutions ===\n");
for (i = 0; i < map->evolution_count; i++) {
print_evolution(&map->evolutions[i]);
}
}
int benchmark_parser(const char* filename, double time_limit_seconds) {
clock_t start, end, current;
double elapsed_time;
int iterations = 0;
double avg_time_ns;
FILE* file;
long size;
char* buffer;
/* Load file once before benchmarking */
file = fopen(filename, "r");
if (!file) {
printf("Error: Failed to open file %s\n", filename);
return 1;
}
fseek(file, 0, SEEK_END);
size = ftell(file);
fseek(file, 0, SEEK_SET);
if (size < 0 || size > 10 * 1024 * 1024) {
fclose(file);
printf("Error: Invalid file size\n");
return 1;
}
buffer = (char*)malloc(size + 1);
if (!buffer) {
fclose(file);
printf("Error: Failed to allocate buffer\n");
return 1;
}
fread(buffer, 1, size, file);
buffer[size] = '\0';
fclose(file);
/* Now benchmark only the parse_string function */
start = clock();
while (1) {
wmap_map_t* map = wmap_parse_string(buffer);
if (!map) {
printf("Error: Failed to parse file on iteration %d\n", iterations);
free(buffer);
return 1;
}
wmap_map_free(map);
iterations++;
current = clock();
elapsed_time = ((double)(current - start)) / CLOCKS_PER_SEC;
if (elapsed_time >= time_limit_seconds) {
break;
}
}
end = clock();
elapsed_time = ((double)(end - start)) / CLOCKS_PER_SEC;
avg_time_ns = (elapsed_time / iterations) * 1000000000.0;
printf(" Iterations: %d\n", iterations);
printf(" Avg time: %.2f ns/iter\n", avg_time_ns);
printf(" Ops/sec: %.2f\n", iterations / elapsed_time);
free(buffer);
return 0;
}
void run_benchmark_suite(const char* example_file, const char* huge_file) {
printf("=== Parsing Benchmark ===\n\n");
printf("Example map:\n");
benchmark_parser(example_file, 1.0);
printf("\nHuge map:\n");
benchmark_parser(huge_file, 1.0);
}
int main(int argc, char* argv[]) {
const char* filename;
wmap_map_t* map;
if (argc < 2) {
printf("Usage: %s <wmap_file> [benchmark]\n", argv[0]);
printf(" %s benchmark\n", argv[0]);
return 1;
}
/* Check if we're running the benchmark suite */
if (argc == 2 && strcmp(argv[1], "benchmark") == 0) {
run_benchmark_suite("doc/example.wmap", "doc/huge.wmap");
return 0;
}
filename = argv[1];
printf("=== WMAP Parser Test ===\n");
printf("Parsing file: %s\n", filename);
map = wmap_parse_file(filename);
if (!map) {
printf("Error: Failed to parse file %s\n", filename);
return 1;
}
print_map_stats(map);
if (argc > 2 && strcmp(argv[2], "benchmark") == 0) {
wmap_map_free(map);
printf("\nRunning benchmark for %s...\n", filename);
benchmark_parser(filename, 1.0);
} else {
print_map_contents(map);
wmap_map_free(map);
}
printf("=== Test Complete ===\n");
return 0;
}
|