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, see <https://www.gnu.org/licenses/>.
18 */
19
24
26
27// Code under test
28#include <sch_sheet.h>
29#include <sch_sheet_pin.h>
30#include <sch_screen.h>
31#include <schematic.h>
32
35
37{
38public:
40 m_schematic( nullptr ),
41 m_sheet(),
43 {
44 }
45
48
50
53};
54
55
60std::ostream& operator<<( std::ostream& os, DANGLING_END_ITEM const& d )
61{
62 os << "DANGLING_END_ITEM[ type " << d.GetType() << " @(" << d.GetPosition().x << ", "
63 << d.GetPosition().y << "), item " << d.GetItem() << ", parent " << d.GetParent() << " ]";
64 return os;
65}
66
70BOOST_FIXTURE_TEST_SUITE( SchSheet, TEST_SCH_SHEET_FIXTURE )
71
72
73
77{
78 BOOST_CHECK_EQUAL( m_csheet.GetPosition(), VECTOR2I( 0, 0 ) );
79
80 BOOST_CHECK_EQUAL( m_sheet.GetParent(), nullptr );
81 BOOST_CHECK_EQUAL( m_sheet.CountSheets(), 1 );
82
83 BOOST_CHECK_EQUAL( m_csheet.GetScreenCount(), 0 );
84
85 BOOST_CHECK_EQUAL( m_sheet.SymbolCount(), 0 );
86}
87
91BOOST_AUTO_TEST_CASE( SchematicParent )
92{
93 m_sheet.SetParent( &m_schematic );
94
95 BOOST_CHECK_EQUAL( m_sheet.IsVirtualRootSheet(), false );
96 BOOST_CHECK_EQUAL( m_sheet.IsTopLevelSheet(), false ); // Not yet a top-level sheet
97
98 SCH_SCREEN* screen = new SCH_SCREEN( &m_schematic );
99 m_sheet.SetScreen( screen );
100 m_schematic.AddTopLevelSheet( &m_sheet );
101
102 // After AddTopLevelSheet, the sheet becomes a top-level sheet under the virtual root
103 BOOST_CHECK_EQUAL( m_sheet.IsVirtualRootSheet(), false ); // Sheet is not the virtual root
104 BOOST_CHECK_EQUAL( m_sheet.IsTopLevelSheet(), true ); // Sheet is now a top-level sheet
105 BOOST_CHECK_EQUAL( m_schematic.Root().IsVirtualRootSheet(), true ); // The root is virtual
106
107 m_schematic.RemoveTopLevelSheet( &m_sheet );
108}
109
114{
115 const VECTOR2I pinPos{ 42, 13 };
116
117 // we should catch null insertions
118 CHECK_WX_ASSERT( m_sheet.AddPin( nullptr ) );
119
120 auto newPin = std::make_unique<SCH_SHEET_PIN>( &m_sheet, pinPos, "pinname" );
121
122 // can't be const because of RemovePin (?!)
123 SCH_SHEET_PIN& pinRef = *newPin;
124
125 m_sheet.AddPin( newPin.release() );
126
127 // now we can find it in the list
128 BOOST_CHECK_EQUAL( m_sheet.HasPins(), true );
129 BOOST_CHECK_EQUAL( m_sheet.HasPin( "pinname" ), true );
130 BOOST_CHECK_EQUAL( m_sheet.HasPin( "PINname" ), false );
131
132 BOOST_CHECK_EQUAL( m_sheet.GetPin( pinPos ), &pinRef );
133
134 // check the actual list can be retrieved
135 std::vector<SCH_SHEET_PIN*>& pins = m_sheet.GetPins();
136 BOOST_CHECK_EQUAL( pins[0], &pinRef );
137
138 // catch the bad call
139 CHECK_WX_ASSERT( m_sheet.RemovePin( nullptr ) );
140
141 m_sheet.RemovePin( &pinRef );
142
143 // and it's gone
144 BOOST_CHECK_EQUAL( m_sheet.HasPins(), false );
145 BOOST_CHECK_EQUAL( m_sheet.HasPin( "pinname" ), false );
146 BOOST_CHECK_EQUAL( m_sheet.GetPin( pinPos ), nullptr );
147
148 delete &pinRef;
149}
150
154BOOST_AUTO_TEST_CASE( PinRenumbering )
155{
156 for( int i = 0; i < 5; ++i )
157 {
158 SCH_SHEET_PIN* pin = new SCH_SHEET_PIN( &m_sheet, VECTOR2I{ i, i }, "name" );
159
160 // set the pins to have the same number going in
161 pin->SetNumber( 2 );
162
163 m_sheet.AddPin( pin );
164 }
165
166 std::vector<SCH_SHEET_PIN*>& pins = m_sheet.GetPins();
167
168 std::vector<int> numbers;
169
170 for( SCH_SHEET_PIN* pin : pins )
171 numbers.push_back( pin->GetNumber() );
172
173 // and now...they are all unique
174 BOOST_CHECK_PREDICATE( KI_TEST::CollectionHasNoDuplicates<decltype( numbers )>, ( numbers ) );
175}
176
177
179{
180 std::string m_pin_name;
182};
183
184
189BOOST_AUTO_TEST_CASE( EndconnectionPoints )
190{
191 // x = zero because the pin is clamped to the left side by default
192 const std::vector<TEST_END_CONN_PIN> pin_defs = {
193 {
194 "1name",
195 { 0, 13 },
196 },
197 {
198 "2name",
199 { 0, 130 },
200 },
201 };
202
203 // Insert the pins into the sheet
204 for( const auto& pin : pin_defs )
205 m_sheet.AddPin( new SCH_SHEET_PIN( &m_sheet, pin.m_pos, pin.m_pin_name ) );
206
207 std::vector<SCH_SHEET_PIN*>& pins = m_sheet.GetPins();
208
209 // make sure the pins made it in
210 BOOST_CHECK_EQUAL( pins.size(), pin_defs.size() );
211
212 // Check that the End getter gets the right things
213 {
214 std::vector<DANGLING_END_ITEM> expectedDangling;
215
216 // Construct expected from the pin, not defs, as we need the pin address
217 for( SCH_SHEET_PIN* pin : pins )
218 {
219 expectedDangling.emplace_back( DANGLING_END_T::SHEET_LABEL_END, pin,
220 pin->GetPosition(), pin );
221 }
222
223 std::vector<DANGLING_END_ITEM> dangling;
224 m_sheet.GetEndPoints( dangling );
225
226 BOOST_CHECK_EQUAL_COLLECTIONS( dangling.begin(), dangling.end(),
227 expectedDangling.begin(), expectedDangling.end() );
228 }
229
230 // And check the connection getter
231 {
232 std::vector<VECTOR2I> expectedConnections;
233
234 // we want to see every pin that we just added
235 for( const auto& pin : pin_defs )
236 {
237 expectedConnections.push_back( pin.m_pos );
238 }
239
240 std::vector<VECTOR2I> connections = m_sheet.GetConnectionPoints();
241
242 BOOST_CHECK_EQUAL_COLLECTIONS( connections.begin(), connections.end(),
243 expectedConnections.begin(), expectedConnections.end() );
244 }
245}
246
247
Helper class used to store the state of schematic items that can be connected to other schematic item...
Definition sch_item.h:93
DANGLING_END_T GetType() const
Definition sch_item.h:129
const EDA_ITEM * GetParent() const
Definition sch_item.h:128
EDA_ITEM * GetItem() const
Definition sch_item.h:127
VECTOR2I GetPosition() const
Definition sch_item.h:126
Holds all the data relating to one schematic.
Definition schematic.h:90
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:44
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:83
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:683