blob: 7b97404ed3d9a338fe6282ab9c6ca844c9dfb5b1 [file] [log] [blame]
Matthew Sloyanfbba3642020-10-15 13:53:27 +01001//
2// Copyright © 2020 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include <armnnUtils/FloatingPointComparison.hpp>
7
Sadik Armagan1625efc2021-06-10 18:24:34 +01008#include <doctest/doctest.h>
Matthew Sloyanfbba3642020-10-15 13:53:27 +01009
10using namespace armnnUtils;
11
Sadik Armagan1625efc2021-06-10 18:24:34 +010012TEST_SUITE("FloatingPointComparisonSuite")
13{
14TEST_CASE("FloatingPointComparisonDefaultTolerance")
Matthew Sloyanfbba3642020-10-15 13:53:27 +010015{
16 // 1% range of 1.2 is 1.188 -> 1.212
17 // Just below tolerance.
Sadik Armagan1625efc2021-06-10 18:24:34 +010018 CHECK(!within_percentage_tolerance(1.2f, 1.17f));
Matthew Sloyanfbba3642020-10-15 13:53:27 +010019 // Just above tolerance.
Sadik Armagan1625efc2021-06-10 18:24:34 +010020 CHECK(!within_percentage_tolerance(1.2f, 1.213f));
Matthew Sloyanfbba3642020-10-15 13:53:27 +010021 // Just inside the lower range.
Sadik Armagan1625efc2021-06-10 18:24:34 +010022 CHECK(within_percentage_tolerance(1.2f, 1.189f));
Matthew Sloyanfbba3642020-10-15 13:53:27 +010023 // Just inside the upper range.
Sadik Armagan1625efc2021-06-10 18:24:34 +010024 CHECK(within_percentage_tolerance(1.2f, 1.210f));
Matthew Sloyanfbba3642020-10-15 13:53:27 +010025 // Exact match
Sadik Armagan1625efc2021-06-10 18:24:34 +010026 CHECK(within_percentage_tolerance(1.2f, 1.2f));
Matthew Sloyanfbba3642020-10-15 13:53:27 +010027
28 // Negative value tests.
Sadik Armagan1625efc2021-06-10 18:24:34 +010029 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));
Matthew Sloyanfbba3642020-10-15 13:53:27 +010034
35 // Negative & positive tests
Sadik Armagan1625efc2021-06-10 18:24:34 +010036 CHECK(!within_percentage_tolerance(1.2f, -1.2f));
37 CHECK(!within_percentage_tolerance(-1.2f, 1.2f));
Matthew Sloyanfbba3642020-10-15 13:53:27 +010038
39 // Negative and positive test with large float values.
Sadik Armagan1625efc2021-06-10 18:24:34 +010040 CHECK(!within_percentage_tolerance(3.3E+38f, -1.17549435e38f));
41 CHECK(!within_percentage_tolerance(-1.17549435e38f, 3.3E+38f));
Matthew Sloyanfbba3642020-10-15 13:53:27 +010042
43 // 1% range of 0.04 is 0.0396 -> 0.0404
44 // Just below tolerance.
Sadik Armagan1625efc2021-06-10 18:24:34 +010045 CHECK(!within_percentage_tolerance(0.04f, 0.039f));
Matthew Sloyanfbba3642020-10-15 13:53:27 +010046 // Just above tolerance.
Sadik Armagan1625efc2021-06-10 18:24:34 +010047 CHECK(!within_percentage_tolerance(0.04f, 0.04041f));
Matthew Sloyanfbba3642020-10-15 13:53:27 +010048 // Just inside the lower range.
Sadik Armagan1625efc2021-06-10 18:24:34 +010049 CHECK(within_percentage_tolerance(0.04f, 0.0397f));
Matthew Sloyanfbba3642020-10-15 13:53:27 +010050 // Just inside the upper range.
Sadik Armagan1625efc2021-06-10 18:24:34 +010051 CHECK(within_percentage_tolerance(0.04f, 0.04039f));
Matthew Sloyanfbba3642020-10-15 13:53:27 +010052 // Exact match
Sadik Armagan1625efc2021-06-10 18:24:34 +010053 CHECK(within_percentage_tolerance(0.04f, 0.04f));
Matthew Sloyanfbba3642020-10-15 13:53:27 +010054}
55
Sadik Armagan1625efc2021-06-10 18:24:34 +010056TEST_CASE("FloatingPointComparisonLargePositiveNumbersDefaultTolerance")
Matthew Sloyanfbba3642020-10-15 13:53:27 +010057{
58 // Just below tolerance.
Sadik Armagan1625efc2021-06-10 18:24:34 +010059 CHECK(!within_percentage_tolerance(3.3E+38f, (3.3E+38f * 0.989f)));
Matthew Sloyanfbba3642020-10-15 13:53:27 +010060 // Just above tolerance.
Sadik Armagan1625efc2021-06-10 18:24:34 +010061 CHECK(!within_percentage_tolerance(3.3E+38f, (3.3E+38f * 1.011f)));
Matthew Sloyanfbba3642020-10-15 13:53:27 +010062 // Just inside the lower range.
Sadik Armagan1625efc2021-06-10 18:24:34 +010063 CHECK(within_percentage_tolerance(3.3E+38f, (3.3E+38f * 0.992f)));
Matthew Sloyanfbba3642020-10-15 13:53:27 +010064 // Just inside the upper range.
Sadik Armagan1625efc2021-06-10 18:24:34 +010065 CHECK(within_percentage_tolerance(3.3E+38f, (3.3E+38f * 1.009f)));
Matthew Sloyanfbba3642020-10-15 13:53:27 +010066 // Exact match
Sadik Armagan1625efc2021-06-10 18:24:34 +010067 CHECK(within_percentage_tolerance(3.3E+38f, 3.3E+38f));
Matthew Sloyanfbba3642020-10-15 13:53:27 +010068}
69
Sadik Armagan1625efc2021-06-10 18:24:34 +010070TEST_CASE("FloatingPointComparisonLargeNegativeNumbersDefaultTolerance")
Matthew Sloyanfbba3642020-10-15 13:53:27 +010071{
72 // Just below tolerance.
Sadik Armagan1625efc2021-06-10 18:24:34 +010073 CHECK(!within_percentage_tolerance(-1.17549435e38f, (-1.17549435e38f * -1.009f)));
Matthew Sloyanfbba3642020-10-15 13:53:27 +010074 // Just above tolerance.
Sadik Armagan1625efc2021-06-10 18:24:34 +010075 CHECK(!within_percentage_tolerance(-1.17549435e38f, (-1.17549435e38f * 1.011f)));
Matthew Sloyanfbba3642020-10-15 13:53:27 +010076 // Just inside the lower range.
Sadik Armagan1625efc2021-06-10 18:24:34 +010077 CHECK(within_percentage_tolerance(-1.17549435e38f, -1.17549435e38f - (-1.17549435e38f * 0.0099f)));
Matthew Sloyanfbba3642020-10-15 13:53:27 +010078 // Just inside the upper range.
Sadik Armagan1625efc2021-06-10 18:24:34 +010079 CHECK(within_percentage_tolerance(-1.17549435e38f, -1.17549435e38f + (-1.17549435e38f * 0.0099f)));
Matthew Sloyanfbba3642020-10-15 13:53:27 +010080 // Exact match
Sadik Armagan1625efc2021-06-10 18:24:34 +010081 CHECK(within_percentage_tolerance(-1.17549435e38f, -1.17549435e38f));
Matthew Sloyanfbba3642020-10-15 13:53:27 +010082}
83
Sadik Armagan1625efc2021-06-10 18:24:34 +010084TEST_CASE("FloatingPointComparisonSpecifiedTolerance")
Matthew Sloyanfbba3642020-10-15 13:53:27 +010085{
86 // 2% range of 1.2 is 1.176 -> 1.224
87 // Just below tolerance.
Sadik Armagan1625efc2021-06-10 18:24:34 +010088 CHECK(!within_percentage_tolerance(1.2f, 1.175f, 2.0f));
Matthew Sloyanfbba3642020-10-15 13:53:27 +010089 // Just above tolerance.
Sadik Armagan1625efc2021-06-10 18:24:34 +010090 CHECK(!within_percentage_tolerance(1.2f, 1.226f, 2.0f));
Matthew Sloyanfbba3642020-10-15 13:53:27 +010091 // Just inside the lower range.
Sadik Armagan1625efc2021-06-10 18:24:34 +010092 CHECK(within_percentage_tolerance(1.2f, 1.18f, 2.0f));
Matthew Sloyanfbba3642020-10-15 13:53:27 +010093 // Just inside the upper range.
Sadik Armagan1625efc2021-06-10 18:24:34 +010094 CHECK(within_percentage_tolerance(1.2f, 1.22f, 2.0f));
Matthew Sloyanfbba3642020-10-15 13:53:27 +010095 // Exact match.
Sadik Armagan1625efc2021-06-10 18:24:34 +010096 CHECK(within_percentage_tolerance(1.2f, 1.2f, 2.0f));
Matthew Sloyanfbba3642020-10-15 13:53:27 +010097
98 // 5% range of 6.2 is 5.89 -> 6.51
99 // Just below tolerance.
Sadik Armagan1625efc2021-06-10 18:24:34 +0100100 CHECK(!within_percentage_tolerance(6.2f, 5.88f, 5.0f));
Matthew Sloyanfbba3642020-10-15 13:53:27 +0100101 // Just above tolerance.
Sadik Armagan1625efc2021-06-10 18:24:34 +0100102 CHECK(!within_percentage_tolerance(6.2f, 6.52f, 5.0f));
Matthew Sloyanfbba3642020-10-15 13:53:27 +0100103 // Just inside the lower range.
Sadik Armagan1625efc2021-06-10 18:24:34 +0100104 CHECK(within_percentage_tolerance(6.2f, 5.9f, 5.0f));
Matthew Sloyanfbba3642020-10-15 13:53:27 +0100105 // Just inside the upper range.
Sadik Armagan1625efc2021-06-10 18:24:34 +0100106 CHECK(within_percentage_tolerance(6.2f, 6.5f, 5.0f));
Matthew Sloyanfbba3642020-10-15 13:53:27 +0100107
108 // Larger tolerance (unlikely to be used).
Sadik Armagan1625efc2021-06-10 18:24:34 +0100109 CHECK(within_percentage_tolerance(10.0f, 9.01f, 10.0f));
110 CHECK(!within_percentage_tolerance(10.0f, 8.99f, 10.0f));
Matthew Sloyanfbba3642020-10-15 13:53:27 +0100111}
112
Sadik Armagan1625efc2021-06-10 18:24:34 +0100113TEST_CASE("FloatingPointComparisonLargePositiveNumbersSpecifiedTolerance")
Matthew Sloyanfbba3642020-10-15 13:53:27 +0100114{
115 // Just below tolerance.
Sadik Armagan1625efc2021-06-10 18:24:34 +0100116 CHECK(!within_percentage_tolerance(3.3E+38f, (3.3E+38f * 0.979f), 2.0f));
Matthew Sloyanfbba3642020-10-15 13:53:27 +0100117 // Just above tolerance.
Sadik Armagan1625efc2021-06-10 18:24:34 +0100118 CHECK(!within_percentage_tolerance(3.3E+38f, (3.3E+38f * 1.021f), 2.0f));
Matthew Sloyanfbba3642020-10-15 13:53:27 +0100119 // Just inside the lower range.
Sadik Armagan1625efc2021-06-10 18:24:34 +0100120 CHECK(within_percentage_tolerance(3.3E+38f, (3.3E+38f * 0.982f), 2.0f));
Matthew Sloyanfbba3642020-10-15 13:53:27 +0100121 // Just inside the upper range.
Sadik Armagan1625efc2021-06-10 18:24:34 +0100122 CHECK(within_percentage_tolerance(3.3E+38f, (3.3E+38f * 1.019f), 2.0f));
Matthew Sloyanfbba3642020-10-15 13:53:27 +0100123}
124
Sadik Armagan1625efc2021-06-10 18:24:34 +0100125TEST_CASE("FloatingPointComparisonLargeNegativeNumbersSpecifiedTolerance")
Matthew Sloyanfbba3642020-10-15 13:53:27 +0100126{
127 // Just below tolerance.
Sadik Armagan1625efc2021-06-10 18:24:34 +0100128 CHECK(!within_percentage_tolerance(-1.17549435e38f, (-1.17549435e38f * -1.019f), 2.0f));
Matthew Sloyanfbba3642020-10-15 13:53:27 +0100129 // Just above tolerance.
Sadik Armagan1625efc2021-06-10 18:24:34 +0100130 CHECK(!within_percentage_tolerance(-1.17549435e38f, (-1.17549435e38f * 1.021f), 2.0f));
Matthew Sloyanfbba3642020-10-15 13:53:27 +0100131 // Just inside the lower range.
Sadik Armagan1625efc2021-06-10 18:24:34 +0100132 CHECK(within_percentage_tolerance(-1.17549435e38f, -1.17549435e38f - (-1.17549435e38f * 0.0089f), 2.0f));
Matthew Sloyanfbba3642020-10-15 13:53:27 +0100133 // Just inside the upper range.
Sadik Armagan1625efc2021-06-10 18:24:34 +0100134 CHECK(within_percentage_tolerance(-1.17549435e38f, -1.17549435e38f + (-1.17549435e38f * 0.0089f), 2.0f));
Matthew Sloyanfbba3642020-10-15 13:53:27 +0100135}
136
Sadik Armagan1625efc2021-06-10 18:24:34 +0100137}