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 The 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
28
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.IsVirtualRootSheet(), false );
99 BOOST_CHECK_EQUAL( m_sheet.IsTopLevelSheet(), false ); // Not yet a top-level sheet
100
101 m_schematic.SetRoot( &m_sheet );
102
103 // After SetRoot, the sheet becomes a top-level sheet under the virtual root
104 BOOST_CHECK_EQUAL( m_sheet.IsVirtualRootSheet(), false ); // Sheet is not the virtual root
105 BOOST_CHECK_EQUAL( m_sheet.IsTopLevelSheet(), true ); // Sheet is now a top-level sheet
106 BOOST_CHECK_EQUAL( m_schematic.Root().IsVirtualRootSheet(), true ); // The root is virtual
107}
108
113{
114 const VECTOR2I pinPos{ 42, 13 };
115
116 // we should catch null insertions
117 CHECK_WX_ASSERT( m_sheet.AddPin( nullptr ) );
118
119 auto newPin = std::make_unique<SCH_SHEET_PIN>( &m_sheet, pinPos, "pinname" );
120
121 // can't be const because of RemovePin (?!)
122 SCH_SHEET_PIN& pinRef = *newPin;
123
124 m_sheet.AddPin( newPin.release() );
125
126 // now we can find it in the list
127 BOOST_CHECK_EQUAL( m_sheet.HasPins(), true );
128 BOOST_CHECK_EQUAL( m_sheet.HasPin( "pinname" ), true );
129 BOOST_CHECK_EQUAL( m_sheet.HasPin( "PINname" ), false );
130
131 BOOST_CHECK_EQUAL( m_sheet.GetPin( pinPos ), &pinRef );
132
133 // check the actual list can be retrieved
134 std::vector<SCH_SHEET_PIN*>& pins = m_sheet.GetPins();
135 BOOST_CHECK_EQUAL( pins[0], &pinRef );
136
137 // catch the bad call
138 CHECK_WX_ASSERT( m_sheet.RemovePin( nullptr ) );
139
140 m_sheet.RemovePin( &pinRef );
141
142 // and it's gone
143 BOOST_CHECK_EQUAL( m_sheet.HasPins(), false );
144 BOOST_CHECK_EQUAL( m_sheet.HasPin( "pinname" ), false );
145 BOOST_CHECK_EQUAL( m_sheet.GetPin( pinPos ), nullptr );
146
147 delete &pinRef;
148}
149
153BOOST_AUTO_TEST_CASE( PinRenumbering )
154{
155 for( int i = 0; i < 5; ++i )
156 {
157 SCH_SHEET_PIN* pin = new SCH_SHEET_PIN( &m_sheet, VECTOR2I{ i, i }, "name" );
158
159 // set the pins to have the same number going in
160 pin->SetNumber( 2 );
161
162 m_sheet.AddPin( pin );
163 }
164
165 std::vector<SCH_SHEET_PIN*>& pins = m_sheet.GetPins();
166
167 std::vector<int> numbers;
168
169 for( SCH_SHEET_PIN* pin : pins )
170 numbers.push_back( pin->GetNumber() );
171
172 // and now...they are all unique
173 BOOST_CHECK_PREDICATE( KI_TEST::CollectionHasNoDuplicates<decltype( numbers )>, ( numbers ) );
174}
175
176
178{
179 std::string m_pin_name;
181};
182
183
188BOOST_AUTO_TEST_CASE( EndconnectionPoints )
189{
190 // x = zero because the pin is clamped to the left side by default
191 const std::vector<TEST_END_CONN_PIN> pin_defs = {
192 {
193 "1name",
194 { 0, 13 },
195 },
196 {
197 "2name",
198 { 0, 130 },
199 },
200 };
201
202 // Insert the pins into the sheet
203 for( const auto& pin : pin_defs )
204 m_sheet.AddPin( new SCH_SHEET_PIN( &m_sheet, pin.m_pos, pin.m_pin_name ) );
205
206 std::vector<SCH_SHEET_PIN*>& pins = m_sheet.GetPins();
207
208 // make sure the pins made it in
209 BOOST_CHECK_EQUAL( pins.size(), pin_defs.size() );
210
211 // Check that the End getter gets the right things
212 {
213 std::vector<DANGLING_END_ITEM> expectedDangling;
214
215 // Construct expected from the pin, not defs, as we need the pin address
216 for( SCH_SHEET_PIN* pin : pins )
217 {
218 expectedDangling.emplace_back( DANGLING_END_T::SHEET_LABEL_END, pin,
219 pin->GetPosition(), pin );
220 }
221
222 std::vector<DANGLING_END_ITEM> dangling;
223 m_sheet.GetEndPoints( dangling );
224
225 BOOST_CHECK_EQUAL_COLLECTIONS( dangling.begin(), dangling.end(),
226 expectedDangling.begin(), expectedDangling.end() );
227 }
228
229 // And check the connection getter
230 {
231 std::vector<VECTOR2I> expectedConnections;
232
233 // we want to see every pin that we just added
234 for( const auto& pin : pin_defs )
235 {
236 expectedConnections.push_back( pin.m_pos );
237 }
238
239 std::vector<VECTOR2I> connections = m_sheet.GetConnectionPoints();
240
241 BOOST_CHECK_EQUAL_COLLECTIONS( connections.begin(), connections.end(),
242 expectedConnections.begin(), expectedConnections.end() );
243 }
244}
245
246
Helper class used to store the state of schematic items that can be connected to other schematic item...
Definition sch_item.h:96
DANGLING_END_T GetType() const
Definition sch_item.h:132
const EDA_ITEM * GetParent() const
Definition sch_item.h:131
EDA_ITEM * GetItem() const
Definition sch_item.h:130
VECTOR2I GetPosition() const
Definition sch_item.h:129
Holds all the data relating to one schematic.
Definition schematic.h:88
Define a sheet pin (label) used in sheets to create hierarchical schematics.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:47
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)
bool CollectionHasNoDuplicates(const T &aCollection)
Predicate to check a collection has no duplicate elements.
@ SHEET_LABEL_END
Definition sch_item.h:86
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE_END()
std::ostream & operator<<(std::ostream &os, DANGLING_END_ITEM const &d)
Print helper.
BOOST_AUTO_TEST_CASE(Default)
Declare the test suite.
BOOST_CHECK_PREDICATE(ArePolylineEndPointsNearCircle,(chain)(c.m_geom.m_center_point)(radius)(accuracy+epsilon))
BOOST_CHECK_EQUAL(result, "25.4")
#define CHECK_WX_ASSERT(STATEMENT)
A test macro to check a wxASSERT is thrown.
Test utilities for timestamps.
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695