KiCad PCB EDA Suite
test_sch_sheet.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 (C) 2019-2021 KiCad Developers, see CHANGELOG.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 
30 
31 // Code under test
32 #include <sch_sheet.h>
33 #include <sch_sheet_pin.h>
34 #include <schematic.h>
35 
36 #include "uuid_test_utils.h"
37 
39 
41 {
42 public:
44  m_schematic( nullptr ),
45  m_sheet(),
46  m_csheet( m_sheet )
47  {
48  }
49 
52 
54 
57 };
58 
59 
64 std::ostream& operator<<( std::ostream& os, DANGLING_END_ITEM const& d )
65 {
66  os << "DANGLING_END_ITEM[ type " << d.GetType() << " @(" << d.GetPosition().x << ", "
67  << d.GetPosition().y << "), item " << d.GetItem() << ", parent " << d.GetParent() << " ]";
68  return os;
69 }
70 
74 BOOST_FIXTURE_TEST_SUITE( SchSheet, TEST_SCH_SHEET_FIXTURE )
75 
76 
77 
81 {
82  BOOST_CHECK_EQUAL( m_csheet.GetPosition(), wxPoint( 0, 0 ) );
83 
84  BOOST_CHECK_EQUAL( m_sheet.GetParent(), nullptr );
85  BOOST_CHECK_EQUAL( m_sheet.CountSheets(), 1 );
86 
87  BOOST_CHECK_EQUAL( m_csheet.GetScreenCount(), 0 );
88 
89  BOOST_CHECK_EQUAL( m_sheet.SymbolCount(), 0 );
90 }
91 
95 BOOST_AUTO_TEST_CASE( SchematicParent )
96 {
97  m_sheet.SetParent( &m_schematic );
98 
99  BOOST_CHECK_EQUAL( m_sheet.IsRootSheet(), false );
100 
101  m_schematic.SetRoot( &m_sheet );
102 
103  BOOST_CHECK_EQUAL( m_sheet.IsRootSheet(), true );
104 }
105 
110 {
111  const wxPoint pinPos{ 42, 13 };
112 
113  // we should catch null insertions
114  CHECK_WX_ASSERT( m_sheet.AddPin( nullptr ) );
115 
116  auto newPin = std::make_unique<SCH_SHEET_PIN>( &m_sheet, pinPos, "pinname" );
117 
118  // can't be const because of RemovePin (?!)
119  SCH_SHEET_PIN& pinRef = *newPin;
120 
121  m_sheet.AddPin( newPin.release() );
122 
123  // now we can find it in the list
124  BOOST_CHECK_EQUAL( m_sheet.HasPins(), true );
125  BOOST_CHECK_EQUAL( m_sheet.HasPin( "pinname" ), true );
126  BOOST_CHECK_EQUAL( m_sheet.HasPin( "PINname" ), true );
127 
128  BOOST_CHECK_EQUAL( m_sheet.GetPin( pinPos ), &pinRef );
129 
130  // check the actual list can be retrieved
131  std::vector<SCH_SHEET_PIN*>& pins = m_sheet.GetPins();
132  BOOST_CHECK_EQUAL( pins[0], &pinRef );
133 
134  // catch the bad call
135  CHECK_WX_ASSERT( m_sheet.RemovePin( nullptr ) );
136 
137  m_sheet.RemovePin( &pinRef );
138 
139  // and it's gone
140  BOOST_CHECK_EQUAL( m_sheet.HasPins(), false );
141  BOOST_CHECK_EQUAL( m_sheet.HasPin( "pinname" ), false );
142  BOOST_CHECK_EQUAL( m_sheet.GetPin( pinPos ), nullptr );
143 
144  delete &pinRef;
145 }
146 
150 BOOST_AUTO_TEST_CASE( PinRenumbering )
151 {
152  for( int i = 0; i < 5; ++i )
153  {
154  SCH_SHEET_PIN* pin = new SCH_SHEET_PIN( &m_sheet, wxPoint{ i, i }, "name" );
155 
156  // set the pins to have the same number going in
157  pin->SetNumber( 2 );
158 
159  m_sheet.AddPin( pin );
160  }
161 
162  std::vector<SCH_SHEET_PIN*>& pins = m_sheet.GetPins();
163 
164  std::vector<int> numbers;
165 
166  for( SCH_SHEET_PIN* pin : pins )
167  numbers.push_back( pin->GetNumber() );
168 
169  // and now...they are all unique
170  BOOST_CHECK_PREDICATE( KI_TEST::CollectionHasNoDuplicates<decltype( numbers )>, ( numbers ) );
171 }
172 
173 
175 {
176  std::string m_pin_name;
177  wxPoint m_pos;
178 };
179 
180 
185 BOOST_AUTO_TEST_CASE( EndconnectionPoints )
186 {
187  // x = zero because the pin is clamped to the left side by default
188  const std::vector<TEST_END_CONN_PIN> pin_defs = {
189  {
190  "1name",
191  { 0, 13 },
192  },
193  {
194  "2name",
195  { 0, 130 },
196  },
197  };
198 
199  // Insert the pins into the sheet
200  for( const auto& pin : pin_defs )
201  m_sheet.AddPin( new SCH_SHEET_PIN( &m_sheet, pin.m_pos, pin.m_pin_name ) );
202 
203  std::vector<SCH_SHEET_PIN*>& pins = m_sheet.GetPins();
204 
205  // make sure the pins made it in
206  BOOST_CHECK_EQUAL( pins.size(), pin_defs.size() );
207 
208  // Check that the EndPoint getter gets the right things
209  {
210  std::vector<DANGLING_END_ITEM> expectedDangling;
211 
212  // Construct expected from the pin, not defs, as we need the pin address
213  for( SCH_SHEET_PIN* pin : pins )
214  {
215  expectedDangling.emplace_back( DANGLING_END_T::SHEET_LABEL_END, pin,
216  pin->GetPosition(), pin );
217  }
218 
219  std::vector<DANGLING_END_ITEM> dangling;
220  m_sheet.GetEndPoints( dangling );
221 
222  BOOST_CHECK_EQUAL_COLLECTIONS( dangling.begin(), dangling.end(),
223  expectedDangling.begin(), expectedDangling.end() );
224  }
225 
226  // And check the connection getter
227  {
228  std::vector<wxPoint> expectedConnections;
229 
230  // we want to see every pin that we just added
231  for( const auto& pin : pin_defs )
232  {
233  expectedConnections.push_back( pin.m_pos );
234  }
235 
236  std::vector<wxPoint> connections = m_sheet.GetConnectionPoints();
237 
238  BOOST_CHECK_EQUAL_COLLECTIONS( connections.begin(), connections.end(),
239  expectedConnections.begin(), expectedConnections.end() );
240  }
241 }
242 
243 
244 BOOST_AUTO_TEST_SUITE_END()
wxPoint GetPosition() const
Definition: sch_item.h:129
Holds all the data relating to one schematic.
Definition: schematic.h:59
const EDA_ITEM * GetParent() const
Definition: sch_item.h:131
BOOST_AUTO_TEST_CASE(Default)
Declare the test suite.
#define CHECK_WX_ASSERT(STATEMENT)
A test macro to check a wxASSERT is thrown.
const SCH_SHEET & m_csheet
Can use when you need a const ref (lots of places need fixing here)
EDA_ITEM * GetItem() const
Definition: sch_item.h:130
Define a sheet pin (label) used in sheets to create hierarchical schematics.
Definition: sch_sheet_pin.h:65
SCHEMATIC m_schematic
Dummy schematic to attach the test sheet to
bool CollectionHasNoDuplicates(const T &aCollection)
Predicate to check a collection has no duplicate elements.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:54
DANGLING_END_T GetType() const
Definition: sch_item.h:132
std::string m_pin_name
Helper class used to store the state of schematic items that can be connected to other schematic item...
Definition: sch_item.h:87
std::ostream & operator<<(std::ostream &os, DANGLING_END_ITEM const &d)
Print helper.
Test utilities for timestamps.