Skip to content

Commit

Permalink
Tests: use TTS_RELATIVE_EQUAL
Browse files Browse the repository at this point in the history
instead of floats_are_same
  • Loading branch information
SylvainJoube committed Jun 1, 2024
1 parent 4795c77 commit 9ab0ca0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
12 changes: 6 additions & 6 deletions test/algorithm/algos/for_each.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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);
};


Expand All @@ -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<int, input_size> expected_result;
for (std::size_t i = 0; i < input_size; ++i) { expected_result[i] = i + 1; }
Expand Down Expand Up @@ -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<int, input_size> expected_result;
std::size_t cpt{0};
Expand Down Expand Up @@ -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<data_type, input_size> expected_result;
std::size_t cpt{0};
Expand Down Expand Up @@ -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<int, input_size> expected_result;
std::size_t cpt{0};
Expand Down
13 changes: 7 additions & 6 deletions test/algorithm/algos/numeric/1d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down Expand Up @@ -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_EQUAL(transform_count, input_size);
};

Expand All @@ -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);
};


Expand Down Expand Up @@ -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));
};

Expand All @@ -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));
};
Expand All @@ -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));
};
Expand Down
4 changes: 2 additions & 2 deletions test/algorithm/algos/reduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};


Expand All @@ -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<decltype(view_in)::value_type>(res2), res_std + 87);
TTS_EQUAL(count, input_size);
};
22 changes: 12 additions & 10 deletions test/test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@ template<typename N> using up_to = std::make_index_sequence<N::value>;
#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<typename T, typename S>
Expand Down

0 comments on commit 9ab0ca0

Please sign in to comment.