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 (C) 2018 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 <functional>
35#include <optional>
36#include <set>
37
38#include <wx/gdicmn.h>
39
49#if BOOST_VERSION >= 105900
50#define HAVE_EXPECTED_FAILURES
51#endif
52
62#undef BOOST_TEST
63
64
65#if BOOST_VERSION < 105900
66
67/*
68 * BOOST_TEST_INFO is not available before 1.59. It's not critical for
69 * test pass/fail, it's just info, so just pass along to a logging
70 * function.
71 *
72 * This can be removed when our minimum boost version is 1.59 or higher.
73 */
74#define BOOST_TEST_INFO( A ) BOOST_TEST_MESSAGE( A )
75
76/*
77 *
78 * BOOST_TEST_CONTEXT provides scoped info, but again, only after 1.59.
79 * Replacing with a call to BOOST_TEST_MESSAGE will work, and the
80 * scoping will still work for newer boosts.
81 *
82 * This can be removed when our minimum boost version is 1.59 or higher.
83 */
84#define BOOST_TEST_CONTEXT( A ) BOOST_TEST_MESSAGE( A );
85
86#endif
87
88/*
89 * Boost hides the configuration point for print_log_value in different
90 * namespaces between < 1.59 and >= 1.59.
91 *
92 * The macros can be used to open and close the right level of namespacing
93 * based on the version.
94 *
95 * We could just use a conditionally defined namespace alias, but that
96 * doesn't work in GCC <7 (GCC bug #56480)
97 *
98 * From Boost 1.64, this should be done with boost_test_print_type,
99 * and these defines can be removed once all logging functions use that.
100 */
101#if BOOST_VERSION >= 105900
102#define BOOST_TEST_PRINT_NAMESPACE_OPEN \
103 boost \
104 { \
105 namespace test_tools \
106 { \
107 namespace tt_detail
108#define BOOST_TEST_PRINT_NAMESPACE_CLOSE }}
109#else
110#define BOOST_TEST_PRINT_NAMESPACE_OPEN \
111 boost \
112 { \
113 namespace test_tools
114#define BOOST_TEST_PRINT_NAMESPACE_CLOSE }
115#endif
116
123#if BOOST_VERSION < 106400
124
126{
127template <>
128struct print_log_value<std::nullptr_t>
129{
130 inline void operator()( std::ostream& os, std::nullptr_t const& p )
131 {
132 os << "nullptr";
133 }
134};
135}
137
138#endif
139
140
141template<class T>
143{
144 PRINTABLE_OPT( const std::optional<T>& aOpt ) : m_Opt( aOpt ){};
145 PRINTABLE_OPT( const T& aVal ) : m_Opt( aVal ){};
146
147 std::optional<T> m_Opt;
148};
149
150
154#define KI_CHECK_OPT_EQUAL( lhs, rhs ) \
155 BOOST_CHECK_EQUAL( PRINTABLE_OPT( lhs ), PRINTABLE_OPT( rhs ) )
156
157
158template <class T>
159inline std::ostream& operator<<( std::ostream& aOs, const PRINTABLE_OPT<T>& aOptional )
160{
161 if( aOptional.m_Opt.has_value() )
162 aOs << *aOptional.m_Opt;
163 else
164 aOs << "nullopt";
165
166 return aOs;
167}
168
169
170template <class L, class R>
171inline bool operator==( const PRINTABLE_OPT<L>& aLhs, const PRINTABLE_OPT<R>& aRhs )
172{
173 if( !aLhs.m_Opt.has_value() && !aRhs.m_Opt.has_value() )
174 return true; // both nullopt
175
176 return aLhs.m_Opt.has_value() && aRhs.m_Opt.has_value() && *aLhs.m_Opt == *aRhs.m_Opt;
177}
178
179
180template <class L, class R>
181inline bool operator!=( const PRINTABLE_OPT<L>& aLhs, const PRINTABLE_OPT<R>& aRhs )
182{
183 return !( aLhs == aRhs );
184}
185
186
188{
189
193template <typename T>
194struct print_log_value<std::vector<T>>
195{
196 inline void operator()( std::ostream& os, std::vector<T> const& aVec )
197 {
198 os << "std::vector size " << aVec.size() << "[";
199
200 for( const auto& i : aVec )
201 {
202 os << "\n ";
203 print_log_value<T>()( os, i );
204 }
205
206 os << "]";
207 }
208};
209
214template <>
215struct print_log_value<wxPoint>
216{
217 void operator()( std::ostream& os, wxPoint const& aVec );
218};
219
220}
222
223
224namespace KI_TEST
225{
226
227template <typename EXP_CONT> using EXP_OBJ = typename EXP_CONT::value_type;
228template <typename FOUND_CONT> using FOUND_OBJ = typename FOUND_CONT::value_type;
229
246template <typename EXP_OBJ, typename FOUND_OBJ>
247using MATCH_PRED = std::function<bool( const EXP_OBJ&, const FOUND_OBJ& )>;
248
283template <typename EXP_CONT, typename FOUND_CONT, typename MATCH_PRED>
284void CheckUnorderedMatches( const EXP_CONT& aExpected, const FOUND_CONT& aFound,
285 MATCH_PRED aMatchPredicate )
286{
287 using EXP_OBJ = typename EXP_CONT::value_type;
288
289 // set of object we've already found
290 std::set<const EXP_OBJ*> matched;
291
292 // fill the set of object that match
293 for( const auto& found : aFound )
294 {
295 for( const auto& expected : aExpected )
296 {
297 if( aMatchPredicate( expected, found ) )
298 {
299 matched.insert( &expected );
300 break;
301 }
302 }
303 }
304
305 // first check every expected object was "found"
306 for( const EXP_OBJ& exp : aExpected )
307 {
308 BOOST_CHECK_MESSAGE( matched.count( &exp ) > 0, "Expected item was not found. Expected: \n"
309 << exp );
310 }
311
312 // check every "found" object was expected
313 for( const EXP_OBJ* found : matched )
314 {
315 const bool was_expected = std::find_if( aExpected.begin(), aExpected.end(),
316 [found]( const EXP_OBJ& aObj )
317 {
318 return &aObj == found;
319 } ) != aExpected.end();
320
321 BOOST_CHECK_MESSAGE( was_expected, "Found item was not expected. Found: \n" << *found );
322 }
323}
324
325
329template <typename T>
330bool CollectionHasNoDuplicates( const T& aCollection )
331{
332 T sorted = aCollection;
333 std::sort( sorted.begin(), sorted.end() );
334
335 return std::adjacent_find( sorted.begin(), sorted.end() ) == sorted.end();
336}
337
338
345#ifdef DEBUG
346#define CHECK_WX_ASSERT( STATEMENT ) BOOST_CHECK_THROW( STATEMENT, KI_TEST::WX_ASSERT_ERROR );
347#else
348#define CHECK_WX_ASSERT( STATEMENT )
349#endif
350
359std::string GetEeschemaTestDataDir();
360
361} // namespace KI_TEST
362
363#endif // UNIT_TEST_UTILS__H
Before Boost 1.64, nullptr_t wasn't handled.
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.
std::string GetEeschemaTestDataDir()
Get the configured location of Eeschema test data.
bool CollectionHasNoDuplicates(const T &aCollection)
Predicate to check a collection has no duplicate elements.
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,...
typename FOUND_CONT::value_type FOUND_OBJ
typename EXP_CONT::value_type EXP_OBJ
STL namespace.
void operator()(std::ostream &os, std::nullptr_t const &p)
void operator()(std::ostream &os, std::vector< T > const &aVec)
std::optional< T > m_Opt
PRINTABLE_OPT(const T &aVal)
PRINTABLE_OPT(const std::optional< T > &aOpt)
VECTOR3I expected(15, 30, 45)
#define BOOST_TEST_PRINT_NAMESPACE_CLOSE
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)