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
39namespace Nektar
40{
41namespace RealComparisonUnitTests
42{
43BOOST_AUTO_TEST_CASE(TestRealComparisonSmall)
44{
45 // Small difference, should return true
46 double ad1 = 1.00001;
47 double ad2 = ad1 * (1.0 + 2 * std::numeric_limits<double>::epsilon());
48 BOOST_CHECK(LibUtilities::IsRealEqual(ad1, ad2));
49
50 double ad3 = 0.00001;
51 double ad4 = ad3 * (1.0 + 2 * std::numeric_limits<double>::epsilon());
52 BOOST_CHECK(LibUtilities::IsRealEqual(ad3, ad4));
53}
54
55BOOST_AUTO_TEST_CASE(TestRealComparisonNotSmall)
56{
57 // "Large" difference, should return false
58 double ad1 = 1.00001;
59 double ad2 = ad1 * (1.0 + 50 * std::numeric_limits<double>::epsilon());
60 BOOST_CHECK(!LibUtilities::IsRealEqual(ad1, ad2));
61
62 double ad3 = 0.00001;
63 double ad4 = ad3 * (1.0 + 50 * std::numeric_limits<double>::epsilon());
64 BOOST_CHECK(!LibUtilities::IsRealEqual(ad3, ad4));
65}
66
67BOOST_AUTO_TEST_CASE(TestRealComparisonSmallCustomTolerance)
68{
69 // Small difference, custom tolerance should return true
70 double ad1 = 1.00001;
71 double ad2 = ad1 * (1.0 + std::numeric_limits<double>::epsilon());
72 BOOST_CHECK(LibUtilities::IsRealEqual(ad1, ad2, 1));
73
74 double ad3 = 0.00001;
75 double ad4 = ad3 * (1.0 + std::numeric_limits<double>::epsilon());
76 BOOST_CHECK(LibUtilities::IsRealEqual(ad3, ad4, 1));
77}
78
79BOOST_AUTO_TEST_CASE(TestRealComparisonNotSmallCustomTolerance)
80{
81 // "Large" difference, custom tolerance should return true
82 double ad1 = 1.00001;
83 double ad2 = ad1 * (1.0 + 50 * std::numeric_limits<double>::epsilon());
84 BOOST_CHECK(LibUtilities::IsRealEqual(ad1, ad2, 100));
85}
86
87BOOST_AUTO_TEST_CASE(TestRealComparisonZero)
88{
89 // Both zero, dist and ref value will be zero
90 double ad1 = 0.0;
91 double ad2 = 0.0;
92 BOOST_CHECK(LibUtilities::IsRealEqual(ad1, ad2));
93}
94
95BOOST_AUTO_TEST_CASE(TestRealComparisonZeroNotZero)
96{
97 // This should return false
98 double ad1 = 0.0;
99 double ad2 = 1.0;
100 BOOST_CHECK(!LibUtilities::IsRealEqual(ad1, ad2));
101}
102
103BOOST_AUTO_TEST_CASE(TestRealComparisonRealLiteral)
104{
105 // Large difference, should return false
106 double ad1 = 1.00001;
107 BOOST_CHECK(!LibUtilities::IsRealEqual(ad1, 1.000011));
108}
109
110BOOST_AUTO_TEST_CASE(TestRealComparisonLiteral)
111{
112 // Large difference, should return false
113 BOOST_CHECK(!LibUtilities::IsRealEqual(1.00001, 1.000011));
114}
115
116BOOST_AUTO_TEST_CASE(TestRealComparisonConstNotConst)
117{
118 // This should return false
119 const double ad1 = 0.0;
120 double ad2 = 1.0;
121 BOOST_CHECK(!LibUtilities::IsRealEqual(ad1, ad2));
122}
123
124BOOST_AUTO_TEST_CASE(TestRealComparisonNotConstConst)
125{
126 // This should return false
127 double ad1 = 0.0;
128 const double ad2 = 1.0;
129 BOOST_CHECK(!LibUtilities::IsRealEqual(ad1, ad2));
130}
131
132BOOST_AUTO_TEST_CASE(TestRealComparisonRef)
133{
134 // This should return false
135 double ad1 = 0.0;
136 double &adr = ad1;
137 BOOST_CHECK(!LibUtilities::IsRealEqual(adr, 1.0));
138}
139
140// // This should not compile because comparing to an int
141// BOOST_AUTO_TEST_CASE(TestRealComparisonRef)
142// {
143// // This should return false
144// double ad1 = 0.0;
145// double& adr = ad1;
146// BOOST_CHECK(!LibUtilities::IsRealEqual(adr, 1));
147// }
148
149// The precondition is tested only in debug mode
150#if defined(NEKTAR_DEBUG) || defined(NEKTAR_FULLDEBUG)
151
152BOOST_AUTO_TEST_CASE(TestRealComparisonThrow)
153{
154 // This should throw
155 double ad1 = 0.0;
156 double ad2 = 0.0;
157 BOOST_CHECK_THROW(LibUtilities::IsRealEqual(ad1, ad2, 0),
158 std::runtime_error);
159}
160
161BOOST_AUTO_TEST_CASE(TestRealComparisonNoThrow)
162{
163 // This should not throw
164 double ad1 = 0.0;
165 double ad2 = 0.0;
166 BOOST_CHECK(LibUtilities::IsRealEqual(ad1, ad2, 1));
167}
168
169#endif
170
171// This should not compile because of the different float types
172// BOOST_AUTO_TEST_CASE(TestRealComparisonMixRealTypeThrow)
173// {
174// double ad1 = 0.0;
175// float ad2 = 0.0;
176// BOOST_CHECK_THROW(LibUtilities::IsRealEqual(ad1, ad2, 0),
177// std::runtime_error);
178// }
179} // namespace RealComparisonUnitTests
180} // namespace Nektar
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)
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2