/* * Basic support for C unit tests. * * This file is part of Beastie * SPDX-FileCopyrightText: 2024 Norman Gray * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include "c-unit.h" static jmp_buf jmp_env; int ntests = 0; int nsuites = 0; int total_fails = 0; #define RED_TEXT(s) "\033[41;37m " s " \033[0m" #define GREEN_TEXT(s) "\033[42;30m " s " \033[0m" // compare signed/unsigned ints void assert_equal_int_fn(const char* label, int actual, int expected) { ntests++; if (actual != expected) { fprintf(stderr, "Test %s " RED_TEXT("failed") "\n " RED_TEXT("actual") ": %d\n " GREEN_TEXT("expected") ": %d\n", label, actual, expected); longjmp(jmp_env, 1); } } void assert_equal_uint_fn(const char* label, unsigned int actual, unsigned int expected) { ntests++; if (actual != expected) { fprintf(stderr, "Test %s " RED_TEXT("failed") "\n " RED_TEXT("actual") ": %d\n " GREEN_TEXT("expected") ": %d\n", label, actual, expected); longjmp(jmp_env, 1); } } #define NOTCOMPARE_ARRAYS \ ntests++; \ for (int i=0; i\n "); } else { fprintf(stderr, ": %.*s\n ", actual_len, actual); } fprintf(stderr, GREEN_TEXT("expected") ": %s\n", (expected == NULL ? "" : expected)); longjmp(jmp_env, 1); } return; } int ok = 1; for (int i=0; i