ArmNN
 20.11
FloatingPointComparisonTest.cpp File Reference
#include <armnnUtils/FloatingPointComparison.hpp>
#include <boost/test/unit_test.hpp>

Go to the source code of this file.

Functions

 BOOST_AUTO_TEST_CASE (FloatingPointComparisonDefaultTolerance)
 
 BOOST_AUTO_TEST_CASE (FloatingPointComparisonLargePositiveNumbersDefaultTolerance)
 
 BOOST_AUTO_TEST_CASE (FloatingPointComparisonLargeNegativeNumbersDefaultTolerance)
 
 BOOST_AUTO_TEST_CASE (FloatingPointComparisonSpecifiedTolerance)
 
 BOOST_AUTO_TEST_CASE (FloatingPointComparisonLargePositiveNumbersSpecifiedTolerance)
 
 BOOST_AUTO_TEST_CASE (FloatingPointComparisonLargeNegativeNumbersSpecifiedTolerance)
 

Function Documentation

◆ BOOST_AUTO_TEST_CASE() [1/6]

BOOST_AUTO_TEST_CASE ( FloatingPointComparisonDefaultTolerance  )

Definition at line 14 of file FloatingPointComparisonTest.cpp.

References armnnUtils::within_percentage_tolerance().

15 {
16  // 1% range of 1.2 is 1.188 -> 1.212
17  // Just below tolerance.
18  BOOST_TEST(!within_percentage_tolerance(1.2f, 1.17f));
19  // Just above tolerance.
20  BOOST_TEST(!within_percentage_tolerance(1.2f, 1.213f));
21  // Just inside the lower range.
22  BOOST_TEST(within_percentage_tolerance(1.2f, 1.189f));
23  // Just inside the upper range.
24  BOOST_TEST(within_percentage_tolerance(1.2f, 1.210f));
25  // Exact match
26  BOOST_TEST(within_percentage_tolerance(1.2f, 1.2f));
27 
28  // Negative value tests.
29  BOOST_TEST(!within_percentage_tolerance(-1.2f, -1.17f));
30  BOOST_TEST(!within_percentage_tolerance(-1.2f, -1.213f));
31  BOOST_TEST(within_percentage_tolerance(-1.2f, -1.189f));
32  BOOST_TEST(within_percentage_tolerance(-1.2f, -1.210f));
33  BOOST_TEST(within_percentage_tolerance(-1.2f, -1.2f));
34 
35  // Negative & positive tests
36  BOOST_TEST(!within_percentage_tolerance(1.2f, -1.2f));
37  BOOST_TEST(!within_percentage_tolerance(-1.2f, 1.2f));
38 
39  // Negative and positive test with large float values.
40  BOOST_TEST(!within_percentage_tolerance(3.3E+38f, -1.17549435e38f));
41  BOOST_TEST(!within_percentage_tolerance(-1.17549435e38f, 3.3E+38f));
42 
43  // 1% range of 0.04 is 0.0396 -> 0.0404
44  // Just below tolerance.
45  BOOST_TEST(!within_percentage_tolerance(0.04f, 0.039f));
46  // Just above tolerance.
47  BOOST_TEST(!within_percentage_tolerance(0.04f, 0.04041f));
48  // Just inside the lower range.
49  BOOST_TEST(within_percentage_tolerance(0.04f, 0.0397f));
50  // Just inside the upper range.
51  BOOST_TEST(within_percentage_tolerance(0.04f, 0.04039f));
52  // Exact match
53  BOOST_TEST(within_percentage_tolerance(0.04f, 0.04f));
54 }
bool within_percentage_tolerance(float a, float b, float tolerancePercent=1.0f)
Compare two floats and return true if their values are within a specified tolerance of each other...

◆ BOOST_AUTO_TEST_CASE() [2/6]

BOOST_AUTO_TEST_CASE ( FloatingPointComparisonLargePositiveNumbersDefaultTolerance  )

Definition at line 56 of file FloatingPointComparisonTest.cpp.

References armnnUtils::within_percentage_tolerance().

57 {
58  // Just below tolerance.
59  BOOST_TEST(!within_percentage_tolerance(3.3E+38f, (3.3E+38f * 0.989f)));
60  // Just above tolerance.
61  BOOST_TEST(!within_percentage_tolerance(3.3E+38f, (3.3E+38f * 1.011f)));
62  // Just inside the lower range.
63  BOOST_TEST(within_percentage_tolerance(3.3E+38f, (3.3E+38f * 0.992f)));
64  // Just inside the upper range.
65  BOOST_TEST(within_percentage_tolerance(3.3E+38f, (3.3E+38f * 1.009f)));
66  // Exact match
67  BOOST_TEST(within_percentage_tolerance(3.3E+38f, 3.3E+38f));
68 }
bool within_percentage_tolerance(float a, float b, float tolerancePercent=1.0f)
Compare two floats and return true if their values are within a specified tolerance of each other...

◆ BOOST_AUTO_TEST_CASE() [3/6]

BOOST_AUTO_TEST_CASE ( FloatingPointComparisonLargeNegativeNumbersDefaultTolerance  )

Definition at line 70 of file FloatingPointComparisonTest.cpp.

References armnnUtils::within_percentage_tolerance().

71 {
72  // Just below tolerance.
73  BOOST_TEST(!within_percentage_tolerance(-1.17549435e38f, (-1.17549435e38f * -1.009f)));
74  // Just above tolerance.
75  BOOST_TEST(!within_percentage_tolerance(-1.17549435e38f, (-1.17549435e38f * 1.011f)));
76  // Just inside the lower range.
77  BOOST_TEST(within_percentage_tolerance(-1.17549435e38f, -1.17549435e38f - (-1.17549435e38f * 0.0099f)));
78  // Just inside the upper range.
79  BOOST_TEST(within_percentage_tolerance(-1.17549435e38f, -1.17549435e38f + (-1.17549435e38f * 0.0099f)));
80  // Exact match
81  BOOST_TEST(within_percentage_tolerance(-1.17549435e38f, -1.17549435e38f));
82 }
bool within_percentage_tolerance(float a, float b, float tolerancePercent=1.0f)
Compare two floats and return true if their values are within a specified tolerance of each other...

◆ BOOST_AUTO_TEST_CASE() [4/6]

BOOST_AUTO_TEST_CASE ( FloatingPointComparisonSpecifiedTolerance  )

Definition at line 84 of file FloatingPointComparisonTest.cpp.

References armnnUtils::within_percentage_tolerance().

85 {
86  // 2% range of 1.2 is 1.176 -> 1.224
87  // Just below tolerance.
88  BOOST_TEST(!within_percentage_tolerance(1.2f, 1.175f, 2.0f));
89  // Just above tolerance.
90  BOOST_TEST(!within_percentage_tolerance(1.2f, 1.226f, 2.0f));
91  // Just inside the lower range.
92  BOOST_TEST(within_percentage_tolerance(1.2f, 1.18f, 2.0f));
93  // Just inside the upper range.
94  BOOST_TEST(within_percentage_tolerance(1.2f, 1.22f, 2.0f));
95  // Exact match.
96  BOOST_TEST(within_percentage_tolerance(1.2f, 1.2f, 2.0f));
97 
98  // 5% range of 6.2 is 5.89 -> 6.51
99  // Just below tolerance.
100  BOOST_TEST(!within_percentage_tolerance(6.2f, 5.88f, 5.0f));
101  // Just above tolerance.
102  BOOST_TEST(!within_percentage_tolerance(6.2f, 6.52f, 5.0f));
103  // Just inside the lower range.
104  BOOST_TEST(within_percentage_tolerance(6.2f, 5.9f, 5.0f));
105  // Just inside the upper range.
106  BOOST_TEST(within_percentage_tolerance(6.2f, 6.5f, 5.0f));
107 
108  // Larger tolerance (unlikely to be used).
109  BOOST_TEST(within_percentage_tolerance(10.0f, 9.01f, 10.0f));
110  BOOST_TEST(!within_percentage_tolerance(10.0f, 8.99f, 10.0f));
111 }
bool within_percentage_tolerance(float a, float b, float tolerancePercent=1.0f)
Compare two floats and return true if their values are within a specified tolerance of each other...

◆ BOOST_AUTO_TEST_CASE() [5/6]

BOOST_AUTO_TEST_CASE ( FloatingPointComparisonLargePositiveNumbersSpecifiedTolerance  )

Definition at line 113 of file FloatingPointComparisonTest.cpp.

References armnnUtils::within_percentage_tolerance().

114 {
115  // Just below tolerance.
116  BOOST_TEST(!within_percentage_tolerance(3.3E+38f, (3.3E+38f * 0.979f), 2.0f));
117  // Just above tolerance.
118  BOOST_TEST(!within_percentage_tolerance(3.3E+38f, (3.3E+38f * 1.021f), 2.0f));
119  // Just inside the lower range.
120  BOOST_TEST(within_percentage_tolerance(3.3E+38f, (3.3E+38f * 0.982f), 2.0f));
121  // Just inside the upper range.
122  BOOST_TEST(within_percentage_tolerance(3.3E+38f, (3.3E+38f * 1.019f), 2.0f));
123 }
bool within_percentage_tolerance(float a, float b, float tolerancePercent=1.0f)
Compare two floats and return true if their values are within a specified tolerance of each other...

◆ BOOST_AUTO_TEST_CASE() [6/6]

BOOST_AUTO_TEST_CASE ( FloatingPointComparisonLargeNegativeNumbersSpecifiedTolerance  )

Definition at line 125 of file FloatingPointComparisonTest.cpp.

References BOOST_AUTO_TEST_SUITE_END(), and armnnUtils::within_percentage_tolerance().

126 {
127  // Just below tolerance.
128  BOOST_TEST(!within_percentage_tolerance(-1.17549435e38f, (-1.17549435e38f * -1.019f), 2.0f));
129  // Just above tolerance.
130  BOOST_TEST(!within_percentage_tolerance(-1.17549435e38f, (-1.17549435e38f * 1.021f), 2.0f));
131  // Just inside the lower range.
132  BOOST_TEST(within_percentage_tolerance(-1.17549435e38f, -1.17549435e38f - (-1.17549435e38f * 0.0089f), 2.0f));
133  // Just inside the upper range.
134  BOOST_TEST(within_percentage_tolerance(-1.17549435e38f, -1.17549435e38f + (-1.17549435e38f * 0.0089f), 2.0f));
135 }
bool within_percentage_tolerance(float a, float b, float tolerancePercent=1.0f)
Compare two floats and return true if their values are within a specified tolerance of each other...