KiCad PCB EDA Suite
Loading...
Searching...
No Matches
unit_test_utils.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#ifndef UNIT_TEST_UTILS__H
25#define UNIT_TEST_UTILS__H
26
27#define BOOST_NO_AUTO_PTR
28
29#include <boost/test/unit_test.hpp>
30#include <turtle/mock.hpp>
31
33
34#include <cstdint>
35#include <functional>
36#include <optional>
37#include <set>
38#include <vector>
39
40#include <wx/gdicmn.h>
41#include <wx/string.h>
42
43// C++20 deleted operator<<(ostream&, const wchar_t*) (P1423R3), which breaks wxString streaming
44// via implicit wchar_t* conversion in Boost.Test's lazy_ostream (used by BOOST_TEST_CONTEXT,
45// BOOST_TEST_MESSAGE, BOOST_CHECK_MESSAGE, and BOOST_FAIL).
46inline std::ostream& operator<<( std::ostream& os, const wxString& aStr )
47{
48#if wxUSE_UNICODE
49 os << aStr.ToUTF8().data();
50#else
51 os << aStr.c_str();
52#endif
53 return os;
54}
55
56
57template<class T>
59{
60 PRINTABLE_OPT( const std::optional<T>& aOpt ) : m_Opt( aOpt ){};
61 PRINTABLE_OPT( const T& aVal ) : m_Opt( aVal ){};
62
63 std::optional<T> m_Opt;
64};
65
66
70#define KI_CHECK_OPT_EQUAL( lhs, rhs ) \
71 BOOST_CHECK_EQUAL( PRINTABLE_OPT( lhs ), PRINTABLE_OPT( rhs ) )
72
73
74template <class T>
75inline std::ostream& operator<<( std::ostream& aOs, const PRINTABLE_OPT<T>& aOptional )
76{
77 if( aOptional.m_Opt.has_value() )
78 aOs << *aOptional.m_Opt;
79 else
80 aOs << "nullopt";
81
82 return aOs;
83}
84
85
86template <class L, class R>
87inline bool operator==( const PRINTABLE_OPT<L>& aLhs, const PRINTABLE_OPT<R>& aRhs )
88{
89 if( !aLhs.m_Opt.has_value() && !aRhs.m_Opt.has_value() )
90 return true; // both nullopt
91
92 return aLhs.m_Opt.has_value() && aRhs.m_Opt.has_value() && *aLhs.m_Opt == *aRhs.m_Opt;
93}
94
95
96template <class L, class R>
97inline bool operator!=( const PRINTABLE_OPT<L>& aLhs, const PRINTABLE_OPT<R>& aRhs )
98{
99 return !( aLhs == aRhs );
100}
101
102
103// boost_test_print_type has to be in the same namespace as the printed type
104namespace std
105{
106
110template <typename T>
111std::ostream& boost_test_print_type( std::ostream& os, std::vector<T> const& aVec )
112{
113 os << "std::vector size " << aVec.size() << " [";
114
115 for( const auto& i : aVec )
116 {
117 os << "\n " << i;
118 }
119
120 os << "]";
121 return os;
122}
123
127template <typename K, typename V>
128std::ostream& boost_test_print_type( std::ostream& os, std::map<K, V> const& aMap )
129{
130 os << "std::map size " << aMap.size() << " [";
131
132 for( const auto& [key, value] : aMap )
133 {
134 os << "\n " << key << " = " << value;
135 }
136
137 os << "]";
138 return os;
139}
140
144template <typename K, typename V>
145std::ostream& boost_test_print_type( std::ostream& os, std::pair<K, V> const& aPair )
146{
147 os << "[" << aPair.first << ", " << aPair.second << "]";
148 return os;
149}
150
151} // namespace std
152
153
154//-----------------------------------------------------------------------------+
155// Boost.Test printing helpers for wx types / wide string literals
156//-----------------------------------------------------------------------------+
157namespace boost { namespace test_tools { namespace tt_detail {
158
159template<>
160struct print_log_value<wxString>
161{
162 void operator()( std::ostream& os, wxString const& v )
163 {
164#if wxUSE_UNICODE
165 os << v.ToUTF8().data();
166#else
167 os << v;
168#endif
169 }
170};
171
172// Wide string literal arrays
173template<std::size_t N>
174struct print_log_value<wchar_t[ N ]>
175{
176 void operator()( std::ostream& os, const wchar_t (&ws)[ N ] )
177 {
178 wxString tmp( ws );
179#if wxUSE_UNICODE
180 os << tmp.ToUTF8().data();
181#else
182 os << tmp;
183#endif
184 }
185};
186
187}}} // namespace boost::test_tools::tt_detail
188
189
194std::ostream& boost_test_print_type( std::ostream& os, wxPoint const& aVec );
195
196namespace KI_TEST
197{
198
199template <typename EXP_CONT> using EXP_OBJ = typename EXP_CONT::value_type;
200template <typename FOUND_CONT> using FOUND_OBJ = typename FOUND_CONT::value_type;
201
218template <typename EXP_OBJ, typename FOUND_OBJ>
219using MATCH_PRED = std::function<bool( const EXP_OBJ&, const FOUND_OBJ& )>;
220
255template <typename EXP_CONT, typename FOUND_CONT, typename MATCH_PRED>
256void CheckUnorderedMatches( const EXP_CONT& aExpected, const FOUND_CONT& aFound,
257 MATCH_PRED aMatchPredicate )
258{
259 using EXP_OBJ = typename EXP_CONT::value_type;
260
261 // set of object we've already found
262 std::set<const EXP_OBJ*> matched;
263
264 // fill the set of object that match
265 for( const auto& found : aFound )
266 {
267 for( const auto& expected : aExpected )
268 {
269 if( aMatchPredicate( expected, found ) )
270 {
271 matched.insert( &expected );
272 break;
273 }
274 }
275 }
276
277 // first check every expected object was "found"
278 for( const EXP_OBJ& exp : aExpected )
279 {
280 BOOST_CHECK_MESSAGE( matched.count( &exp ) > 0, "Expected item was not found. Expected: \n"
281 << exp );
282 }
283
284 // check every "found" object was expected
285 for( const EXP_OBJ* found : matched )
286 {
287 const bool was_expected = std::find_if( aExpected.begin(), aExpected.end(),
288 [found]( const EXP_OBJ& aObj )
289 {
290 return &aObj == found;
291 } ) != aExpected.end();
292
293 BOOST_CHECK_MESSAGE( was_expected, "Found item was not expected. Found: \n" << *found );
294 }
295}
296
297
301template <typename T>
302bool CollectionHasNoDuplicates( const T& aCollection )
303{
304 T sorted = aCollection;
305 std::sort( sorted.begin(), sorted.end() );
306
307 return std::adjacent_find( sorted.begin(), sorted.end() ) == sorted.end();
308}
309
310
318{
319 std::string m_CaseName;
320
321 friend std::ostream& operator<<( std::ostream& os, const NAMED_CASE& aCase )
322 {
323 os << aCase.m_CaseName;
324 return os;
325 }
326};
327
328
335#ifdef DEBUG
336#define CHECK_WX_ASSERT( STATEMENT ) BOOST_CHECK_THROW( STATEMENT, KI_TEST::WX_ASSERT_ERROR );
337#else
338#define CHECK_WX_ASSERT( STATEMENT )
339#endif
340
349std::string GetEeschemaTestDataDir();
350
351std::string GetTestDataRootDir();
352
361std::vector<uint8_t> LoadBinaryData( const std::string& aFilePath, std::optional<size_t> aLoadBytes = std::nullopt );
362
363void SetMockConfigDir();
364
365} // namespace KI_TEST
366
367#endif // UNIT_TEST_UTILS__H
std::string GetTestDataRootDir()
std::vector< uint8_t > LoadBinaryData(const std::string &aFilePath, std::optional< size_t > aLoadBytes=std::nullopt)
Load the contents of a file into a vector of bytes.
void CheckUnorderedMatches(const EXP_CONT &aExpected, const FOUND_CONT &aFound, MATCH_PRED aMatchPredicate)
Check that a container of "found" objects matches a container of "expected" objects.
typename FOUND_CONT::value_type FOUND_OBJ
std::function< bool(const EXP_OBJ &, const FOUND_OBJ &)> MATCH_PRED
A match predicate: check that a "found" object is equivalent to or represents an "expected" object,...
std::string GetEeschemaTestDataDir()
Get the configured location of Eeschema test data.
typename EXP_CONT::value_type EXP_OBJ
bool CollectionHasNoDuplicates(const T &aCollection)
Predicate to check a collection has no duplicate elements.
void SetMockConfigDir()
STL namespace.
std::ostream & boost_test_print_type(std::ostream &os, std::vector< T > const &aVec)
Boost print helper for generic vectors.
A named data-driven test case.
friend std::ostream & operator<<(std::ostream &os, const NAMED_CASE &aCase)
std::optional< T > m_Opt
PRINTABLE_OPT(const T &aVal)
PRINTABLE_OPT(const std::optional< T > &aOpt)
void operator()(std::ostream &os, const wchar_t(&ws)[N])
void operator()(std::ostream &os, wxString const &v)
VECTOR3I expected(15, 30, 45)
bool operator!=(const PRINTABLE_OPT< L > &aLhs, const PRINTABLE_OPT< R > &aRhs)
bool operator==(const PRINTABLE_OPT< L > &aLhs, const PRINTABLE_OPT< R > &aRhs)
std::ostream & operator<<(std::ostream &os, const wxString &aStr)
std::ostream & boost_test_print_type(std::ostream &os, wxPoint const &aVec)
Boost print helper for wxPoint.