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 "hikogui/polynomial.hpp"
8#include "hikogui/math.hpp"
9#include <gtest/gtest.h>
10#include <iostream>
11#include <string>
12
13template<typename T, typename U, int N>
14double maxAbsDiff(hi::results<T, N> const &lhs, hi::results<U, N> const &rhs)
15{
16 if (lhs.size() != rhs.size()) {
18 }
19
20 double max_diff = 0.0;
21 assert(lhs.size() <= lhs.capacity());
22 for (auto i = 0_uz; i != lhs.size(); ++i) {
23 // Compare with the closest value in rhs.
24 double min_diff = std::numeric_limits<double>::infinity();
25 for (auto j = 0_uz; j != rhs.size(); ++j) {
26 hi::inplace_min(min_diff, std::abs(lhs[i] - rhs[j]));
27 }
28
29 hi::inplace_max(max_diff, min_diff);
30 }
31 return max_diff;
32}
33
34template<typename T, typename U, int N>
35testing::AssertionResult ResultsNearPredFormat(
36 const char *expr1,
37 const char *expr2,
38 const char *abs_error_expr,
39 hi::results<T, N> val1,
40 hi::results<U, N> val2,
41 double abs_error)
42{
43 hilet diff = maxAbsDiff(val1, val2);
44 if (diff <= abs_error)
45 return testing::AssertionSuccess();
46
47 return testing::AssertionFailure() << "The difference between " << expr1 << " and " << expr2 << " is " << diff
48 << ", which exceeds " << abs_error_expr << ", where\n"
49 << expr1 << " evaluates to " << val1 << ",\n"
50 << expr2 << " evaluates to " << val2 << ", and\n"
51 << abs_error_expr << " evaluates to " << abs_error << ".";
52}
53
54#define ASSERT_RESULTS_NEAR(val1, val2, abs_error) ASSERT_PRED_FORMAT3(ResultsNearPredFormat, val1, val2, abs_error)
55
56#define ASSERT_RESULTS(val1, val2) ASSERT_RESULTS_NEAR(val1, val2, 0.000001)
57
58namespace hi::inline v1 {
59using results1 = hi::results<double, 1>;
60using results2 = hi::results<double, 2>;
61using results3 = hi::results<double, 3>;
62} // namespace hi::inline v1
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:15
T infinity(T... args)