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 <functional>
35#include <optional>
36#include <set>
37
38#include <wx/gdicmn.h>
39
40
41
42template<class T>
44{
45 PRINTABLE_OPT( const std::optional<T>& aOpt ) : m_Opt( aOpt ){};
46 PRINTABLE_OPT( const T& aVal ) : m_Opt( aVal ){};
47
48 std::optional<T> m_Opt;
49};
50
51
55#define KI_CHECK_OPT_EQUAL( lhs, rhs ) \
56 BOOST_CHECK_EQUAL( PRINTABLE_OPT( lhs ), PRINTABLE_OPT( rhs ) )
57
58
59template <class T>
60inline std::ostream& operator<<( std::ostream& aOs, const PRINTABLE_OPT<T>& aOptional )
61{
62 if( aOptional.m_Opt.has_value() )
63 aOs << *aOptional.m_Opt;
64 else
65 aOs << "nullopt";
66
67 return aOs;
68}
69
70
71template <class L, class R>
72inline bool operator==( const PRINTABLE_OPT<L>& aLhs, const PRINTABLE_OPT<R>& aRhs )
73{
74 if( !aLhs.m_Opt.has_value() && !aRhs.m_Opt.has_value() )
75 return true; // both nullopt
76
77 return aLhs.m_Opt.has_value() && aRhs.m_Opt.has_value() && *aLhs.m_Opt == *aRhs.m_Opt;
78}
79
80
81template <class L, class R>
82inline bool operator!=( const PRINTABLE_OPT<L>& aLhs, const PRINTABLE_OPT<R>& aRhs )
83{
84 return !( aLhs == aRhs );
85}
86
87
88// boost_test_print_type has to be in the same namespace as the printed type
89namespace std
90{
91
95template <typename T>
96std::ostream& boost_test_print_type( std::ostream& os, std::vector<T> const& aVec )
97{
98 os << "std::vector size " << aVec.size() << " [";
99
100 for( const auto& i : aVec )
101 {
102 os << "\n " << i;
103 }
104
105 os << "]";
106 return os;
107}
108
112template <typename K, typename V>
113std::ostream& boost_test_print_type( std::ostream& os, std::map<K, V> const& aMap )
114{
115 os << "std::map size " << aMap.size() << " [";
116
117 for( const auto& [key, value] : aMap )
118 {
119 os << "\n " << key << " = " << value;
120 }
121
122 os << "]";
123 return os;
124}
125
129template <typename K, typename V>
130std::ostream& boost_test_print_type( std::ostream& os, std::pair<K, V> const& aPair )
131{
132 os << "[" << aPair.first << ", " << aPair.second << "]";
133 return os;
134}
135
136} // namespace std
137
138
143std::ostream& boost_test_print_type( std::ostream& os, wxPoint const& aVec );
144
145namespace KI_TEST
146{
147
148template <typename EXP_CONT> using EXP_OBJ = typename EXP_CONT::value_type;
149template <typename FOUND_CONT> using FOUND_OBJ = typename FOUND_CONT::value_type;
150
167template <typename EXP_OBJ, typename FOUND_OBJ>
168using MATCH_PRED = std::function<bool( const EXP_OBJ&, const FOUND_OBJ& )>;
169
204template <typename EXP_CONT, typename FOUND_CONT, typename MATCH_PRED>
205void CheckUnorderedMatches( const EXP_CONT& aExpected, const FOUND_CONT& aFound,
206 MATCH_PRED aMatchPredicate )
207{
208 using EXP_OBJ = typename EXP_CONT::value_type;
209
210 // set of object we've already found
211 std::set<const EXP_OBJ*> matched;
212
213 // fill the set of object that match
214 for( const auto& found : aFound )
215 {
216 for( const auto& expected : aExpected )
217 {
218 if( aMatchPredicate( expected, found ) )
219 {
220 matched.insert( &expected );
221 break;
222 }
223 }
224 }
225
226 // first check every expected object was "found"
227 for( const EXP_OBJ& exp : aExpected )
228 {
229 BOOST_CHECK_MESSAGE( matched.count( &exp ) > 0, "Expected item was not found. Expected: \n"
230 << exp );
231 }
232
233 // check every "found" object was expected
234 for( const EXP_OBJ* found : matched )
235 {
236 const bool was_expected = std::find_if( aExpected.begin(), aExpected.end(),
237 [found]( const EXP_OBJ& aObj )
238 {
239 return &aObj == found;
240 } ) != aExpected.end();
241
242 BOOST_CHECK_MESSAGE( was_expected, "Found item was not expected. Found: \n" << *found );
243 }
244}
245
246
250template <typename T>
251bool CollectionHasNoDuplicates( const T& aCollection )
252{
253 T sorted = aCollection;
254 std::sort( sorted.begin(), sorted.end() );
255
256 return std::adjacent_find( sorted.begin(), sorted.end() ) == sorted.end();
257}
258
259
267{
268 std::string m_CaseName;
269
270 friend std::ostream& operator<<( std::ostream& os, const NAMED_CASE& aCase )
271 {
272 os << aCase.m_CaseName;
273 return os;
274 }
275};
276
277
284#ifdef DEBUG
285#define CHECK_WX_ASSERT( STATEMENT ) BOOST_CHECK_THROW( STATEMENT, KI_TEST::WX_ASSERT_ERROR );
286#else
287#define CHECK_WX_ASSERT( STATEMENT )
288#endif
289
298std::string GetEeschemaTestDataDir();
299
300std::string GetTestDataRootDir();
301
302void SetMockConfigDir();
303
304} // namespace KI_TEST
305
306#endif // UNIT_TEST_UTILS__H
std::ostream & operator<<(std::ostream &aStream, const EDA_TEXT &aText)
Definition: eda_text.cpp:1307
std::string GetTestDataRootDir()
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
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)
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 & boost_test_print_type(std::ostream &os, wxPoint const &aVec)
Boost print helper for wxPoint.