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
152BOOST_AUTO_TEST_CASE( PinRenumbering )
153{
154 for( int i = 0; i < 5; ++i )
155 {
156 SCH_SHEET_PIN* pin = new SCH_SHEET_PIN( &m_sheet, VECTOR2I{ i, i }, "name" );
157
158 // set the pins to have the same number going in
159 pin->SetNumber( 2 );
160
161 m_sheet.AddPin( pin );
162 }
163
164 std::vector<SCH_SHEET_PIN*>& pins = m_sheet.GetPins();
165
166 std::vector<int> numbers;
167
168 for( SCH_SHEET_PIN* pin : pins )
169 numbers.push_back( pin->GetNumber() );
170
171 // and now...they are all unique
172 BOOST_CHECK_PREDICATE( KI_TEST::CollectionHasNoDuplicates<decltype( numbers )>, ( numbers ) );
173}
174
175
177{
178 std::string m_pin_name;
180};
181
182
187BOOST_AUTO_TEST_CASE( EndconnectionPoints )
188{
189 // x = zero because the pin is clamped to the left side by default
190 const std::vector<TEST_END_CONN_PIN> pin_defs = {
191 {
192 "1name",
193 { 0, 13 },
194 },
195 {
196 "2name",
197 { 0, 130 },
198 },
199 };
200
201 // Insert the pins into the sheet
202 for( const auto& pin : pin_defs )
203 m_sheet.AddPin( new SCH_SHEET_PIN( &m_sheet, pin.m_pos, pin.m_pin_name ) );
204
205 std::vector<SCH_SHEET_PIN*>& pins = m_sheet.GetPins();
206
207 // make sure the pins made it in
208 BOOST_CHECK_EQUAL( pins.size(), pin_defs.size() );
209
210 // Check that the End getter gets the right things
211 {
212 std::vector<DANGLING_END_ITEM> expectedDangling;
213
214 // Construct expected from the pin, not defs, as we need the pin address
215 for( SCH_SHEET_PIN* pin : pins )
216 {
217 expectedDangling.emplace_back( DANGLING_END_T::SHEET_LABEL_END, pin,
218 pin->GetPosition(), pin );
219 }
220
221 std::vector<DANGLING_END_ITEM> dangling;
222 m_sheet.GetEndPoints( dangling );
223
224 BOOST_CHECK_EQUAL_COLLECTIONS( dangling.begin(), dangling.end(),
225 expectedDangling.begin(), expectedDangling.end() );
226 }
227
228 // And check the connection getter
229 {
230 std::vector<VECTOR2I> expectedConnections;
231
232 // we want to see every pin that we just added
233 for( const auto& pin : pin_defs )
234 {
235 expectedConnections.push_back( pin.m_pos );
236 }
237
238 std::vector<VECTOR2I> connections = m_sheet.GetConnectionPoints();
239
240 BOOST_CHECK_EQUAL_COLLECTIONS( connections.begin(), connections.end(),
241 expectedConnections.begin(), expectedConnections.end() );
242 }
243}
244
245
Helper class used to store the state of schematic items that can be connected to other schematic item...
Definition sch_item.h:101
DANGLING_END_T GetType() const
Definition sch_item.h:137
const EDA_ITEM * GetParent() const
Definition sch_item.h:136
EDA_ITEM * GetItem() const
Definition sch_item.h:135
VECTOR2I GetPosition() const
Definition sch_item.h:134
Holds all the data relating to one schematic.
Definition schematic.h:149
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
SCHEMATIC m_schematic
Dummy schematic to attach the test sheet to.
const SCH_SHEET & m_csheet
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:91
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_CHECK_EQUAL_COLLECTIONS(mixed.begin(), mixed.end(), expMixed.begin(), expMixed.end())
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.
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708