KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_tuning_bridging_clamp.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 <limits>
24
25#include <base_units.h>
26#include <core/minoptmax.h>
27#include <net_chain_bridging.h>
28
29
30static void applyBridging( MINOPTMAX<int>& aTarget, long long aBridgingIU )
31{
32 if( aTarget.HasMin() )
33 aTarget.SetMin( SubtractBridgingClamped( aTarget.Min(), aBridgingIU ) );
34
35 if( aTarget.HasOpt() )
36 aTarget.SetOpt( SubtractBridgingClamped( aTarget.Opt(), aBridgingIU ) );
37
38 if( aTarget.HasMax() )
39 aTarget.SetMax( SubtractBridgingClamped( aTarget.Max(), aBridgingIU ) );
40}
41
42
43BOOST_AUTO_TEST_SUITE( TuningBridgingClamp )
44
45
46BOOST_AUTO_TEST_CASE( TimeDomainAttosecondsClampsToZero )
47{
48 constexpr long long bridging = 3'000LL * static_cast<long long>( pcbIUScale.IU_PER_PS );
49
50 BOOST_CHECK_GT( bridging, static_cast<long long>( std::numeric_limits<int>::max() ) );
51
52 MINOPTMAX<int> target;
53 target.SetMin( 1'000'000'000 );
54 target.SetMax( 1'200'000'000 );
55
56 applyBridging( target, bridging );
57
58 BOOST_CHECK_EQUAL( target.Min(), 0 );
59 BOOST_CHECK_EQUAL( target.Max(), 0 );
60}
61
62
63BOOST_AUTO_TEST_CASE( LengthModeUnaffectedWithinIntRange )
64{
65 MINOPTMAX<int> target;
66 target.SetMin( 100'000'000 );
67 target.SetMax( 120'000'000 );
68
69 applyBridging( target, 5'000'000 );
70
71 BOOST_CHECK_EQUAL( target.Min(), 95'000'000 );
72 BOOST_CHECK_EQUAL( target.Max(), 115'000'000 );
73}
74
75
76BOOST_AUTO_TEST_CASE( BridgingExceedsTargetFloorsAtZero )
77{
78 MINOPTMAX<int> target;
79 target.SetMin( 100 );
80 target.SetMax( 200 );
81
82 applyBridging( target, 1'000'000'000LL );
83
84 BOOST_CHECK_EQUAL( target.Min(), 0 );
85 BOOST_CHECK_EQUAL( target.Max(), 0 );
86}
87
88
89// Pre-fix code truncated bridgingDelayIU to int, wrapping the value before subtraction;
90// verify the long long path keeps the magnitude intact.
91BOOST_AUTO_TEST_CASE( WideArithmeticPreservesMagnitude )
92{
93 constexpr long long bridging = 500'000'000'000'000LL;
94
95 long long truncated = static_cast<long long>( static_cast<int>( bridging ) );
96
97 BOOST_CHECK_NE( truncated, bridging );
98
99 MINOPTMAX<int> target;
100 target.SetMin( 2'000'000'000 );
101
102 applyBridging( target, bridging );
103
104 BOOST_CHECK_EQUAL( target.Min(), 0 );
105}
106
107
108BOOST_AUTO_TEST_CASE( OptStaysWithinMinMaxAfterBridging )
109{
110 MINOPTMAX<int> target;
111 target.SetMin( 100'000'000 );
112 target.SetOpt( 110'000'000 );
113 target.SetMax( 120'000'000 );
114
115 applyBridging( target, 5'000'000 );
116
117 BOOST_CHECK_EQUAL( target.Min(), 95'000'000 );
118 BOOST_CHECK_EQUAL( target.Opt(), 105'000'000 );
119 BOOST_CHECK_EQUAL( target.Max(), 115'000'000 );
120
121 BOOST_CHECK_LE( target.Min(), target.Opt() );
122 BOOST_CHECK_LE( target.Opt(), target.Max() );
123}
124
125
126BOOST_AUTO_TEST_CASE( OptClampsToZeroWhenBridgingExceedsTarget )
127{
128 MINOPTMAX<int> target;
129 target.SetMin( 1'000'000'000 );
130 target.SetOpt( 1'100'000'000 );
131 target.SetMax( 1'200'000'000 );
132
133 constexpr long long bridging = 3'000LL * static_cast<long long>( pcbIUScale.IU_PER_PS );
134
135 applyBridging( target, bridging );
136
137 BOOST_CHECK_EQUAL( target.Min(), 0 );
138 BOOST_CHECK_EQUAL( target.Opt(), 0 );
139 BOOST_CHECK_EQUAL( target.Max(), 0 );
140}
141
142
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:125
T Min() const
Definition minoptmax.h:33
void SetMin(T v)
Definition minoptmax.h:41
bool HasMax() const
Definition minoptmax.h:38
void SetOpt(T v)
Definition minoptmax.h:43
bool HasMin() const
Definition minoptmax.h:37
void SetMax(T v)
Definition minoptmax.h:42
T Max() const
Definition minoptmax.h:34
T Opt() const
Definition minoptmax.h:35
bool HasOpt() const
Definition minoptmax.h:39
int SubtractBridgingClamped(int aValue, long long aDelta)
Saturating subtract for bridge-adjusting MINOPTMAX<int> bounds without overflow when the delta is in ...
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_EQUAL(result, "25.4")
static void applyBridging(MINOPTMAX< int > &aTarget, long long aBridgingIU)
BOOST_AUTO_TEST_CASE(TimeDomainAttosecondsClampsToZero)