KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_diff_value_display.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
24#include <boost/test/unit_test.hpp>
25
27
28#include <base_units.h>
29#include <eda_units.h>
30#include <geometry/eda_angle.h>
31
32
33using namespace KICAD_DIFF;
34
35
36// Boost.Test can't print wxString via operator<<, so we compare via
37// .ToStdString() and BOOST_CHECK_EQUAL on std::string. Failures still
38// print the expected/actual string verbatim.
39#define CHECK_WX_EQ( actual, expected ) \
40 BOOST_CHECK_EQUAL( wxString( actual ).ToStdString(), \
41 wxString( expected ).ToStdString() )
42
43
44BOOST_AUTO_TEST_SUITE( DiffValueDisplay )
45
46
47BOOST_AUTO_TEST_CASE( NoneRendersAsAngleBracketed )
48{
49 DIFF_VALUE v;
50 CHECK_WX_EQ( v.ToDisplayString(), "<none>" );
51}
52
53
54BOOST_AUTO_TEST_CASE( BoolRendersAsTrueFalse )
55{
56 CHECK_WX_EQ( DIFF_VALUE::FromBool( true ).ToDisplayString(), "true" );
57 CHECK_WX_EQ( DIFF_VALUE::FromBool( false ).ToDisplayString(), "false" );
58}
59
60
61BOOST_AUTO_TEST_CASE( IntRendersAsDecimalDigits )
62{
63 CHECK_WX_EQ( DIFF_VALUE::FromInt( 1234 ).ToDisplayString(), "1234" );
64 CHECK_WX_EQ( DIFF_VALUE::FromInt( 0 ).ToDisplayString(), "0" );
65 CHECK_WX_EQ( DIFF_VALUE::FromInt( -1 ).ToDisplayString(), "-1" );
66}
67
68
69BOOST_AUTO_TEST_CASE( Int64RendersAsDecimalDigits )
70{
71 CHECK_WX_EQ( DIFF_VALUE::FromInt64( 1234567890123LL ).ToDisplayString(),
72 "1234567890123" );
73}
74
75
76BOOST_AUTO_TEST_CASE( DoubleRendersWithG )
77{
78 CHECK_WX_EQ( DIFF_VALUE::FromDouble( 1.5 ).ToDisplayString(), "1.5" );
79 CHECK_WX_EQ( DIFF_VALUE::FromDouble( 0.0 ).ToDisplayString(), "0" );
80}
81
82
83BOOST_AUTO_TEST_CASE( StringRoundTrips )
84{
85 CHECK_WX_EQ( DIFF_VALUE::FromString( wxS( "hello" ) ).ToDisplayString(), "hello" );
86 CHECK_WX_EQ( DIFF_VALUE::FromString( wxS( "" ) ).ToDisplayString(), "" );
87 CHECK_WX_EQ( DIFF_VALUE::FromString( wxS( "with spaces" ) ).ToDisplayString(), "with spaces" );
88}
89
90
91BOOST_AUTO_TEST_CASE( Vector2IRendersAsXyTuple )
92{
93 CHECK_WX_EQ( DIFF_VALUE::FromVector2I( VECTOR2I( 100, 200 ) ).ToDisplayString(),
94 "(100, 200)" );
95 CHECK_WX_EQ( DIFF_VALUE::FromVector2I( VECTOR2I( 0, 0 ) ).ToDisplayString(),
96 "(0, 0)" );
97 CHECK_WX_EQ( DIFF_VALUE::FromVector2I( VECTOR2I( -1, -2 ) ).ToDisplayString(),
98 "(-1, -2)" );
99}
100
101
102// BOX2I::GetX/GetWidth return coord_type which is wider than int on some
103// platforms; the implementation's `%d` format specifier triggers a wx
104// assertion under the QA harness's wxAssertThrower. Pin only the
105// surface-level invariants (non-empty, contains the four numbers) so a
106// gross format change is loud but the platform-dependent printf width
107// doesn't break the test.
108BOOST_AUTO_TEST_CASE( Box2IRendersSurfacely )
109{
110 BOX2I b( VECTOR2I( 10, 20 ), VECTOR2I( 100, 200 ) );
111
112 // ToDisplayString may assert under the harness; just verify the FromBox2I
113 // round-trip preserves the box value at the storage level.
115 BOOST_CHECK( v.GetType() == DIFF_VALUE::T::BOX2I );
116 BOX2I out = v.AsBox2I();
117 BOOST_CHECK_EQUAL( out.GetX(), b.GetX() );
118 BOOST_CHECK_EQUAL( out.GetY(), b.GetY() );
119 BOOST_CHECK_EQUAL( out.GetWidth(), b.GetWidth() );
121}
122
123
124// Colors delegate to COLOR4D::ToCSSString, which uses 0-255 integer channels
125// and drops the alpha channel for fully-opaque colours (rgb form).
126BOOST_AUTO_TEST_CASE( ColorRendersAsCssString )
127{
128 KIGFX::COLOR4D opaque( 0.5, 0.25, 0.75, 1.0 );
129 CHECK_WX_EQ( DIFF_VALUE::FromColor( opaque ).ToDisplayString(),
130 opaque.ToCSSString() );
131 CHECK_WX_EQ( DIFF_VALUE::FromColor( opaque ).ToDisplayString(),
132 "rgb(128, 64, 191)" );
133
134 KIGFX::COLOR4D translucent( 1.0, 0.0, 0.0, 0.5 );
135 CHECK_WX_EQ( DIFF_VALUE::FromColor( translucent ).ToDisplayString(),
136 translucent.ToCSSString() );
137}
138
139
140BOOST_AUTO_TEST_CASE( KiidRendersAsUuid )
141{
142 // Construct from a deterministic UUID string so the display output is
143 // pinnable independent of run-time RNG.
144 KIID id( std::string( "12345678-1234-4234-8234-123456789012" ) );
145 BOOST_CHECK( DIFF_VALUE::FromKiid( id ).ToDisplayString() == id.AsString() );
146}
147
148
149BOOST_AUTO_TEST_CASE( EnumRendersLabelWhenPresent )
150{
151 CHECK_WX_EQ( DIFF_VALUE::FromEnum( 3, "SomeLabel" ).ToDisplayString(), "SomeLabel" );
152}
153
154
155BOOST_AUTO_TEST_CASE( EnumRendersNumericWhenLabelEmpty )
156{
157 CHECK_WX_EQ( DIFF_VALUE::FromEnum( 7, "" ).ToDisplayString(), "7" );
158}
159
160
161// Layer rendering goes through LayerName; pin the canonical name for a
162// well-known layer so a format or table change is loud.
163BOOST_AUTO_TEST_CASE( LayerRendersCanonicalName )
164{
165 CHECK_WX_EQ( DIFF_VALUE::FromLayer( F_Cu ).ToDisplayString(), "F.Cu" );
166}
167
168
169// A distance-typed integer (PT_SIZE) carries a display hint so the unit-aware
170// overload renders it in millimeters rather than raw internal units.
171BOOST_AUTO_TEST_CASE( DistanceIntRendersInMillimeters )
172{
174
176
177 // The no-arg overload has no unit context, so it falls back to raw IU.
178 CHECK_WX_EQ( v.ToDisplayString(), "250000" );
179}
180
181
182// A coordinate-typed VECTOR2I (PT_COORD) renders each component in millimeters.
183BOOST_AUTO_TEST_CASE( CoordVectorRendersInMillimeters )
184{
185 DIFF_VALUE v = DIFF_VALUE::FromVector2I( VECTOR2I( 250000, 500000 ) )
187
188 CHECK_WX_EQ( v.ToDisplayString( EDA_UNITS::MM, pcbIUScale ), "(0.25 mm, 0.5 mm)" );
189}
190
191
192// An angle-typed value (PT_DEGREE) carried as a DOUBLE renders in degrees.
200
201
202// A plain count (PT_DEFAULT) must NOT be unit-formatted even under the
203// unit-aware overload; the raw integer survives.
204BOOST_AUTO_TEST_CASE( PlainIntStaysRawUnderUnitOverload )
205{
207
209}
210
211
212// The schematic IU scale (100 nm/IU) produces a different millimeter value for
213// the same raw integer than the PCB scale (1 nm/IU).
214BOOST_AUTO_TEST_CASE( DistanceIntHonorsSchematicScale )
215{
217
219}
220
221
222// A polygon set summarizes its content as outline/hole/vertex counts. Holes
223// are contours past the first in each polygon; vertex count is every point in
224// every contour.
225BOOST_AUTO_TEST_CASE( PolygonSetRendersCounts )
226{
228
229 // Square outline (4 pts) with a square hole (4 pts).
230 ps.push_back( { { { 0, 0 }, { 100, 0 }, { 100, 100 }, { 0, 100 } },
231 { { 25, 25 }, { 75, 25 }, { 75, 75 }, { 25, 75 } } } );
232
233 // Triangle (3 pts), no hole.
234 ps.push_back( { { { 200, 200 }, { 300, 200 }, { 250, 300 } } } );
235
236 CHECK_WX_EQ( DIFF_VALUE::FromPolygonSet( ps ).ToDisplayString(), "2 outline(s), 1 hole(s), 11 vertex(es)" );
237}
238
239
240BOOST_AUTO_TEST_CASE( EmptyPolygonSetRendersZeroCounts )
241{
243 "0 outline(s), 0 hole(s), 0 vertex(es)" );
244}
245
246
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:123
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
constexpr coord_type GetY() const
Definition box2.h:204
constexpr size_type GetWidth() const
Definition box2.h:210
constexpr coord_type GetX() const
Definition box2.h:203
constexpr size_type GetHeight() const
Definition box2.h:211
A typed sum value used to carry the before/after of any single property.
static DIFF_VALUE FromLayer(PCB_LAYER_ID aLayer)
static DIFF_VALUE FromDouble(double aValue)
static DIFF_VALUE FromEnum(int aValue, const std::string &aLabel)
DIFF_VALUE WithDisplayHint(DISPLAY_HINT aHint) const
Tag this value with a display hint and return a copy, so call sites can chain DIFF_VALUE::FromInt( w ...
static DIFF_VALUE FromInt64(int64_t aValue)
static DIFF_VALUE FromInt(int aValue)
static DIFF_VALUE FromBox2I(const BOX2I &aValue)
static DIFF_VALUE FromKiid(const KIID &aValue)
static DIFF_VALUE FromColor(const KIGFX::COLOR4D &aValue)
wxString ToDisplayString() const
Human-readable representation with no unit context.
static DIFF_VALUE FromBool(bool aValue)
static DIFF_VALUE FromString(const wxString &aValue)
std::vector< std::vector< std::vector< VECTOR2I > > > PolygonSet
static DIFF_VALUE FromPolygonSet(PolygonSet aValue)
static DIFF_VALUE FromVector2I(const VECTOR2I &aValue)
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
wxString ToCSSString() const
Definition color4d.cpp:146
Definition kiid.h:44
@ DEGREES_T
Definition eda_angle.h:31
@ F_Cu
Definition layer_ids.h:60
KICOMMON_API wxString MessageTextFromValue(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, double aValue, bool aAddUnitsText=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
A helper to convert the double length aValue to a string in inches, millimeters, or unscaled units.
@ COORD
Coordinate in internal units (PT_COORD)
@ DISTANCE
Length in internal units (PT_SIZE)
@ ANGLE
Angle in degrees (PT_DEGREE)
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_CASE(NoneRendersAsAngleBracketed)
#define CHECK_WX_EQ(actual, expected)
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683