Matthew Sloyan | fbba364 | 2020-10-15 13:53:27 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2020 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include <armnnUtils/FloatingPointComparison.hpp> |
| 7 | |
| 8 | #include <boost/test/unit_test.hpp> |
| 9 | |
| 10 | using namespace armnnUtils; |
| 11 | |
| 12 | BOOST_AUTO_TEST_SUITE(FloatingPointComparisonSuite) |
| 13 | |
| 14 | BOOST_AUTO_TEST_CASE(FloatingPointComparisonDefaultTolerance) |
| 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 | } |
| 55 | |
| 56 | BOOST_AUTO_TEST_CASE(FloatingPointComparisonLargePositiveNumbersDefaultTolerance) |
| 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 | } |
| 69 | |
| 70 | BOOST_AUTO_TEST_CASE(FloatingPointComparisonLargeNegativeNumbersDefaultTolerance) |
| 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 | } |
| 83 | |
| 84 | BOOST_AUTO_TEST_CASE(FloatingPointComparisonSpecifiedTolerance) |
| 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 | } |
| 112 | |
| 113 | BOOST_AUTO_TEST_CASE(FloatingPointComparisonLargePositiveNumbersSpecifiedTolerance) |
| 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 | } |
| 124 | |
| 125 | BOOST_AUTO_TEST_CASE(FloatingPointComparisonLargeNegativeNumbersSpecifiedTolerance) |
| 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 | } |
| 136 | |
| 137 | BOOST_AUTO_TEST_SUITE_END() |