#include "wmap_parser.h" #include 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 [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; }