aboutsummaryrefslogtreecommitdiff
path: root/src/cmocka.c
diff options
context:
space:
mode:
authorArnaud Gelas <arnaud.gelas@sensefly.com>2019-01-15 17:33:14 +0100
committerAndreas Schneider <asn@cryptomilk.org>2019-01-22 13:24:26 +0100
commit10f50a294f876b6419bd1e297f2a84913736a35c (patch)
tree725fc1ef60bfa30edcba36e3abe63e93ebc64624 /src/cmocka.c
parentbd0667c6d99019843b4893393f2ab4d3e6b43468 (diff)
downloadcmocka-10f50a294f876b6419bd1e297f2a84913736a35c.tar.gz
cmocka-10f50a294f876b6419bd1e297f2a84913736a35c.tar.xz
cmocka-10f50a294f876b6419bd1e297f2a84913736a35c.zip
include: Add new assert macros to compare 2 floats given an epsilon.
assert_float_equal and assert_float_not_equal
Diffstat (limited to 'src/cmocka.c')
-rw-r--r--src/cmocka.c77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/cmocka.c b/src/cmocka.c
index 4ae65b7..ff79161 100644
--- a/src/cmocka.c
+++ b/src/cmocka.c
@@ -43,6 +43,7 @@
#include <stdlib.h>
#include <string.h>
#include <time.h>
+#include <float.h>
/*
* This allows to add a platform specific header file. Some embedded platforms
@@ -1122,6 +1123,62 @@ void _expect_function_call(
list_add_value(&global_call_ordering_head, ordering, count);
}
+/* Returns 1 if the specified float values are equal, else returns 0. */
+static int float_compare(const float left,
+ const float right,
+ const float epsilon) {
+ float absLeft;
+ float absRight;
+ float largest;
+ float relDiff;
+
+ float 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 float values are equal. If the values are not equal
+ * an error is displayed and 0 is returned. */
+static int float_values_equal_display_error(const float left,
+ const float right,
+ const float epsilon) {
+ const int equal = float_compare(left, right, epsilon);
+ if (!equal) {
+ cm_print_error(FloatPrintfFormat " != "
+ FloatPrintfFormat "\n", left, right);
+ }
+ return equal;
+}
+
+/* Returns 1 if the specified float values are different. If the values are equal
+ * an error is displayed and 0 is returned. */
+static int float_values_not_equal_display_error(const float left,
+ const float right,
+ const float epsilon) {
+ const int not_equal = (float_compare(left, right, epsilon) == 0);
+ if (!not_equal) {
+ cm_print_error(FloatPrintfFormat " == "
+ FloatPrintfFormat "\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,
@@ -1714,6 +1771,26 @@ void _assert_return_code(const LargestIntegralType result,
}
}
+void _assert_float_equal(const float a,
+ const float b,
+ const float epsilon,
+ const char * const file,
+ const int line) {
+ if (!float_values_equal_display_error(a, b, epsilon)) {
+ _fail(file, line);
+ }
+}
+
+void _assert_float_not_equal(const float a,
+ const float b,
+ const float epsilon,
+ const char * const file,
+ const int line) {
+ if (!float_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) {