jaulib v1.3.0
Jau Support Library (C++, Java, ..)
catch2_ext.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2020 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 *************************************************************************
25 *
26 * Distributed under the Boost Software License, Version 1.0.
27 * (See accompanying file LICENSE_1_0.txt or copy at
28 * https://www.boost.org/LICENSE_1_0.txt)
29 *
30 * SPDX-License-Identifier: BSL-1.0
31 */
32
33#ifndef CATCH2_EXT_H
34#define CATCH2_EXT_H
35
36#include <catch2/catch_amalgamated.hpp>
37#include <jau/float_math.hpp>
38
39// namespace Catch {
40///////////////////////////////////////////////////////////////////////////////
41#define INTERNAL_CATCH_TEST_M( msg, macroName, resultDisposition, ... ) \
42 do { \
43 /* The expression should not be evaluated, but warnings should hopefully be checked */ \
44 CATCH_INTERNAL_IGNORE_BUT_WARN(__VA_ARGS__); \
45 std::string s1( macroName##_catch_sr ); \
46 s1.append(msg); \
47 s1.append(": "); \
48 Catch::AssertionHandler catchAssertionHandler( s1, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__), resultDisposition ); \
49 INTERNAL_CATCH_TRY { \
50 CATCH_INTERNAL_START_WARNINGS_SUPPRESSION \
51 CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS \
52 catchAssertionHandler.handleExpr( Catch::Decomposer() <= __VA_ARGS__ ); \
53 CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION \
54 } INTERNAL_CATCH_CATCH( catchAssertionHandler ) \
55 INTERNAL_CATCH_REACT( catchAssertionHandler ) \
56 } while( (void)0, (false) && static_cast<bool>( !!(__VA_ARGS__) ) )
57
58 #define REQUIRE_MSG(MSG, ... ) INTERNAL_CATCH_TEST_M( MSG, "REQUIRE: ", Catch::ResultDisposition::Normal, __VA_ARGS__ )
59
60#define INTERNAL_CHECK_THAT_M( msg, macroName, matcher, resultDisposition, arg ) \
61 do { \
62 std::string s1( macroName##_catch_sr ); \
63 s1.append(": "); \
64 s1.append(msg); \
65 s1.append(": "); \
66 Catch::AssertionHandler catchAssertionHandler( s1, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(arg) ", " CATCH_INTERNAL_STRINGIFY(matcher), resultDisposition ); \
67 INTERNAL_CATCH_TRY { \
68 catchAssertionHandler.handleExpr( Catch::makeMatchExpr( arg, matcher, #matcher##_catch_sr ) ); \
69 } INTERNAL_CATCH_CATCH( catchAssertionHandler ) \
70 INTERNAL_CATCH_REACT( catchAssertionHandler ) \
71 } while( false )
72
73 #define REQUIRE_THAT_MSG(MSG, arg, matcher ) INTERNAL_CHECK_THAT_M( MSG, "REQUIRE_THAT", matcher, Catch::ResultDisposition::Normal, arg )
74
75 #define INFO_STR( msg ) INTERNAL_CATCH_INFO( "INFO", static_cast<std::string>(msg) )
76
77 #define REQUIRE_EPSI(a,b) INTERNAL_CATCH_TEST( "REQUIRE: ", Catch::ResultDisposition::Normal, jau::equals(a, b))
78 #define REQUIRE_EPSI_MSG(m,a,b) INTERNAL_CATCH_TEST_M( m, "REQUIRE: ", Catch::ResultDisposition::Normal, jau::equals(a, b))
79
80 #define REQUIRE_DIFF(a,b,d) INTERNAL_CATCH_TEST( "REQUIRE: ", Catch::ResultDisposition::Normal, jau::equals(a, b, 1, d))
81 #define REQUIRE_DIFF_MSG(m,a,b,d) INTERNAL_CATCH_TEST_M( m, "REQUIRE: ", Catch::ResultDisposition::Normal, jau::equals(a, b, 1, d))
82
83
84 #define COMPARE_SARRAYS(lhs, rhs) compareStdArrays(Catch::getResultCapture().getCurrentTestName(), __LINE__, lhs, rhs)
85 #define COMPARE_SARRAYS_EPS(lhs, rhs, eps) compareStdArrays(Catch::getResultCapture().getCurrentTestName(), __LINE__, lhs, rhs, eps)
86
87 template < typename T, size_t N >
88 void compareStdArrays(const std::string & test, unsigned line, const std::array<T, N>& lhs, const std::array<T, N>& rhs) {
89 std::vector<T> lv(lhs.begin(), lhs.end());
90 std::vector<T> rv(rhs.begin(), rhs.end());
91 REQUIRE_MSG("["+test+"] at line "+std::to_string(line), lv == rv);
92 }
93 template < typename T, size_t N >
94 void compareStdArrays(const std::string & test, unsigned line, const std::array<T, N>& lhs, const std::array<T, N>& rhs, const T epsilon) {
95 const std::string m = "["+test+"] at line "+std::to_string(line)+", element ";
96 for(size_t i=0; i<N; ++i) {
97 REQUIRE_THAT_MSG(m+std::to_string(i)+"/"+std::to_string(N-1), lhs[i], Catch::Matchers::WithinAbs(rhs[i], epsilon) );
98 }
99 }
100
101 #define COMPARE_NARRAYS(lhs, rhs, len) compareNativeArrays(Catch::getResultCapture().getCurrentTestName(), __LINE__, lhs, rhs, len)
102 #define COMPARE_NARRAYS_EPS(lhs, rhs, len, eps) compareNativeArrays(Catch::getResultCapture().getCurrentTestName(), __LINE__, lhs, rhs, len, eps)
103
104 template < typename T >
105 void compareNativeArrays(const std::string & test, unsigned line, const T lhs[], const T rhs[], const size_t len) {
106 const std::string m = "["+test+"] at line "+std::to_string(line)+", element ";
107 for(size_t i=0; i<len; ++i) {
108 REQUIRE_MSG(m+std::to_string(i)+"/"+std::to_string(len-1), rhs[i] == lhs[i] );
109 }
110 }
111 template < typename T >
112 void compareNativeArrays(const std::string & test, unsigned line, const T lhs[], const T rhs[], const size_t len, const T epsilon) {
113 const std::string m = "["+test+"] at line "+std::to_string(line)+", element ";
114 for(size_t i=0; i<len; ++i) {
115 REQUIRE_THAT_MSG(m+std::to_string(i)+"/"+std::to_string(len-1), lhs[i], Catch::Matchers::WithinAbs(rhs[i], epsilon) );
116 }
117 }
118// }
119
120#if !defined(CATCH_CONFIG_MAIN)
122#endif /* !defined(CATCH_CONFIG_MAIN) */
123
124#endif // CATCH2_EXT_H
125
#define REQUIRE_MSG(MSG,...)
Definition: catch2_ext.hpp:58
void compareNativeArrays(const std::string &test, unsigned line, const T lhs[], const T rhs[], const size_t len)
Definition: catch2_ext.hpp:105
void compareStdArrays(const std::string &test, unsigned line, const std::array< T, N > &lhs, const std::array< T, N > &rhs)
Definition: catch2_ext.hpp:88
#define REQUIRE_THAT_MSG(MSG, arg, matcher)
Definition: catch2_ext.hpp:73
std::string to_string(const alphabet &v) noexcept
Definition: base_codec.hpp:97