diff options
-rw-r--r-- | lily-test.h | 217 | ||||
-rw-r--r-- | tests/helpers.c | 1 | ||||
-rw-r--r-- | tests/macro-tests.c | 196 | ||||
-rw-r--r-- | tests/main.c | 1 | ||||
-rw-r--r-- | tests/tests.h | 2 |
5 files changed, 415 insertions, 2 deletions
diff --git a/lily-test.h b/lily-test.h index 54f214a..6d2d0a4 100644 --- a/lily-test.h +++ b/lily-test.h @@ -65,6 +65,12 @@ #endif +/* self-location macro */ +#ifndef LILY_TEST_H_LOCATION +#define LILY_TEST_H_LOCATION "lily-test.h" +#endif + + /* counter macros */ #define LILY_HEX_(x) 0x##x #define LILY_HEX(x) LILY_HEX_(x) @@ -75,12 +81,41 @@ #define LILY_COUNTER_(a, b, c) LILY_COUNTER__(a, b, c) #define LILY_COUNTER LILY_COUNTER_(LILY_COUNTER_POS_2, LILY_COUNTER_POS_1, LILY_COUNTER_POS_0) +/* automatic registration macros */ +typedef struct lily_ll_node_t { + struct lily_ll_node_t *next; + const char *description; + void (*f)(); +} lily_ll_node_t; +#define LILY_FILE_BEGIN() \ +static lily_ll_node_t lily_ll_last = { NULL, NULL, NULL }; +#define LILY_LIST_HEAD lily_ll_last +#define LILY_LIST_NEXT lily_ll_last +#define LILY_PUSH_TEST() LILY_TEST_H_LOCATION +#define LILY_REGISTER_TESTS() LILY_TEST_H_LOCATION + +#define LILY_CAT_(x, y) x##y +#define LILY_CAT(x, y) LILY_CAT_(x, y) +#define LILY_ANON(x) LILY_CAT(x, LILY_COUNTER) +#define LILY_ANON_FUNC LILY_ANON(LILY_FUNCTION_) +#define LILY_ANON_NODE LILY_ANON(LILY_NODE_) +#define LILY_ANON_DESC LILY_ANON(LILY_DESCRIPTION_) + +#define LILY_TEST_(f, desc_str, description) \ +static const char * const desc_str = description; \ +static void f() + +#define LILY_TEST(description) LILY_TEST_(LILY_ANON_FUNC, LILY_ANON_DESC, description) + /* ifdef LILY_TEST_H */ #else /* counter incrementing */ /* this counter can count up to 4096, but it should be pretty trivial to extend it more or less arbitrarily far */ +/* to increment the counter, just #define LILY_COUNTER_INCREMENT and then include this file again */ #ifdef LILY_COUNTER_INCREMENT +#undef LILY_COUNTER_INCREMENT + /* ones position */ # if LILY_HEX(LILY_COUNTER_POS_0) == 0 # undef LILY_COUNTER_POS_0 # define LILY_COUNTER_POS_0 1 @@ -129,6 +164,7 @@ # elif LILY_HEX(LILY_COUNTER_POS_0) == 15 # undef LILY_COUNTER_POS_0 # define LILY_COUNTER_POS_0 0 + /* sixteens position */ # if LILY_HEX(LILY_COUNTER_POS_1) == 0 # undef LILY_COUNTER_POS_1 # define LILY_COUNTER_POS_1 1 @@ -177,6 +213,7 @@ # elif LILY_HEX(LILY_COUNTER_POS_1) == 15 # undef LILY_COUNTER_POS_1 # define LILY_COUNTER_POS_1 0 + /* 256s position */ # if LILY_HEX(LILY_COUNTER_POS_2) == 0 # undef LILY_COUNTER_POS_2 # define LILY_COUNTER_POS_2 1 @@ -228,6 +265,186 @@ # endif # endif # endif + +#elif defined(LILY_COUNTER_DECREMENT) +#undef LILY_COUNTER_DECREMENT + /* ones position */ +# if LILY_HEX(LILY_COUNTER_POS_0) == 15 +# undef LILY_COUNTER_POS_0 +# define LILY_COUNTER_POS_0 e +# elif LILY_HEX(LILY_COUNTER_POS_0) == 14 +# undef LILY_COUNTER_POS_0 +# define LILY_COUNTER_POS_0 d +# elif LILY_HEX(LILY_COUNTER_POS_0) == 13 +# undef LILY_COUNTER_POS_0 +# define LILY_COUNTER_POS_0 c +# elif LILY_HEX(LILY_COUNTER_POS_0) == 12 +# undef LILY_COUNTER_POS_0 +# define LILY_COUNTER_POS_0 b +# elif LILY_HEX(LILY_COUNTER_POS_0) == 11 +# undef LILY_COUNTER_POS_0 +# define LILY_COUNTER_POS_0 a +# elif LILY_HEX(LILY_COUNTER_POS_0) == 10 +# undef LILY_COUNTER_POS_0 +# define LILY_COUNTER_POS_0 9 +# elif LILY_HEX(LILY_COUNTER_POS_0) == 9 +# undef LILY_COUNTER_POS_0 +# define LILY_COUNTER_POS_0 8 +# elif LILY_HEX(LILY_COUNTER_POS_0) == 8 +# undef LILY_COUNTER_POS_0 +# define LILY_COUNTER_POS_0 7 +# elif LILY_HEX(LILY_COUNTER_POS_0) == 7 +# undef LILY_COUNTER_POS_0 +# define LILY_COUNTER_POS_0 6 +# elif LILY_HEX(LILY_COUNTER_POS_0) == 6 +# undef LILY_COUNTER_POS_0 +# define LILY_COUNTER_POS_0 5 +# elif LILY_HEX(LILY_COUNTER_POS_0) == 5 +# undef LILY_COUNTER_POS_0 +# define LILY_COUNTER_POS_0 4 +# elif LILY_HEX(LILY_COUNTER_POS_0) == 4 +# undef LILY_COUNTER_POS_0 +# define LILY_COUNTER_POS_0 3 +# elif LILY_HEX(LILY_COUNTER_POS_0) == 3 +# undef LILY_COUNTER_POS_0 +# define LILY_COUNTER_POS_0 2 +# elif LILY_HEX(LILY_COUNTER_POS_0) == 2 +# undef LILY_COUNTER_POS_0 +# define LILY_COUNTER_POS_0 1 +# elif LILY_HEX(LILY_COUNTER_POS_0) == 1 +# undef LILY_COUNTER_POS_0 +# define LILY_COUNTER_POS_0 0 +# elif LILY_HEX(LILY_COUNTER_POS_0) == 0 +# undef LILY_COUNTER_POS_0 +# define LILY_COUNTER_POS_0 f + /* sixteens position */ +# if LILY_HEX(LILY_COUNTER_POS_1) == 15 +# undef LILY_COUNTER_POS_1 +# define LILY_COUNTER_POS_1 e +# elif LILY_HEX(LILY_COUNTER_POS_1) == 14 +# undef LILY_COUNTER_POS_1 +# define LILY_COUNTER_POS_1 d +# elif LILY_HEX(LILY_COUNTER_POS_1) == 13 +# undef LILY_COUNTER_POS_1 +# define LILY_COUNTER_POS_1 c +# elif LILY_HEX(LILY_COUNTER_POS_1) == 12 +# undef LILY_COUNTER_POS_1 +# define LILY_COUNTER_POS_1 b +# elif LILY_HEX(LILY_COUNTER_POS_1) == 11 +# undef LILY_COUNTER_POS_1 +# define LILY_COUNTER_POS_1 a +# elif LILY_HEX(LILY_COUNTER_POS_1) == 10 +# undef LILY_COUNTER_POS_1 +# define LILY_COUNTER_POS_1 9 +# elif LILY_HEX(LILY_COUNTER_POS_1) == 9 +# undef LILY_COUNTER_POS_1 +# define LILY_COUNTER_POS_1 8 +# elif LILY_HEX(LILY_COUNTER_POS_1) == 8 +# undef LILY_COUNTER_POS_1 +# define LILY_COUNTER_POS_1 7 +# elif LILY_HEX(LILY_COUNTER_POS_1) == 7 +# undef LILY_COUNTER_POS_1 +# define LILY_COUNTER_POS_1 6 +# elif LILY_HEX(LILY_COUNTER_POS_1) == 6 +# undef LILY_COUNTER_POS_1 +# define LILY_COUNTER_POS_1 5 +# elif LILY_HEX(LILY_COUNTER_POS_1) == 5 +# undef LILY_COUNTER_POS_1 +# define LILY_COUNTER_POS_1 4 +# elif LILY_HEX(LILY_COUNTER_POS_1) == 4 +# undef LILY_COUNTER_POS_1 +# define LILY_COUNTER_POS_1 3 +# elif LILY_HEX(LILY_COUNTER_POS_1) == 3 +# undef LILY_COUNTER_POS_1 +# define LILY_COUNTER_POS_1 2 +# elif LILY_HEX(LILY_COUNTER_POS_1) == 2 +# undef LILY_COUNTER_POS_1 +# define LILY_COUNTER_POS_1 1 +# elif LILY_HEX(LILY_COUNTER_POS_1) == 1 +# undef LILY_COUNTER_POS_1 +# define LILY_COUNTER_POS_1 0 +# elif LILY_HEX(LILY_COUNTER_POS_1) == 0 +# undef LILY_COUNTER_POS_1 +# define LILY_COUNTER_POS_1 f + /* 256s position */ +# if LILY_HEX(LILY_COUNTER_POS_2) == 15 +# undef LILY_COUNTER_POS_2 +# define LILY_COUNTER_POS_2 e +# elif LILY_HEX(LILY_COUNTER_POS_2) == 14 +# undef LILY_COUNTER_POS_2 +# define LILY_COUNTER_POS_2 d +# elif LILY_HEX(LILY_COUNTER_POS_2) == 13 +# undef LILY_COUNTER_POS_2 +# define LILY_COUNTER_POS_2 c +# elif LILY_HEX(LILY_COUNTER_POS_2) == 12 +# undef LILY_COUNTER_POS_2 +# define LILY_COUNTER_POS_2 b +# elif LILY_HEX(LILY_COUNTER_POS_2) == 11 +# undef LILY_COUNTER_POS_2 +# define LILY_COUNTER_POS_2 a +# elif LILY_HEX(LILY_COUNTER_POS_2) == 10 +# undef LILY_COUNTER_POS_2 +# define LILY_COUNTER_POS_2 9 +# elif LILY_HEX(LILY_COUNTER_POS_2) == 9 +# undef LILY_COUNTER_POS_2 +# define LILY_COUNTER_POS_2 8 +# elif LILY_HEX(LILY_COUNTER_POS_2) == 8 +# undef LILY_COUNTER_POS_2 +# define LILY_COUNTER_POS_2 7 +# elif LILY_HEX(LILY_COUNTER_POS_2) == 7 +# undef LILY_COUNTER_POS_2 +# define LILY_COUNTER_POS_2 6 +# elif LILY_HEX(LILY_COUNTER_POS_2) == 6 +# undef LILY_COUNTER_POS_2 +# define LILY_COUNTER_POS_2 5 +# elif LILY_HEX(LILY_COUNTER_POS_2) == 5 +# undef LILY_COUNTER_POS_2 +# define LILY_COUNTER_POS_2 4 +# elif LILY_HEX(LILY_COUNTER_POS_2) == 4 +# undef LILY_COUNTER_POS_2 +# define LILY_COUNTER_POS_2 3 +# elif LILY_HEX(LILY_COUNTER_POS_2) == 3 +# undef LILY_COUNTER_POS_2 +# define LILY_COUNTER_POS_2 2 +# elif LILY_HEX(LILY_COUNTER_POS_2) == 2 +# undef LILY_COUNTER_POS_2 +# define LILY_COUNTER_POS_2 1 +# elif LILY_HEX(LILY_COUNTER_POS_2) == 1 +# undef LILY_COUNTER_POS_2 +# define LILY_COUNTER_POS_2 0 +# elif LILY_HEX(LILY_COUNTER_POS_2) == 0 +# undef LILY_COUNTER_POS_2 +# define LILY_COUNTER_POS_2 f +# endif +# endif +# endif + + +#elif defined(LILY_FILE_END) +/* ending a file */ + +/* LILY_LIST_HEAD is one too high, so we decrement */ +#define LILY_COUNTER_DECREMENT +#include LILY_TEST_H_LOCATION + + +#else +/* register new test */ + +static lily_ll_node_t LILY_ANON_NODE = { +#define LILY_COUNTER_DECREMENT +#include LILY_TEST_H_LOCATION + &LILY_LIST_HEAD, +#define LILY_COUNTER_INCREMENT +#include LILY_TEST_H_LOCATION + LILY_ANON_DESC, + LILY_ANON_FUNC, +}; +#undef LILY_LIST_HEAD +#define LILY_LIST_HEAD LILY_ANON_NODE +#define LILY_COUNTER_INCREMENT +#include LILY_TEST_H_LOCATION + #endif #endif diff --git a/tests/helpers.c b/tests/helpers.c index 37afcae..b20483a 100644 --- a/tests/helpers.c +++ b/tests/helpers.c @@ -1,6 +1,5 @@ #include <stdio.h> -#include "lily-test.h" #include "tests.h" void _run_test(const char *name, const char* (*test)()) diff --git a/tests/macro-tests.c b/tests/macro-tests.c index b5f0ac5..81a2932 100644 --- a/tests/macro-tests.c +++ b/tests/macro-tests.c @@ -160,3 +160,199 @@ const char * test_LILY_COUNTER() return 0; } + + +const char * test_LILY_COUNTER_DECREMENT() +{ + if (LILY_COUNTER != 20) + return "LILY_COUNTER is not 20!"; + + #define LILY_COUNTER_DECREMENT + #include "lily-test.h" + + if (LILY_COUNTER != 19) + return "LILY_COUNTER is not 19!"; + + #define LILY_COUNTER_DECREMENT + #include "lily-test.h" + + if (LILY_COUNTER != 18) + return "LILY_COUNTER is not 18!"; + + #define LILY_COUNTER_DECREMENT + #include "lily-test.h" + + if (LILY_COUNTER != 17) + return "LILY_COUNTER is not 17!"; + + #define LILY_COUNTER_DECREMENT + #include "lily-test.h" + + if (LILY_COUNTER != 16) + return "LILY_COUNTER is not 16!"; + + #define LILY_COUNTER_DECREMENT + #include "lily-test.h" + + if (LILY_COUNTER != 15) + return "LILY_COUNTER is not 15!"; + + #define LILY_COUNTER_DECREMENT + #include "lily-test.h" + + if (LILY_COUNTER != 14) + return "LILY_COUNTER is not 14!"; + + #define LILY_COUNTER_DECREMENT + #include "lily-test.h" + + if (LILY_COUNTER != 13) + return "LILY_COUNTER is not 13!"; + + #define LILY_COUNTER_DECREMENT + #include "lily-test.h" + + if (LILY_COUNTER != 12) + return "LILY_COUNTER is not 12!"; + + #define LILY_COUNTER_DECREMENT + #include "lily-test.h" + + if (LILY_COUNTER != 11) + return "LILY_COUNTER is not 11!"; + + #define LILY_COUNTER_DECREMENT + #include "lily-test.h" + + if (LILY_COUNTER != 10) + return "LILY_COUNTER is not 10!"; + + #define LILY_COUNTER_DECREMENT + #include "lily-test.h" + + if (LILY_COUNTER != 9) + return "LILY_COUNTER is not 9!"; + + #define LILY_COUNTER_DECREMENT + #include "lily-test.h" + + if (LILY_COUNTER != 8) + return "LILY_COUNTER is not 8!"; + + #define LILY_COUNTER_DECREMENT + #include "lily-test.h" + + if (LILY_COUNTER != 7) + return "LILY_COUNTER is not 7!"; + + #define LILY_COUNTER_DECREMENT + #include "lily-test.h" + + if (LILY_COUNTER != 6) + return "LILY_COUNTER is not 6!"; + + #define LILY_COUNTER_DECREMENT + #include "lily-test.h" + + if (LILY_COUNTER != 5) + return "LILY_COUNTER is not 5!"; + + #define LILY_COUNTER_DECREMENT + #include "lily-test.h" + + if (LILY_COUNTER != 4) + return "LILY_COUNTER is not 4!"; + + #define LILY_COUNTER_DECREMENT + #include "lily-test.h" + + if (LILY_COUNTER != 3) + return "LILY_COUNTER is not 3!"; + + #define LILY_COUNTER_DECREMENT + #include "lily-test.h" + + if (LILY_COUNTER != 2) + return "LILY_COUNTER is not 2!"; + + #define LILY_COUNTER_DECREMENT + #include "lily-test.h" + + if (LILY_COUNTER != 1) + return "LILY_COUNTER is not 1!"; + + #define LILY_COUNTER_DECREMENT + #include "lily-test.h" + + if (LILY_COUNTER != 0) + return "LILY_COUNTER is not 0!"; + + #define LILY_COUNTER_DECREMENT + #include "lily-test.h" + + if (LILY_COUNTER != 0xfff) + return "LILY_COUNTER is not 0xfff!"; + + #define LILY_COUNTER_INCREMENT + #include "lily-test.h" + + if (LILY_COUNTER != 0) + return "LILY_COUNTER is not 0 again!"; + + return 0; +} + + +LILY_FILE_BEGIN() + + +int array[3] = { 0, 0, 0 }; + +LILY_TEST("set array[0] to 1") +{ + array[0] = 1; +} +#include LILY_PUSH_TEST() + + +LILY_TEST("set array[1] to 2") +{ + array[1] = 2; +} +#include LILY_PUSH_TEST() + + +LILY_TEST("set array[2] to 3") +{ + array[2] = 3; +} +#include LILY_PUSH_TEST() + + +#define LILY_FILE_END +#include LILY_REGISTER_TESTS() + +const char * test_auto_registration() +{ + lily_ll_node_t *n = &LILY_LIST_HEAD; + if (n->next == NULL) { + return "No functions were registered!"; + } + if (n->next == n) { + return "List loops!"; + } + + while (n->next != NULL) { + n->f(); + n = n->next; + } + + if (array[0] != 1) + return "Function 0 didn't run!"; + if (array[1] != 2) + return "Function 1 didn't run!"; + if (array[2] != 3) + return "Function 2 didn't run!"; + + return 0; +} diff --git a/tests/main.c b/tests/main.c index 49c6aa8..1598190 100644 --- a/tests/main.c +++ b/tests/main.c @@ -3,7 +3,6 @@ #include <string.h> #include <stdlib.h> -#include "lily-test.h" #include "tests.h" int main() diff --git a/tests/tests.h b/tests/tests.h index 86c9b88..481260e 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -10,6 +10,8 @@ int validate_message(const char* received, const char* expected, #define TESTS \ X(test_LILY_LOCATION) \ X(test_LILY_COUNTER) \ + X(test_LILY_COUNTER_DECREMENT) \ + X(test_auto_registration) \ #define X(test) const char * test(); TESTS |