Nektar++
TestRealComparison.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: TestRealComparison.cpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: unit test for IsRealEqual()
32//
33///////////////////////////////////////////////////////////////////////////////
34
36
37#include <boost/test/unit_test.hpp>
38
40{
41BOOST_AUTO_TEST_CASE(TestRealComparisonSmall)
42{
43 // Small difference, should return true
44 double ad1 = 1.00001;
45 double ad2 = ad1 * (1.0 + 2 * std::numeric_limits<double>::epsilon());
46 BOOST_CHECK(LibUtilities::IsRealEqual(ad1, ad2));
47
48 double ad3 = 0.00001;
49 double ad4 = ad3 * (1.0 + 2 * std::numeric_limits<double>::epsilon());
50 BOOST_CHECK(LibUtilities::IsRealEqual(ad3, ad4));
51}
52
53BOOST_AUTO_TEST_CASE(TestRealComparisonNotSmall)
54{
55 // "Large" difference, should return false
56 double ad1 = 1.00001;
57 double ad2 = ad1 * (1.0 + 50 * std::numeric_limits<double>::epsilon());
58 BOOST_CHECK(!LibUtilities::IsRealEqual(ad1, ad2));
59
60 double ad3 = 0.00001;
61 double ad4 = ad3 * (1.0 + 50 * std::numeric_limits<double>::epsilon());
62 BOOST_CHECK(!LibUtilities::IsRealEqual(ad3, ad4));
63}
64
65BOOST_AUTO_TEST_CASE(TestRealComparisonSmallCustomTolerance)
66{
67 // Small difference, custom tolerance should return true
68 double ad1 = 1.00001;
69 double ad2 = ad1 * (1.0 + std::numeric_limits<double>::epsilon());
70 BOOST_CHECK(LibUtilities::IsRealEqual(ad1, ad2, 1));
71
72 double ad3 = 0.00001;
73 double ad4 = ad3 * (1.0 + std::numeric_limits<double>::epsilon());
74 BOOST_CHECK(LibUtilities::IsRealEqual(ad3, ad4, 1));
75}
76
77BOOST_AUTO_TEST_CASE(TestRealComparisonNotSmallCustomTolerance)
78{
79 // "Large" difference, custom tolerance should return true
80 double ad1 = 1.00001;
81 double ad2 = ad1 * (1.0 + 50 * std::numeric_limits<double>::epsilon());
82 BOOST_CHECK(LibUtilities::IsRealEqual(ad1, ad2, 100));
83}
84
85BOOST_AUTO_TEST_CASE(TestRealComparisonZero)
86{
87 // Both zero, dist and ref value will be zero
88 double ad1 = 0.0;
89 double ad2 = 0.0;
90 BOOST_CHECK(LibUtilities::IsRealEqual(ad1, ad2));
91}
92
93BOOST_AUTO_TEST_CASE(TestRealComparisonZeroNotZero)
94{
95 // This should return false
96 double ad1 = 0.0;
97 double ad2 = 1.0;
98 BOOST_CHECK(!LibUtilities::IsRealEqual(ad1, ad2));
99}
100
101BOOST_AUTO_TEST_CASE(TestRealComparisonRealLiteral)
102{
103 // Large difference, should return false
104 double ad1 = 1.00001;
105 BOOST_CHECK(!LibUtilities::IsRealEqual(ad1, 1.000011));
106}
107
108BOOST_AUTO_TEST_CASE(TestRealComparisonLiteral)
109{
110 // Large difference, should return false
111 BOOST_CHECK(!LibUtilities::IsRealEqual(1.00001, 1.000011));
112}
113
114BOOST_AUTO_TEST_CASE(TestRealComparisonConstNotConst)
115{
116 // This should return false
117 const double ad1 = 0.0;
118 double ad2 = 1.0;
119 BOOST_CHECK(!LibUtilities::IsRealEqual(ad1, ad2));
120}
121
122BOOST_AUTO_TEST_CASE(TestRealComparisonNotConstConst)
123{
124 // This should return false
125 double ad1 = 0.0;
126 const double ad2 = 1.0;
127 BOOST_CHECK(!LibUtilities::IsRealEqual(ad1, ad2));
128}
129
130BOOST_AUTO_TEST_CASE(TestRealComparisonRef)
131{
132 // This should return false
133 double ad1 = 0.0;
134 double &adr = ad1;
135 BOOST_CHECK(!LibUtilities::IsRealEqual(adr, 1.0));
136}
137
138// // This should not compile because comparing to an int
139// BOOST_AUTO_TEST_CASE(TestRealComparisonRef)
140// {
141// // This should return false
142// double ad1 = 0.0;
143// double& adr = ad1;
144// BOOST_CHECK(!LibUtilities::IsRealEqual(adr, 1));
145// }
146
147// The precondition is tested only in debug mode
148#if defined(NEKTAR_DEBUG) || defined(NEKTAR_FULLDEBUG)
149
150BOOST_AUTO_TEST_CASE(TestRealComparisonThrow)
151{
152 // This should throw
153 double ad1 = 0.0;
154 double ad2 = 0.0;
155 BOOST_CHECK_THROW(LibUtilities::IsRealEqual(ad1, ad2, 0),
156 std::runtime_error);
157}
158
159BOOST_AUTO_TEST_CASE(TestRealComparisonNoThrow)
160{
161 // This should not throw
162 double ad1 = 0.0;
163 double ad2 = 0.0;
164 BOOST_CHECK(LibUtilities::IsRealEqual(ad1, ad2, 1));
165}
166
167#endif
168
169// This should not compile because of the different float types
170// BOOST_AUTO_TEST_CASE(TestRealComparisonMixRealTypeThrow)
171// {
172// double ad1 = 0.0;
173// float ad2 = 0.0;
174// BOOST_CHECK_THROW(LibUtilities::IsRealEqual(ad1, ad2, 0),
175// std::runtime_error);
176// }
177} // namespace Nektar::RealComparisonUnitTests
bool IsRealEqual(T1 &&lhs, T2 &&rhs, const unsigned int factor=NekConstants::kNekFloatCompFact)
compare reals of same type with relative tolerance
BOOST_AUTO_TEST_CASE(TestRealComparisonSmall)