blob: b545a9ebbadabcb89981b17a74a5a8c70e4c90ee [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{
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010039// Cast char values to int so that their numeric value are printed.
40inline int make_printable(int8_t value)
41{
42 return value;
43}
44
45inline unsigned int make_printable(uint8_t value)
46{
47 return value;
48}
49
50// Everything else can be printed as its own type.
51template <typename T>
Moritz Pflanzer24a82462017-08-04 11:34:44 +010052inline T make_printable(T &&value)
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010053{
54 return value;
55}
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010056
Moritz Pflanzerc7d15032017-07-18 16:21:16 +010057#define ARM_COMPUTE_TEST_INFO(INFO) \
58 { \
59 std::stringstream info; \
60 info << INFO; \
61 arm_compute::test::framework::Framework::get().add_test_info(info.str()); \
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010062 }
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010063
Moritz Pflanzer24a82462017-08-04 11:34:44 +010064namespace detail
65{
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. [" \
Moritz Pflanzer24a82462017-08-04 11:34:44 +010074 << std::boolalpha << arm_compute::test::framework::make_printable(x) \
Moritz Pflanzer2ac50402017-07-24 15:52:54 +010075 << " " #COMP " " \
Moritz Pflanzer24a82462017-08-04 11:34:44 +010076 << std::boolalpha << arm_compute::test::framework::make_printable(y) \
Moritz Pflanzer2ac50402017-07-24 15:52:54 +010077 << "]\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 Pflanzer24a82462017-08-04 11:34:44 +010084ARM_COMPUTE_TEST_COMP_FACTORY(EXPECT, Expectation, ==, EQUAL, arm_compute::test::framework::Framework::get().log_failed_expectation(arm_compute::test::framework::TestError(msg.str(), level));)
85ARM_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 +010086ARM_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 Pflanzer24a82462017-08-04 11:34:44 +010088} // namespace detail
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010089
Moritz Pflanzerc7d15032017-07-18 16:21:16 +010090#define ARM_COMPUTE_ASSERT_NOT_EQUAL(X, Y) \
Moritz Pflanzer2ac50402017-07-24 15:52:54 +010091 arm_compute::test::framework::detail::ARM_COMPUTE_ASSERT_NOT_EQUAL_IMPL(X, Y, #X, #Y, LogLevel::ERRORS)
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010092
Moritz Pflanzerc7d15032017-07-18 16:21:16 +010093#define ARM_COMPUTE_ASSERT_EQUAL(X, Y) \
Moritz Pflanzer2ac50402017-07-24 15:52:54 +010094 arm_compute::test::framework::detail::ARM_COMPUTE_ASSERT_EQUAL_IMPL(X, Y, #X, #Y, LogLevel::ERRORS)
Moritz Pflanzerc7d15032017-07-18 16:21:16 +010095
Moritz Pflanzer2ac50402017-07-24 15:52:54 +010096#define ARM_COMPUTE_EXPECT_EQUAL(X, Y, LEVEL) \
97 arm_compute::test::framework::detail::ARM_COMPUTE_EXPECT_EQUAL_IMPL(X, Y, #X, #Y, LEVEL)
Moritz Pflanzerc7d15032017-07-18 16:21:16 +010098
Moritz Pflanzer2ac50402017-07-24 15:52:54 +010099#define ARM_COMPUTE_EXPECT_NOT_EQUAL(X, Y, LEVEL) \
100 arm_compute::test::framework::detail::ARM_COMPUTE_EXPECT_NOT_EQUAL_IMPL(X, Y, #X, #Y, LEVEL)
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100101
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100102#define ARM_COMPUTE_ASSERT(X) \
103 do \
104 { \
105 const auto &x = X; \
106 if(!x) \
107 { \
108 std::stringstream msg; \
109 msg << "Assertion '" #X "' failed.\n"; \
110 arm_compute::test::framework::Framework::get().print_test_info(msg); \
111 throw arm_compute::test::framework::TestError(msg.str(), arm_compute::test::framework::LogLevel::ERRORS); \
112 } \
113 arm_compute::test::framework::Framework::get().clear_test_info(); \
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +0100114 } while(false)
115
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100116#define ARM_COMPUTE_EXPECT(X, LEVEL) \
117 do \
118 { \
119 const auto &x = X; \
120 if(!x) \
121 { \
122 std::stringstream msg; \
123 msg << "Expectation '" #X "' failed.\n"; \
124 arm_compute::test::framework::Framework::get().print_test_info(msg); \
125 arm_compute::test::framework::Framework::get().log_failed_expectation(arm_compute::test::framework::TestError(msg.str(), LEVEL)); \
126 } \
127 arm_compute::test::framework::Framework::get().clear_test_info(); \
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +0100128 } while(false)
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100129} // namespace framework
130} // namespace test
131} // namespace arm_compute
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +0100132#endif /* ARM_COMPUTE_TEST_FRAMEWORK_ASSERTS */