From bf0d77d7d448e964e9716d5af67c48f3d014f090 Mon Sep 17 00:00:00 2001 From: Filip Wandzio Date: Sun, 1 Mar 2026 01:03:39 +0100 Subject: Scaffold basic project tree, implement benchmarking logic Implement unit testing guardian --- tests/test_syntax_essentials.c | 125 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 tests/test_syntax_essentials.c (limited to 'tests') 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 @@ +#include "benchmark.h" +#include "syntax_essentials.h" +#include + +static int total_tests = 0; +static int passed_tests = 0; +static int any_failure = 0; + +/** + * + * @param name + * @param func + * @param arg + * @param expected + * @param iterations + */ +static void run_test(const char* name, + const function_to_benchmark func, + const char* arg, + const int expected, + const size_t iterations) +{ + total_tests++; + + const double avg_ms = benchmark_func(func, arg, iterations); + const int result = func(arg); + + const int logic_ok = (result == expected); + const int perf_ok = (avg_ms <= MAX_ALLOWED_MS); + const int ok = logic_ok && perf_ok; + + if (ok) + passed_tests++; + else + any_failure = 1; + + report_result(name, ok, avg_ms, MAX_ALLOWED_MS); +} + +/** + * + */ +static void test_names(void) +{ + run_test("validate_first_name: valid", + validate_first_name, + "Jan", + 1, + ITERATIONS_FAST); + run_test("validate_first_name: too short", + validate_first_name, + "J", + 0, + ITERATIONS_FAST); + run_test("validate_last_name: valid", + validate_last_name, + "Kowalski", + 1, + ITERATIONS_FAST); + run_test("validate_last_name: invalid char", + validate_last_name, + "Kowalski3", + 0, + ITERATIONS_FAST); +} + +/** + * + */ +static void test_full_name(void) +{ + run_test("validate_full_name: valid", + validate_full_name, + "Jan Kowalski", + 1, + ITERATIONS_SLOW); + run_test("validate_full_name: invalid first", + validate_full_name, + "J Kowalski", + 0, + ITERATIONS_SLOW); +} + +/** + * + */ +static void test_index(void) +{ + run_test("validate_index: valid", + validate_index, + "123456", + 1, + ITERATIONS_FAST); + run_test("validate_index: too short", + validate_index, + "12345", + 0, + ITERATIONS_FAST); +} + +/** + * + * @return + */ +int main(void) +{ + printf("=== Embedded Syntax Essentials Test Suite ===\n"); + + test_names(); + test_full_name(); + test_index(); + + printf("\n====================================\n"); + printf("Tests passed: %d / %d\n", passed_tests, total_tests); + + if (!any_failure) { + printf( + "%sALL CRITERIA MET — PASS%s\n", COLOR_GREEN, COLOR_RESET); + return 0; + } + printf("%sSUITE FAILED — performance or logic violation%s\n", + COLOR_RED, + COLOR_RESET); + return 1; +} -- cgit v1.2.3