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 | { |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 39 | // Cast char values to int so that their numeric value are printed. |
| 40 | inline int make_printable(int8_t value) |
| 41 | { |
| 42 | return value; |
| 43 | } |
| 44 | |
| 45 | inline unsigned int make_printable(uint8_t value) |
| 46 | { |
| 47 | return value; |
| 48 | } |
| 49 | |
| 50 | // Everything else can be printed as its own type. |
| 51 | template <typename T> |
Moritz Pflanzer | 24a8246 | 2017-08-04 11:34:44 +0100 | [diff] [blame] | 52 | inline T make_printable(T &&value) |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 53 | { |
| 54 | return value; |
| 55 | } |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 56 | |
steniu01 | 172c58d | 2017-08-31 13:49:08 +0100 | [diff] [blame] | 57 | inline void ARM_COMPUTE_PRINT_INFO() |
| 58 | { |
| 59 | std::stringstream msg; |
| 60 | arm_compute::test::framework::Framework::get().print_test_info(msg); |
| 61 | arm_compute::test::framework::Framework::get().log_info(msg.str()); |
| 62 | arm_compute::test::framework::Framework::get().clear_test_info(); |
| 63 | } |
| 64 | |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 65 | #define ARM_COMPUTE_TEST_INFO(INFO) \ |
| 66 | { \ |
| 67 | std::stringstream info; \ |
| 68 | info << INFO; \ |
| 69 | arm_compute::test::framework::Framework::get().add_test_info(info.str()); \ |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 70 | } |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 71 | |
Moritz Pflanzer | 24a8246 | 2017-08-04 11:34:44 +0100 | [diff] [blame] | 72 | namespace detail |
| 73 | { |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 74 | #define ARM_COMPUTE_TEST_COMP_FACTORY(SEVERITY, SEVERITY_NAME, COMP, COMP_NAME, ERROR_CALL) \ |
| 75 | template <typename T, typename U> \ |
| 76 | void ARM_COMPUTE_##SEVERITY##_##COMP_NAME##_IMPL(T &&x, U &&y, const std::string &x_str, const std::string &y_str, LogLevel level) \ |
| 77 | { \ |
| 78 | if(!(x COMP y)) \ |
| 79 | { \ |
| 80 | std::stringstream msg; \ |
| 81 | msg << #SEVERITY_NAME " '" << x_str << " " #COMP " " << y_str << "' failed. [" \ |
Moritz Pflanzer | 24a8246 | 2017-08-04 11:34:44 +0100 | [diff] [blame] | 82 | << std::boolalpha << arm_compute::test::framework::make_printable(x) \ |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 83 | << " " #COMP " " \ |
Moritz Pflanzer | 24a8246 | 2017-08-04 11:34:44 +0100 | [diff] [blame] | 84 | << std::boolalpha << arm_compute::test::framework::make_printable(y) \ |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 85 | << "]\n"; \ |
| 86 | arm_compute::test::framework::Framework::get().print_test_info(msg); \ |
| 87 | ERROR_CALL \ |
| 88 | } \ |
| 89 | arm_compute::test::framework::Framework::get().clear_test_info(); \ |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 90 | } |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 91 | |
Moritz Pflanzer | 24a8246 | 2017-08-04 11:34:44 +0100 | [diff] [blame] | 92 | ARM_COMPUTE_TEST_COMP_FACTORY(EXPECT, Expectation, ==, EQUAL, arm_compute::test::framework::Framework::get().log_failed_expectation(arm_compute::test::framework::TestError(msg.str(), level));) |
| 93 | ARM_COMPUTE_TEST_COMP_FACTORY(EXPECT, Expectation, !=, NOT_EQUAL, arm_compute::test::framework::Framework::get().log_failed_expectation(arm_compute::test::framework::TestError(msg.str(), level));) |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 94 | ARM_COMPUTE_TEST_COMP_FACTORY(ASSERT, Assertion, ==, EQUAL, throw arm_compute::test::framework::TestError(msg.str(), level);) |
| 95 | ARM_COMPUTE_TEST_COMP_FACTORY(ASSERT, Assertion, !=, NOT_EQUAL, throw arm_compute::test::framework::TestError(msg.str(), level);) |
Moritz Pflanzer | 24a8246 | 2017-08-04 11:34:44 +0100 | [diff] [blame] | 96 | } // namespace detail |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 97 | |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 98 | #define ARM_COMPUTE_ASSERT_NOT_EQUAL(X, Y) \ |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 99 | arm_compute::test::framework::detail::ARM_COMPUTE_ASSERT_NOT_EQUAL_IMPL(X, Y, #X, #Y, LogLevel::ERRORS) |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 100 | |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 101 | #define ARM_COMPUTE_ASSERT_EQUAL(X, Y) \ |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 102 | arm_compute::test::framework::detail::ARM_COMPUTE_ASSERT_EQUAL_IMPL(X, Y, #X, #Y, LogLevel::ERRORS) |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 103 | |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 104 | #define ARM_COMPUTE_EXPECT_EQUAL(X, Y, LEVEL) \ |
| 105 | arm_compute::test::framework::detail::ARM_COMPUTE_EXPECT_EQUAL_IMPL(X, Y, #X, #Y, LEVEL) |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 106 | |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 107 | #define ARM_COMPUTE_EXPECT_NOT_EQUAL(X, Y, LEVEL) \ |
| 108 | arm_compute::test::framework::detail::ARM_COMPUTE_EXPECT_NOT_EQUAL_IMPL(X, Y, #X, #Y, LEVEL) |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 109 | |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 110 | #define ARM_COMPUTE_ASSERT(X) \ |
| 111 | do \ |
| 112 | { \ |
| 113 | const auto &x = X; \ |
| 114 | if(!x) \ |
| 115 | { \ |
| 116 | std::stringstream msg; \ |
| 117 | msg << "Assertion '" #X "' failed.\n"; \ |
| 118 | arm_compute::test::framework::Framework::get().print_test_info(msg); \ |
| 119 | throw arm_compute::test::framework::TestError(msg.str(), arm_compute::test::framework::LogLevel::ERRORS); \ |
| 120 | } \ |
| 121 | arm_compute::test::framework::Framework::get().clear_test_info(); \ |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 122 | } while(false) |
| 123 | |
Moritz Pflanzer | 24a8246 | 2017-08-04 11:34:44 +0100 | [diff] [blame] | 124 | #define ARM_COMPUTE_EXPECT(X, LEVEL) \ |
| 125 | do \ |
| 126 | { \ |
| 127 | const auto &x = X; \ |
| 128 | if(!x) \ |
| 129 | { \ |
| 130 | std::stringstream msg; \ |
| 131 | msg << "Expectation '" #X "' failed.\n"; \ |
| 132 | arm_compute::test::framework::Framework::get().print_test_info(msg); \ |
| 133 | arm_compute::test::framework::Framework::get().log_failed_expectation(arm_compute::test::framework::TestError(msg.str(), LEVEL)); \ |
| 134 | } \ |
| 135 | arm_compute::test::framework::Framework::get().clear_test_info(); \ |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 136 | } while(false) |
steniu01 | 172c58d | 2017-08-31 13:49:08 +0100 | [diff] [blame] | 137 | |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 138 | #define ARM_COMPUTE_ASSERT_FAIL(MSG) \ |
| 139 | do \ |
| 140 | { \ |
| 141 | std::stringstream msg; \ |
| 142 | msg << "Assertion '" << MSG << "' failed.\n"; \ |
| 143 | arm_compute::test::framework::Framework::get().print_test_info(msg); \ |
| 144 | throw arm_compute::test::framework::TestError(msg.str(), arm_compute::test::framework::LogLevel::ERRORS); \ |
| 145 | arm_compute::test::framework::Framework::get().clear_test_info(); \ |
| 146 | } while(false) |
| 147 | |
| 148 | #define ARM_COMPUTE_EXPECT_FAIL(MSG, LEVEL) \ |
| 149 | do \ |
| 150 | { \ |
| 151 | std::stringstream msg; \ |
| 152 | msg << "Expectation '" << MSG << "' failed.\n"; \ |
| 153 | arm_compute::test::framework::Framework::get().print_test_info(msg); \ |
| 154 | arm_compute::test::framework::Framework::get().log_failed_expectation(arm_compute::test::framework::TestError(msg.str(), LEVEL)); \ |
| 155 | arm_compute::test::framework::Framework::get().clear_test_info(); \ |
| 156 | } while(false) |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 157 | } // namespace framework |
| 158 | } // namespace test |
| 159 | } // namespace arm_compute |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 160 | #endif /* ARM_COMPUTE_TEST_FRAMEWORK_ASSERTS */ |