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-2020 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 <schematic.h>
34 
35 #include "uuid_test_utils.h"
36 
38 
40 {
41 public:
43  m_schematic( nullptr ),
44  m_sheet(),
45  m_csheet( m_sheet )
46  {
47  }
48 
51 
53 
56 };
57 
58 
63 std::ostream& operator<<( std::ostream& os, DANGLING_END_ITEM const& d )
64 {
65  os << "DANGLING_END_ITEM[ type " << d.GetType() << " @(" << d.GetPosition().x << ", "
66  << d.GetPosition().y << "), item " << d.GetItem() << ", parent " << d.GetParent() << " ]";
67  return os;
68 }
69 
73 BOOST_FIXTURE_TEST_SUITE( SchSheet, TEST_SCH_SHEET_FIXTURE )
74 
75 
76 
80 {
81  BOOST_CHECK_EQUAL( m_csheet.GetPosition(), wxPoint( 0, 0 ) );
82 
83  BOOST_CHECK_EQUAL( m_sheet.GetParent(), nullptr );
84  BOOST_CHECK_EQUAL( m_sheet.CountSheets(), 1 );
85 
86  BOOST_CHECK_EQUAL( m_csheet.GetScreenCount(), 0 );
87 
88  BOOST_CHECK_EQUAL( m_sheet.ComponentCount(), 0 );
89 }
90 
94 BOOST_AUTO_TEST_CASE( SchematicParent )
95 {
96  m_sheet.SetParent( &m_schematic );
97 
98  BOOST_CHECK_EQUAL( m_sheet.IsRootSheet(), false );
99 
100  m_schematic.SetRoot( &m_sheet );
101 
102  BOOST_CHECK_EQUAL( m_sheet.IsRootSheet(), true );
103 }
104 
109 {
110  const wxPoint pinPos{ 42, 13 };
111 
112  // we should catch null insertions
113  CHECK_WX_ASSERT( m_sheet.AddPin( nullptr ) );
114 
115  auto newPin = std::make_unique<SCH_SHEET_PIN>( &m_sheet, pinPos, "pinname" );
116 
117  // can't be const because of RemovePin (?!)
118  SCH_SHEET_PIN& pinRef = *newPin;
119 
120  m_sheet.AddPin( newPin.release() );
121 
122  // now we can find it in the list
123  BOOST_CHECK_EQUAL( m_sheet.HasPins(), true );
124  BOOST_CHECK_EQUAL( m_sheet.HasPin( "pinname" ), true );
125  BOOST_CHECK_EQUAL( m_sheet.HasPin( "PINname" ), true );
126 
127  BOOST_CHECK_EQUAL( m_sheet.GetPin( pinPos ), &pinRef );
128 
129  // check the actual list can be retrieved
130  std::vector<SCH_SHEET_PIN*>& pins = m_sheet.GetPins();
131  BOOST_CHECK_EQUAL( pins[0], &pinRef );
132 
133  // catch the bad call
134  CHECK_WX_ASSERT( m_sheet.RemovePin( nullptr ) );
135 
136  m_sheet.RemovePin( &pinRef );
137 
138  // and it's gone
139  BOOST_CHECK_EQUAL( m_sheet.HasPins(), false );
140  BOOST_CHECK_EQUAL( m_sheet.HasPin( "pinname" ), false );
141  BOOST_CHECK_EQUAL( m_sheet.GetPin( pinPos ), nullptr );
142 }
143 
147 BOOST_AUTO_TEST_CASE( PinRenumbering )
148 {
149  for( int i = 0; i < 5; ++i )
150  {
151  SCH_SHEET_PIN* pin = new SCH_SHEET_PIN( &m_sheet, wxPoint{ i, i }, "name" );
152 
153  // set the pins to have the same number going in
154  pin->SetNumber( 2 );
155 
156  m_sheet.AddPin( pin );
157  }
158 
159  std::vector<SCH_SHEET_PIN*>& pins = m_sheet.GetPins();
160 
161  std::vector<int> numbers;
162 
163  for( SCH_SHEET_PIN* pin : pins )
164  numbers.push_back( pin->GetNumber() );
165 
166  // and now...they are all unique
167  BOOST_CHECK_PREDICATE( KI_TEST::CollectionHasNoDuplicates<decltype( numbers )>, ( numbers ) );
168 }
169 
170 
172 {
173  std::string m_pin_name;
174  wxPoint m_pos;
175 };
176 
177 
182 BOOST_AUTO_TEST_CASE( EndconnectionPoints )
183 {
184  // x = zero because the pin is clamped to the left side by default
185  const std::vector<TEST_END_CONN_PIN> pin_defs = {
186  {
187  "1name",
188  { 0, 13 },
189  },
190  {
191  "2name",
192  { 0, 130 },
193  },
194  };
195 
196  // Insert the pins into the sheet
197  for( const auto& pin : pin_defs )
198  m_sheet.AddPin( new SCH_SHEET_PIN( &m_sheet, pin.m_pos, pin.m_pin_name ) );
199 
200  std::vector<SCH_SHEET_PIN*>& pins = m_sheet.GetPins();
201 
202  // make sure the pins made it in
203  BOOST_CHECK_EQUAL( pins.size(), pin_defs.size() );
204 
205  // Check that the EndPoint getter gets the right things
206  {
207  std::vector<DANGLING_END_ITEM> expectedDangling;
208 
209  // Construct expected from the pin, not defs, as we need the pin address
210  for( SCH_SHEET_PIN* pin : pins )
211  {
212  expectedDangling.emplace_back( DANGLING_END_T::SHEET_LABEL_END, pin,
213  pin->GetPosition(), pin );
214  }
215 
216  std::vector<DANGLING_END_ITEM> dangling;
217  m_sheet.GetEndPoints( dangling );
218 
219  BOOST_CHECK_EQUAL_COLLECTIONS( dangling.begin(), dangling.end(),
220  expectedDangling.begin(), expectedDangling.end() );
221  }
222 
223  // And check the connection getter
224  {
225  std::vector<wxPoint> expectedConnections;
226 
227  // we want to see every pin that we just added
228  for( const auto& pin : pin_defs )
229  {
230  expectedConnections.push_back( pin.m_pos );
231  }
232 
233  std::vector<wxPoint> connections = m_sheet.GetConnectionPoints();
234 
235  BOOST_CHECK_EQUAL_COLLECTIONS( connections.begin(), connections.end(),
236  expectedConnections.begin(), expectedConnections.end() );
237  }
238 }
239 
240 
241 BOOST_AUTO_TEST_SUITE_END()
wxPoint GetPosition() const
Definition: sch_item.h:140
Holds all the data relating to one schematic A schematic may consist of one or more sheets (and one r...
Definition: schematic.h:44
const EDA_ITEM * GetParent() const
Definition: sch_item.h:142
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:141
Define a sheet pin (label) used in sheets to create hierarchical schematics.
Definition: sch_sheet.h:85
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:219
void SetNumber(int aNumber)
Set the sheet label number.
DANGLING_END_T GetType() const
Definition: sch_item.h:143
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:85
std::ostream & operator<<(std::ostream &os, DANGLING_END_ITEM const &d)
Print helper.
Test utilities for timestamps.