1 // 2 // Copyright © 2020 Arm Ltd. All rights reserved. 3 // SPDX-License-Identifier: MIT 4 // 5 6 #include <armnnUtils/FloatingPointComparison.hpp> 7 8 #include <doctest/doctest.h> 9 10 using namespace armnnUtils; 11 12 TEST_SUITE("FloatingPointComparisonSuite") 13 { 14 TEST_CASE("FloatingPointComparisonDefaultTolerance") 15 { 16 // 1% range of 1.2 is 1.188 -> 1.212 17 // Just below tolerance. 18 CHECK(!within_percentage_tolerance(1.2f, 1.17f)); 19 // Just above tolerance. 20 CHECK(!within_percentage_tolerance(1.2f, 1.213f)); 21 // Just inside the lower range. 22 CHECK(within_percentage_tolerance(1.2f, 1.189f)); 23 // Just inside the upper range. 24 CHECK(within_percentage_tolerance(1.2f, 1.210f)); 25 // Exact match 26 CHECK(within_percentage_tolerance(1.2f, 1.2f)); 27 28 // Negative value tests. 29 CHECK(!within_percentage_tolerance(-1.2f, -1.17f)); 30 CHECK(!within_percentage_tolerance(-1.2f, -1.213f)); 31 CHECK(within_percentage_tolerance(-1.2f, -1.189f)); 32 CHECK(within_percentage_tolerance(-1.2f, -1.210f)); 33 CHECK(within_percentage_tolerance(-1.2f, -1.2f)); 34 35 // Negative & positive tests 36 CHECK(!within_percentage_tolerance(1.2f, -1.2f)); 37 CHECK(!within_percentage_tolerance(-1.2f, 1.2f)); 38 39 // Negative and positive test with large float values. 40 CHECK(!within_percentage_tolerance(3.3E+38f, -1.17549435e38f)); 41 CHECK(!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 CHECK(!within_percentage_tolerance(0.04f, 0.039f)); 46 // Just above tolerance. 47 CHECK(!within_percentage_tolerance(0.04f, 0.04041f)); 48 // Just inside the lower range. 49 CHECK(within_percentage_tolerance(0.04f, 0.0397f)); 50 // Just inside the upper range. 51 CHECK(within_percentage_tolerance(0.04f, 0.04039f)); 52 // Exact match 53 CHECK(within_percentage_tolerance(0.04f, 0.04f)); 54 } 55 56 TEST_CASE("FloatingPointComparisonLargePositiveNumbersDefaultTolerance") 57 { 58 // Just below tolerance. 59 CHECK(!within_percentage_tolerance(3.3E+38f, (3.3E+38f * 0.989f))); 60 // Just above tolerance. 61 CHECK(!within_percentage_tolerance(3.3E+38f, (3.3E+38f * 1.011f))); 62 // Just inside the lower range. 63 CHECK(within_percentage_tolerance(3.3E+38f, (3.3E+38f * 0.992f))); 64 // Just inside the upper range. 65 CHECK(within_percentage_tolerance(3.3E+38f, (3.3E+38f * 1.009f))); 66 // Exact match 67 CHECK(within_percentage_tolerance(3.3E+38f, 3.3E+38f)); 68 } 69 70 TEST_CASE("FloatingPointComparisonLargeNegativeNumbersDefaultTolerance") 71 { 72 // Just below tolerance. 73 CHECK(!within_percentage_tolerance(-1.17549435e38f, (-1.17549435e38f * -1.009f))); 74 // Just above tolerance. 75 CHECK(!within_percentage_tolerance(-1.17549435e38f, (-1.17549435e38f * 1.011f))); 76 // Just inside the lower range. 77 CHECK(within_percentage_tolerance(-1.17549435e38f, -1.17549435e38f - (-1.17549435e38f * 0.0099f))); 78 // Just inside the upper range. 79 CHECK(within_percentage_tolerance(-1.17549435e38f, -1.17549435e38f + (-1.17549435e38f * 0.0099f))); 80 // Exact match 81 CHECK(within_percentage_tolerance(-1.17549435e38f, -1.17549435e38f)); 82 } 83 84 TEST_CASE("FloatingPointComparisonSpecifiedTolerance") 85 { 86 // 2% range of 1.2 is 1.176 -> 1.224 87 // Just below tolerance. 88 CHECK(!within_percentage_tolerance(1.2f, 1.175f, 2.0f)); 89 // Just above tolerance. 90 CHECK(!within_percentage_tolerance(1.2f, 1.226f, 2.0f)); 91 // Just inside the lower range. 92 CHECK(within_percentage_tolerance(1.2f, 1.18f, 2.0f)); 93 // Just inside the upper range. 94 CHECK(within_percentage_tolerance(1.2f, 1.22f, 2.0f)); 95 // Exact match. 96 CHECK(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 CHECK(!within_percentage_tolerance(6.2f, 5.88f, 5.0f)); 101 // Just above tolerance. 102 CHECK(!within_percentage_tolerance(6.2f, 6.52f, 5.0f)); 103 // Just inside the lower range. 104 CHECK(within_percentage_tolerance(6.2f, 5.9f, 5.0f)); 105 // Just inside the upper range. 106 CHECK(within_percentage_tolerance(6.2f, 6.5f, 5.0f)); 107 108 // Larger tolerance (unlikely to be used). 109 CHECK(within_percentage_tolerance(10.0f, 9.01f, 10.0f)); 110 CHECK(!within_percentage_tolerance(10.0f, 8.99f, 10.0f)); 111 } 112 113 TEST_CASE("FloatingPointComparisonLargePositiveNumbersSpecifiedTolerance") 114 { 115 // Just below tolerance. 116 CHECK(!within_percentage_tolerance(3.3E+38f, (3.3E+38f * 0.979f), 2.0f)); 117 // Just above tolerance. 118 CHECK(!within_percentage_tolerance(3.3E+38f, (3.3E+38f * 1.021f), 2.0f)); 119 // Just inside the lower range. 120 CHECK(within_percentage_tolerance(3.3E+38f, (3.3E+38f * 0.982f), 2.0f)); 121 // Just inside the upper range. 122 CHECK(within_percentage_tolerance(3.3E+38f, (3.3E+38f * 1.019f), 2.0f)); 123 } 124 125 TEST_CASE("FloatingPointComparisonLargeNegativeNumbersSpecifiedTolerance") 126 { 127 // Just below tolerance. 128 CHECK(!within_percentage_tolerance(-1.17549435e38f, (-1.17549435e38f * -1.019f), 2.0f)); 129 // Just above tolerance. 130 CHECK(!within_percentage_tolerance(-1.17549435e38f, (-1.17549435e38f * 1.021f), 2.0f)); 131 // Just inside the lower range. 132 CHECK(within_percentage_tolerance(-1.17549435e38f, -1.17549435e38f - (-1.17549435e38f * 0.0089f), 2.0f)); 133 // Just inside the upper range. 134 CHECK(within_percentage_tolerance(-1.17549435e38f, -1.17549435e38f + (-1.17549435e38f * 0.0089f), 2.0f)); 135 } 136 137 } 138