KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_fp_lib_differ.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
27
31
32#include <footprint.h>
33#include <pad.h>
35#include <pcb_shape.h>
36
37#include <json_common.h>
38
39#include <wx/filename.h>
40
41
42using namespace KICAD_DIFF;
43
44
45BOOST_AUTO_TEST_SUITE( FpLibDiffer )
46
47
48static wxString getFixturePath()
49{
50 // qa/data/pcbnew/.. -> qa/data/libraries/Resistor_SMD.pretty
51 wxFileName fn( KI_TEST::GetPcbnewTestDataDir(), wxEmptyString );
52 fn.RemoveLastDir();
53 fn.AppendDir( wxS( "libraries" ) );
54 fn.AppendDir( wxS( "Resistor_SMD.pretty" ) );
55 return fn.GetPath();
56}
57
58
59BOOST_AUTO_TEST_CASE( IdenticalLibrariesDiffEmpty )
60{
61 auto [ownersA, mapA] = FP_LIB_DIFFER::LoadLibrary( getFixturePath() );
62 auto [ownersB, mapB] = FP_LIB_DIFFER::LoadLibrary( getFixturePath() );
63
64 BOOST_REQUIRE( !mapA.empty() );
65
66 FP_LIB_DIFFER differ( mapA, mapB, wxS( "Resistor_SMD.pretty" ) );
67 DOCUMENT_DIFF result = differ.Diff();
68
69 BOOST_CHECK( result.Empty() );
70 BOOST_CHECK_EQUAL( result.docType.ToStdString(), "pretty" );
71}
72
73
74BOOST_AUTO_TEST_CASE( FootprintRemovedShowsAsRemoved )
75{
76 auto [ownersA, mapA] = FP_LIB_DIFFER::LoadLibrary( getFixturePath() );
77 auto [ownersB, mapB] = FP_LIB_DIFFER::LoadLibrary( getFixturePath() );
78
79 BOOST_REQUIRE( !mapB.empty() );
80 mapB.erase( mapB.begin() );
81
82 FP_LIB_DIFFER differ( mapA, mapB );
83 DOCUMENT_DIFF result = differ.Diff();
84
85 BOOST_REQUIRE_EQUAL( result.changes.size(), 1u );
86 BOOST_CHECK( result.changes[0].kind == CHANGE_KIND::REMOVED );
87}
88
89
90BOOST_AUTO_TEST_CASE( FootprintChangesCarryDrawableBBoxes )
91{
92 auto [ownersA, mapA] = FP_LIB_DIFFER::LoadLibrary( getFixturePath() );
93 auto [ownersB, mapB] = FP_LIB_DIFFER::LoadLibrary( getFixturePath() );
94
95 BOOST_REQUIRE( !ownersB.empty() );
96 FOOTPRINT* subject = ownersB.front().get();
97 subject->Move( VECTOR2I( 1000, 0 ) );
98
99 FP_LIB_DIFFER differ( mapA, mapB );
100 DOCUMENT_DIFF result = differ.Diff();
101
102 BOOST_REQUIRE_EQUAL( result.changes.size(), 1u );
103 BOOST_CHECK( result.changes[0].kind == CHANGE_KIND::MODIFIED );
104 BOOST_CHECK_GT( result.changes[0].bbox.GetWidth(), 0 );
105 BOOST_CHECK_GT( result.changes[0].bbox.GetHeight(), 0 );
106}
107
108
109BOOST_AUTO_TEST_CASE( ExtractFootprintGeometryProducesLayeredContext )
110{
111 auto [owners, map] = FP_LIB_DIFFER::LoadLibrary( getFixturePath() );
112
113 BOOST_REQUIRE( !owners.empty() );
114
115 DOCUMENT_GEOMETRY geometry = ExtractFootprintGeometry( *owners.front(), KIGFX::COLOR4D( 0.38, 0.38, 0.38, 0.55 ) );
116
117 BOOST_CHECK( !geometry.Empty() );
118 BOOST_CHECK( GeometryLayerSet( geometry ).any() );
119}
120
121
122BOOST_AUTO_TEST_CASE( DiffJsonRoundTrip )
123{
124 auto [ownersA, mapA] = FP_LIB_DIFFER::LoadLibrary( getFixturePath() );
125 auto [ownersB, mapB] = FP_LIB_DIFFER::LoadLibrary( getFixturePath() );
126
127 if( !mapB.empty() )
128 mapB.erase( mapB.begin() );
129
130 FP_LIB_DIFFER differ( mapA, mapB, wxS( "lib.pretty" ) );
131 DOCUMENT_DIFF result = differ.Diff();
132
133 nlohmann::json j = result.ToJson();
135 BOOST_CHECK_EQUAL( back.changes.size(), result.changes.size() );
136}
137
138
139// LibraryItemKiidPath must be deterministic for the same footprint name
140// across calls -- differ + applier must agree on the synthetic UUID or
141// per-item resolution lookup silently misses.
142BOOST_AUTO_TEST_CASE( LibraryItemKiidPathIsDeterministicForSameName )
143{
144 KIID_PATH a = LibraryItemKiidPath( wxS( "R_0402_1005Metric" ) );
145 KIID_PATH b = LibraryItemKiidPath( wxS( "R_0402_1005Metric" ) );
146
147 BOOST_REQUIRE_EQUAL( a.size(), 1u );
148 BOOST_REQUIRE_EQUAL( b.size(), 1u );
149 BOOST_CHECK( a == b );
150}
151
152
153BOOST_AUTO_TEST_CASE( LibraryItemKiidPathDiffersForDifferentNames )
154{
155 KIID_PATH a = LibraryItemKiidPath( wxS( "R_0402_1005Metric" ) );
156 KIID_PATH b = LibraryItemKiidPath( wxS( "R_0603_1608Metric" ) );
157
158 BOOST_CHECK( !( a == b ) );
159}
160
161
162// Multiple removals: each footprint must surface as its own change record
163// rather than being merged.
164BOOST_AUTO_TEST_CASE( MultipleFootprintRemovalsAllEmitChanges )
165{
166 auto [ownersA, mapA] = FP_LIB_DIFFER::LoadLibrary( getFixturePath() );
167 auto [ownersB, mapB] = FP_LIB_DIFFER::LoadLibrary( getFixturePath() );
168
169 BOOST_REQUIRE_GE( mapB.size(), 3u );
170
171 std::vector<wxString> victims;
172
173 for( int i = 0; i < 3 && !mapB.empty(); ++i )
174 {
175 victims.push_back( mapB.begin()->first );
176 mapB.erase( mapB.begin() );
177 }
178
179 FP_LIB_DIFFER differ( mapA, mapB );
180 DOCUMENT_DIFF result = differ.Diff();
181
182 BOOST_CHECK_EQUAL( result.changes.size(), victims.size() );
183
184 for( const ITEM_CHANGE& c : result.changes )
185 {
187 }
188}
189
190
191// Output ordering must be deterministic for the same input pair so JSON
192// diffs are byte-stable across runs.
193BOOST_AUTO_TEST_CASE( OutputOrderingIsDeterministicAcrossRuns )
194{
195 auto [ownersA1, mapA1] = FP_LIB_DIFFER::LoadLibrary( getFixturePath() );
196 auto [ownersB1, mapB1] = FP_LIB_DIFFER::LoadLibrary( getFixturePath() );
197 auto [ownersA2, mapA2] = FP_LIB_DIFFER::LoadLibrary( getFixturePath() );
198 auto [ownersB2, mapB2] = FP_LIB_DIFFER::LoadLibrary( getFixturePath() );
199
200 BOOST_REQUIRE_GE( mapB1.size(), 2u );
201
202 mapB1.erase( mapB1.begin() );
203 mapB2.erase( mapB2.begin() );
204
205 FP_LIB_DIFFER differ1( mapA1, mapB1 );
206 FP_LIB_DIFFER differ2( mapA2, mapB2 );
207
208 DOCUMENT_DIFF r1 = differ1.Diff();
209 DOCUMENT_DIFF r2 = differ2.Diff();
210
211 BOOST_CHECK_EQUAL( r1.ToJson().dump(), r2.ToJson().dump() );
212}
213
214
215BOOST_AUTO_TEST_CASE( FootprintElementsFindTheEditedPad )
216{
217 auto [ownersA, mapA] = FP_LIB_DIFFER::LoadLibrary( getFixturePath() );
218 auto [ownersB, mapB] = FP_LIB_DIFFER::LoadLibrary( getFixturePath() );
219
220 BOOST_REQUIRE( !ownersB.empty() && !ownersB.front()->Pads().empty() );
221
222 PAD* pad = ownersB.front()->Pads().front();
223 pad->SetLocalClearance( pcbIUScale.mmToIU( 0.3 ) );
224
225 std::vector<LIB_ELEMENT> elements = DiffFootprintElements( ownersA.front().get(), ownersB.front().get() );
226
227 BOOST_REQUIRE_EQUAL( elements.size(), 1u );
228 BOOST_CHECK( elements[0].kind == CHANGE_KIND::MODIFIED );
229 BOOST_CHECK( elements[0].item == pad );
230}
231
232
233// The detail list reads the edited elements from the change, as it does for symbols
234BOOST_AUTO_TEST_CASE( DiffCarriesTheEditedPad )
235{
236 auto [ownersA, mapA] = FP_LIB_DIFFER::LoadLibrary( getFixturePath() );
237 auto [ownersB, mapB] = FP_LIB_DIFFER::LoadLibrary( getFixturePath() );
238
239 BOOST_REQUIRE( !ownersB.empty() && !ownersB.front()->Pads().empty() );
240
241 ownersB.front()->Pads().front()->SetLocalClearance( pcbIUScale.mmToIU( 0.3 ) );
242
243 DOCUMENT_DIFF result = FP_LIB_DIFFER( mapA, mapB ).Diff();
244
245 BOOST_REQUIRE_EQUAL( result.changes.size(), 1u );
246 BOOST_REQUIRE_EQUAL( result.changes[0].children.size(), 1u );
247 BOOST_CHECK( !result.changes[0].children[0].properties.empty() );
248}
249
250
251BOOST_AUTO_TEST_CASE( FootprintElementsFindAMovedPolygonCorner )
252{
253 const wxString path = KI_TEST::GetPcbnewTestDataDir() + "issue24045/QFN-24_L4.0-W4.0-P0.50-BL-EP2.6.kicad_mod";
254
256 wxString name;
257 std::unique_ptr<FOOTPRINT> before = io.ImportFootprint( path, name );
258 std::unique_ptr<FOOTPRINT> after = io.ImportFootprint( path, name );
259
260 BOOST_REQUIRE( before && after );
261
262 PCB_SHAPE* polygon = nullptr;
263
264 for( BOARD_ITEM* item : after->GraphicalItems() )
265 {
266 if( item->Type() == PCB_SHAPE_T && static_cast<PCB_SHAPE*>( item )->GetShape() == SHAPE_T::POLY )
267 polygon = static_cast<PCB_SHAPE*>( item );
268 }
269
271
272 // Pulling an inner corner inwards keeps the position and bounding box, so only the geometry differs
273 polygon->GetPolyShape().SetVertex( 2, polygon->GetPolyShape().CVertex( 2 ) + VECTOR2I( 50000, 50000 ) );
274
275 std::vector<LIB_ELEMENT> elements = DiffFootprintElements( before.get(), after.get() );
276
277 BOOST_REQUIRE_EQUAL( elements.size(), 1u );
278 BOOST_CHECK( elements[0].kind == CHANGE_KIND::MODIFIED );
279 BOOST_CHECK( elements[0].item == polygon );
280}
281
282
const char * name
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:128
General utilities for PCB file IO for QA programs.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:99
SHAPE_T GetShape() const
Definition eda_shape.h:178
void Move(const VECTOR2I &aMoveVector) override
Move this object.
Diff two .pretty footprint library directories.
static std::pair< std::vector< std::unique_ptr< FOOTPRINT > >, FOOTPRINT_MAP > LoadLibrary(const wxString &aPrettyPath)
Load a .pretty directory into a FOOTPRINT_MAP.
DOCUMENT_DIFF Diff() override
Produce a DOCUMENT_DIFF of the inputs the concrete differ was constructed with.
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
Definition pad.h:61
A PCB_IO derivation for saving and loading Pcbnew s-expression formatted files.
std::unique_ptr< FOOTPRINT > ImportFootprint(const wxString &aFootprintPath, wxString &aFootprintNameOut, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Load a single footprint from aFootprintPath and put its name in aFootprintNameOut.
A type-safe container of any type.
Definition ki_any.h:92
LSET GeometryLayerSet(const DOCUMENT_GEOMETRY &aGeometry)
Return the union of every non-empty layer set carried by the geometry.
std::vector< LIB_ELEMENT > DiffFootprintElements(const FOOTPRINT *aBefore, const FOOTPRINT *aAfter)
Compare the elements of two library footprints.
KIID_PATH LibraryItemKiidPath(const wxString &aName)
Build a deterministic synthetic KIID_PATH from a library item name (symbol name or footprint name).
DOCUMENT_GEOMETRY ExtractFootprintGeometry(const FOOTPRINT &aFootprint, const KIGFX::COLOR4D &aColor)
Extract drawable context geometry from a single FOOTPRINT.
std::string GetPcbnewTestDataDir()
Utility which returns a path to the data directory where the test board files are stored.
The full set of changes between two parsed documents of one type.
nlohmann::json ToJson() const
static DOCUMENT_DIFF FromJson(const nlohmann::json &aJson)
std::vector< ITEM_CHANGE > changes
Aggregate of background geometry extracted from one source document.
Definition diff_scene.h:163
One change record on a single item.
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
BOOST_REQUIRE_EQUAL(rows.size(), 1)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_REQUIRE(res.columns()==1)
static wxString getFixturePath()
BOOST_AUTO_TEST_CASE(IdenticalLibrariesDiffEmpty)
BOOST_AUTO_TEST_SUITE_END()
std::string path
static wxString getFixturePath()
wxString result
Test unit parsing edge cases and error handling.
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition typeinfo.h:80
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708
static PIECE polygon(std::vector< VIA_CROSS_SECTION_GEOMETRY::POINT > aPoints, bool aMixed=false)