summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2022-12-31 13:52:55 -0600
committersanine <sanine.not@pm.me>2022-12-31 13:52:55 -0600
commit8f2536a0d2e653cb393eb81292ce4d029129d404 (patch)
tree4dad74c5cb0f1af550fe723eb1c133ec4270d936
parentbf71f069d4861ba2285c96dd98bdf1bbe0322fc2 (diff)
modify error message format and add CHECK_EQF
-rw-r--r--lily-test.h25
-rw-r--r--tests/assertions.c31
-rw-r--r--tests/tests.h1
3 files changed, 52 insertions, 5 deletions
diff --git a/lily-test.h b/lily-test.h
index e6925aa..c26237a 100644
--- a/lily-test.h
+++ b/lily-test.h
@@ -11,7 +11,8 @@
* Permission is hereby granted, free of charge, to any person or
* organization (the "User") obtaining a copy of this software and associated
* documentation files (the "Software"), to use, copy, modify, merge,
- * distribute, and/or sell copies of the Software, subject to the following conditions:
+ * distribute, and/or sell copies of the Software, subject to the following
+ * conditions:
*
* 1. The above copyright notice and this permission notice shall be included
* in all copies or modified versions of the Software.
@@ -56,6 +57,7 @@
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
+#include <math.h>
#define STR_IMP(x) #x
#define STR(x) STR_IMP(x)
@@ -139,7 +141,9 @@ void lily_check(int x, const char *location, const char *fmt, ...);
#define CHECK(x) LILY_CHECK(x)
#define LILY_CHECK_CMP_(x, y, xstr, ystr, cmp, cmpstr, fmt) \
- lily_check(x cmp y, LILY_LOCATION, "CHECK failed: %s " cmpstr " %s (" fmt " " cmpstr " " fmt ")", xstr, ystr, x, y)
+ lily_check(x cmp y, LILY_LOCATION, \
+ "CHECK failed: %s " cmpstr " %s\n %s = " fmt "\n %s = " fmt,\
+ xstr, ystr, xstr, x, ystr, y)
#define LILY_CHECK_CMP(x, cmp, y, fmt) LILY_CHECK_CMP_(x, y, #x, #y, cmp, #cmp, fmt)
/* check comparision assertions */
@@ -150,6 +154,12 @@ void lily_check(int x, const char *location, const char *fmt, ...);
#define CHECK_GT(x, y, fmt) LILY_CHECK_CMP(x, >, y, fmt)
#define CHECK_GE(x, y, fmt) LILY_CHECK_CMP(x, >=, y, fmt)
+#define LILY_CHECK_EQF(x, y, xstr, ystr, fmt) \
+ lily_check(fabs(x-y) < lily_g.epsilon, LILY_LOCATION, \
+ "CHECK failed: %s == %s\n %s = " fmt "\n %s = " fmt "\n epsilon: %f", \
+ xstr, ystr, xstr, x, ystr, y, lily_g.epsilon)
+#define CHECK_EQF(x, y, fmt) LILY_CHECK_EQF(x, y, #x, #y, fmt)
+
void lily_require(int x, const char *location, const char *fmt, ...);
#define LILY_REQUIRE_(str, x, location) \
@@ -158,7 +168,9 @@ void lily_require(int x, const char *location, const char *fmt, ...);
#define REQUIRE(x) LILY_REQUIRE(x)
#define LILY_REQUIRE_CMP_(x, y, xstr, ystr, cmp, cmpstr, fmt) \
- lily_require(x cmp y, LILY_LOCATION, "REQUIRE failed: %s " cmpstr " %s (" fmt " " cmpstr " " fmt ")", xstr, ystr, x, y)
+ lily_require(x cmp y, LILY_LOCATION, \
+ "REQUIRE failed: %s " cmpstr " %s\n %s = " fmt "\n %s = " fmt,\
+ xstr, ystr, xstr, x, ystr, y)
#define LILY_REQUIRE_CMP(x, cmp, y, fmt) LILY_REQUIRE_CMP_(x, y, #x, #y, cmp, #cmp, fmt)
/* require comparison assertions */
@@ -173,6 +185,7 @@ void lily_require(int x, const char *location, const char *fmt, ...);
void lily_begin();
void lily_finish();
void lily_run_test(void (*test)());
+void lily_set_epsilon(double epsilon);
struct lily_global_t {
@@ -325,6 +338,12 @@ void lily_finish()
printf("checked %d asserts (%d failed)\n",
lily_g.n_assertions, lily_g.n_assertions_failed);
}
+
+
+void lily_set_epsilon(double epsilon)
+{
+ lily_g.epsilon = epsilon;
+}
#endif
/* ifdef LILY_TEST_H */
diff --git a/tests/assertions.c b/tests/assertions.c
index 39db7bd..6d0c0e1 100644
--- a/tests/assertions.c
+++ b/tests/assertions.c
@@ -87,7 +87,34 @@ const char * test_CHECK_EQ()
if (lily_g.HEAD.next->next != NULL)
return "CHECK_EQ appended more than one message";
- if (strcmp(lily_g.HEAD.next->msg, "CHECK failed: 4 == k (04 == 05)") != 0)
+ if (strcmp(lily_g.HEAD.next->msg, "CHECK failed: 4 == k\n 4 = 04\n k = 05") != 0)
+ return "incorrect message";
+
+ lily_msg_destroy(lily_g.HEAD.next);
+ return 0;
+}
+
+
+const char * test_CHECK_EQF()
+{
+ lily_g.HEAD.next = NULL;
+ lily_g.TAIL = &(lily_g.HEAD);
+ lily_g.failed = 0;
+
+ lily_set_epsilon(0.2);
+ CHECK_EQF(0.5, 0.4, "%0.1f");
+ CHECK_EQF(0.5, 0.0, "%0.1f");
+
+ if (lily_g.HEAD.next == NULL)
+ return "CHECK_EQF did not append any failure message";
+
+ if (lily_g.HEAD.next->next != NULL)
+ return "CHECK_EQF appended more than one message";
+
+ if (strcmp(
+ lily_g.HEAD.next->msg,
+ "CHECK failed: 0.5 == 0.0\n 0.5 = 0.5\n 0.0 = 0.0\n epsilon: 0.200000"
+ ) != 0)
return "incorrect message";
lily_msg_destroy(lily_g.HEAD.next);
@@ -158,7 +185,7 @@ const char * test_REQUIRE_EQ()
if (lily_g.HEAD.next == NULL)
return "test did not generate any messages";
- if (strcmp(lily_g.HEAD.next->msg, "REQUIRE failed: 2 == 4 (2 == 4)") != 0)
+ if (strcmp(lily_g.HEAD.next->msg, "REQUIRE failed: 2 == 4\n 2 = 2\n 4 = 4") != 0)
return "test generated incorrect message";
if (lily_g.HEAD.next->next != NULL)
diff --git a/tests/tests.h b/tests/tests.h
index c450e13..631044e 100644
--- a/tests/tests.h
+++ b/tests/tests.h
@@ -15,6 +15,7 @@ int validate_message(const char* received, const char* expected,
X(test_INFO) \
X(test_CHECK) \
X(test_CHECK_EQ) \
+ X(test_CHECK_EQF) \
X(test_REQUIRE) \
X(test_REQUIRE_EQ) \