aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorGravatar Tim Segers <tsegers@pm.me>2023-01-02 21:54:31 +0100
committerGravatar Tim Segers <tsegers@pm.me>2023-01-07 15:24:51 +0100
commit650812777953ecc1ddb1280c86b63fa4b5761ed6 (patch)
tree4715b5f67e42dc14c59a25d5d90a98c2c2ecb241
parent2fcfc12ea05a8707910376f2e025506db0d89164 (diff)
downloadopendeco-650812777953ecc1ddb1280c86b63fa4b5761ed6.tar.gz
Add macro to compare floats with user-defined max error
-rw-r--r--minunit/minunit.h16
-rw-r--r--test/deco_test.c8
2 files changed, 21 insertions, 3 deletions
diff --git a/minunit/minunit.h b/minunit/minunit.h
index 608fddf..a20b8b8 100644
--- a/minunit/minunit.h
+++ b/minunit/minunit.h
@@ -201,6 +201,22 @@ extern void (*minunit_teardown)(void);
}\
)
+#define mu_assert_double_near(expected, result, maxabserr) MU__SAFE_BLOCK(\
+ double minunit_tmp_e;\
+ double minunit_tmp_r;\
+ minunit_assert++;\
+ minunit_tmp_e = (expected);\
+ minunit_tmp_r = (result);\
+ if (fabs(minunit_tmp_e-minunit_tmp_r) > maxabserr) {\
+ int minunit_significant_figures = 1 - log10(maxabserr);\
+ snprintf(minunit_last_message, MINUNIT_MESSAGE_LEN, "%s failed:\n\t%s:%d: %.*g expected but was %.*g", __func__, __FILE__, __LINE__, minunit_significant_figures, minunit_tmp_e, minunit_significant_figures, minunit_tmp_r);\
+ minunit_status = 1;\
+ return;\
+ } else {\
+ printf(".");\
+ }\
+)
+
#define mu_assert_string_eq(expected, result) MU__SAFE_BLOCK(\
const char* minunit_tmp_e = expected;\
const char* minunit_tmp_r = result;\
diff --git a/test/deco_test.c b/test/deco_test.c
index 1f9ba9f..f87cffc 100644
--- a/test/deco_test.c
+++ b/test/deco_test.c
@@ -19,6 +19,8 @@ MU_TEST(test_abs_gauge)
MU_TEST(test_gas)
{
+ double max_mod_err = 1E-3;
+
gas_t foo = gas_new(21, 35, MOD_AUTO);
gas_t bar = gas_new(21, 0, MOD_AUTO);
gas_t baz = gas_new(21, 35, MOD_AUTO);
@@ -33,9 +35,9 @@ MU_TEST(test_gas)
mu_check(gas_equal(&foo, &baz));
mu_check(!gas_equal(&foo, &qux));
- mu_assert_double_eq(abs_depth(msw_to_bar(51.6)), gas_mod(&foo));
- mu_assert_double_eq(abs_depth(msw_to_bar(30)), gas_mod(&bar));
- mu_assert_double_eq(99, gas_mod(&qux));
+ mu_assert_double_near(abs_depth(msw_to_bar(51.6)), gas_mod(&foo), max_mod_err);
+ mu_assert_double_near(abs_depth(msw_to_bar(30)), gas_mod(&bar), max_mod_err);
+ mu_assert_double_near(99, gas_mod(&qux), max_mod_err);
}
void testsuite_deco_setup(void)