aboutsummaryrefslogtreecommitdiff
path: root/src/cmocka.c
diff options
context:
space:
mode:
authorArnaud Gelas <arnaud.gelas@sensefly.com>2019-02-04 15:52:30 +0100
committerAndreas Schneider <asn@cryptomilk.org>2019-11-19 15:38:34 +0100
commit226a89cc434649ca4a4230cccac81976dfda9592 (patch)
tree42bf9e0e620a9abafeb2e93d49e3451626884f21 /src/cmocka.c
parent1e21400a7923a2e539c7a4b078fb3e445a493c35 (diff)
downloadcmocka-226a89cc434649ca4a4230cccac81976dfda9592.tar.gz
cmocka-226a89cc434649ca4a4230cccac81976dfda9592.tar.xz
cmocka-226a89cc434649ca4a4230cccac81976dfda9592.zip
cmocka: Add new assert macros to compare 2 double given an epsilon.
assert_double_equal and assert_double_not_equal Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
Diffstat (limited to 'src/cmocka.c')
-rw-r--r--src/cmocka.c87
1 files changed, 87 insertions, 0 deletions
diff --git a/src/cmocka.c b/src/cmocka.c
index 76a9b93..6a31f77 100644
--- a/src/cmocka.c
+++ b/src/cmocka.c
@@ -1183,6 +1183,73 @@ static int float_values_not_equal_display_error(const float left,
return not_equal;
}
+/* Returns 1 if the specified double values are equal, else returns 0. */
+static int double_compare(const double left,
+ const double right,
+ const double epsilon) {
+ double absLeft;
+ double absRight;
+ double largest;
+ double relDiff;
+
+ double diff = left - right;
+ diff = (diff >= 0.f) ? diff : -diff;
+
+ /*
+ * Check if the numbers are really close -- needed
+ * when comparing numbers near zero.
+ */
+ if (diff <= epsilon) {
+ return 1;
+ }
+
+ absLeft = (left >= 0.f) ? left : -left;
+ absRight = (right >= 0.f) ? right : -right;
+
+ largest = (absRight > absLeft) ? absRight : absLeft;
+ relDiff = largest * FLT_EPSILON;
+
+ if (diff > relDiff) {
+ return 0;
+ }
+
+ return 1;
+}
+
+/*
+ * Returns 1 if the specified double values are equal. If the values are not
+ * equal an error is displayed and 0 is returned.
+ */
+static int double_values_equal_display_error(const double left,
+ const double right,
+ const double epsilon) {
+ const int equal = double_compare(left, right, epsilon);
+
+ if (!equal) {
+ cm_print_error(DoublePrintfFormat " != "
+ DoublePrintfFormat "\n", left, right);
+ }
+
+ return equal;
+}
+
+/*
+ * Returns 1 if the specified double values are different. If the values are
+ * equal an error is displayed and 0 is returned.
+ */
+static int double_values_not_equal_display_error(const double left,
+ const double right,
+ const double epsilon) {
+ const int not_equal = (double_compare(left, right, epsilon) == 0);
+
+ if (!not_equal) {
+ cm_print_error(DoublePrintfFormat " == "
+ DoublePrintfFormat "\n", left, right);
+ }
+
+ return not_equal;
+}
+
/* Returns 1 if the specified values are equal. If the values are not equal
* an error is displayed and 0 is returned. */
static int values_equal_display_error(const LargestIntegralType left,
@@ -1795,6 +1862,26 @@ void _assert_float_not_equal(const float a,
}
}
+void _assert_double_equal(const double a,
+ const double b,
+ const double epsilon,
+ const char * const file,
+ const int line) {
+ if (!double_values_equal_display_error(a, b, epsilon)) {
+ _fail(file, line);
+ }
+}
+
+void _assert_double_not_equal(const double a,
+ const double b,
+ const double epsilon,
+ const char * const file,
+ const int line) {
+ if (!double_values_not_equal_display_error(a, b, epsilon)) {
+ _fail(file, line);
+ }
+}
+
void _assert_int_equal(
const LargestIntegralType a, const LargestIntegralType b,
const char * const file, const int line) {