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
59{
61 {
63 .Map( WX_ANY_TEST_ENUM::ALPHA, wxT( "ALPHA" ) )
64 .Map( WX_ANY_TEST_ENUM::BETA, wxT( "BETA" ) )
65 .Map( WX_ANY_TEST_ENUM::GAMMA, wxT( "GAMMA" ) );
66 }
68
69
70BOOST_AUTO_TEST_SUITE( WxAnyUtils )
71
72
73BOOST_AUTO_TEST_CASE( SameType_SameValue )
74{
75 BOOST_CHECK( KiWxAnyEquals( wxAny( 42 ), wxAny( 42 ) ) );
76 BOOST_CHECK( KiWxAnyEquals( wxAny( wxString( "x" ) ), wxAny( wxString( "x" ) ) ) );
77 BOOST_CHECK( KiWxAnyEquals( wxAny( true ), wxAny( true ) ) );
78 BOOST_CHECK( KiWxAnyEquals( wxAny( std::string( "y" ) ), wxAny( std::string( "y" ) ) ) );
79}
80
81
82BOOST_AUTO_TEST_CASE( SameType_DifferentValue )
83{
84 BOOST_CHECK( !KiWxAnyEquals( wxAny( 42 ), wxAny( 43 ) ) );
85 BOOST_CHECK( !KiWxAnyEquals( wxAny( true ), wxAny( false ) ) );
86 BOOST_CHECK( !KiWxAnyEquals( wxAny( wxString( "a" ) ), wxAny( wxString( "b" ) ) ) );
87 BOOST_CHECK( !KiWxAnyEquals( wxAny( std::string( "a" ) ), wxAny( std::string( "b" ) ) ) );
88}
89
90
91BOOST_AUTO_TEST_CASE( DifferentTypes )
92{
93 // Mixed types should not compare equal; the comparator treats a type
94 // mismatch as inequality.
95 BOOST_CHECK( !KiWxAnyEquals( wxAny( 42 ), wxAny( 42.0 ) ) );
96 BOOST_CHECK( !KiWxAnyEquals( wxAny( true ), wxAny( 1 ) ) );
97}
98
99
100BOOST_AUTO_TEST_CASE( NumericTypes )
101{
102 // The comparator must cover long, long long, unsigned, and float in
103 // addition to int and double; otherwise a property carrying e.g. a float
104 // silently produces a false delta on every comparison.
105 BOOST_CHECK( KiWxAnyEquals( wxAny( 1.5 ), wxAny( 1.5 ) ) );
106 BOOST_CHECK( !KiWxAnyEquals( wxAny( 1.5 ), wxAny( 2.5 ) ) );
107
108 BOOST_CHECK( KiWxAnyEquals( wxAny( 2.5f ), wxAny( 2.5f ) ) );
109 BOOST_CHECK( !KiWxAnyEquals( wxAny( 2.5f ), wxAny( 3.5f ) ) );
110
111 BOOST_CHECK( KiWxAnyEquals( wxAny( 42L ), wxAny( 42L ) ) );
112 BOOST_CHECK( KiWxAnyEquals( wxAny( 42LL ), wxAny( 42LL ) ) );
113 BOOST_CHECK( !KiWxAnyEquals( wxAny( 42LL ), wxAny( 43LL ) ) );
114
115 BOOST_CHECK( KiWxAnyEquals( wxAny( 42u ), wxAny( 42u ) ) );
116 BOOST_CHECK( !KiWxAnyEquals( wxAny( 42u ), wxAny( 43u ) ) );
117}
118
119
121{
122 std::optional<int> a = 5, b = 5, c = 6, 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 BOOST_CHECK( KiWxAnyEquals( wxAny( e ), wxAny( std::optional<int>{} ) ) );
128}
129
130
131BOOST_AUTO_TEST_CASE( OptionalDouble )
132{
133 std::optional<double> a = 5.0, b = 5.0, c = 6.0, e;
134
135 BOOST_CHECK( KiWxAnyEquals( wxAny( a ), wxAny( b ) ) );
136 BOOST_CHECK( !KiWxAnyEquals( wxAny( a ), wxAny( c ) ) );
137 BOOST_CHECK( !KiWxAnyEquals( wxAny( a ), wxAny( e ) ) );
138}
139
140
142{
143 EDA_ANGLE a( 90.0, DEGREES_T ), b( 90.0, DEGREES_T ), c( 45.0, DEGREES_T );
144
145 BOOST_CHECK( KiWxAnyEquals( wxAny( a ), wxAny( b ) ) );
146 BOOST_CHECK( !KiWxAnyEquals( wxAny( a ), wxAny( c ) ) );
147}
148
149
151{
152 VECTOR2I a( 1, 2 ), b( 1, 2 ), c( 3, 4 );
153
154 BOOST_CHECK( KiWxAnyEquals( wxAny( a ), wxAny( b ) ) );
155 BOOST_CHECK( !KiWxAnyEquals( wxAny( a ), wxAny( c ) ) );
156}
157
158
160{
161 BOX2I a( VECTOR2I( 0, 0 ), VECTOR2I( 10, 10 ) );
162 BOX2I b( VECTOR2I( 0, 0 ), VECTOR2I( 10, 10 ) );
163 BOX2I c( VECTOR2I( 1, 0 ), VECTOR2I( 10, 10 ) );
164
165 BOOST_CHECK( KiWxAnyEquals( wxAny( a ), wxAny( b ) ) );
166 BOOST_CHECK( !KiWxAnyEquals( wxAny( a ), wxAny( c ) ) );
167}
168
169
171{
172 KIGFX::COLOR4D a( 1.0, 0.5, 0.0, 1.0 );
173 KIGFX::COLOR4D b( 1.0, 0.5, 0.0, 1.0 );
174 KIGFX::COLOR4D c( 0.5, 0.5, 0.0, 1.0 );
175
176 BOOST_CHECK( KiWxAnyEquals( wxAny( a ), wxAny( b ) ) );
177 BOOST_CHECK( !KiWxAnyEquals( wxAny( a ), wxAny( c ) ) );
178}
179
180
182{
183 KIID a, b;
184
185 BOOST_CHECK( KiWxAnyEquals( wxAny( a ), wxAny( a ) ) );
186 BOOST_CHECK( !KiWxAnyEquals( wxAny( a ), wxAny( b ) ) );
187}
188
189
191{
192 BOOST_CHECK( KiWxAnyEquals( wxAny( F_Cu ), wxAny( F_Cu ) ) );
193 BOOST_CHECK( !KiWxAnyEquals( wxAny( F_Cu ), wxAny( B_Cu ) ) );
194}
195
196
197BOOST_AUTO_TEST_CASE( UnsupportedReturnsFalse )
198{
199 // Unsupported types conservatively report "not equal" so a property delta
200 // is generated rather than silently passing.
201 struct Custom
202 {
203 int n;
204 };
205
206 BOOST_CHECK( !KiWxAnyEquals( wxAny( Custom{ 1 } ), wxAny( Custom{ 1 } ) ) );
207}
208
209
210namespace
211{
212
213struct ENUM_OWNER
214{
215 WX_ANY_TEST_ENUM getValue() const { return m_value; }
216 void setValue( WX_ANY_TEST_ENUM aValue ) { m_value = aValue; }
217
219};
220
221
222struct ENUM_TEST_FIXTURE
223{
224 PROPERTY_ENUM<ENUM_OWNER, WX_ANY_TEST_ENUM> prop{ wxT( "Value" ), &ENUM_OWNER::setValue,
225 &ENUM_OWNER::getValue };
226};
227
228} // namespace
229
230
231BOOST_FIXTURE_TEST_CASE( EnumViaProperty, ENUM_TEST_FIXTURE )
232{
233 BOOST_REQUIRE( prop.HasChoices() );
234
235 wxAny alpha = WX_ANY_TEST_ENUM::ALPHA;
236 wxAny alpha2 = WX_ANY_TEST_ENUM::ALPHA;
237 wxAny beta = WX_ANY_TEST_ENUM::BETA;
238
239 BOOST_CHECK( KiWxAnyEquals( alpha, alpha2, &prop ) );
240 BOOST_CHECK( !KiWxAnyEquals( alpha, beta, &prop ) );
241}
242
243
BOX2< VECTOR2I > BOX2I
Definition box2.h:927
static ENUM_MAP< T > & Instance()
Definition property.h:770
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
Definition kiid.h:46
@ 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:877
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)
static struct WX_ANY_TEST_ENUM_DESC s_WX_ANY_TEST_ENUM_DESC
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.