summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorsanine-a <sanine.not@pm.me>2022-12-14 13:36:56 -0600
committersanine-a <sanine.not@pm.me>2022-12-14 13:36:56 -0600
commitebf24e6f40f1c6cd322ee16fd1ad004b845bbc5c (patch)
tree33cea54b9d0123bf1d7f1d59d2c9dc75a1e6e350 /tests
parent64477ce06280b8882730459d627ae7028b15bbeb (diff)
begin refactor
Diffstat (limited to 'tests')
-rw-r--r--tests/assertions.c307
-rw-r--r--tests/macro-tests.c12
-rw-r--r--tests/main.c21
-rw-r--r--tests/mock_queue.c120
-rw-r--r--tests/tests.h19
5 files changed, 21 insertions, 458 deletions
diff --git a/tests/assertions.c b/tests/assertions.c
deleted file mode 100644
index 0e14a2b..0000000
--- a/tests/assertions.c
+++ /dev/null
@@ -1,307 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "lily-test.h"
-#include "tests.h"
-
-const char *test_LILY_LOCATION()
-{
- // if you move this line, you MUST update the expected string!
- const char *location = LILY_LOCATION;
- int diff = strcmp(location, "tests/assertions.c:11");
- if (diff != 0)
- return "LILY_LOCATION did not resolve correctly!";
-
- return 0;
-}
-
-
-/* overarching assert function */
-const char *test_assert_msg()
-{
- int val = setjmp(_lily_globals.env);
- if (val != 0)
- return "true assertion failed incorrectly!";
-
- lily_assert_msg(true, LILY_LOCATION, "should not fail!");
-
- int passed_thru = 0;
- _lily_globals.error_msg = NULL;
- _lily_globals.error_location = "";
- val = setjmp(_lily_globals.env);
-
- if (passed_thru == 0) {
- passed_thru = 1;
- // another line that you SHOULD NOT MOVE!
- lily_assert_msg(false, LILY_LOCATION, "%s %s!", "should", "fail");
- return "false assertion incorrectly succeeded!";
- }
- else {
- if (strcmp(_lily_globals.error_msg, "should fail!") != 0)
- return "false assertion produced incorrect error message!";
- if (strcmp(_lily_globals.error_location, "tests/assertions.c:37"))
- return "false assertion produced incorrect error location!";
- }
-
- free(_lily_globals.error_msg);
- return 0;
-}
-
-
-/* basic asserts */
-const char *test_assert_true()
-{
- int val = setjmp(_lily_globals.env);
- if (val != 0)
- return "true assertion failed incorrectly!";
-
- lily_assert_true(true);
-
- int passed_thru = 0;
- _lily_globals.error_msg = NULL;
- _lily_globals.error_location = "";
- val = setjmp(_lily_globals.env);
-
- if (passed_thru == 0) {
- passed_thru = 1;
- lily_assert_true(false);
- return "false assertion incorrectly succeeded!";
- }
- else {
- if (strcmp(_lily_globals.error_msg, "false is not true") != 0)
- return "false assertion produced incorrect error message!";
- }
-
- free(_lily_globals.error_msg);
- return 0;
-}
-
-
-const char *test_assert_false()
-{
- int val = setjmp(_lily_globals.env);
- if (val != 0)
- return "true assertion failed incorrectly!";
-
- lily_assert_false(false);
-
- int passed_thru = 0;
- _lily_globals.error_msg = NULL;
- _lily_globals.error_location = "";
- val = setjmp(_lily_globals.env);
-
- if (passed_thru == 0) {
- passed_thru = 1;
- lily_assert_false(true);
- return "false assertion incorrectly succeeded!";
- }
- else {
- if (strcmp(_lily_globals.error_msg, "true is not false") != 0)
- return "false assertion produced incorrect error message!";
- }
-
- free(_lily_globals.error_msg);
- return 0;
-}
-
-
-/* pointer assertions */
-const char *test_assert_not_null()
-{
- int a = 5;
-
- int val = setjmp(_lily_globals.env);
- if (val != 0)
- return "true assertion failed incorrectly!";
-
- lily_assert_not_null(&a);
-
- int passed_thru = 0;
- _lily_globals.error_msg = NULL;
- _lily_globals.error_location = "";
- val = setjmp(_lily_globals.env);
-
- int *ptr = NULL;
-
- if (passed_thru == 0) {
- passed_thru = 1;
- lily_assert_not_null(ptr);
- return "false assertion incorrectly succeeded!";
- }
- else {
- if (strcmp(_lily_globals.error_msg, "ptr is NULL") != 0)
- return "false assertion produced incorrect error message!";
- }
-
- free(_lily_globals.error_msg);
- return 0;
-}
-
-
-const char *test_assert_null()
-{
- int a = 5;
- int *ptr = NULL;
-
- int val = setjmp(_lily_globals.env);
- if (val != 0)
- return "true assertion failed incorrectly!";
-
- lily_assert_null(ptr);
-
- int passed_thru = 0;
- _lily_globals.error_msg = NULL;
- _lily_globals.error_location = "";
- val = setjmp(_lily_globals.env);
-
- if (passed_thru == 0) {
- passed_thru = 1;
- lily_assert_null(&a);
- return "false assertion incorrectly succeeded!";
- }
- else {
- char buf[256];
- sprintf(buf, "&a (%p) is not NULL", &a);
- if (strcmp(_lily_globals.error_msg, buf) != 0)
- return "false assertion produced incorrect error message!";
- }
-
- free(_lily_globals.error_msg);
- return 0;
-}
-
-
-const char *test_assert_ptr_equal()
-{
- int a = 0;
- int b = 0;
- int *ptr = &a;
-
- int val = setjmp(_lily_globals.env);
- if (val != 0)
- return "true assertion failed incorrectly!";
-
- lily_assert_ptr_equal(&a, ptr);
-
- int passed_thru = 0;
- _lily_globals.error_msg = NULL;
- _lily_globals.error_location = "";
- val = setjmp(_lily_globals.env);
- ptr = &b;
-
- if (passed_thru == 0) {
- passed_thru = 1;
- lily_assert_ptr_equal(&a, ptr);
- return "false assertion incorrectly succeeded!";
- }
- else {
- char buf[256];
- sprintf(buf, "&a (%p) is not equal to ptr (%p)", &a, &b);
- if (strcmp(_lily_globals.error_msg, buf) != 0)
- return "false assertion produced incorrect error message!";
- }
-
- free(_lily_globals.error_msg);
- return 0;
-}
-
-
-const char *test_assert_ptr_not_equal()
-{
- int a = 0;
- int b = 0;
- int *ptr = &b;
-
- int val = setjmp(_lily_globals.env);
- if (val != 0)
- return "true assertion failed incorrectly!";
-
- lily_assert_ptr_not_equal(&a, ptr);
-
- int passed_thru = 0;
- _lily_globals.error_msg = NULL;
- _lily_globals.error_location = "";
- val = setjmp(_lily_globals.env);
- ptr = &a;
-
- if (passed_thru == 0) {
- passed_thru = 1;
- lily_assert_ptr_not_equal(&a, ptr);
- return "false assertion incorrectly succeeded!";
- }
- else {
- char buf[256];
- sprintf(buf, "&a (%p) is equal to ptr", &a);
- if (strcmp(_lily_globals.error_msg, buf) != 0)
- return "false assertion produced incorrect error message!";
- }
-
- free(_lily_globals.error_msg);
- return 0;
-}
-
-
-/* integer assertions */
-const char *test_assert_int_equal()
-{
- int a = 56;
- int b = 56;
-
- int val = setjmp(_lily_globals.env);
- if (val != 0)
- return "true assertion failed incorrectly!";
-
- lily_assert_int_equal(a, b);
-
- int passed_thru = 0;
- _lily_globals.error_msg = NULL;
- _lily_globals.error_location = "";
- val = setjmp(_lily_globals.env);
- a = 25;
-
- if (passed_thru == 0) {
- passed_thru = 1;
- lily_assert_int_equal(a, b);
- return "false assertion succeeded incorrectly!";
- }
- else {
- if (strcmp(_lily_globals.error_msg, "a (25) is not equal to b (56)") != 0)
- return "false assertion produced incorrect error message!";
- }
-
- free(_lily_globals.error_msg);
- return 0;
-}
-
-
-const char *test_assert_int_not_equal()
-{
- int a = 25;
- int b = 26;
-
- int val = setjmp(_lily_globals.env);
- if (val != 0)
- return "true assertion failed incorrectly!";
-
- lily_assert_int_not_equal(a, b);
-
- int passed_thru = 0;
- _lily_globals.error_msg = NULL;
- _lily_globals.error_location = "";
- val = setjmp(_lily_globals.env);
- b = 25;
-
- if (passed_thru == 0) {
- passed_thru = 1;
- lily_assert_int_not_equal(a, b);
- return "false assertion incorrectly succeeded!";
- }
- else {
- if (strcmp(_lily_globals.error_msg, "a (25) is equal to b") != 0)
- return "false assertion produced incorrect error message!";
- }
-
- free(_lily_globals.error_msg);
- return 0;
-}
diff --git a/tests/macro-tests.c b/tests/macro-tests.c
new file mode 100644
index 0000000..71e9189
--- /dev/null
+++ b/tests/macro-tests.c
@@ -0,0 +1,12 @@
+#include "lily-test.h"
+#include "tests.h"
+
+
+const char * test_LILY_LOCATION()
+{
+ /* do NOT move the following line, or this test will break! */
+ if (strcmp(LILY_LOCATION, "tests/macro-tests.c:8") != 0) {
+ return "LILY_LOCATION refers to the wrong location!";
+ }
+ return 0;
+}
diff --git a/tests/main.c b/tests/main.c
index f3f1f06..49c6aa8 100644
--- a/tests/main.c
+++ b/tests/main.c
@@ -8,21 +8,8 @@
int main()
{
- run_test(test_LILY_LOCATION);
- run_test(test_assert_msg);
- run_test(test_assert_true);
- run_test(test_assert_false);
- run_test(test_assert_not_null);
- run_test(test_assert_null);
- run_test(test_assert_ptr_equal);
- run_test(test_assert_ptr_not_equal);
- run_test(test_assert_int_equal);
- run_test(test_assert_int_not_equal);
-
- run_test(test_mock_enqueue_dequeue_int);
- run_test(test_mock_enqueue_dequeue_heterogenous);
- run_test(test_LILY_NARGS);
- run_test(test_lily_mock_call);
-
- return 0;
+ #define X(test) run_test(test);
+ TESTS
+ #undef X
+ return 0;
}
diff --git a/tests/mock_queue.c b/tests/mock_queue.c
deleted file mode 100644
index 493d397..0000000
--- a/tests/mock_queue.c
+++ /dev/null
@@ -1,120 +0,0 @@
-#include <stdio.h>
-#include <string.h>
-
-#include "lily-test.h"
-#include "tests.h"
-
-
-const char* test_mock_enqueue_dequeue_int() {
- lily_queue_t *q = lily_queue_create();
-
- /* enqueue A259482, for no particular reason */
- lily_enqueue(q, int, 2);
- lily_enqueue(q, int, 6);
- lily_enqueue(q, int, 44);
- int n = 2014;
- lily_enqueue(q, int, n);
-
- lily_dequeue(q, int, &n);
- if (n != 2) return "dequeued incorrect first value";
- lily_dequeue(q, int, &n);
-
- /* queue next value after already popping */
- lily_enqueue(q, long, 1651377);
-
- if (n != 6) return "dequeued incorrect second value";
- lily_dequeue(q, int, &n);
- if (n != 44) return "dequeued incorrect third value";
- lily_dequeue(q, int, &n);
- if (n != 2014) return "dequeued incorrect fourth value";
- long m;
- lily_dequeue(q, long, &m);
- if (m != 1651377) return "dequeued incorrect fifth value";
-
- lily_queue_destroy(q);
-
- return 0;
-}
-
-
-const char* test_mock_enqueue_dequeue_heterogenous()
-{
- lily_queue_t *q = lily_queue_create();
-
- lily_enqueue(q, int, 2);
- lily_enqueue(q, lily_queue_t *, q);
- lily_enqueue(q, const char *, "hello, world!");
-
- int n;
- lily_dequeue(q, int, &n);
- if (n != 2) return "dequeued incorrect int value";
- lily_queue_t *p;
- lily_dequeue(q, lily_queue_t *, &p);
- if (p != q) return "dequeued incorrect pointer value";
- const char *str;
- lily_dequeue(q, const char *, &str);
- if (strcmp(str, "hello, world!") != 0) return "dequeued incorrect string value";
-
- lily_queue_destroy(q);
-
- return 0;
-}
-
-
-const char* test_LILY_NARGS()
-{
- int n = 5;
- const char *str = "hello, world!";
- lily_queue_t *q;
-
- struct lily_mock_arg_t args[] =
- { { sizeof(int), &n },
- { sizeof(const char *), &str },
- { sizeof(lily_queue_t *), &q },
- };
- if (LILY_NARGS(args) != 3) return "incorrect argument count!";
-
- return 0;
-}
-
-
-const char* test_lily_mock_call()
-{
- lily_mock_t *m = lily_mock_create();
-
- int n = 5;
- const char *str = "hello, world!";
- lily_queue_t *q;
-
- struct lily_mock_arg_t args[] =
- { { sizeof(int), &n },
- { sizeof(const char *), &str },
- { sizeof(lily_queue_t *), &q },
- };
-
- lily_mock_call(m, args);
- n = 16;
- str = "hi there";
- lily_mock_call(m, args);
-
- if (m->n_calls != 2) return "incorrect number of calls registered";
-
- int k; const char *s; lily_queue_t *p;
- struct lily_mock_arg_t get_args[] =
- { { sizeof(int), &k },
- { sizeof(const char *), &s },
- { sizeof(lily_queue_t *), &p },
- };
- lily_get_call(m, get_args, 0);
- if (k != 5) return "incorrect int argument 0 registered";
- if (strcmp(s, "hello, world!") != 0) return "incorrect string argument 0 registered";
- if (p != q) return "incorrect pointer argument 0 registered";
-
- lily_get_call(m, get_args, 1);
- if (k != 16) return "incorrect int argument 1 registered";
- if (strcmp(s, "hi there") != 0) return "incorrect string argument 1 registered";
- if (p != q) return "incorrect pointer argument 1 registered";
-
- lily_mock_destroy(m);
- return 0;
-}
diff --git a/tests/tests.h b/tests/tests.h
index 9584cac..f1ea9c1 100644
--- a/tests/tests.h
+++ b/tests/tests.h
@@ -7,20 +7,11 @@ int validate_message(const char* received, const char* expected,
const char* FILE, unsigned int LINE);
// test cases
-const char* test_LILY_LOCATION();
-const char* test_assert_msg();
-const char* test_assert_true();
-const char* test_assert_false();
-const char* test_assert_not_null();
-const char* test_assert_null();
-const char* test_assert_ptr_equal();
-const char* test_assert_ptr_not_equal();
-const char* test_assert_int_equal();
-const char* test_assert_int_not_equal();
+#define TESTS \
+ X(test_LILY_LOCATION) \
-const char* test_mock_enqueue_dequeue_int();
-const char* test_mock_enqueue_dequeue_heterogenous();
-const char* test_LILY_NARGS();
-const char* test_lily_mock_call();
+#define X(test) const char * test();
+TESTS
+#undef X
#endif