blob: 7adfa8f2f3fd7dc399791bb5af55b898b99445ed [file] [log] [blame]
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +01001/*
Francesco Petrogalli553f6952022-06-30 10:22:01 +00002 * Copyright (c) 2017-2022 Arm Limited.
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +01003 *
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
Ramy Elgammal91780022022-07-20 14:57:37 +010033#include "utils/TypePrinter.h"
34
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010035namespace arm_compute
36{
Moritz Pflanzerc7d15032017-07-18 16:21:16 +010037namespace test
38{
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010039namespace framework
40{
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010041// Cast char values to int so that their numeric value are printed.
42inline int make_printable(int8_t value)
43{
44 return value;
45}
46
Ramy Elgammal91780022022-07-20 14:57:37 +010047inline std::string make_printable(const arm_compute::WeightFormat wf)
Francesco Petrogalli553f6952022-06-30 10:22:01 +000048{
Ramy Elgammal91780022022-07-20 14:57:37 +010049 return arm_compute::to_string(wf);
Francesco Petrogalli553f6952022-06-30 10:22:01 +000050}
51
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010052inline unsigned int make_printable(uint8_t value)
53{
54 return value;
55}
56
57// Everything else can be printed as its own type.
58template <typename T>
Moritz Pflanzer24a82462017-08-04 11:34:44 +010059inline T make_printable(T &&value)
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010060{
61 return value;
62}
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010063
steniu01172c58d2017-08-31 13:49:08 +010064inline void ARM_COMPUTE_PRINT_INFO()
65{
66 std::stringstream msg;
67 arm_compute::test::framework::Framework::get().print_test_info(msg);
68 arm_compute::test::framework::Framework::get().log_info(msg.str());
69 arm_compute::test::framework::Framework::get().clear_test_info();
70}
71
Moritz Pflanzerc7d15032017-07-18 16:21:16 +010072#define ARM_COMPUTE_TEST_INFO(INFO) \
73 { \
74 std::stringstream info; \
75 info << INFO; \
76 arm_compute::test::framework::Framework::get().add_test_info(info.str()); \
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010077 }
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010078
Moritz Pflanzer24a82462017-08-04 11:34:44 +010079namespace detail
80{
Moritz Pflanzer2ac50402017-07-24 15:52:54 +010081#define ARM_COMPUTE_TEST_COMP_FACTORY(SEVERITY, SEVERITY_NAME, COMP, COMP_NAME, ERROR_CALL) \
82 template <typename T, typename U> \
83 void ARM_COMPUTE_##SEVERITY##_##COMP_NAME##_IMPL(T &&x, U &&y, const std::string &x_str, const std::string &y_str, LogLevel level) \
84 { \
85 if(!(x COMP y)) \
86 { \
87 std::stringstream msg; \
88 msg << #SEVERITY_NAME " '" << x_str << " " #COMP " " << y_str << "' failed. [" \
Moritz Pflanzer24a82462017-08-04 11:34:44 +010089 << std::boolalpha << arm_compute::test::framework::make_printable(x) \
Moritz Pflanzer2ac50402017-07-24 15:52:54 +010090 << " " #COMP " " \
Moritz Pflanzer24a82462017-08-04 11:34:44 +010091 << std::boolalpha << arm_compute::test::framework::make_printable(y) \
Moritz Pflanzer2ac50402017-07-24 15:52:54 +010092 << "]\n"; \
93 arm_compute::test::framework::Framework::get().print_test_info(msg); \
94 ERROR_CALL \
95 } \
96 arm_compute::test::framework::Framework::get().clear_test_info(); \
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010097 }
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010098
Moritz Pflanzer24a82462017-08-04 11:34:44 +010099ARM_COMPUTE_TEST_COMP_FACTORY(EXPECT, Expectation, ==, EQUAL, arm_compute::test::framework::Framework::get().log_failed_expectation(arm_compute::test::framework::TestError(msg.str(), level));)
100ARM_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 Pflanzer2ac50402017-07-24 15:52:54 +0100101ARM_COMPUTE_TEST_COMP_FACTORY(ASSERT, Assertion, ==, EQUAL, throw arm_compute::test::framework::TestError(msg.str(), level);)
102ARM_COMPUTE_TEST_COMP_FACTORY(ASSERT, Assertion, !=, NOT_EQUAL, throw arm_compute::test::framework::TestError(msg.str(), level);)
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100103} // namespace detail
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +0100104
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100105#define ARM_COMPUTE_ASSERT_NOT_EQUAL(X, Y) \
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100106 arm_compute::test::framework::detail::ARM_COMPUTE_ASSERT_NOT_EQUAL_IMPL(X, Y, #X, #Y, LogLevel::ERRORS)
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +0100107
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100108#define ARM_COMPUTE_ASSERT_EQUAL(X, Y) \
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100109 arm_compute::test::framework::detail::ARM_COMPUTE_ASSERT_EQUAL_IMPL(X, Y, #X, #Y, LogLevel::ERRORS)
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100110
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100111#define ARM_COMPUTE_EXPECT_EQUAL(X, Y, LEVEL) \
112 arm_compute::test::framework::detail::ARM_COMPUTE_EXPECT_EQUAL_IMPL(X, Y, #X, #Y, LEVEL)
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100113
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100114#define ARM_COMPUTE_EXPECT_NOT_EQUAL(X, Y, LEVEL) \
115 arm_compute::test::framework::detail::ARM_COMPUTE_EXPECT_NOT_EQUAL_IMPL(X, Y, #X, #Y, LEVEL)
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100116
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100117#define ARM_COMPUTE_ASSERT(X) \
118 do \
119 { \
120 const auto &x = X; \
121 if(!x) \
122 { \
123 std::stringstream msg; \
124 msg << "Assertion '" #X "' failed.\n"; \
125 arm_compute::test::framework::Framework::get().print_test_info(msg); \
126 throw arm_compute::test::framework::TestError(msg.str(), arm_compute::test::framework::LogLevel::ERRORS); \
127 } \
128 arm_compute::test::framework::Framework::get().clear_test_info(); \
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +0100129 } while(false)
130
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100131#define ARM_COMPUTE_EXPECT(X, LEVEL) \
132 do \
133 { \
134 const auto &x = X; \
135 if(!x) \
136 { \
137 std::stringstream msg; \
138 msg << "Expectation '" #X "' failed.\n"; \
139 arm_compute::test::framework::Framework::get().print_test_info(msg); \
140 arm_compute::test::framework::Framework::get().log_failed_expectation(arm_compute::test::framework::TestError(msg.str(), LEVEL)); \
141 } \
142 arm_compute::test::framework::Framework::get().clear_test_info(); \
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +0100143 } while(false)
steniu01172c58d2017-08-31 13:49:08 +0100144
Georgios Pinitas0b5af9f2020-06-19 23:22:08 +0100145#define ARM_COMPUTE_EXPECT_NO_THROW(X, LEVEL) \
146 do \
147 { \
148 try \
149 { \
150 const auto &x = X; \
151 (void)x; \
152 } \
153 catch(...) \
154 { \
155 std::stringstream msg; \
156 msg << "Expectation '" #X "' to not throw failed.\n"; \
157 arm_compute::test::framework::Framework::get().print_test_info(msg); \
158 arm_compute::test::framework::Framework::get().log_failed_expectation(arm_compute::test::framework::TestError(msg.str(), LEVEL)); \
159 } \
160 arm_compute::test::framework::Framework::get().clear_test_info(); \
161 } while(false)
162
Michele Di Giorgiod340eb22021-04-08 11:05:23 +0100163#if defined(ARM_COMPUTE_ASSERTS_ENABLED)
Georgios Pinitas0b5af9f2020-06-19 23:22:08 +0100164#define ARM_COMPUTE_EXPECT_THROW(X, LEVEL) \
165 do \
166 { \
167 bool exception_caught = false; \
168 try \
169 { \
170 const auto &x = X; \
171 (void)x; \
172 } \
173 catch(...) \
174 { \
175 exception_caught = true; \
176 } \
177 if(!exception_caught) \
178 { \
179 std::stringstream msg; \
180 msg << "Expectation '" #X "' to throw failed.\n"; \
181 arm_compute::test::framework::Framework::get().print_test_info(msg); \
182 arm_compute::test::framework::Framework::get().log_failed_expectation(arm_compute::test::framework::TestError(msg.str(), LEVEL)); \
183 } \
184 arm_compute::test::framework::Framework::get().clear_test_info(); \
185 } while(false)
Michele Di Giorgiod340eb22021-04-08 11:05:23 +0100186#else // defined(ARM_COMPUTE_ASSERTS_ENABLED)
187#define ARM_COMPUTE_EXPECT_THROW(X, LEVEL) \
188 do \
189 { \
190 std::stringstream msg; \
191 msg << "'" #X "' Skipped: asserts disabled, cannot throw\n"; \
192 arm_compute::test::framework::Framework::get().print_test_info(msg); \
193 arm_compute::test::framework::Framework::get().log_info(msg.str()); \
194 arm_compute::test::framework::Framework::get().clear_test_info(); \
195 } while(false)
196#endif // defined(ARM_COMPUTE_ASSERTS_ENABLED)
Georgios Pinitas0b5af9f2020-06-19 23:22:08 +0100197
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +0100198#define ARM_COMPUTE_ASSERT_FAIL(MSG) \
199 do \
200 { \
201 std::stringstream msg; \
202 msg << "Assertion '" << MSG << "' failed.\n"; \
203 arm_compute::test::framework::Framework::get().print_test_info(msg); \
204 throw arm_compute::test::framework::TestError(msg.str(), arm_compute::test::framework::LogLevel::ERRORS); \
205 arm_compute::test::framework::Framework::get().clear_test_info(); \
206 } while(false)
207
208#define ARM_COMPUTE_EXPECT_FAIL(MSG, LEVEL) \
209 do \
210 { \
211 std::stringstream msg; \
212 msg << "Expectation '" << MSG << "' failed.\n"; \
213 arm_compute::test::framework::Framework::get().print_test_info(msg); \
214 arm_compute::test::framework::Framework::get().log_failed_expectation(arm_compute::test::framework::TestError(msg.str(), LEVEL)); \
215 arm_compute::test::framework::Framework::get().clear_test_info(); \
216 } while(false)
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100217} // namespace framework
218} // namespace test
219} // namespace arm_compute
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +0100220#endif /* ARM_COMPUTE_TEST_FRAMEWORK_ASSERTS */