diff options
| author | Filip Wandzio <contact@philw.dev> | 2026-03-01 01:03:39 +0100 |
|---|---|---|
| committer | Filip Wandzio <contact@philw.dev> | 2026-03-01 01:03:39 +0100 |
| commit | bf0d77d7d448e964e9716d5af67c48f3d014f090 (patch) | |
| tree | e55f1e91a8c20cd737dfb01dc12a954c25711e01 /tests | |
| download | embedded_guardian-bf0d77d7d448e964e9716d5af67c48f3d014f090.tar.gz embedded_guardian-bf0d77d7d448e964e9716d5af67c48f3d014f090.zip | |
Scaffold basic project tree, implement benchmarking logic
Implement unit testing guardian
Diffstat (limited to '')
| -rw-r--r-- | tests/test_syntax_essentials.c | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/tests/test_syntax_essentials.c b/tests/test_syntax_essentials.c new file mode 100644 index 0000000..e0f0744 --- /dev/null +++ b/tests/test_syntax_essentials.c | |||
| @@ -0,0 +1,125 @@ | |||
| 1 | #include "benchmark.h" | ||
| 2 | #include "syntax_essentials.h" | ||
| 3 | #include <stdio.h> | ||
| 4 | |||
| 5 | static int total_tests = 0; | ||
| 6 | static int passed_tests = 0; | ||
| 7 | static int any_failure = 0; | ||
| 8 | |||
| 9 | /** | ||
| 10 | * | ||
| 11 | * @param name | ||
| 12 | * @param func | ||
| 13 | * @param arg | ||
| 14 | * @param expected | ||
| 15 | * @param iterations | ||
| 16 | */ | ||
| 17 | static void run_test(const char* name, | ||
| 18 | const function_to_benchmark func, | ||
| 19 | const char* arg, | ||
| 20 | const int expected, | ||
| 21 | const size_t iterations) | ||
| 22 | { | ||
| 23 | total_tests++; | ||
| 24 | |||
| 25 | const double avg_ms = benchmark_func(func, arg, iterations); | ||
| 26 | const int result = func(arg); | ||
| 27 | |||
| 28 | const int logic_ok = (result == expected); | ||
| 29 | const int perf_ok = (avg_ms <= MAX_ALLOWED_MS); | ||
| 30 | const int ok = logic_ok && perf_ok; | ||
| 31 | |||
| 32 | if (ok) | ||
| 33 | passed_tests++; | ||
| 34 | else | ||
| 35 | any_failure = 1; | ||
| 36 | |||
| 37 | report_result(name, ok, avg_ms, MAX_ALLOWED_MS); | ||
| 38 | } | ||
| 39 | |||
| 40 | /** | ||
| 41 | * | ||
| 42 | */ | ||
| 43 | static void test_names(void) | ||
| 44 | { | ||
| 45 | run_test("validate_first_name: valid", | ||
| 46 | validate_first_name, | ||
| 47 | "Jan", | ||
| 48 | 1, | ||
| 49 | ITERATIONS_FAST); | ||
| 50 | run_test("validate_first_name: too short", | ||
| 51 | validate_first_name, | ||
| 52 | "J", | ||
| 53 | 0, | ||
| 54 | ITERATIONS_FAST); | ||
| 55 | run_test("validate_last_name: valid", | ||
| 56 | validate_last_name, | ||
| 57 | "Kowalski", | ||
| 58 | 1, | ||
| 59 | ITERATIONS_FAST); | ||
| 60 | run_test("validate_last_name: invalid char", | ||
| 61 | validate_last_name, | ||
| 62 | "Kowalski3", | ||
| 63 | 0, | ||
| 64 | ITERATIONS_FAST); | ||
| 65 | } | ||
| 66 | |||
| 67 | /** | ||
| 68 | * | ||
| 69 | */ | ||
| 70 | static void test_full_name(void) | ||
| 71 | { | ||
| 72 | run_test("validate_full_name: valid", | ||
| 73 | validate_full_name, | ||
| 74 | "Jan Kowalski", | ||
| 75 | 1, | ||
| 76 | ITERATIONS_SLOW); | ||
| 77 | run_test("validate_full_name: invalid first", | ||
| 78 | validate_full_name, | ||
| 79 | "J Kowalski", | ||
| 80 | 0, | ||
| 81 | ITERATIONS_SLOW); | ||
| 82 | } | ||
| 83 | |||
| 84 | /** | ||
| 85 | * | ||
| 86 | */ | ||
| 87 | static void test_index(void) | ||
| 88 | { | ||
| 89 | run_test("validate_index: valid", | ||
| 90 | validate_index, | ||
| 91 | "123456", | ||
| 92 | 1, | ||
| 93 | ITERATIONS_FAST); | ||
| 94 | run_test("validate_index: too short", | ||
| 95 | validate_index, | ||
| 96 | "12345", | ||
| 97 | 0, | ||
| 98 | ITERATIONS_FAST); | ||
| 99 | } | ||
| 100 | |||
| 101 | /** | ||
| 102 | * | ||
| 103 | * @return | ||
| 104 | */ | ||
| 105 | int main(void) | ||
| 106 | { | ||
| 107 | printf("=== Embedded Syntax Essentials Test Suite ===\n"); | ||
| 108 | |||
| 109 | test_names(); | ||
| 110 | test_full_name(); | ||
| 111 | test_index(); | ||
| 112 | |||
| 113 | printf("\n====================================\n"); | ||
| 114 | printf("Tests passed: %d / %d\n", passed_tests, total_tests); | ||
| 115 | |||
| 116 | if (!any_failure) { | ||
| 117 | printf( | ||
| 118 | "%sALL CRITERIA MET — PASS%s\n", COLOR_GREEN, COLOR_RESET); | ||
| 119 | return 0; | ||
| 120 | } | ||
| 121 | printf("%sSUITE FAILED — performance or logic violation%s\n", | ||
| 122 | COLOR_RED, | ||
| 123 | COLOR_RESET); | ||
| 124 | return 1; | ||
| 125 | } | ||
