1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2019-2021 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 /**
25  * @file
26  * Test suite for LIB_SYMBOL
27  */
28 
29 #include <qa_utils/wx_utils/unit_test_utils.h>
30 
31 // Code under test
32 #include <lib_shape.h>
33 #include <lib_pin.h>
34 
35 #include "lib_field_test_utils.h"
36 
37 class TEST_LIB_SYMBOL_FIXTURE
38 {
39 public:
TEST_LIB_SYMBOL_FIXTURE()40     TEST_LIB_SYMBOL_FIXTURE() :
41         m_part_no_data( "part_name", nullptr )
42     {
43     }
44 
45     ///> Part with no extra data set
46     LIB_SYMBOL m_part_no_data;
47 };
48 
49 
50 /**
51  * Declare the test suite
52  */
BOOST_FIXTURE_TEST_SUITE(LibPart,TEST_LIB_SYMBOL_FIXTURE)53 BOOST_FIXTURE_TEST_SUITE( LibPart, TEST_LIB_SYMBOL_FIXTURE )
54 
55 
56 /**
57  * Check that we can get the basic properties out as expected
58  */
59 BOOST_AUTO_TEST_CASE( DefaultProperties )
60 {
61     BOOST_CHECK_EQUAL( m_part_no_data.GetName(), "part_name" );
62 
63     // Didn't set a library, so this is empty
64     BOOST_CHECK_EQUAL( m_part_no_data.GetLibraryName(), "" );
65     BOOST_CHECK_EQUAL( m_part_no_data.GetLib(), nullptr );
66 
67     // only get the root
68     BOOST_CHECK_EQUAL( m_part_no_data.IsRoot(), true );
69     BOOST_CHECK_EQUAL( m_part_no_data.IsAlias(), false );
70     BOOST_CHECK_EQUAL( m_part_no_data.SharedPtr().use_count(), 2 );
71 
72     // no sub units
73     BOOST_CHECK_EQUAL( m_part_no_data.GetUnitCount(), 1 );
74     BOOST_CHECK_EQUAL( m_part_no_data.IsMulti(), false );
75 
76     // no conversion
77     BOOST_CHECK_EQUAL( m_part_no_data.HasConversion(), false );
78 }
79 
80 
81 /**
82  * Check the drawings on a "blank" LIB_SYMBOL
83  */
BOOST_AUTO_TEST_CASE(DefaultDrawings)84 BOOST_AUTO_TEST_CASE( DefaultDrawings )
85 {
86     // default drawings exist
87     BOOST_CHECK_EQUAL( m_part_no_data.GetDrawItems().size(), 4 );
88     BOOST_CHECK_EQUAL( m_part_no_data.GetNextDrawItem( nullptr, LIB_PIN_T ), nullptr );
89 }
90 
91 
92 /**
93  * Check the default fields are present as expected
94  */
BOOST_AUTO_TEST_CASE(DefaultFields)95 BOOST_AUTO_TEST_CASE( DefaultFields )
96 {
97     std::vector<LIB_FIELD> fields;
98     m_part_no_data.GetFields( fields );
99 
100     // Should get the 4 default fields
101     BOOST_CHECK_PREDICATE( KI_TEST::AreDefaultFieldsCorrect, ( fields ) );
102 
103     // but no more (we didn't set them)
104     BOOST_CHECK_EQUAL( fields.size(), MANDATORY_FIELD_T::MANDATORY_FIELDS );
105 
106     // also check the default field accessors
107     BOOST_CHECK_PREDICATE( KI_TEST::FieldNameIdMatches,
108             ( m_part_no_data.GetReferenceField() )( "Reference" )( MANDATORY_FIELD_T::REFERENCE_FIELD ) );
109     BOOST_CHECK_PREDICATE( KI_TEST::FieldNameIdMatches,
110             ( m_part_no_data.GetValueField() )( "Value" )( MANDATORY_FIELD_T::VALUE_FIELD ) );
111     BOOST_CHECK_PREDICATE( KI_TEST::FieldNameIdMatches,
112             ( m_part_no_data.GetFootprintField() )( "Footprint" )( MANDATORY_FIELD_T::FOOTPRINT_FIELD ) );
113 }
114 
115 
116 /**
117  * Test adding fields to a LIB_SYMBOL
118  */
BOOST_AUTO_TEST_CASE(AddedFields)119 BOOST_AUTO_TEST_CASE( AddedFields )
120 {
121     std::vector<LIB_FIELD> fields;
122     m_part_no_data.GetFields( fields );
123 
124     // Ctor takes non-const ref (?!)
125     const std::string newFieldName = "new_field";
126     wxString          nonConstNewFieldName = newFieldName;
127     fields.push_back( LIB_FIELD( 42, nonConstNewFieldName ) );
128 
129     // fairly roundabout way to add a field, but it is what it is
130     m_part_no_data.SetFields( fields );
131 
132     // Should get the 4 default fields
133     BOOST_CHECK_PREDICATE( KI_TEST::AreDefaultFieldsCorrect, ( fields ) );
134 
135     // and our new one
136     BOOST_REQUIRE_EQUAL( fields.size(), MANDATORY_FIELD_T::MANDATORY_FIELDS + 1 );
137 
138     BOOST_CHECK_PREDICATE( KI_TEST::FieldNameIdMatches,
139             ( fields[MANDATORY_FIELD_T::MANDATORY_FIELDS] )( newFieldName )( 42 ) );
140 
141     // Check by-id lookup
142 
143     LIB_FIELD* gotNewField = m_part_no_data.GetFieldById( 42 );
144 
145     BOOST_REQUIRE_NE( gotNewField, nullptr );
146 
147     BOOST_CHECK_PREDICATE( KI_TEST::FieldNameIdMatches, ( *gotNewField )( newFieldName )( 42 ) );
148 
149     // Check by-name lookup
150 
151     gotNewField = m_part_no_data.FindField( newFieldName );
152 
153     BOOST_REQUIRE_NE( gotNewField, nullptr );
154     BOOST_CHECK_PREDICATE( KI_TEST::FieldNameIdMatches, ( *gotNewField )( newFieldName )( 42 ) );
155 }
156 
157 
158 /**
159  * Test adding draw items to a LIB_SYMBOL
160  */
BOOST_AUTO_TEST_CASE(AddedDrawItems)161 BOOST_AUTO_TEST_CASE( AddedDrawItems )
162 {
163 }
164 
165 
166 struct TEST_LIB_SYMBOL_SUBREF_CASE
167 {
168     int         m_index;
169     bool        m_addSep;
170     std::string m_expSubRef;
171 };
172 
173 
174 /**
175  * Test the subreference indexing
176  */
BOOST_AUTO_TEST_CASE(SubReference)177 BOOST_AUTO_TEST_CASE( SubReference )
178 {
179     const std::vector<TEST_LIB_SYMBOL_SUBREF_CASE> cases = {
180         {
181             1,
182             false,
183             "A",
184         },
185         {
186             2,
187             false,
188             "B",
189         },
190         {
191             26,
192             false,
193             "Z",
194         },
195         {
196             27,
197             false,
198             "AA",
199         },
200         { // haven't configured a separator, so should be nothing
201             1,
202             true,
203             "A",
204         },
205     };
206 
207     for( const auto& c : cases )
208     {
209         BOOST_TEST_CONTEXT(
210                 "Subref: " << c.m_index << ", " << c.m_addSep << " -> '" << c.m_expSubRef << "'" )
211         {
212             const auto subref = m_part_no_data.SubReference( c.m_index, c.m_addSep );
213             BOOST_CHECK_EQUAL( subref, c.m_expSubRef );
214         }
215     }
216 }
217 
218 
219 /**
220  * Check the compare method.
221  */
BOOST_AUTO_TEST_CASE(Compare)222 BOOST_AUTO_TEST_CASE( Compare )
223 {
224     // Identical root part to m_part_no_data sans time stamp.
225     LIB_SYMBOL testPart( "part_name" );
226 
227     // Self comparison test.
228     BOOST_CHECK_EQUAL( m_part_no_data.Compare( m_part_no_data ), 0 );
229 
230     // Test for identical LIB_SYMBOL.
231     BOOST_CHECK_EQUAL( m_part_no_data.Compare( testPart ), 0 );
232 
233     // Test name.
234     testPart.SetName( "tart_name" );
235     BOOST_CHECK( m_part_no_data.Compare( testPart ) < 0 );
236     testPart.SetName( "cart_name" );
237     BOOST_CHECK( m_part_no_data.Compare( testPart ) > 0 );
238     testPart.SetName( "part_name" );
239 
240     // LIB_ID comparison tests.
241     LIB_ID id = testPart.GetLibId();
242     id.SetLibItemName( "tart_name" );
243     testPart.SetLibId( id );
244     BOOST_CHECK( m_part_no_data.Compare( testPart ) < 0 );
245     id.SetLibItemName( "cart_name" );
246     testPart.SetLibId( id );
247     BOOST_CHECK( m_part_no_data.Compare( testPart ) > 0 );
248     id.SetLibItemName( "part_name" );
249     testPart.SetLibId( id );
250 
251     // Unit count comparison tests.
252     testPart.SetUnitCount( 2 );
253     BOOST_CHECK( m_part_no_data.Compare( testPart ) < 0 );
254     testPart.SetUnitCount( 1 );
255     m_part_no_data.SetUnitCount( 2 );
256     BOOST_CHECK( m_part_no_data.Compare( testPart ) > 0 );
257     m_part_no_data.SetUnitCount( 1 );
258 
259     // Options flag comparison tests.
260     testPart.SetPower();
261     BOOST_CHECK( m_part_no_data.Compare( testPart ) < 0 );
262     testPart.SetNormal();
263     m_part_no_data.SetPower();
264     BOOST_CHECK( m_part_no_data.Compare( testPart ) > 0 );
265     m_part_no_data.SetNormal();
266 
267     // Draw item list size comparison tests.
268     testPart.AddDrawItem( new LIB_SHAPE( &testPart, SHAPE_T::RECT ) );
269     m_part_no_data.AddDrawItem( new LIB_SHAPE( &m_part_no_data, SHAPE_T::RECT ) );
270     BOOST_CHECK_EQUAL( m_part_no_data.Compare( testPart ), 0 );
271     m_part_no_data.RemoveDrawItem( m_part_no_data.GetNextDrawItem( nullptr, LIB_SHAPE_T ) );
272     BOOST_CHECK( m_part_no_data.Compare( testPart ) < 0 );
273     testPart.RemoveDrawItem( testPart.GetNextDrawItem( nullptr, LIB_SHAPE_T ) );
274     m_part_no_data.AddDrawItem( new LIB_SHAPE( &m_part_no_data, SHAPE_T::RECT ) );
275     BOOST_CHECK( m_part_no_data.Compare( testPart ) > 0 );
276     m_part_no_data.RemoveDrawItem( m_part_no_data.GetNextDrawItem( nullptr, LIB_SHAPE_T ) );
277 
278     // Draw item list contents comparison tests.
279     testPart.AddDrawItem( new LIB_SHAPE( &testPart, SHAPE_T::RECT ) );
280     m_part_no_data.AddDrawItem( new LIB_SHAPE( &m_part_no_data, SHAPE_T::ARC ) );
281     BOOST_CHECK( m_part_no_data.Compare( testPart ) > 0 );
282     m_part_no_data.RemoveDrawItem( m_part_no_data.GetNextDrawItem( nullptr, LIB_SHAPE_T ) );
283     m_part_no_data.AddDrawItem( new LIB_PIN( &m_part_no_data ) );
284     BOOST_CHECK( m_part_no_data.Compare( testPart ) > 0 );
285     m_part_no_data.RemoveDrawItem( m_part_no_data.GetNextDrawItem( nullptr, LIB_PIN_T ) );
286     testPart.RemoveDrawItem( testPart.GetNextDrawItem( nullptr, LIB_SHAPE_T ) );
287 
288     // Footprint filter array comparison tests.
289     wxArrayString footPrintFilters;
290     BOOST_CHECK( m_part_no_data.GetFPFilters() == footPrintFilters );
291     footPrintFilters.Add( "b" );
292     testPart.SetFPFilters( footPrintFilters );
293     BOOST_CHECK( m_part_no_data.Compare( testPart ) < 0 );
294     m_part_no_data.SetFPFilters( footPrintFilters );
295     footPrintFilters.Clear();
296     testPart.SetFPFilters( footPrintFilters );
297     BOOST_CHECK( m_part_no_data.Compare( testPart ) > 0 );
298     footPrintFilters.Clear();
299     m_part_no_data.SetFPFilters( footPrintFilters );
300     testPart.SetFPFilters( footPrintFilters );
301 
302     // Description string tests.
303     m_part_no_data.SetDescription( "b" );
304     testPart.SetDescription( "b" );
305     BOOST_CHECK_EQUAL( m_part_no_data.Compare( testPart ), 0 );
306     m_part_no_data.SetDescription( "a" );
307     BOOST_CHECK( m_part_no_data.Compare( testPart ) < 0 );
308     m_part_no_data.SetDescription( "c" );
309     BOOST_CHECK( m_part_no_data.Compare( testPart ) > 0 );
310     m_part_no_data.SetDescription( wxEmptyString );
311     testPart.SetDescription( wxEmptyString );
312 
313     // Key word string tests.
314     m_part_no_data.SetKeyWords( "b" );
315     testPart.SetKeyWords( "b" );
316     BOOST_CHECK_EQUAL( m_part_no_data.Compare( testPart ), 0 );
317     m_part_no_data.SetKeyWords( "a" );
318     BOOST_CHECK( m_part_no_data.Compare( testPart ) < 0 );
319     m_part_no_data.SetKeyWords( "c" );
320     BOOST_CHECK( m_part_no_data.Compare( testPart ) > 0 );
321     m_part_no_data.SetKeyWords( wxEmptyString );
322     testPart.SetKeyWords( wxEmptyString );
323 
324     // Pin name offset comparison tests.
325     testPart.SetPinNameOffset( testPart.GetPinNameOffset() + 1 );
326     BOOST_CHECK( m_part_no_data.Compare( testPart ) < 0 );
327     testPart.SetPinNameOffset( testPart.GetPinNameOffset() - 2 );
328     BOOST_CHECK( m_part_no_data.Compare( testPart ) > 0 );
329     testPart.SetPinNameOffset( testPart.GetPinNameOffset() + 1 );
330 
331     // Units locked flag comparison tests.
332     testPart.LockUnits( true );
333     BOOST_CHECK( m_part_no_data.Compare( testPart ) < 0 );
334     testPart.LockUnits( false );
335     m_part_no_data.LockUnits( true );
336     BOOST_CHECK( m_part_no_data.Compare( testPart ) > 0 );
337     m_part_no_data.LockUnits( false );
338 
339     // Include in BOM support tests.
340     testPart.SetIncludeInBom( false );
341     BOOST_CHECK( m_part_no_data.Compare( testPart ) > 0 );
342     testPart.SetIncludeInBom( true );
343     m_part_no_data.SetIncludeInBom( false );
344     BOOST_CHECK( m_part_no_data.Compare( testPart ) < 0 );
345     m_part_no_data.SetIncludeInBom( true );
346 
347     // Include on board support tests.
348     testPart.SetIncludeOnBoard( false );
349     BOOST_CHECK( m_part_no_data.Compare( testPart ) > 0 );
350     testPart.SetIncludeOnBoard( true );
351     m_part_no_data.SetIncludeOnBoard( false );
352     BOOST_CHECK( m_part_no_data.Compare( testPart ) < 0 );
353     m_part_no_data.SetIncludeOnBoard( true );
354 
355     // Show pin names flag comparison tests.
356     m_part_no_data.SetShowPinNames( false );
357     BOOST_CHECK( m_part_no_data.Compare( testPart ) < 0 );
358     m_part_no_data.SetShowPinNames( true );
359     testPart.SetShowPinNames( false );
360     BOOST_CHECK( m_part_no_data.Compare( testPart ) > 0 );
361     testPart.SetShowPinNames( true );
362 
363     // Show pin numbers flag comparison tests.
364     m_part_no_data.SetShowPinNumbers( false );
365     BOOST_CHECK( m_part_no_data.Compare( testPart ) < 0 );
366     m_part_no_data.SetShowPinNumbers( true );
367     testPart.SetShowPinNumbers( false );
368     BOOST_CHECK( m_part_no_data.Compare( testPart ) > 0 );
369     testPart.SetShowPinNumbers( true );
370 
371     // Time stamp comparison tests.
372 
373     // Check to see if we broke the copy ctor.
374     LIB_SYMBOL* copy = new LIB_SYMBOL( testPart );
375     BOOST_CHECK( testPart.Compare( *copy ) == 0 );
376 }
377 
378 
379 /**
380  * Check the fetch unit items code.
381  */
BOOST_AUTO_TEST_CASE(GetUnitItems)382 BOOST_AUTO_TEST_CASE( GetUnitItems )
383 {
384     // There are no unit draw items in the empty LIB_SYMBOL object.
385     BOOST_CHECK( m_part_no_data.GetUnitDrawItems( 1, 1 ).size() == 0 );
386 
387     // A single unique unit with 1 pin common to all units and all body styles.
388     LIB_PIN* pin1 = new LIB_PIN( &m_part_no_data );
389     m_part_no_data.AddDrawItem( pin1 );
390     BOOST_CHECK( m_part_no_data.GetUnitDrawItems( 0, 0 ).size() == 1 );
391 
392     // A single unique unit with 1 pin in unit 1 and common to all body styles.
393     pin1->SetUnit( 1 );
394     BOOST_CHECK( m_part_no_data.GetUnitDrawItems( 1, 0 ).size() == 1 );
395 
396     // A single unique unit with 1 pin in unit 1 and body style 1.
397     pin1->SetConvert( 1 );
398     BOOST_CHECK( m_part_no_data.GetUnitDrawItems( 1, 1 ).size() == 1 );
399 
400     // Two unique units with pin 1 assigned to unit 1 and body style 1 and pin 2 assigned to
401     // unit 2 and body style 1.
402     LIB_PIN* pin2 = new LIB_PIN( &m_part_no_data );
403     m_part_no_data.SetUnitCount( 2 );
404     pin2->SetUnit( 2 );
405     pin2->SetConvert( 2 );
406     pin2->SetNumber( "4" );
407     m_part_no_data.AddDrawItem( pin2 );
408     BOOST_CHECK( m_part_no_data.GetUnitDrawItems( 2, 2 ).size() == 1 );
409 
410     // Make pin 1 body style common to all units.
411     pin1->SetConvert( 0 );
412     BOOST_CHECK( m_part_no_data.GetUnitDrawItems( 1, 1 ).size() == 0 );
413     BOOST_CHECK( m_part_no_data.GetUnitDrawItems( 2, 1 ).size() == 1 );
414 
415     m_part_no_data.RemoveDrawItem( pin2 );
416     m_part_no_data.RemoveDrawItem( pin1 );
417     m_part_no_data.RemoveDrawItem( m_part_no_data.GetNextDrawItem() );
418 }
419 
420 
421 /**
422  * Check the fetch unit draw items code.
423  */
BOOST_AUTO_TEST_CASE(GetUnitDrawItems)424 BOOST_AUTO_TEST_CASE( GetUnitDrawItems )
425 {
426     // There are no unit draw items in the empty LIB_SYMBOL object.
427     BOOST_CHECK( m_part_no_data.GetUnitDrawItems().size() == 0 );
428 
429     // A single unique unit with 1 pin common to all units and all body styles.
430     LIB_PIN* pin1 = new LIB_PIN( &m_part_no_data );
431     pin1->SetNumber( "1" );
432     m_part_no_data.AddDrawItem( pin1 );
433     std::vector<struct LIB_SYMBOL_UNIT> units = m_part_no_data.GetUnitDrawItems();
434     BOOST_CHECK( units.size() == 1 );
435     BOOST_CHECK( units[0].m_unit == 0 );
436     BOOST_CHECK( units[0].m_convert == 0 );
437     BOOST_CHECK( units[0].m_items[0] == pin1 );
438 }
439 
440 
441 /**
442  * Check inheritance support.
443  */
BOOST_AUTO_TEST_CASE(Inheritance)444 BOOST_AUTO_TEST_CASE( Inheritance )
445 {
446     std::unique_ptr<LIB_SYMBOL> parent = std::make_unique<LIB_SYMBOL>( "parent" );
447     BOOST_CHECK( parent->IsRoot() );
448     std::unique_ptr<LIB_SYMBOL> child1 = std::make_unique<LIB_SYMBOL>( "child1", parent.get() );
449     BOOST_CHECK( child1->IsAlias() );
450     LIB_SYMBOL_SPTR parentRef = child1->GetParent().lock();
451     BOOST_CHECK( parentRef );
452     BOOST_CHECK( parentRef == parent->SharedPtr() );
453     BOOST_CHECK_EQUAL( parent->SharedPtr().use_count(), 3 );
454     BOOST_CHECK_EQUAL( child1->GetUnitCount(), 1 );
455     parent->SetUnitCount( 4 );
456     BOOST_CHECK_EQUAL( child1->GetUnitCount(), 4 );
457     child1->SetParent();
458     BOOST_CHECK_EQUAL( child1->GetUnitCount(), 1 );
459     parentRef.reset();
460     BOOST_CHECK_EQUAL( parent->SharedPtr().use_count(), 2 );
461 }
462 
463 
464 /**
465  * Check the copy constructor.
466  */
BOOST_AUTO_TEST_CASE(CopyConstructor)467 BOOST_AUTO_TEST_CASE( CopyConstructor )
468 {
469     std::shared_ptr<LIB_SYMBOL> copy = std::make_shared<LIB_SYMBOL>( m_part_no_data );
470     BOOST_CHECK( m_part_no_data == *copy.get() );
471 }
472 
473 
474 BOOST_AUTO_TEST_SUITE_END()
475