KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_sim_signal_utils.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 3
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
21
23
24using namespace SIM_SIGNAL;
25
26
27BOOST_AUTO_TEST_SUITE( SimSignalUtils )
28
29
30BOOST_AUTO_TEST_CASE( SplitMeasurementKeepsComparisons )
31{
32 wxString func, signal, params;
33
34 SplitMeasurement( wxS( "MAX (V(out) >= 0.5)" ), func, signal, params );
35 BOOST_CHECK_EQUAL( func, wxS( "MAX" ) );
36 BOOST_CHECK_EQUAL( signal, wxS( "(V(out) >= 0.5)" ) );
37 BOOST_CHECK( params.IsEmpty() );
38
39 SplitMeasurement( wxS( "MAX (V(out) >= 0.5) from=1m to=2m" ), func, signal, params );
40 BOOST_CHECK_EQUAL( signal, wxS( "(V(out) >= 0.5)" ) );
41 BOOST_CHECK_EQUAL( params, wxS( "from=1m to=2m" ) );
42
43 SplitMeasurement( wxS( "AVG V(out) == 1" ), func, signal, params );
44 BOOST_CHECK_EQUAL( signal, wxS( "V(out) == 1" ) );
45 BOOST_CHECK( params.IsEmpty() );
46
47 SplitMeasurement( wxS( "AVG V(out) + k == 2" ), func, signal, params );
48 BOOST_CHECK_EQUAL( signal, wxS( "V(out) + k == 2" ) );
49 BOOST_CHECK( params.IsEmpty() );
50
51 SplitMeasurement( wxS( "MIN V(\"a b=c\") from = 1m" ), func, signal, params );
52 BOOST_CHECK_EQUAL( signal, wxS( "V(\"a b=c\")" ) );
53 BOOST_CHECK_EQUAL( params, wxS( "from = 1m" ) );
54}
55
56
57BOOST_AUTO_TEST_CASE( MeasurableSignalMatchesPlot )
58{
59 wxString result;
60
61 // AC gain is plotted in dB
62 BOOST_CHECK( MeasurableSignal( wxS( "V(out)" ), PART::GAIN, result ) );
63 BOOST_CHECK_EQUAL( result, wxS( "vdb(out)" ) );
64
65 BOOST_CHECK( MeasurableSignal( wxS( "V(a)-V(b)" ), PART::GAIN, result ) );
66 BOOST_CHECK_EQUAL( result, wxS( "db(V(a)-V(b))" ) );
67
68 // S-parameter amplitude is plotted linear, and a bare complex vector measures its real part
70 BOOST_CHECK_EQUAL( result, wxS( "mag(S_1_1)" ) );
71
72 BOOST_CHECK( MeasurableSignal( wxS( "V(out)" ), PART::AMPLITUDE, result ) );
73 BOOST_CHECK_EQUAL( result, wxS( "mag(V(out))" ) );
74
75 BOOST_CHECK( !MeasurableSignal( wxS( "V(out)" ), PART::PHASE, result ) );
76}
77
78
79BOOST_AUTO_TEST_CASE( ExpandDeviceVoltagesSinglePass )
80{
81 std::map<wxString, wxString> devices = { { wxS( "C1" ), wxS( "V(r2) - V(n1)" ) },
82 { wxS( "R2" ), wxS( "V(x)" ) } };
83
84 // A net spelled like another part inside an expansion is not expanded again
85 BOOST_CHECK_EQUAL( ExpandDeviceVoltages( wxS( "2*V(C1)" ), devices ), wxS( "2*(V(r2) - V(n1))" ) );
86
87 BOOST_CHECK_EQUAL( ExpandDeviceVoltages( wxS( "vdb(c1)" ), devices ), wxS( "db(V(r2) - V(n1))" ) );
88 BOOST_CHECK_EQUAL( ExpandDeviceVoltages( wxS( "IV(R2)+V(R2)" ), devices ), wxS( "IV(R2)+(V(x))" ) );
89 BOOST_CHECK_EQUAL( ExpandDeviceVoltages( wxS( "V(\"R2\")" ), devices ), wxS( "V(\"R2\")" ) );
90 BOOST_CHECK_EQUAL( ExpandDeviceVoltages( wxS( "V(out)" ), devices ), wxS( "V(out)" ) );
91}
92
93
94BOOST_AUTO_TEST_CASE( QuoteNetNamesIgnoresCase )
95{
96 std::vector<wxString> nets = { wxS( "OUT-1" ), wxS( "IN" ) };
97
98 BOOST_CHECK_EQUAL( QuoteNetNames( wxS( "V(out-1)" ), nets ), wxS( "V(\"out-1\")" ) );
99 BOOST_CHECK_EQUAL( QuoteNetNames( wxS( "sin(V(in))" ), nets ), wxS( "sin(V(\"in\"))" ) );
100 BOOST_CHECK_EQUAL( QuoteNetNames( wxS( "V(\"OUT-1\")" ), nets ), wxS( "V(\"OUT-1\")" ) );
101}
102
103
104BOOST_AUTO_TEST_CASE( ExtractSignalRefsOnlyNamesSignals )
105{
106 std::vector<wxString> refs = ExtractSignalRefs( wxS( "im(V(out))" ) );
107
108 BOOST_REQUIRE_EQUAL( refs.size(), 1 );
109 BOOST_CHECK_EQUAL( refs[0], wxS( "V(out)" ) );
110
111 refs = ExtractSignalRefs( wxS( "vdb(a, 0) * I(V1) / P(\"R,1\")" ) );
112
113 BOOST_REQUIRE_EQUAL( refs.size(), 3 );
114 BOOST_CHECK_EQUAL( refs[0], wxS( "V(a)" ) );
115 BOOST_CHECK_EQUAL( refs[1], wxS( "I(V1)" ) );
116 BOOST_CHECK_EQUAL( refs[2], wxS( "P(R,1)" ) );
117
118 BOOST_CHECK( ExtractSignalRefs( wxS( "sin(2*pi*1k*time)" ) ).empty() );
119 BOOST_CHECK( ParensBalanced( wxS( "V(\"a)\")" ) ) );
120 BOOST_CHECK( !ParensBalanced( wxS( "V(a))(" ) ) );
121}
122
123
124BOOST_AUTO_TEST_CASE( DeviceVoltageFollowsModelPinOrder )
125{
126 // Device:D numbers the cathode 1, while the diode model declares the anode first
127 std::vector<std::string> modelPins = { "2", "1" };
128 std::vector<std::string> pinNumbers = { "1", "2" };
129
130 BOOST_CHECK_EQUAL( DeviceVoltageExpression( modelPins, pinNumbers, { "k", "a" } ), wxS( "V(a) - V(k)" ) );
131 BOOST_CHECK_EQUAL( DeviceVoltageExpression( modelPins, pinNumbers, { "0", "a" } ), wxS( "V(a)" ) );
132 BOOST_CHECK_EQUAL( DeviceVoltageExpression( modelPins, pinNumbers, { "k", "GND" } ), wxS( "-V(k)" ) );
133 BOOST_CHECK( DeviceVoltageExpression( modelPins, pinNumbers, { "a", "a" } ).IsEmpty() );
134 BOOST_CHECK( DeviceVoltageExpression( modelPins, { "1" }, { "k" } ).IsEmpty() );
135}
136
137
138BOOST_AUTO_TEST_CASE( DerivedVectorsAreExactNames )
139{
140 BOOST_CHECK( IsDerivedSignalVector( wxS( "user3" ) ) );
141 BOOST_CHECK( IsDerivedSignalVector( wxS( "KICAD_VDEV12" ) ) );
142 BOOST_CHECK( !IsDerivedSignalVector( wxS( "user_clk" ) ) );
143 BOOST_CHECK( !IsDerivedSignalVector( wxS( "kicad_vdev" ) ) );
144}
145
146
static bool empty(const wxTextEntryBase *aCtrl)
Text handling for simulator signal expressions and measurement commands.
bool ParensBalanced(const wxString &aExpression)
bool MeasurableSignal(const wxString &aBase, PART aPart, wxString &aResult)
Build the signal a measurement should read so it matches what is plotted.
wxString QuoteNetNames(const wxString &aExpression, const std::vector< wxString > &aNetnames)
Quote every whole occurrence of a net name that ngspice would otherwise parse as an expression.
void SplitMeasurement(const wxString &aCmd, wxString &aFunc, wxString &aSignal, wxString &aParams)
Split "MAX V(out) from=1m to=2m" into its function, signal and trailing parameters.
wxString DeviceVoltageExpression(const std::vector< std::string > &aModelPins, const std::vector< std::string > &aPinNumbers, const std::vector< std::string > &aPinNetNames)
Build the voltage across a two-terminal part in its model's pin order, matching the sign of I() and P...
bool IsDerivedSignalVector(const wxString &aVectorName)
std::vector< wxString > ExtractSignalRefs(const wxString &aExpression)
Extract the V() and I() and P() node or device references from an expression, unquoted.
@ GAIN
AC magnitude, plotted in dB.
@ AMPLITUDE
S-parameter magnitude, plotted linear.
wxString ExpandDeviceVoltages(const wxString &aExpression, const std::map< wxString, wxString > &aDeviceExprs)
Replace V(R1), VDB(R1), VM(R1), ... with the terminal voltage expression of the part.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
BOOST_REQUIRE_EQUAL(rows.size(), 1)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(SplitMeasurementKeepsComparisons)
wxString result
Test unit parsing edge cases and error handling.