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 <sch_screen.h>
35#include <schematic.h>
36
39
41{
42public:
44 m_schematic( nullptr ),
45 m_sheet(),
47 {
48 }
49
52
54
57};
58
59
64std::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
74BOOST_FIXTURE_TEST_SUITE( SchSheet, TEST_SCH_SHEET_FIXTURE )
75
76
77
81{
82 BOOST_CHECK_EQUAL( m_csheet.GetPosition(), VECTOR2I( 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
95BOOST_AUTO_TEST_CASE( SchematicParent )
96{
97 m_sheet.SetParent( &m_schematic );
98
99 BOOST_CHECK_EQUAL( m_sheet.IsVirtualRootSheet(), false );
100 BOOST_CHECK_EQUAL( m_sheet.IsTopLevelSheet(), false ); // Not yet a top-level sheet
101
102 SCH_SCREEN* screen = new SCH_SCREEN( &m_schematic );
103 m_sheet.SetScreen( screen );
104 m_schematic.AddTopLevelSheet( &m_sheet );
105
106 // After AddTopLevelSheet, the sheet becomes a top-level sheet under the virtual root
107 BOOST_CHECK_EQUAL( m_sheet.IsVirtualRootSheet(), false ); // Sheet is not the virtual root
108 BOOST_CHECK_EQUAL( m_sheet.IsTopLevelSheet(), true ); // Sheet is now a top-level sheet
109 BOOST_CHECK_EQUAL( m_schematic.Root().IsVirtualRootSheet(), true ); // The root is virtual
110
111 m_schematic.RemoveTopLevelSheet( &m_sheet );
112}
113
118{
119 const VECTOR2I pinPos{ 42, 13 };
120
121 // we should catch null insertions
122 CHECK_WX_ASSERT( m_sheet.AddPin( nullptr ) );
123
124 auto newPin = std::make_unique<SCH_SHEET_PIN>( &m_sheet, pinPos, "pinname" );
125
126 // can't be const because of RemovePin (?!)
127 SCH_SHEET_PIN& pinRef = *newPin;
128
129 m_sheet.AddPin( newPin.release() );
130
131 // now we can find it in the list
132 BOOST_CHECK_EQUAL( m_sheet.HasPins(), true );
133 BOOST_CHECK_EQUAL( m_sheet.HasPin( "pinname" ), true );
134 BOOST_CHECK_EQUAL( m_sheet.HasPin( "PINname" ), false );
135
136 BOOST_CHECK_EQUAL( m_sheet.GetPin( pinPos ), &pinRef );
137
138 // check the actual list can be retrieved
139 std::vector<SCH_SHEET_PIN*>& pins = m_sheet.GetPins();
140 BOOST_CHECK_EQUAL( pins[0], &pinRef );
141
142 // catch the bad call
143 CHECK_WX_ASSERT( m_sheet.RemovePin( nullptr ) );
144
145 m_sheet.RemovePin( &pinRef );
146
147 // and it's gone
148 BOOST_CHECK_EQUAL( m_sheet.HasPins(), false );
149 BOOST_CHECK_EQUAL( m_sheet.HasPin( "pinname" ), false );
150 BOOST_CHECK_EQUAL( m_sheet.GetPin( pinPos ), nullptr );
151
152 delete &pinRef;
153}
154
158BOOST_AUTO_TEST_CASE( PinRenumbering )
159{
160 for( int i = 0; i < 5; ++i )
161 {
162 SCH_SHEET_PIN* pin = new SCH_SHEET_PIN( &m_sheet, VECTOR2I{ i, i }, "name" );
163
164 // set the pins to have the same number going in
165 pin->SetNumber( 2 );
166
167 m_sheet.AddPin( pin );
168 }
169
170 std::vector<SCH_SHEET_PIN*>& pins = m_sheet.GetPins();
171
172 std::vector<int> numbers;
173
174 for( SCH_SHEET_PIN* pin : pins )
175 numbers.push_back( pin->GetNumber() );
176
177 // and now...they are all unique
178 BOOST_CHECK_PREDICATE( KI_TEST::CollectionHasNoDuplicates<decltype( numbers )>, ( numbers ) );
179}
180
181
183{
184 std::string m_pin_name;
186};
187
188
193BOOST_AUTO_TEST_CASE( EndconnectionPoints )
194{
195 // x = zero because the pin is clamped to the left side by default
196 const std::vector<TEST_END_CONN_PIN> pin_defs = {
197 {
198 "1name",
199 { 0, 13 },
200 },
201 {
202 "2name",
203 { 0, 130 },
204 },
205 };
206
207 // Insert the pins into the sheet
208 for( const auto& pin : pin_defs )
209 m_sheet.AddPin( new SCH_SHEET_PIN( &m_sheet, pin.m_pos, pin.m_pin_name ) );
210
211 std::vector<SCH_SHEET_PIN*>& pins = m_sheet.GetPins();
212
213 // make sure the pins made it in
214 BOOST_CHECK_EQUAL( pins.size(), pin_defs.size() );
215
216 // Check that the End getter gets the right things
217 {
218 std::vector<DANGLING_END_ITEM> expectedDangling;
219
220 // Construct expected from the pin, not defs, as we need the pin address
221 for( SCH_SHEET_PIN* pin : pins )
222 {
223 expectedDangling.emplace_back( DANGLING_END_T::SHEET_LABEL_END, pin,
224 pin->GetPosition(), pin );
225 }
226
227 std::vector<DANGLING_END_ITEM> dangling;
228 m_sheet.GetEndPoints( dangling );
229
230 BOOST_CHECK_EQUAL_COLLECTIONS( dangling.begin(), dangling.end(),
231 expectedDangling.begin(), expectedDangling.end() );
232 }
233
234 // And check the connection getter
235 {
236 std::vector<VECTOR2I> expectedConnections;
237
238 // we want to see every pin that we just added
239 for( const auto& pin : pin_defs )
240 {
241 expectedConnections.push_back( pin.m_pos );
242 }
243
244 std::vector<VECTOR2I> connections = m_sheet.GetConnectionPoints();
245
246 BOOST_CHECK_EQUAL_COLLECTIONS( connections.begin(), connections.end(),
247 expectedConnections.begin(), expectedConnections.end() );
248 }
249}
250
251
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:48
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()
KIBIS_PIN * pin
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