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
44template<class T>
46{
47 PRINTABLE_OPT( const std::optional<T>& aOpt ) : m_Opt( aOpt ){};
48 PRINTABLE_OPT( const T& aVal ) : m_Opt( aVal ){};
49
50 std::optional<T> m_Opt;
51};
52
53
57#define KI_CHECK_OPT_EQUAL( lhs, rhs ) \
58 BOOST_CHECK_EQUAL( PRINTABLE_OPT( lhs ), PRINTABLE_OPT( rhs ) )
59
60
61template <class T>
62inline std::ostream& operator<<( std::ostream& aOs, const PRINTABLE_OPT<T>& aOptional )
63{
64 if( aOptional.m_Opt.has_value() )
65 aOs << *aOptional.m_Opt;
66 else
67 aOs << "nullopt";
68
69 return aOs;
70}
71
72
73template <class L, class R>
74inline bool operator==( const PRINTABLE_OPT<L>& aLhs, const PRINTABLE_OPT<R>& aRhs )
75{
76 if( !aLhs.m_Opt.has_value() && !aRhs.m_Opt.has_value() )
77 return true; // both nullopt
78
79 return aLhs.m_Opt.has_value() && aRhs.m_Opt.has_value() && *aLhs.m_Opt == *aRhs.m_Opt;
80}
81
82
83template <class L, class R>
84inline bool operator!=( const PRINTABLE_OPT<L>& aLhs, const PRINTABLE_OPT<R>& aRhs )
85{
86 return !( aLhs == aRhs );
87}
88
89
90// boost_test_print_type has to be in the same namespace as the printed type
91namespace std
92{
93
97template <typename T>
98std::ostream& boost_test_print_type( std::ostream& os, std::vector<T> const& aVec )
99{
100 os << "std::vector size " << aVec.size() << " [";
101
102 for( const auto& i : aVec )
103 {
104 os << "\n " << i;
105 }
106
107 os << "]";
108 return os;
109}
110
114template <typename K, typename V>
115std::ostream& boost_test_print_type( std::ostream& os, std::map<K, V> const& aMap )
116{
117 os << "std::map size " << aMap.size() << " [";
118
119 for( const auto& [key, value] : aMap )
120 {
121 os << "\n " << key << " = " << value;
122 }
123
124 os << "]";
125 return os;
126}
127
131template <typename K, typename V>
132std::ostream& boost_test_print_type( std::ostream& os, std::pair<K, V> const& aPair )
133{
134 os << "[" << aPair.first << ", " << aPair.second << "]";
135 return os;
136}
137
138} // namespace std
139
140
141//-----------------------------------------------------------------------------+
142// Boost.Test printing helpers for wx types / wide string literals
143//-----------------------------------------------------------------------------+
144
145// C++20 removed operator<<(ostream&, const wchar_t*) (P1423R3), which breaks wxString streaming
146// via implicit wchar_t* conversion in Boost.Test's lazy_ostream (used by BOOST_TEST_CONTEXT,
147// BOOST_TEST_MESSAGE, BOOST_CHECK_MESSAGE, and BOOST_FAIL).
148inline std::ostream& boost_test_print_type( std::ostream& os, const wxString& v )
149{
150#if wxUSE_UNICODE
151 os << v.ToUTF8().data();
152#else
153 os << v.c_str();
154#endif
155 return os;
156}
157
158
159// Wide string literal arrays
160template <std::size_t N>
161std::ostream& boost_test_print_type( std::ostream& os, const wchar_t ( &ws )[N] )
162{
163 wxString tmp( ws );
164#if wxUSE_UNICODE
165 os << tmp.ToUTF8().data();
166#else
167 os << tmp;
168#endif
169 return os;
170}
171
172
173namespace boost { namespace test_tools { namespace tt_detail {
174
175template<std::size_t N>
176struct print_log_value<wchar_t[ N ]>
177{
178 void operator()( std::ostream& os, const wchar_t (&ws)[ N ] )
179 {
180 wxString tmp( ws );
181#if wxUSE_UNICODE
182 os << tmp.ToUTF8().data();
183#else
184 os << tmp;
185#endif
186 }
187};
188
189}}} // namespace boost::test_tools::tt_detail
190
191
196std::ostream& boost_test_print_type( std::ostream& os, wxPoint const& aVec );
197
198namespace KI_TEST
199{
200
201template <typename EXP_CONT> using EXP_OBJ = typename EXP_CONT::value_type;
202template <typename FOUND_CONT> using FOUND_OBJ = typename FOUND_CONT::value_type;
203
220template <typename EXP_OBJ, typename FOUND_OBJ>
221using MATCH_PRED = std::function<bool( const EXP_OBJ&, const FOUND_OBJ& )>;
222
257template <typename EXP_CONT, typename FOUND_CONT, typename MATCH_PRED>
258void CheckUnorderedMatches( const EXP_CONT& aExpected, const FOUND_CONT& aFound,
259 MATCH_PRED aMatchPredicate )
260{
261 using EXP_OBJ = typename EXP_CONT::value_type;
262
263 // set of object we've already found
264 std::set<const EXP_OBJ*> matched;
265
266 // fill the set of object that match
267 for( const auto& found : aFound )
268 {
269 for( const auto& expected : aExpected )
270 {
271 if( aMatchPredicate( expected, found ) )
272 {
273 matched.insert( &expected );
274 break;
275 }
276 }
277 }
278
279 // first check every expected object was "found"
280 for( const EXP_OBJ& exp : aExpected )
281 {
282 BOOST_CHECK_MESSAGE( matched.count( &exp ) > 0, "Expected item was not found. Expected: \n"
283 << exp );
284 }
285
286 // check every "found" object was expected
287 for( const EXP_OBJ* found : matched )
288 {
289 const bool was_expected = std::find_if( aExpected.begin(), aExpected.end(),
290 [found]( const EXP_OBJ& aObj )
291 {
292 return &aObj == found;
293 } ) != aExpected.end();
294
295 BOOST_CHECK_MESSAGE( was_expected, "Found item was not expected. Found: \n" << *found );
296 }
297}
298
299
303template <typename T>
304bool CollectionHasNoDuplicates( const T& aCollection )
305{
306 T sorted = aCollection;
307 std::sort( sorted.begin(), sorted.end() );
308
309 return std::adjacent_find( sorted.begin(), sorted.end() ) == sorted.end();
310}
311
312
320{
321 std::string m_CaseName;
322
323 friend std::ostream& operator<<( std::ostream& os, const NAMED_CASE& aCase )
324 {
325 os << aCase.m_CaseName;
326 return os;
327 }
328};
329
330
337#ifdef DEBUG
338#define CHECK_WX_ASSERT( STATEMENT ) BOOST_CHECK_THROW( STATEMENT, KI_TEST::WX_ASSERT_ERROR );
339#else
340#define CHECK_WX_ASSERT( STATEMENT )
341#endif
342
351std::string GetEeschemaTestDataDir();
352
353std::string GetTestDataRootDir();
354
363std::vector<uint8_t> LoadBinaryData( const std::string& aFilePath, std::optional<size_t> aLoadBytes = std::nullopt );
364
365void SetMockConfigDir();
366
367} // namespace KI_TEST
368
369#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])
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 &aOs, const PRINTABLE_OPT< T > &aOptional)
std::ostream & boost_test_print_type(std::ostream &os, const wxString &v)