From ae27119a8c25106826918088207c73d9e6e41775 Mon Sep 17 00:00:00 2001 From: Sylvain Joube Date: Sat, 1 Jun 2024 16:05:34 +0200 Subject: [PATCH] Tests: use TTS_RELATIVE_EQUAL instead of floats_are_same --- test/algorithm/algos/for_each.cpp | 12 ++++++------ test/algorithm/algos/numeric/1d.cpp | 13 +++++++------ test/algorithm/algos/reduce.cpp | 4 ++-- test/test.hpp | 22 ++++++++++++---------- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/test/algorithm/algos/for_each.cpp b/test/algorithm/algos/for_each.cpp index 4a67882f..5d304ac9 100644 --- a/test/algorithm/algos/for_each.cpp +++ b/test/algorithm/algos/for_each.cpp @@ -98,7 +98,7 @@ TTS_CASE("Check for kwk::for_each(func, container) 3D with float") kwk::for_each( [&](auto e) { ++count; total += e; }, view); TTS_EQUAL(count, input_size); - TTS_EXPECT(floats_are_same(total, chk_total)); + TTS_RELATIVE_EQUAL(total, chk_total, FLOAT_TOLERANCE_PERCENT); }; TTS_CASE("Check for kwk::for_each(func, container) 4D") @@ -121,7 +121,7 @@ TTS_CASE("Check for kwk::for_each(func, container) 4D") kwk::for_each( [&](auto e) { ++count; total += e; }, view); TTS_EQUAL(count, input_size); - TTS_EXPECT(floats_are_same(total, chk_total)); + TTS_RELATIVE_EQUAL(total, chk_total, FLOAT_TOLERANCE_PERCENT); }; @@ -141,7 +141,7 @@ TTS_CASE("Check for kwk::for_each_index(func, container) 1D") kwk::for_each_index( [&](auto& e, auto i0) { ++count; total += e; e = i0 + 1; }, view); TTS_EQUAL(count, input_size); - TTS_EXPECT(floats_are_same(total, chk_total)); + TTS_RELATIVE_EQUAL(total, chk_total, FLOAT_TOLERANCE_PERCENT); std::array expected_result; for (std::size_t i = 0; i < input_size; ++i) { expected_result[i] = i + 1; } @@ -185,7 +185,7 @@ TTS_CASE("Check for kwk::for_each_index(func, container) 2D") kwk::for_each_index( [&](auto& e, auto i0, auto i1) { ++count; total += e; e = pos_to_value(i0, i1); }, view); TTS_EQUAL(count, input_size); - TTS_EXPECT(floats_are_same(total, chk_total)); + TTS_RELATIVE_EQUAL(total, chk_total, FLOAT_TOLERANCE_PERCENT); std::array expected_result; std::size_t cpt{0}; @@ -226,7 +226,7 @@ TTS_CASE("Check for kwk::for_each_index(func, container) 3D with float") , view); TTS_EQUAL(count, input_size); - TTS_EXPECT(floats_are_same(total, chk_total)); + TTS_RELATIVE_EQUAL(total, chk_total, FLOAT_TOLERANCE_PERCENT); std::array expected_result; std::size_t cpt{0}; @@ -268,7 +268,7 @@ TTS_CASE("Check for kwk::for_each_index(func, container) 4D") , view); TTS_EQUAL(count, input_size); - TTS_EXPECT(floats_are_same(total, chk_total)); + TTS_RELATIVE_EQUAL(total, chk_total, FLOAT_TOLERANCE_PERCENT); std::array expected_result; std::size_t cpt{0}; diff --git a/test/algorithm/algos/numeric/1d.cpp b/test/algorithm/algos/numeric/1d.cpp index 2aad7b1c..102623ba 100644 --- a/test/algorithm/algos/numeric/1d.cpp +++ b/test/algorithm/algos/numeric/1d.cpp @@ -44,7 +44,8 @@ TTS_CASE("Check for kwk::transform_reduce(In1, In2, init, Reduce, Transform) 1D" , [](auto i1, auto i2) { return (i1 * i2); }); TTS_EQUAL(typeid(res), typeid(float)); - TTS_EXPECT(floats_are_same(res, chk)); + TTS_RELATIVE_EQUAL(res, chk, FLOAT_TOLERANCE_PERCENT); + // TTS_EXPECT(floats_are_same(res, chk)); TTS_EQUAL(reduce_count, input_size); TTS_EQUAL(transform_count, input_size); }; @@ -75,7 +76,7 @@ TTS_CASE("Check for kwk::transform_reduce(In1, In2, init, Transform) 1D") auto chk = std::transform_reduce(input1.begin(), input1.end(), input2.begin(), init_value); TTS_EQUAL(typeid(res), typeid(double)); - TTS_EXPECT(floats_are_same(res, chk)); + TTS_RELATIVE_EQUAL(res, chk, FLOAT_TOLERANCE_PERCENT); TTS_EQUAL(transform_count, input_size); }; @@ -99,7 +100,7 @@ TTS_CASE("Check for kwk::transform_reduce(In1, In2) 1D") auto chk = std::transform_reduce(input1.begin(), input1.end(), input2.begin(), init_value); TTS_EQUAL(typeid(res), typeid(double)); - TTS_EXPECT(floats_are_same(res, chk)); + TTS_RELATIVE_EQUAL(res, chk, FLOAT_TOLERANCE_PERCENT); }; @@ -130,7 +131,7 @@ TTS_CASE("Check for kwk::inner_product(In1, In2, value, sum, product) 1D") , [](auto i1, auto i2) { return (i1 * i2); } ); - TTS_EXPECT(floats_are_same(res, res_std)); + TTS_RELATIVE_EQUAL(res, res_std, FLOAT_TOLERANCE_PERCENT); TTS_EQUAL(typeid(res), typeid(float)); }; @@ -155,7 +156,7 @@ TTS_CASE("Check for kwk::inner_product(In1, In2, init) 1D") auto res = kwk::inner_product(view1, view2, init_value); auto res_std = std::inner_product (input1.begin(), input1.end(), input2.begin(), init_value); - TTS_EXPECT(floats_are_same(res, res_std)); + TTS_RELATIVE_EQUAL(res, res_std, FLOAT_TOLERANCE_PERCENT); TTS_EQUAL(typeid(res), typeid(float)); TTS_EQUAL(typeid(res_std), typeid(float)); }; @@ -180,7 +181,7 @@ TTS_CASE("Check for kwk::inner_product(In1, In2) 1D") auto res = kwk::inner_product(view1, view2); auto res_std = std::inner_product(input1.begin(), input1.end(), input2.begin(), float{}); - TTS_EXPECT(floats_are_same(res, res_std)); + TTS_RELATIVE_EQUAL(res, res_std, FLOAT_TOLERANCE_PERCENT); TTS_EQUAL(typeid(res), typeid(float)); TTS_EQUAL(typeid(res_std), typeid(float)); }; diff --git a/test/algorithm/algos/reduce.cpp b/test/algorithm/algos/reduce.cpp index be951f97..88e4dbf0 100644 --- a/test/algorithm/algos/reduce.cpp +++ b/test/algorithm/algos/reduce.cpp @@ -25,7 +25,7 @@ TTS_CASE("Check for kwk::reduce(in) 1D") auto res = kwk::reduce(view_in); auto res_std = std::reduce(input.begin(), input.end()); - TTS_EXPECT(floats_are_same(res, res_std)); + TTS_RELATIVE_EQUAL(res, res_std, FLOAT_TOLERANCE_PERCENT); }; @@ -45,7 +45,7 @@ TTS_CASE("Check for kwk::reduce(in, func) and kwk::reduce(in, func, init) 1D") auto res2 = kwk::reduce(view_in, [&](auto e1, auto e2) { return e1 + e2 + 1; }, 87); auto res_std = std::reduce(input.begin(), input.end(), 0, [&](auto e1, auto e2) { return e1 + e2 + 1; }); - TTS_EXPECT(floats_are_same(res, res_std)); + TTS_RELATIVE_EQUAL(res, res_std, FLOAT_TOLERANCE_PERCENT); TTS_EQUAL(static_cast(res2), res_std + 87); TTS_EQUAL(count, input_size); }; diff --git a/test/test.hpp b/test/test.hpp index 5b53e4c5..713bd0b7 100644 --- a/test/test.hpp +++ b/test/test.hpp @@ -20,17 +20,19 @@ template using up_to = std::make_index_sequence; #define TTS_CUSTOM_DRIVER_FUNCTION kwk_entry_point #include "tts.hpp" -bool floats_are_same(double a, double b) -{ - // If the distance between a and b is negligible compared to a and b, - // (i.e. if a and b only differs from at most max_ratio): - // abs(a-b) < abs(a) * max_ratio - // && abs(a-b) < abs(b) * max_ratio - double max_ratio = 0.0001; - double diff = std::fabs(a - b); +#define FLOAT_TOLERANCE_PERCENT 0.01 - return (diff <= std::fabs(a) * max_ratio) && (diff <= fabs(b) * max_ratio); -} +// bool floats_are_same(double a, double b) +// { +// // If the distance between a and b is negligible compared to a and b, +// // (i.e. if a and b only differs from at most max_ratio): +// // abs(a-b) < abs(a) * max_ratio +// // && abs(a-b) < abs(b) * max_ratio +// double max_ratio = 0.0001; +// double diff = std::fabs(a - b); + +// return (diff <= std::fabs(a) * max_ratio) && (diff <= fabs(b) * max_ratio); +// } template