KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_net_chain_bridging_delay.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 */
20
21#include <boost/test/unit_test.hpp>
22
23#include <base_units.h>
24#include <board.h>
26#include <footprint.h>
27#include <net_chain_bridging.h>
28#include <netinfo.h>
29#include <pad.h>
30#include <padstack.h>
31
32
33namespace
34{
35
36constexpr int PAD_SIZE_NM = 500'000;
37
38
39PAD* addPad( FOOTPRINT* aFp, NETINFO_ITEM* aNet, const VECTOR2I& aPos )
40{
41 PAD* pad = new PAD( aFp );
43 pad->SetSize( PADSTACK::ALL_LAYERS, VECTOR2I( PAD_SIZE_NM, PAD_SIZE_NM ) );
44 pad->SetPosition( aPos );
45 pad->SetNet( aNet );
46 aFp->Add( pad );
47 return pad;
48}
49
50
51NETINFO_ITEM* addNet( BOARD* aBoard, const wxString& aName, int aCode, const wxString& aChain )
52{
53 NETINFO_ITEM* n = new NETINFO_ITEM( aBoard, aName, aCode );
54 n->SetNetChain( aChain );
55 aBoard->Add( n );
56 return n;
57}
58
59
60FOOTPRINT* addFootprint( BOARD* aBoard )
61{
62 FOOTPRINT* fp = new FOOTPRINT( aBoard );
63 aBoard->Add( fp );
64 return fp;
65}
66
67} // namespace
68
69
70BOOST_AUTO_TEST_SUITE( NetChainBridgingDelay )
71
72
73BOOST_AUTO_TEST_CASE( EmptyInputsReturnZero )
74{
75 BOARD board;
76
77 auto [len, delay] = BoardChainBridging( &board, wxS( "SIG" ) );
78
79 BOOST_CHECK_EQUAL( len, 0.0 );
80 BOOST_CHECK_EQUAL( delay, 0.0 );
81
82 auto [len2, delay2] = BoardChainBridging( nullptr, wxS( "SIG" ) );
83
84 BOOST_CHECK_EQUAL( len2, 0.0 );
85 BOOST_CHECK_EQUAL( delay2, 0.0 );
86
87 auto [len3, delay3] = BoardChainBridging( &board, wxString() );
88
89 BOOST_CHECK_EQUAL( len3, 0.0 );
90 BOOST_CHECK_EQUAL( delay3, 0.0 );
91}
92
93
94BOOST_AUTO_TEST_CASE( BridgingLengthAndDelayMatchFallback )
95{
96 BOARD board;
97
98 NETINFO_ITEM* netA = addNet( &board, wxS( "/A" ), 1, wxS( "SIG" ) );
99 NETINFO_ITEM* netB = addNet( &board, wxS( "/B" ), 2, wxS( "SIG" ) );
100
101 FOOTPRINT* fp = addFootprint( &board );
102 addPad( fp, netA, VECTOR2I( -1'000'000, 0 ) );
103 addPad( fp, netB, VECTOR2I( 1'000'000, 0 ) );
104
105 auto [len, delay] = BoardChainBridging( &board, wxS( "SIG" ) );
106
107 // 2 mm bridging span.
108 BOOST_CHECK_CLOSE( len, 2'000'000.0, 0.001 );
109
110 // 2 mm at fallback ps/mm, expressed in delay IU (attoseconds).
111 double expectedDelayIU = DEFAULT_PROPAGATION_DELAY_PS_PER_MM * pcbIUScale.IU_PER_PS * 2.0;
112 BOOST_CHECK_CLOSE( delay, expectedDelayIU, 0.001 );
113 BOOST_CHECK_GT( delay, 0.0 );
114}
115
116
117BOOST_AUTO_TEST_CASE( SingleNetFootprintContributesNothing )
118{
119 BOARD board;
120
121 NETINFO_ITEM* netA = addNet( &board, wxS( "/A" ), 1, wxS( "SIG" ) );
122
123 FOOTPRINT* fp = addFootprint( &board );
124 addPad( fp, netA, VECTOR2I( -1'000'000, 0 ) );
125 addPad( fp, netA, VECTOR2I( 1'000'000, 0 ) );
126
127 auto [len, delay] = BoardChainBridging( &board, wxS( "SIG" ) );
128
129 BOOST_CHECK_EQUAL( len, 0.0 );
130 BOOST_CHECK_EQUAL( delay, 0.0 );
131}
132
133
134BOOST_AUTO_TEST_CASE( MultipleFootprintsSum )
135{
136 BOARD board;
137
138 NETINFO_ITEM* netA = addNet( &board, wxS( "/A" ), 1, wxS( "SIG" ) );
139 NETINFO_ITEM* netB = addNet( &board, wxS( "/B" ), 2, wxS( "SIG" ) );
140 NETINFO_ITEM* netC = addNet( &board, wxS( "/C" ), 3, wxS( "SIG" ) );
141
142 FOOTPRINT* fp1 = addFootprint( &board );
143 addPad( fp1, netA, VECTOR2I( -1'500'000, 0 ) );
144 addPad( fp1, netB, VECTOR2I( 1'500'000, 0 ) );
145
146 FOOTPRINT* fp2 = addFootprint( &board );
147 addPad( fp2, netB, VECTOR2I( 10'000'000, 0 ) );
148 addPad( fp2, netC, VECTOR2I( 13'000'000, 0 ) );
149
150 auto [len, delay] = BoardChainBridging( &board, wxS( "SIG" ) );
151
152 BOOST_CHECK_CLOSE( len, 6'000'000.0, 0.001 );
153
154 double expectedDelayIU = DEFAULT_PROPAGATION_DELAY_PS_PER_MM * pcbIUScale.IU_PER_PS * 6.0;
155 BOOST_CHECK_CLOSE( delay, expectedDelayIU, 0.001 );
156}
157
158
159BOOST_AUTO_TEST_CASE( ChainsArePartitionedByName )
160{
161 BOARD board;
162
163 NETINFO_ITEM* netA = addNet( &board, wxS( "/A" ), 1, wxS( "SIG_X" ) );
164 NETINFO_ITEM* netB = addNet( &board, wxS( "/B" ), 2, wxS( "SIG_X" ) );
165 NETINFO_ITEM* netC = addNet( &board, wxS( "/C" ), 3, wxS( "SIG_Y" ) );
166 NETINFO_ITEM* netD = addNet( &board, wxS( "/D" ), 4, wxS( "SIG_Y" ) );
167
168 FOOTPRINT* fp1 = addFootprint( &board );
169 addPad( fp1, netA, VECTOR2I( -1'000'000, 0 ) );
170 addPad( fp1, netB, VECTOR2I( 1'000'000, 0 ) );
171
172 FOOTPRINT* fp2 = addFootprint( &board );
173 addPad( fp2, netC, VECTOR2I( 10'000'000, 0 ) );
174 addPad( fp2, netD, VECTOR2I( 14'000'000, 0 ) );
175
176 auto [xLen, xDelay] = BoardChainBridging( &board, wxS( "SIG_X" ) );
177 auto [yLen, yDelay] = BoardChainBridging( &board, wxS( "SIG_Y" ) );
178
179 BOOST_CHECK_CLOSE( xLen, 2'000'000.0, 0.001 );
180 BOOST_CHECK_CLOSE( yLen, 4'000'000.0, 0.001 );
181
182 double expectedXDelayIU = DEFAULT_PROPAGATION_DELAY_PS_PER_MM * pcbIUScale.IU_PER_PS * 2.0;
183 double expectedYDelayIU = DEFAULT_PROPAGATION_DELAY_PS_PER_MM * pcbIUScale.IU_PER_PS * 4.0;
184 BOOST_CHECK_CLOSE( xDelay, expectedXDelayIU, 0.001 );
185 BOOST_CHECK_CLOSE( yDelay, expectedYDelayIU, 0.001 );
186 BOOST_CHECK_GT( yDelay, xDelay );
187}
188
189
190BOOST_AUTO_TEST_CASE( LargeBridgingLengthDoesNotOverflowIntCast )
191{
192 BOARD board;
193
194 NETINFO_ITEM* netA = addNet( &board, wxS( "/A" ), 1, wxS( "SIG" ) );
195 NETINFO_ITEM* netB = addNet( &board, wxS( "/B" ), 2, wxS( "SIG" ) );
196
197 // 3 m span (3,000,000,000 nm) exceeds INT_MAX (~2.147e9). The previous
198 // (int) cast on lengthIU silently truncated to a negative value here,
199 // producing a negative delay; the direct-divide form must remain accurate.
200 FOOTPRINT* fp = addFootprint( &board );
201 addPad( fp, netA, VECTOR2I( -1'500'000'000, 0 ) );
202 addPad( fp, netB, VECTOR2I( 1'500'000'000, 0 ) );
203
204 auto [len, delay] = BoardChainBridging( &board, wxS( "SIG" ) );
205
206 BOOST_CHECK_CLOSE( len, 3'000'000'000.0, 0.001 );
207
208 double expectedDelayIU = DEFAULT_PROPAGATION_DELAY_PS_PER_MM * pcbIUScale.IU_PER_PS * 3000.0;
209 BOOST_CHECK_CLOSE( delay, expectedDelayIU, 0.001 );
210 BOOST_CHECK_GT( delay, 0.0 );
211}
212
213
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:125
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:323
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition board.cpp:1247
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Handle the data for a net.
Definition netinfo.h:50
void SetNetChain(const wxString &aNetChain)
Definition netinfo.h:116
static constexpr PCB_LAYER_ID ALL_LAYERS
! Temporary layer identifier to identify code that is not padstack-aware
Definition padstack.h:177
Definition pad.h:55
constexpr double DEFAULT_PROPAGATION_DELAY_PS_PER_MM
std::tuple< double, double > BoardChainBridging(const BOARD *aBoard, const wxString &aNetChain)
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(EmptyInputsReturnZero)
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687