HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
polynomial_tests.hpp
1// Copyright Take Vos 2019, 2021-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "polynomial.hpp"
8#include "../utility/utility.hpp"
9#include "../macros.hpp"
10#include <gtest/gtest.h>
11#include <iostream>
12#include <string>
13
14
15
16template<typename T, typename U, int N>
17double maxAbsDiff(hi::results<T, N> const &lhs, hi::results<U, N> const &rhs)
18{
19 if (lhs.size() != rhs.size()) {
21 }
22
23 double max_diff = 0.0;
24 assert(lhs.size() <= lhs.capacity());
25 for (auto i = 0_uz; i != lhs.size(); ++i) {
26 // Compare with the closest value in rhs.
27 double min_diff = std::numeric_limits<double>::infinity();
28 for (auto j = 0_uz; j != rhs.size(); ++j) {
29 hi::inplace_min(min_diff, std::abs(lhs[i] - rhs[j]));
30 }
31
32 hi::inplace_max(max_diff, min_diff);
33 }
34 return max_diff;
35}
36
37template<typename T, typename U, int N>
38testing::AssertionResult ResultsNearPredFormat(
39 const char *expr1,
40 const char *expr2,
41 const char *abs_error_expr,
44 double abs_error)
45{
46 hilet diff = maxAbsDiff(val1, val2);
47 if (diff <= abs_error)
48 return testing::AssertionSuccess();
49
50 return testing::AssertionFailure() << "The difference between " << expr1 << " and " << expr2 << " is " << diff
51 << ", which exceeds " << abs_error_expr << ", where\n"
52 << expr1 << " evaluates to " << val1 << ",\n"
53 << expr2 << " evaluates to " << val2 << ", and\n"
54 << abs_error_expr << " evaluates to " << abs_error << ".";
55}
56
57#define ASSERT_RESULTS_NEAR(val1, val2, abs_error) ASSERT_PRED_FORMAT3(ResultsNearPredFormat, val1, val2, abs_error)
58
59#define ASSERT_RESULTS(val1, val2) ASSERT_RESULTS_NEAR(val1, val2, 0.000001)
60
61namespace hi::inline v1 {
62using results1 = hi::results<double, 1>;
63using results2 = hi::results<double, 2>;
64using results3 = hi::results<double, 3>;
65} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm.hpp:16
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
T infinity(T... args)