KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2023 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
30
31// Code under test
32#include <sch_sheet.h>
33#include <sch_sheet_pin.h>
34#include <schematic.h>
35
38
40{
41public:
43 m_schematic( nullptr ),
44 m_sheet(),
46 {
47 }
48
51
53
56};
57
58
63std::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
73BOOST_FIXTURE_TEST_SUITE( SchSheet, TEST_SCH_SHEET_FIXTURE )
74
75
76
80{
81 BOOST_CHECK_EQUAL( m_csheet.GetPosition(), VECTOR2I( 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.SymbolCount(), 0 );
89}
90
94BOOST_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 VECTOR2I 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" ), false );
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 delete &pinRef;
144}
145
149BOOST_AUTO_TEST_CASE( PinRenumbering )
150{
151 for( int i = 0; i < 5; ++i )
152 {
153 SCH_SHEET_PIN* pin = new SCH_SHEET_PIN( &m_sheet, VECTOR2I{ i, i }, "name" );
154
155 // set the pins to have the same number going in
156 pin->SetNumber( 2 );
157
158 m_sheet.AddPin( pin );
159 }
160
161 std::vector<SCH_SHEET_PIN*>& pins = m_sheet.GetPins();
162
163 std::vector<int> numbers;
164
165 for( SCH_SHEET_PIN* pin : pins )
166 numbers.push_back( pin->GetNumber() );
167
168 // and now...they are all unique
169 BOOST_CHECK_PREDICATE( KI_TEST::CollectionHasNoDuplicates<decltype( numbers )>, ( numbers ) );
170}
171
172
174{
175 std::string m_pin_name;
177};
178
179
184BOOST_AUTO_TEST_CASE( EndconnectionPoints )
185{
186 // x = zero because the pin is clamped to the left side by default
187 const std::vector<TEST_END_CONN_PIN> pin_defs = {
188 {
189 "1name",
190 { 0, 13 },
191 },
192 {
193 "2name",
194 { 0, 130 },
195 },
196 };
197
198 // Insert the pins into the sheet
199 for( const auto& pin : pin_defs )
200 m_sheet.AddPin( new SCH_SHEET_PIN( &m_sheet, pin.m_pos, pin.m_pin_name ) );
201
202 std::vector<SCH_SHEET_PIN*>& pins = m_sheet.GetPins();
203
204 // make sure the pins made it in
205 BOOST_CHECK_EQUAL( pins.size(), pin_defs.size() );
206
207 // Check that the End getter gets the right things
208 {
209 std::vector<DANGLING_END_ITEM> expectedDangling;
210
211 // Construct expected from the pin, not defs, as we need the pin address
212 for( SCH_SHEET_PIN* pin : pins )
213 {
214 expectedDangling.emplace_back( DANGLING_END_T::SHEET_LABEL_END, pin,
215 pin->GetPosition(), pin );
216 }
217
218 std::vector<DANGLING_END_ITEM> dangling;
219 m_sheet.GetEndPoints( dangling );
220
221 BOOST_CHECK_EQUAL_COLLECTIONS( dangling.begin(), dangling.end(),
222 expectedDangling.begin(), expectedDangling.end() );
223 }
224
225 // And check the connection getter
226 {
227 std::vector<VECTOR2I> expectedConnections;
228
229 // we want to see every pin that we just added
230 for( const auto& pin : pin_defs )
231 {
232 expectedConnections.push_back( pin.m_pos );
233 }
234
235 std::vector<VECTOR2I> connections = m_sheet.GetConnectionPoints();
236
237 BOOST_CHECK_EQUAL_COLLECTIONS( connections.begin(), connections.end(),
238 expectedConnections.begin(), expectedConnections.end() );
239 }
240}
241
242
Helper class used to store the state of schematic items that can be connected to other schematic item...
Definition: sch_item.h:95
DANGLING_END_T GetType() const
Definition: sch_item.h:131
const EDA_ITEM * GetParent() const
Definition: sch_item.h:130
EDA_ITEM * GetItem() const
Definition: sch_item.h:129
VECTOR2I GetPosition() const
Definition: sch_item.h:128
Holds all the data relating to one schematic.
Definition: schematic.h:77
Define a sheet pin (label) used in sheets to create hierarchical schematics.
Definition: sch_sheet_pin.h:66
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:57
const SCH_SHEET & m_csheet
TEST_SCH_SHEET_FIXTURE()
Dummy schematic to attach the test sheet to.
SCH_SHEET m_sheet
Can use when you need a const ref (lots of places need fixing here)
std::ostream & operator<<(std::ostream &aStream, const EDA_TEXT &aText)
Definition: eda_text.cpp:1210
bool CollectionHasNoDuplicates(const T &aCollection)
Predicate to check a collection has no duplicate elements.
@ SHEET_LABEL_END
Definition: sch_item.h:85
std::string m_pin_name
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(Default)
Declare the test suite.
#define CHECK_WX_ASSERT(STATEMENT)
A test macro to check a wxASSERT is thrown.
Test utilities for timestamps.