diff options
| author | Oskari Timperi <oskari.timperi@iki.fi> | 2014-05-14 22:27:34 +0300 |
|---|---|---|
| committer | Oskari Timperi <oskari.timperi@iki.fi> | 2014-05-14 22:27:34 +0300 |
| commit | d45a5b21de22438c004e1db96a8f154da09cdc0e (patch) | |
| tree | c2a7bd2a4d08de03ef5e11932d9a1abf85110c80 /test_util.h | |
| download | lispish-d45a5b21de22438c004e1db96a8f154da09cdc0e.tar.gz lispish-d45a5b21de22438c004e1db96a8f154da09cdc0e.zip | |
Initial commit
- tokenizing, parsing and basic eval support
- arithmetic (+, -, *, /)
- quote
- atom for checking if the arg is an atom (i.e. not a list)
- eq for checking equality
- > for checking order
- if
- some unit testing
- simple repl
- mem management needs improvement :-)
Diffstat (limited to 'test_util.h')
| -rw-r--r-- | test_util.h | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/test_util.h b/test_util.h new file mode 100644 index 0000000..1ebf696 --- /dev/null +++ b/test_util.h @@ -0,0 +1,93 @@ +#ifndef TEST_UTIL_H +#define TEST_UTIL_H + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#ifdef _MSC_VER + +#define CCALL __cdecl +#pragma section(".CRT$XCU",read) +#define INITIALIZER(f) \ + static void __cdecl f(void); \ + __declspec(allocate(".CRT$XCU")) void (__cdecl*f##_)(void) = f; \ + static void __cdecl f(void) + +#elif defined(__GNUC__) + +#define CCALL +#define INITIALIZER(f) \ + static void f(void) __attribute__((constructor)); \ + static void f(void) + +#endif + +// __attribute__((constructor)) static void test_##NAME##_init() + +#define TEST(NAME) \ + static void test_##NAME(); \ + void test_util_add_test(const char *name, void (*func)()); \ + INITIALIZER(test_##NAME##_init) \ + { \ + test_util_add_test(#NAME, &test_##NAME); \ + } \ + static void test_##NAME() + +#define TEST_MAIN() \ + typedef void (*test_func)(); \ + struct test_def { \ + const char *name; \ + test_func func; \ + struct test_def *next; \ + }; \ + static struct test_def test_defs; \ + void test_util_add_test(const char *name, void (*func)()) \ + { \ + struct test_def *def = calloc(1, sizeof(*def)); \ + struct test_def *last = &test_defs; \ + while (last && last->next) last = last->next; \ + last->next = def; \ + def->name = strdup(name); \ + def->func = func; \ + } \ + int main() \ + { \ + struct test_def *def = test_defs.next; \ + while (def) \ + { \ + printf("running test %s\n", def->name); \ + def->func(); \ + def = def->next; \ + } \ + return 0; \ + } + +#define ASSERT(X) \ + do { \ + if (!(X)) { \ + fprintf(stderr, "assertion %s failed at %s:%d\n", \ + #X, __FILE__, __LINE__); \ + exit(1); \ + } \ + } while (0) + +#define ASSERT_MSG(X, FMT, ...) \ + do { \ + if (!(X)) { \ + fprintf(stderr, "assertion %s failed at %s:%d (" FMT ")\n", \ + #X, __FILE__, __LINE__, ##__VA_ARGS__); \ + exit(1); \ + } \ + } while (0) + +#define ASSERT_TRUE(X) ASSERT(X) +#define ASSERT_FALSE(X) ASSERT(!(X)) + +#define ASSERT_EQ(EXPECTED, ACTUAL) ASSERT(((EXPECTED) == (ACTUAL))) +#define ASSERT_NEQ(EXPECTED, ACTUAL) ASSERT(((EXPECTED) != (ACTUAL))) + +#define ASSERT_STREQ(EXPECTED, ACTUAL) ASSERT(strcmp((EXPECTED), (ACTUAL)) == 0) +#define ASSERT_STREQ_N(EXPECTED, ACTUAL, N) ASSERT(strncmp((EXPECTED), (ACTUAL), (N)) == 0) + +#endif |
