KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_wx_any_utils.cpp
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 3
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/gpl-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 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
25
27#include <properties/property.h>
28
29#include <geometry/eda_angle.h>
30#include <gal/color4d.h>
31#include <kiid.h>
32#include <layer_ids.h>
33#include <math/box2.h>
34#include <math/vector2d.h>
35
36#include <wx/any.h>
37#include <wx/string.h>
38
39#include <optional>
40
41
42// Enum-backed properties store the enum type in the wxAny, not int, so the
43// comparator needs the property's wxPGChoices to compare via the integer
44// payload. We register a small enum to exercise that path. The enum and its
45// wxAny adapter must live at global scope (outside the Boost test suite
46// namespace) so the wxAnyValueTypeImpl specialization is well-formed.
47
49{
50 ALPHA = 0,
51 BETA = 1,
53};
54
55
57
58
59BOOST_AUTO_TEST_SUITE( WxAnyUtils )
60
61
62BOOST_AUTO_TEST_CASE( SameType_SameValue )
63{
64 BOOST_CHECK( KiWxAnyEquals( wxAny( 42 ), wxAny( 42 ) ) );
65 BOOST_CHECK( KiWxAnyEquals( wxAny( wxString( "x" ) ), wxAny( wxString( "x" ) ) ) );
66 BOOST_CHECK( KiWxAnyEquals( wxAny( true ), wxAny( true ) ) );
67 BOOST_CHECK( KiWxAnyEquals( wxAny( std::string( "y" ) ), wxAny( std::string( "y" ) ) ) );
68}
69
70
71BOOST_AUTO_TEST_CASE( SameType_DifferentValue )
72{
73 BOOST_CHECK( !KiWxAnyEquals( wxAny( 42 ), wxAny( 43 ) ) );
74 BOOST_CHECK( !KiWxAnyEquals( wxAny( true ), wxAny( false ) ) );
75 BOOST_CHECK( !KiWxAnyEquals( wxAny( wxString( "a" ) ), wxAny( wxString( "b" ) ) ) );
76 BOOST_CHECK( !KiWxAnyEquals( wxAny( std::string( "a" ) ), wxAny( std::string( "b" ) ) ) );
77}
78
79
80BOOST_AUTO_TEST_CASE( DifferentTypes )
81{
82 // Mixed types should not compare equal; the comparator treats a type
83 // mismatch as inequality.
84 BOOST_CHECK( !KiWxAnyEquals( wxAny( 42 ), wxAny( 42.0 ) ) );
85 BOOST_CHECK( !KiWxAnyEquals( wxAny( true ), wxAny( 1 ) ) );
86}
87
88
89BOOST_AUTO_TEST_CASE( NumericTypes )
90{
91 // The comparator must cover long, long long, unsigned, and float in
92 // addition to int and double; otherwise a property carrying e.g. a float
93 // silently produces a false delta on every comparison.
94 BOOST_CHECK( KiWxAnyEquals( wxAny( 1.5 ), wxAny( 1.5 ) ) );
95 BOOST_CHECK( !KiWxAnyEquals( wxAny( 1.5 ), wxAny( 2.5 ) ) );
96
97 BOOST_CHECK( KiWxAnyEquals( wxAny( 2.5f ), wxAny( 2.5f ) ) );
98 BOOST_CHECK( !KiWxAnyEquals( wxAny( 2.5f ), wxAny( 3.5f ) ) );
99
100 BOOST_CHECK( KiWxAnyEquals( wxAny( 42L ), wxAny( 42L ) ) );
101 BOOST_CHECK( KiWxAnyEquals( wxAny( 42LL ), wxAny( 42LL ) ) );
102 BOOST_CHECK( !KiWxAnyEquals( wxAny( 42LL ), wxAny( 43LL ) ) );
103
104 BOOST_CHECK( KiWxAnyEquals( wxAny( 42u ), wxAny( 42u ) ) );
105 BOOST_CHECK( !KiWxAnyEquals( wxAny( 42u ), wxAny( 43u ) ) );
106}
107
108
110{
111 std::optional<int> a = 5, b = 5, c = 6, e;
112
113 BOOST_CHECK( KiWxAnyEquals( wxAny( a ), wxAny( b ) ) );
114 BOOST_CHECK( !KiWxAnyEquals( wxAny( a ), wxAny( c ) ) );
115 BOOST_CHECK( !KiWxAnyEquals( wxAny( a ), wxAny( e ) ) );
116 BOOST_CHECK( KiWxAnyEquals( wxAny( e ), wxAny( std::optional<int>{} ) ) );
117}
118
119
120BOOST_AUTO_TEST_CASE( OptionalDouble )
121{
122 std::optional<double> a = 5.0, b = 5.0, c = 6.0, e;
123
124 BOOST_CHECK( KiWxAnyEquals( wxAny( a ), wxAny( b ) ) );
125 BOOST_CHECK( !KiWxAnyEquals( wxAny( a ), wxAny( c ) ) );
126 BOOST_CHECK( !KiWxAnyEquals( wxAny( a ), wxAny( e ) ) );
127}
128
129
131{
132 EDA_ANGLE a( 90.0, DEGREES_T ), b( 90.0, DEGREES_T ), c( 45.0, DEGREES_T );
133
134 BOOST_CHECK( KiWxAnyEquals( wxAny( a ), wxAny( b ) ) );
135 BOOST_CHECK( !KiWxAnyEquals( wxAny( a ), wxAny( c ) ) );
136}
137
138
140{
141 VECTOR2I a( 1, 2 ), b( 1, 2 ), c( 3, 4 );
142
143 BOOST_CHECK( KiWxAnyEquals( wxAny( a ), wxAny( b ) ) );
144 BOOST_CHECK( !KiWxAnyEquals( wxAny( a ), wxAny( c ) ) );
145}
146
147
149{
150 BOX2I a( VECTOR2I( 0, 0 ), VECTOR2I( 10, 10 ) );
151 BOX2I b( VECTOR2I( 0, 0 ), VECTOR2I( 10, 10 ) );
152 BOX2I c( VECTOR2I( 1, 0 ), VECTOR2I( 10, 10 ) );
153
154 BOOST_CHECK( KiWxAnyEquals( wxAny( a ), wxAny( b ) ) );
155 BOOST_CHECK( !KiWxAnyEquals( wxAny( a ), wxAny( c ) ) );
156}
157
158
160{
161 KIGFX::COLOR4D a( 1.0, 0.5, 0.0, 1.0 );
162 KIGFX::COLOR4D b( 1.0, 0.5, 0.0, 1.0 );
163 KIGFX::COLOR4D c( 0.5, 0.5, 0.0, 1.0 );
164
165 BOOST_CHECK( KiWxAnyEquals( wxAny( a ), wxAny( b ) ) );
166 BOOST_CHECK( !KiWxAnyEquals( wxAny( a ), wxAny( c ) ) );
167}
168
169
171{
172 KIID a, b;
173
174 BOOST_CHECK( KiWxAnyEquals( wxAny( a ), wxAny( a ) ) );
175 BOOST_CHECK( !KiWxAnyEquals( wxAny( a ), wxAny( b ) ) );
176}
177
178
180{
181 BOOST_CHECK( KiWxAnyEquals( wxAny( F_Cu ), wxAny( F_Cu ) ) );
182 BOOST_CHECK( !KiWxAnyEquals( wxAny( F_Cu ), wxAny( B_Cu ) ) );
183}
184
185
186BOOST_AUTO_TEST_CASE( UnsupportedReturnsFalse )
187{
188 // Unsupported types conservatively report "not equal" so a property delta
189 // is generated rather than silently passing.
190 struct Custom
191 {
192 int n;
193 };
194
195 BOOST_CHECK( !KiWxAnyEquals( wxAny( Custom{ 1 } ), wxAny( Custom{ 1 } ) ) );
196}
197
198
199namespace
200{
201
202struct ENUM_OWNER
203{
204 WX_ANY_TEST_ENUM getValue() const { return m_value; }
205 void setValue( WX_ANY_TEST_ENUM aValue ) { m_value = aValue; }
206
208};
209
210
211struct ENUM_TEST_FIXTURE
212{
213 ENUM_TEST_FIXTURE()
214 {
216 .Map( WX_ANY_TEST_ENUM::ALPHA, wxT( "ALPHA" ) )
217 .Map( WX_ANY_TEST_ENUM::BETA, wxT( "BETA" ) )
218 .Map( WX_ANY_TEST_ENUM::GAMMA, wxT( "GAMMA" ) );
219 }
220
221 PROPERTY_ENUM<ENUM_OWNER, WX_ANY_TEST_ENUM> prop{ wxT( "Value" ), &ENUM_OWNER::setValue,
222 &ENUM_OWNER::getValue };
223};
224
225} // namespace
226
227
228BOOST_FIXTURE_TEST_CASE( EnumViaProperty, ENUM_TEST_FIXTURE )
229{
230 BOOST_REQUIRE( prop.HasChoices() );
231
232 wxAny alpha = WX_ANY_TEST_ENUM::ALPHA;
233 wxAny alpha2 = WX_ANY_TEST_ENUM::ALPHA;
234 wxAny beta = WX_ANY_TEST_ENUM::BETA;
235
236 BOOST_CHECK( KiWxAnyEquals( alpha, alpha2, &prop ) );
237 BOOST_CHECK( !KiWxAnyEquals( alpha, beta, &prop ) );
238}
239
240
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
static ENUM_MAP< T > & Instance()
Definition property.h:721
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
Definition kiid.h:44
@ DEGREES_T
Definition eda_angle.h:31
@ B_Cu
Definition layer_ids.h:61
@ F_Cu
Definition layer_ids.h:60
#define ENUM_TO_WXANY(type)
Macro to define read-only fields (no setter method available)
Definition property.h:823
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(SameType_SameValue)
WX_ANY_TEST_ENUM
BOOST_FIXTURE_TEST_CASE(EnumViaProperty, ENUM_TEST_FIXTURE)
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
bool KiWxAnyEquals(const wxAny &aA, const wxAny &aB, const PROPERTY_BASE *aProperty)
Compare two wxAny values for equality across the KiCad property type set.