Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #ifndef ARM_COMPUTE_TEST_FRAMEWORK_ASSERTS |
| 25 | #define ARM_COMPUTE_TEST_FRAMEWORK_ASSERTS |
| 26 | |
| 27 | #include "Exceptions.h" |
| 28 | #include "Framework.h" |
| 29 | |
| 30 | #include <sstream> |
| 31 | #include <type_traits> |
| 32 | |
| 33 | namespace arm_compute |
| 34 | { |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 35 | namespace test |
| 36 | { |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 37 | namespace framework |
| 38 | { |
| 39 | namespace detail |
| 40 | { |
| 41 | // Cast char values to int so that their numeric value are printed. |
| 42 | inline int make_printable(int8_t value) |
| 43 | { |
| 44 | return value; |
| 45 | } |
| 46 | |
| 47 | inline unsigned int make_printable(uint8_t value) |
| 48 | { |
| 49 | return value; |
| 50 | } |
| 51 | |
| 52 | // Everything else can be printed as its own type. |
| 53 | template <typename T> |
| 54 | inline T &&make_printable(T &&value) |
| 55 | { |
| 56 | return value; |
| 57 | } |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 58 | |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 59 | #define ARM_COMPUTE_TEST_INFO(INFO) \ |
| 60 | { \ |
| 61 | std::stringstream info; \ |
| 62 | info << INFO; \ |
| 63 | arm_compute::test::framework::Framework::get().add_test_info(info.str()); \ |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 64 | } |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 65 | |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 66 | #define ARM_COMPUTE_TEST_COMP_FACTORY(SEVERITY, SEVERITY_NAME, COMP, COMP_NAME, ERROR_CALL) \ |
| 67 | template <typename T, typename U> \ |
| 68 | void ARM_COMPUTE_##SEVERITY##_##COMP_NAME##_IMPL(T &&x, U &&y, const std::string &x_str, const std::string &y_str) \ |
| 69 | { \ |
| 70 | if(!(x COMP y)) \ |
| 71 | { \ |
| 72 | std::stringstream msg; \ |
| 73 | msg << #SEVERITY_NAME " '" << x_str << " " #COMP " " << y_str << "' failed. [" \ |
| 74 | << std::boolalpha << arm_compute::test::framework::detail::make_printable(x) \ |
| 75 | << " " #COMP " " \ |
| 76 | << std::boolalpha << arm_compute::test::framework::detail::make_printable(y) \ |
| 77 | << "]\n"; \ |
| 78 | arm_compute::test::framework::Framework::get().print_test_info(msg); \ |
| 79 | ERROR_CALL \ |
| 80 | } \ |
| 81 | arm_compute::test::framework::Framework::get().clear_test_info(); \ |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 82 | } |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 83 | |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 84 | ARM_COMPUTE_TEST_COMP_FACTORY(EXPECT, Expectation, ==, EQUAL, arm_compute::test::framework::Framework::get().log_failed_expectation(msg.str());) |
| 85 | ARM_COMPUTE_TEST_COMP_FACTORY(EXPECT, Expectation, !=, NOT_EQUAL, arm_compute::test::framework::Framework::get().log_failed_expectation(msg.str());) |
| 86 | ARM_COMPUTE_TEST_COMP_FACTORY(ASSERT, Assertion, ==, EQUAL, throw arm_compute::test::framework::TestError(msg.str());) |
| 87 | ARM_COMPUTE_TEST_COMP_FACTORY(ASSERT, Assertion, !=, NOT_EQUAL, throw arm_compute::test::framework::TestError(msg.str());) |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 88 | |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 89 | #define ARM_COMPUTE_ASSERT_NOT_EQUAL(X, Y) \ |
| 90 | arm_compute::test::framework::detail::ARM_COMPUTE_ASSERT_NOT_EQUAL_IMPL(X, Y, #X, #Y) |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 91 | |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 92 | #define ARM_COMPUTE_ASSERT_EQUAL(X, Y) \ |
| 93 | arm_compute::test::framework::detail::ARM_COMPUTE_ASSERT_EQUAL_IMPL(X, Y, #X, #Y) |
| 94 | |
| 95 | #define ARM_COMPUTE_EXPECT_EQUAL(X, Y) \ |
| 96 | arm_compute::test::framework::detail::ARM_COMPUTE_EXPECT_EQUAL_IMPL(X, Y, #X, #Y) |
| 97 | |
| 98 | #define ARM_COMPUTE_EXPECT_NOT_EQUAL(X, Y) \ |
| 99 | arm_compute::test::framework::detail::ARM_COMPUTE_EXPECT_NOT_EQUAL_IMPL(X, Y, #X, #Y) |
| 100 | |
| 101 | #define ARM_COMPUTE_ASSERT(X) \ |
| 102 | do \ |
| 103 | { \ |
| 104 | const auto &x = X; \ |
| 105 | if(!x) \ |
| 106 | { \ |
| 107 | std::stringstream msg; \ |
| 108 | msg << "Assertion '" #X "' failed.\n"; \ |
| 109 | arm_compute::test::framework::Framework::get().print_test_info(msg); \ |
| 110 | throw arm_compute::test::framework::TestError(msg.str()); \ |
| 111 | } \ |
| 112 | arm_compute::test::framework::Framework::get().clear_test_info(); \ |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 113 | } while(false) |
| 114 | |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 115 | #define ARM_COMPUTE_EXPECT(X) \ |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 116 | do \ |
| 117 | { \ |
| 118 | const auto &x = X; \ |
| 119 | if(!x) \ |
| 120 | { \ |
| 121 | std::stringstream msg; \ |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 122 | msg << "Expectation '" #X "' failed.\n"; \ |
| 123 | arm_compute::test::framework::Framework::get().print_test_info(msg); \ |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 124 | arm_compute::test::framework::Framework::get().log_failed_expectation(msg.str()); \ |
| 125 | } \ |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 126 | arm_compute::test::framework::Framework::get().clear_test_info(); \ |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 127 | } while(false) |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 128 | } // namespace detail |
| 129 | } // namespace framework |
| 130 | } // namespace test |
| 131 | } // namespace arm_compute |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 132 | #endif /* ARM_COMPUTE_TEST_FRAMEWORK_ASSERTS */ |