blob: 4fd82abb97bfd2bbb5f26a05e74595a1ed21917b [file] [log] [blame]
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +01001/*
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
33namespace arm_compute
34{
Moritz Pflanzerc7d15032017-07-18 16:21:16 +010035namespace test
36{
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010037namespace framework
38{
39namespace detail
40{
41// 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
47inline unsigned int make_printable(uint8_t value)
48{
49 return value;
50}
51
52// Everything else can be printed as its own type.
53template <typename T>
54inline T &&make_printable(T &&value)
55{
56 return value;
57}
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010058
Moritz Pflanzerc7d15032017-07-18 16:21:16 +010059#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 Pflanzerb7c2a992017-07-18 14:37:35 +010064 }
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010065
Moritz Pflanzer2ac50402017-07-24 15:52:54 +010066#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, LogLevel level) \
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 Pflanzerb7c2a992017-07-18 14:37:35 +010082 }
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010083
Moritz Pflanzer2ac50402017-07-24 15:52:54 +010084ARM_COMPUTE_TEST_COMP_FACTORY(EXPECT, Expectation, ==, EQUAL, arm_compute::test::framework::Framework::get().log_failed_expectation(msg.str(), level);)
85ARM_COMPUTE_TEST_COMP_FACTORY(EXPECT, Expectation, !=, NOT_EQUAL, arm_compute::test::framework::Framework::get().log_failed_expectation(msg.str(), level);)
86ARM_COMPUTE_TEST_COMP_FACTORY(ASSERT, Assertion, ==, EQUAL, throw arm_compute::test::framework::TestError(msg.str(), level);)
87ARM_COMPUTE_TEST_COMP_FACTORY(ASSERT, Assertion, !=, NOT_EQUAL, throw arm_compute::test::framework::TestError(msg.str(), level);)
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010088
Moritz Pflanzerc7d15032017-07-18 16:21:16 +010089#define ARM_COMPUTE_ASSERT_NOT_EQUAL(X, Y) \
Moritz Pflanzer2ac50402017-07-24 15:52:54 +010090 arm_compute::test::framework::detail::ARM_COMPUTE_ASSERT_NOT_EQUAL_IMPL(X, Y, #X, #Y, LogLevel::ERRORS)
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010091
Moritz Pflanzerc7d15032017-07-18 16:21:16 +010092#define ARM_COMPUTE_ASSERT_EQUAL(X, Y) \
Moritz Pflanzer2ac50402017-07-24 15:52:54 +010093 arm_compute::test::framework::detail::ARM_COMPUTE_ASSERT_EQUAL_IMPL(X, Y, #X, #Y, LogLevel::ERRORS)
Moritz Pflanzerc7d15032017-07-18 16:21:16 +010094
Moritz Pflanzer2ac50402017-07-24 15:52:54 +010095#define ARM_COMPUTE_EXPECT_EQUAL(X, Y, LEVEL) \
96 arm_compute::test::framework::detail::ARM_COMPUTE_EXPECT_EQUAL_IMPL(X, Y, #X, #Y, LEVEL)
Moritz Pflanzerc7d15032017-07-18 16:21:16 +010097
Moritz Pflanzer2ac50402017-07-24 15:52:54 +010098#define ARM_COMPUTE_EXPECT_NOT_EQUAL(X, Y, LEVEL) \
99 arm_compute::test::framework::detail::ARM_COMPUTE_EXPECT_NOT_EQUAL_IMPL(X, Y, #X, #Y, LEVEL)
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100100
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100101#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(), arm_compute::test::framework::LogLevel::ERRORS); \
111 } \
112 arm_compute::test::framework::Framework::get().clear_test_info(); \
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +0100113 } while(false)
114
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100115#define ARM_COMPUTE_EXPECT(X, LEVEL) \
116 do \
117 { \
118 const auto &x = X; \
119 if(!x) \
120 { \
121 std::stringstream msg; \
122 msg << "Expectation '" #X "' failed.\n"; \
123 arm_compute::test::framework::Framework::get().print_test_info(msg); \
124 arm_compute::test::framework::Framework::get().log_failed_expectation(msg.str(), LEVEL); \
125 } \
126 arm_compute::test::framework::Framework::get().clear_test_info(); \
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +0100127 } while(false)
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100128} // namespace detail
129} // namespace framework
130} // namespace test
131} // namespace arm_compute
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +0100132#endif /* ARM_COMPUTE_TEST_FRAMEWORK_ASSERTS */