9c40c68
From cefd7b59df628eca240af3c136d66137c8e94888 Mon Sep 17 00:00:00 2001
9c40c68
From: =?UTF-8?q?IOhannes=20m=20zm=C3=B6lnig?= <zmoelnig@iem.at>
9c40c68
Date: Thu, 8 Sep 2022 10:49:36 +0200
9c40c68
Subject: [PATCH] tests: Use fuzzy comparison in test-suite
9c40c68
MIME-Version: 1.0
9c40c68
Content-Type: text/plain; charset=UTF-8
9c40c68
Content-Transfer-Encoding: 8bit
9c40c68
9c40c68
Using exact comparison ("a == b") when comparing expected with computed
9c40c68
test data fails the test-suite on many architectures (including, but not
9c40c68
limited to armhf and arm64).
9c40c68
9c40c68
Instead, use epsilon(for now, FLT_EPSILON and DBL_EPSILON) to compare
9c40c68
floating point numbers for equality.
9c40c68
9c40c68
9c40c68
Closes: https://github.com/libsndfile/libsndfile/issues/866
9c40c68
9c40c68
Signed-off-by: IOhannes m zmölnig <zmoelnig@iem.at>
9c40c68
---
9c40c68
 tests/utils.tpl | 27 +++++++++++++++++++++++++--
9c40c68
 1 file changed, 25 insertions(+), 2 deletions(-)
9c40c68
9c40c68
diff --git a/tests/utils.tpl b/tests/utils.tpl
9c40c68
index c68e3a26e..0d1cd8bb9 100644
9c40c68
--- a/tests/utils.tpl
9c40c68
+++ b/tests/utils.tpl
9c40c68
@@ -193,6 +193,7 @@ sf_count_t		file_length_fd (int fd) ;
9c40c68
 #include <string.h>
9c40c68
 #include <ctype.h>
9c40c68
 #include <math.h>
9c40c68
+#include <float.h>
9c40c68
 #include <fcntl.h>
9c40c68
 #include <sys/stat.h>
9c40c68
 
9c40c68
@@ -215,6 +216,28 @@ sf_count_t		file_length_fd (int fd) ;
9c40c68
 #define O_BINARY 0
9c40c68
 #endif
9c40c68
 
9c40c68
+
9c40c68
+/*
9c40c68
+**      Compare for equality, with epsilon
9c40c68
+*/
9c40c68
+static inline int
9c40c68
+equals_short (const short a, const short b)
9c40c68
+{        return (a == b);
9c40c68
+} /* equals_short */
9c40c68
+static inline int
9c40c68
+equals_int (const int a, const int b)
9c40c68
+{        return (a == b);
9c40c68
+} /* equals_int */
9c40c68
+static inline int
9c40c68
+equals_float (const float a, const float b)
9c40c68
+{        return (fabsf(a - b) <= FLT_EPSILON);
9c40c68
+} /* equals_float */
9c40c68
+static inline int
9c40c68
+equals_double (const double a, const double b)
9c40c68
+{       return (fabs(a - b) <= DBL_EPSILON);
9c40c68
+} /* equals_double */
9c40c68
+
9c40c68
+
9c40c68
 [+ FOR float_type +]
9c40c68
 void
9c40c68
 gen_windowed_sine_[+ (get "name") +] ([+ (get "name") +] *data, int len, double maximum)
9c40c68
@@ -752,8 +775,8 @@ compare_[+ (get "io_element") +]_or_die (const [+ (get "io_element") +] *expecte
9c40c68
 	unsigned k ;
9c40c68
 
9c40c68
 	for (k = 0 ; k < count ; k++)
9c40c68
-		if (expected [k] != actual [k])
9c40c68
-		{	printf ("\n\nLine %d : Error at index %d, got " [+ (get "format_str") +] ", should be " [+ (get "format_str") +] ".\n\n", line_num, k, actual [k], expected [k]) ;
9c40c68
+		if (!equals_[+ (get "io_element") +](expected [k], actual [k]))
9c40c68
+		{	printf ("\n\nLine %d : Error at index %d, got " [+ (get "format_str") +] ", should be " [+ (get "format_str") +] "(delta=" [+ (get "format_str") +] " ).\n\n", line_num, k, actual [k], expected [k], actual [k] - expected [k]) ;
9c40c68
 			exit (1) ;
9c40c68
 			} ;
9c40c68