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 
34 #include "uuid_test_utils.h"
35 
37 
39 {
40 public:
42  {
43  }
44 
46 
49 };
50 
51 
56 std::ostream& operator<<( std::ostream& os, DANGLING_END_ITEM const& d )
57 {
58  os << "DANGLING_END_ITEM[ type " << d.GetType() << " @(" << d.GetPosition().x << ", "
59  << d.GetPosition().y << "), item " << d.GetItem() << ", parent " << d.GetParent() << " ]";
60  return os;
61 }
62 
66 BOOST_FIXTURE_TEST_SUITE( SchSheet, TEST_SCH_SHEET_FIXTURE )
67 
68 
69 
73 {
74  BOOST_CHECK_EQUAL( m_csheet.GetPosition(), wxPoint( 0, 0 ) );
75 
76  // it is it's own root sheet
77  BOOST_CHECK_EQUAL( m_sheet.GetRootSheet(), &m_sheet );
78  BOOST_CHECK_EQUAL( m_sheet.CountSheets(), 1 );
79 
80  BOOST_CHECK_EQUAL( m_csheet.GetScreenCount(), 0 );
81 
82  BOOST_CHECK_EQUAL( m_sheet.ComponentCount(), 0 );
83 }
84 
89 {
90  const wxPoint pinPos{ 42, 13 };
91 
92  // we should catch null insertions
93  CHECK_WX_ASSERT( m_sheet.AddPin( nullptr ) );
94 
95  auto newPin = std::make_unique<SCH_SHEET_PIN>( &m_sheet, pinPos, "pinname" );
96 
97  // can't be const because of RemovePin (?!)
98  SCH_SHEET_PIN& pinRef = *newPin;
99 
100  m_sheet.AddPin( newPin.release() );
101 
102  // now we can find it in the list
103  BOOST_CHECK_EQUAL( m_sheet.HasPins(), true );
104  BOOST_CHECK_EQUAL( m_sheet.HasPin( "pinname" ), true );
105  BOOST_CHECK_EQUAL( m_sheet.HasPin( "PINname" ), true );
106 
107  BOOST_CHECK_EQUAL( m_sheet.GetPin( pinPos ), &pinRef );
108 
109  // check the actual list can be retrieved
110  std::vector<SCH_SHEET_PIN*>& pins = m_sheet.GetPins();
111  BOOST_CHECK_EQUAL( pins[0], &pinRef );
112 
113  // catch the bad call
114  CHECK_WX_ASSERT( m_sheet.RemovePin( nullptr ) );
115 
116  m_sheet.RemovePin( &pinRef );
117 
118  // and it's gone
119  BOOST_CHECK_EQUAL( m_sheet.HasPins(), false );
120  BOOST_CHECK_EQUAL( m_sheet.HasPin( "pinname" ), false );
121  BOOST_CHECK_EQUAL( m_sheet.GetPin( pinPos ), nullptr );
122 }
123 
127 BOOST_AUTO_TEST_CASE( PinRenumbering )
128 {
129  for( int i = 0; i < 5; ++i )
130  {
131  SCH_SHEET_PIN* pin = new SCH_SHEET_PIN( &m_sheet, wxPoint{ i, i }, "name" );
132 
133  // set the pins to have the same number going in
134  pin->SetNumber( 2 );
135 
136  m_sheet.AddPin( pin );
137  }
138 
139  std::vector<SCH_SHEET_PIN*>& pins = m_sheet.GetPins();
140 
141  std::vector<int> numbers;
142 
143  for( SCH_SHEET_PIN* pin : pins )
144  numbers.push_back( pin->GetNumber() );
145 
146  // and now...they are all unique
147  BOOST_CHECK_PREDICATE( KI_TEST::CollectionHasNoDuplicates<decltype( numbers )>, ( numbers ) );
148 }
149 
150 
152 {
153  std::string m_pin_name;
155 };
156 
157 
162 BOOST_AUTO_TEST_CASE( EndconnectionPoints )
163 {
164  // x = zero because the pin is clamped to the left side by default
165  const std::vector<TEST_END_CONN_PIN> pin_defs = {
166  {
167  "1name",
168  { 0, 13 },
169  },
170  {
171  "2name",
172  { 0, 130 },
173  },
174  };
175 
176  // Insert the pins into the sheet
177  for( const auto& pin : pin_defs )
178  m_sheet.AddPin( new SCH_SHEET_PIN( &m_sheet, pin.m_pos, pin.m_pin_name ) );
179 
180  std::vector<SCH_SHEET_PIN*>& pins = m_sheet.GetPins();
181 
182  // make sure the pins made it in
183  BOOST_CHECK_EQUAL( pins.size(), pin_defs.size() );
184 
185  // Check that the EndPoint getter gets the right things
186  {
187  std::vector<DANGLING_END_ITEM> expectedDangling;
188 
189  // Construct expected from the pin, not defs, as we need the pin address
190  for( SCH_SHEET_PIN* pin : pins )
191  {
192  expectedDangling.emplace_back( DANGLING_END_T::SHEET_LABEL_END, pin,
193  pin->GetPosition(), pin );
194  }
195 
196  std::vector<DANGLING_END_ITEM> dangling;
197  m_sheet.GetEndPoints( dangling );
198 
199  BOOST_CHECK_EQUAL_COLLECTIONS( dangling.begin(), dangling.end(),
200  expectedDangling.begin(), expectedDangling.end() );
201  }
202 
203  // And check the connection getter
204  {
205  std::vector<wxPoint> expectedConnections;
206 
207  // we want to see every pin that we just added
208  for( const auto& pin : pin_defs )
209  {
210  expectedConnections.push_back( pin.m_pos );
211  }
212 
213  std::vector<wxPoint> connections;
214  m_sheet.GetConnectionPoints( connections );
215 
216  BOOST_CHECK_EQUAL_COLLECTIONS( connections.begin(), connections.end(),
217  expectedConnections.begin(), expectedConnections.end() );
218  }
219 }
220 
221 
222 BOOST_AUTO_TEST_SUITE_END()
wxPoint GetPosition() const
Definition: sch_item.h:131
const EDA_ITEM * GetParent() const
Definition: sch_item.h:133
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:132
Define a sheet pin (label) used in sheets to create hierarchical schematics.
Definition: sch_sheet.h:84
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:215
void SetNumber(int aNumber)
Set the sheet label number.
DANGLING_END_T GetType() const
Definition: sch_item.h:134
std::string m_pin_name
DANGLING_END_ITEM is a helper class used to store the state of schematic items that can be connected ...
Definition: sch_item.h:76
std::ostream & operator<<(std::ostream &os, DANGLING_END_ITEM const &d)
Print helper.
Test utilities for timestamps.