-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.hpp
72 lines (56 loc) · 1.8 KB
/
test.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2020 Oleksandr Bacherikov.
//
// Distributed under the Boost Software License, Version 1.0
// (see accompanying file LICENSE.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt).
#pragma once
#include <actl/operation/scalar/all.hpp>
#include <actl/range/algorithm.hpp>
#include <actl/range/irange.hpp>
#include <actl/range/operation/comparison.hpp>
#include <actl/std/vector.hpp>
#include <catch2/catch_test_macros.hpp>
#include <cmath>
#include <string_view>
using std::string_view_literals::operator""sv;
using namespace ac;
#define CHECK_EQUAL check_equal
#define CHECK_NOT_EQUAL check_not_equal
#define CHECK_EQUAL_SETS check_sets
#define CHECK_NEAR check_near
namespace ac {
template<class T>
struct abs_rel_error : scalar_operation<abs_rel_error<T>, 2> {
using operation_category = scalar_operation_tag;
struct is_policy;
abs_rel_error(T eps) : eps{eps} {}
T eps;
bool eval_scalar(T lhs, T rhs) const {
T numerator = abs(lhs - rhs);
T denominator = max(max(std::abs(lhs), std::abs(rhs)), T{1});
return numerator <= eps * denominator;
}
};
template<class T>
auto apply_policy(scalar::equal_f, abs_rel_error<T> const& policy) {
return policy;
}
} // namespace ac
template<class T>
void check_sets(std::vector<T> expected, std::vector<T> actual) {
sort(expected);
sort(actual);
CHECK(expected == actual);
}
template<class T, class U, class E>
void check_near(T const& expected, U const& actual, E eps) {
CHECK((ac::equal | ac::abs_rel_error<E>{eps})(expected, actual));
}
template<class T, class U>
void check_equal(T const& expected, U const& actual) {
CHECK(ac::equal(expected, actual));
}
template<class T, class U>
void check_not_equal(T const& not_expected, U const& actual) {
CHECK_FALSE(ac::equal(not_expected, actual));
}