Write test framework
This commit is contained in:
parent
64c3aaf077
commit
3d0008b496
@ -5,6 +5,9 @@ CFLAGS="$CFLAGS -Og -ggdb"
|
||||
|
||||
mkdir -p build
|
||||
|
||||
# Build tests
|
||||
clang $CFLAGS -Itests -c -o build/testing.o tests/testing.c
|
||||
|
||||
# Build application
|
||||
clang $CFLAGS -c -o build/main.o app/main.c
|
||||
clang $CFLAGS -o build/infix-calculator build/main.o
|
||||
|
21
tests/testing.c
Normal file
21
tests/testing.c
Normal file
@ -0,0 +1,21 @@
|
||||
#include "testing.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
test_run_t test_run;
|
||||
|
||||
void testing_fail_msg(const char *func, const char *file, int 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);
|
||||
}
|
60
tests/testing.h
Normal file
60
tests/testing.h
Normal file
@ -0,0 +1,60 @@
|
||||
#ifndef TESTING_H
|
||||
#define TESTING_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define TESTING_BEGIN() \
|
||||
do { \
|
||||
memset(&test_run, 0, sizeof(test_run)); \
|
||||
} 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() \
|
||||
do { \
|
||||
++test_run.fail_count; \
|
||||
testing_fail_msg(__func__, __FILE__, __LINE__); \
|
||||
return; \
|
||||
} while (0)
|
||||
|
||||
#define ASSERT_TRUE(p) \
|
||||
do { \
|
||||
++test_run.assertion_count; \
|
||||
if (!(p)) \
|
||||
ASSERT_FAIL(); \
|
||||
} while (0)
|
||||
|
||||
#define ASSERT_FALSE(p) ASSERT_TRUE(!(p))
|
||||
#define ASSERT_EQUAL(x, y) ASSERT_TRUE((x) == (y))
|
||||
#define ASSERT_NOT_EQUAL(x, y) ASSERT_TRUE((x) != (y))
|
||||
#define ASSERT_NULL(p) ASSERT_TRUE(NULL == (p))
|
||||
#define ASSERT_NOT_NULL(p) ASSERT_TRUE(NULL != (p))
|
||||
#define ASSERT_GT(x, y) ASSERT_TRUE((y) > (x))
|
||||
#define ASSERT_GE(x, y) ASSERT_TRUE((y) >= (x))
|
||||
#define ASSERT_LT(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)
|
||||
|
||||
typedef struct {
|
||||
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_end_msg(void);
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user