-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathtest_math_floating_point.cpp
More file actions
171 lines (157 loc) · 7.77 KB
/
test_math_floating_point.cpp
File metadata and controls
171 lines (157 loc) · 7.77 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Copyright (C) 2020-2026 MEmilio
*
* Authors: Rene Schmieding
*
* Contact: Martin J. Kuehn <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "memilio/ad/ad.h"
#include "memilio/math/floating_point.h"
#include <gtest/gtest.h>
#include <array>
#include <string>
template <class FP>
class TestMathFloatingPoint : public ::testing::Test
{
public:
using ParamType = std::array<FP, 5>;
using TruthTableType = std::array<std::array<bool, 3>, 4>;
FP a = FP(1.1), b = FP(3.2); // arbitrary values s.t. a < b
const ParamType params = {
b - a, // abs_tol
(b - a) / b, // rel_tol
FP(0.0), // zero
FP(10.0), // increased
FP(0.1) // reduced
};
};
using FpTypes = ::testing::Types<float, double, ad::gt1s<double>::type>;
TYPED_TEST_SUITE(TestMathFloatingPoint, FpTypes);
TYPED_TEST(TestMathFloatingPoint, abs_max)
{
TypeParam v1 = this->a, v2 = this->b;
// check for maximum
EXPECT_TRUE(v2 == mio::abs_max(v1, v2));
// check symmetries and signs
EXPECT_TRUE(v2 == mio::abs_max(v2, v1));
// specify type for signed comparisions, as -v may be an intermediate
EXPECT_TRUE(v2 == mio::abs_max<TypeParam>(v1, -v2));
EXPECT_TRUE(v2 == mio::abs_max<TypeParam>(-v2, v1));
EXPECT_TRUE(v2 == mio::abs_max<TypeParam>(-v1, v2));
EXPECT_TRUE(v2 == mio::abs_max<TypeParam>(v2, -v1));
EXPECT_TRUE(v2 == mio::abs_max<TypeParam>(-v1, -v2));
EXPECT_TRUE(v2 == mio::abs_max<TypeParam>(-v2, -v1));
}
/**
* Check floating point comparisions of v1 and v2 against a truth table.
* The table contents are described in the tests below.
*/
template <class FP, class Func>
void test_fp_compare(FP v1, FP v2, typename TestMathFloatingPoint<FP>::ParamType params, Func fp_compare,
typename TestMathFloatingPoint<FP>::TruthTableType truth_table)
{
ASSERT_LT(v1, v2) << "This test is not set up correctly!";
const auto [abs_tol, rel_tol, zero, increased, reduced] = params;
// on error, print out the name of the test, which is the same as the tested function
auto info = std::string(" With fp_compare as ") + testing::UnitTest::GetInstance()->current_test_info()->name();
// check basics
EXPECT_EQ(fp_compare(v1, v1, zero, zero), truth_table[0][0]) << info;
EXPECT_EQ(fp_compare(v1, v2, zero, zero), truth_table[0][1]) << info;
EXPECT_EQ(fp_compare(v2, v1, zero, zero), truth_table[0][2]) << info;
// check equalities with absolute tolerances
EXPECT_EQ(fp_compare(v1, v1, abs_tol, zero), truth_table[1][0]) << info;
EXPECT_EQ(fp_compare(v1, v1, reduced * abs_tol, zero), truth_table[1][1]) << info;
EXPECT_EQ(fp_compare(v1, v1, increased * abs_tol, zero), truth_table[1][2]) << info;
// check equalities with relative tolerances
EXPECT_EQ(fp_compare(v1, v1, zero, rel_tol), truth_table[1][0]) << info;
EXPECT_EQ(fp_compare(v1, v1, zero, reduced * rel_tol), truth_table[1][1]) << info;
EXPECT_EQ(fp_compare(v1, v1, zero, increased * rel_tol), truth_table[1][2]) << info;
// check absolute tolerances
EXPECT_EQ(fp_compare(v1, v2, abs_tol, zero), truth_table[2][0]) << info;
EXPECT_EQ(fp_compare(v1, v2, reduced * abs_tol, zero), truth_table[2][1]) << info;
EXPECT_EQ(fp_compare(v1, v2, increased * abs_tol, zero), truth_table[2][2]) << info;
// check relative tolerances
EXPECT_EQ(fp_compare(v1, v2, zero, rel_tol), truth_table[2][0]) << info;
EXPECT_EQ(fp_compare(v1, v2, zero, reduced * rel_tol), truth_table[2][1]) << info;
EXPECT_EQ(fp_compare(v1, v2, zero, increased * rel_tol), truth_table[2][2]) << info;
// check absolute tolerances, with switched FPs
EXPECT_EQ(fp_compare(v2, v1, abs_tol, zero), truth_table[3][0]) << info;
EXPECT_EQ(fp_compare(v2, v1, reduced * abs_tol, zero), truth_table[3][1]) << info;
EXPECT_EQ(fp_compare(v2, v1, increased * abs_tol, zero), truth_table[3][2]) << info;
// check relative tolerances, with switched FPs
EXPECT_EQ(fp_compare(v2, v1, zero, rel_tol), truth_table[3][0]) << info;
EXPECT_EQ(fp_compare(v2, v1, zero, reduced * rel_tol), truth_table[3][1]) << info;
EXPECT_EQ(fp_compare(v2, v1, zero, increased * rel_tol), truth_table[3][2]) << info;
}
TYPED_TEST(TestMathFloatingPoint, floating_point_equal)
{
typename TestMathFloatingPoint<TypeParam>::TruthTableType truth_table = {{
// {equal, strict less, strict greater}, both tolerances zero
{true, false, false}, // basic fp operations without tolerances
// {exact tolerance, reduced tolerance, increased tolerance} using either abs_tol or rel_tol
{true, true, true}, // equality
{true, false, true}, // less (or equal)
{true, false, true} // greater (or equal)
}};
test_fp_compare(this->a, this->b, this->params, &mio::floating_point_equal<TypeParam>, truth_table);
}
TYPED_TEST(TestMathFloatingPoint, floating_point_less)
{
typename TestMathFloatingPoint<TypeParam>::TruthTableType truth_table = {{
// {equal, strict less, strict greater}, both tolerances zero
{false, true, false}, // basic fp operations without tolerances
// {exact tolerance, reduced tolerance, increased tolerance} using either abs_tol or rel_tol
{false, false, false}, // equality
{false, true, false}, // less (or equal)
{false, false, false} // greater (or equal)
}};
test_fp_compare(this->a, this->b, this->params, &mio::floating_point_less<TypeParam>, truth_table);
}
TYPED_TEST(TestMathFloatingPoint, floating_point_less_equal)
{
typename TestMathFloatingPoint<TypeParam>::TruthTableType truth_table = {{
// {equal, strict less, strict greater}, both tolerances zero
{true, true, false}, // basic fp operations without tolerances
// {exact tolerance, reduced tolerance, increased tolerance} using either abs_tol or rel_tol
{true, true, true}, // equality
{true, true, true}, // less (or equal)
{true, false, true} // greater (or equal)
}};
test_fp_compare(this->a, this->b, this->params, &mio::floating_point_less_equal<TypeParam>, truth_table);
}
TYPED_TEST(TestMathFloatingPoint, floating_point_greater)
{
typename TestMathFloatingPoint<TypeParam>::TruthTableType truth_table = {{
// {equal, strict less, strict greater}, both tolerances zero
{false, false, true}, // basic fp operations without tolerances
// {exact tolerance, reduced tolerance, increased tolerance} using either abs_tol or rel_tol
{false, false, false}, // equality
{false, false, false}, // less (or equal)
{false, true, false} // greater (or equal)
}};
test_fp_compare(this->a, this->b, this->params, &mio::floating_point_greater<TypeParam>, truth_table);
}
TYPED_TEST(TestMathFloatingPoint, floating_point_greater_equal)
{
typename TestMathFloatingPoint<TypeParam>::TruthTableType truth_table = {{
// {equal, strict less, strict greater}, both tolerances zero
{true, false, true}, // basic fp operations without tolerances
// {exact tolerance, reduced tolerance, increased tolerance} using either abs_tol or rel_tol
{true, true, true}, // equality
{true, false, true}, // less (or equal)
{true, true, true} // greater (or equal)
}};
test_fp_compare(this->a, this->b, this->params, &mio::floating_point_greater_equal<TypeParam>, truth_table);
}