Simplify test framework and improve test script

This commit is contained in:
Camden Dixie O'Brien 2024-10-24 15:59:56 +01:00
parent 29deffdce9
commit 810feee55e
3 changed files with 13 additions and 40 deletions

View File

@ -1,3 +1,8 @@
cd "$(git rev-parse --show-toplevel)" cd "$(git rev-parse --show-toplevel)"
echo ":: Evaluator tests ::" && build/evaluator_tests
echo ":: Reader tests ::" && build/reader_tests fails=0
build/evaluator_tests || fails=`expr $fails + 1`
build/reader_tests || fails=`expr $fails + 1`
if [ $fails -eq 0 ]; then echo Tests OK; fi

View File

@ -2,20 +2,9 @@
#include <stdio.h> #include <stdio.h>
test_run_t test_run; int fail_count;
void testing_fail_msg(const char *func, const char *file, int line) void testing_fail_msg(const char *func, const char *file, int line)
{ {
printf("%s: failed assert @ %s:%d\n", func, file, line); printf("%s: failed assert @ %s:%d\n", func, file, line);
} }
void testing_end_msg(void)
{
const double success_rate
= 1 - (double)test_run.fail_count / (double)test_run.test_count;
printf(
"%d tests ran (%d assertions), "
"%0.2f%% successful (%d failures)\n",
test_run.test_count, test_run.assertion_count, 100 * success_rate,
test_run.fail_count);
}

View File

@ -4,33 +4,19 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define TESTING_BEGIN() \ #define TESTING_BEGIN() fail_count = 0
do { \ #define TESTING_END() return 0 == fail_count ? EXIT_SUCCESS : EXIT_FAILURE
memset(&test_run, 0, sizeof(test_run)); \ #define RUN_TEST(test) test()
} while (0)
#define TESTING_END() \
do { \
testing_end_msg(); \
return 0 == test_run.fail_count ? EXIT_SUCCESS : EXIT_FAILURE; \
} while (0)
#define RUN_TEST(test) \
do { \
++test_run.test_count; \
test(); \
} while (0)
#define ASSERT_FAIL() \ #define ASSERT_FAIL() \
do { \ do { \
++test_run.fail_count; \ ++fail_count; \
testing_fail_msg(__func__, __FILE__, __LINE__); \ testing_fail_msg(__func__, __FILE__, __LINE__); \
return; \ return; \
} while (0) } while (0)
#define ASSERT_TRUE(p) \ #define ASSERT_TRUE(p) \
do { \ do { \
++test_run.assertion_count; \
if (!(p)) \ if (!(p)) \
ASSERT_FAIL(); \ ASSERT_FAIL(); \
} while (0) } while (0)
@ -46,15 +32,8 @@
#define ASSERT_LE(x, y) ASSERT_TRUE((y) <= (x)) #define ASSERT_LE(x, y) ASSERT_TRUE((y) <= (x))
#define ASSERT_MEM_EQUAL(p, q, n) ASSERT_TRUE(memcmp(p, q, n) == 0) #define ASSERT_MEM_EQUAL(p, q, n) ASSERT_TRUE(memcmp(p, q, n) == 0)
typedef struct { extern int fail_count;
int test_count;
int fail_count;
int assertion_count;
} test_run_t;
extern test_run_t test_run;
void testing_fail_msg(const char *func, const char *file, int line); void testing_fail_msg(const char *func, const char *file, int line);
void testing_end_msg(void);
#endif #endif