summaryrefslogtreecommitdiff
path: root/lily-test.h
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 /lily-test.h
parentbf71f069d4861ba2285c96dd98bdf1bbe0322fc2 (diff)
modify error message format and add CHECK_EQF
Diffstat (limited to 'lily-test.h')
-rw-r--r--lily-test.h25
1 files changed, 22 insertions, 3 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 */