1 /*
2 * Copyright (c) 2017-2022 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 #include "utils/TypePrinter.h"
34
35 namespace arm_compute
36 {
37 namespace test
38 {
39 namespace framework
40 {
41 // Cast char values to int so that their numeric value are printed.
make_printable(int8_t value)42 inline int make_printable(int8_t value)
43 {
44 return value;
45 }
46
make_printable(const arm_compute::WeightFormat wf)47 inline std::string make_printable(const arm_compute::WeightFormat wf)
48 {
49 return arm_compute::to_string(wf);
50 }
51
make_printable(uint8_t value)52 inline unsigned int make_printable(uint8_t value)
53 {
54 return value;
55 }
56
57 // Everything else can be printed as its own type.
58 template <typename T>
make_printable(T && value)59 inline T make_printable(T &&value)
60 {
61 return value;
62 }
63
ARM_COMPUTE_PRINT_INFO()64 inline 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
72 #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()); \
77 }
78
79 namespace detail
80 {
81 #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. [" \
89 << std::boolalpha << arm_compute::test::framework::make_printable(x) \
90 << " " #COMP " " \
91 << std::boolalpha << arm_compute::test::framework::make_printable(y) \
92 << "]\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(); \
97 }
98
99 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));)
100 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));)
101 ARM_COMPUTE_TEST_COMP_FACTORY(ASSERT, Assertion, ==, EQUAL, throw arm_compute::test::framework::TestError(msg.str(), level);)
102 ARM_COMPUTE_TEST_COMP_FACTORY(ASSERT, Assertion, !=, NOT_EQUAL, throw arm_compute::test::framework::TestError(msg.str(), level);)
103 } // namespace detail
104
105 #define ARM_COMPUTE_ASSERT_NOT_EQUAL(X, Y) \
106 arm_compute::test::framework::detail::ARM_COMPUTE_ASSERT_NOT_EQUAL_IMPL(X, Y, #X, #Y, LogLevel::ERRORS)
107
108 #define ARM_COMPUTE_ASSERT_EQUAL(X, Y) \
109 arm_compute::test::framework::detail::ARM_COMPUTE_ASSERT_EQUAL_IMPL(X, Y, #X, #Y, LogLevel::ERRORS)
110
111 #define ARM_COMPUTE_EXPECT_EQUAL(X, Y, LEVEL) \
112 arm_compute::test::framework::detail::ARM_COMPUTE_EXPECT_EQUAL_IMPL(X, Y, #X, #Y, LEVEL)
113
114 #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)
116
117 #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(); \
129 } while(false)
130
131 #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(); \
143 } while(false)
144
145 #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
163 #if defined(ARM_COMPUTE_ASSERTS_ENABLED)
164 #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)
186 #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)
197
198 #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)
217 } // namespace framework
218 } // namespace test
219 } // namespace arm_compute
220 #endif /* ARM_COMPUTE_TEST_FRAMEWORK_ASSERTS */
221