KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_libeval_compiler.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 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25#include <boost/test/data/test_case.hpp>
26
27#include <wx/wx.h>
28
29#include <layer_ids.h>
31#include <drc/drc_rule.h>
32#include <pcbnew/board.h>
34#include <pcbnew/pcb_track.h>
36#include <properties/property.h>
38
39BOOST_AUTO_TEST_SUITE( Libeval_Compiler )
40
42{
43 wxString expression;
46
47 friend std::ostream& operator<<( std::ostream& os, const EXPR_TO_TEST& expr )
48 {
49 os << expr.expression;
50 return os;
51 }
52};
53
55
56const static std::vector<EXPR_TO_TEST> simpleExpressions = {
57 { "10mm + 20 mm", false, VAL( 30e6 ) },
58 { "3*(7+8)", false, VAL( 3 * ( 7 + 8 ) ) },
59 { "3*7+8", false, VAL( 3 * 7 + 8 ) },
60 { "(3*7)+8", false, VAL( 3 * 7 + 8 ) },
61 { "10mm + 20)", true, VAL( 0 ) },
62
63 { "1", false, VAL(1) },
64 { "1.5", false, VAL(1.5) },
65 { "1,5", false, VAL(1.5) },
66 { "1mm", false, VAL(1e6) },
67 // Any White-space is OK
68 { " 1 + 2 ", false, VAL(3) },
69 // Decimals are OK in expressions
70 { "1.5 + 0.2 + 0.1", false, VAL(1.8) },
71 // Negatives are OK
72 { "3 - 10", false, VAL(-7) },
73 // Lots of operands
74 { "1 + 2 + 10 + 1000.05", false, VAL(1013.05) },
75 // Operator precedence
76 { "1 + 2 - 4 * 20 / 2", false, VAL(-37) },
77 // Parens
78 { "(1)", false, VAL(1) },
79 // Parens affect precedence
80 { "-(1 + (2 - 4)) * 20.8 / 2", false, VAL(10.4) },
81 // Unary addition is a sign, not a leading operator
82 { "+2 - 1", false, VAL(1) }
83};
84
85
86const static std::vector<EXPR_TO_TEST> introspectionExpressions = {
87 { "A.type == 'Pad' && B.type == 'Pad' && (A.existsOnLayer('F.Cu'))", false, VAL( 0.0 ) },
88 { "A.Width > B.Width", false, VAL( 0.0 ) },
89 { "A.Width + B.Width", false, VAL( pcbIUScale.MilsToIU(10) + pcbIUScale.MilsToIU(20) ) },
90 { "A.Netclass", false, VAL( "HV_LINE" ) },
91 { "(A.Netclass == 'HV_LINE') && (B.netclass == 'otherClass') && (B.netclass != 'F.Cu')", false,
92 VAL( 1.0 ) },
93 { "A.Netclass + 1.0", false, VAL( 1.0 ) },
94 { "A.hasNetclass('HV_LINE')", false, VAL( 1.0 ) },
95 { "A.hasNetclass('HV_*')", false, VAL( 1.0 ) },
96 { "A.type == 'Track' && B.type == 'Track' && A.layer == 'F.Cu'", false, VAL( 1.0 ) },
97 { "(A.type == 'Track') && (B.type == 'Track') && (A.layer == 'F.Cu')", false, VAL( 1.0 ) },
98 { "A.type == 'Via' && A.isMicroVia()", false, VAL(0.0) }
99};
100
101
102static bool testEvalExpr( const wxString& expr, const LIBEVAL::VALUE& expectedResult,
103 bool expectError = false, BOARD_ITEM* itemA = nullptr,
104 BOARD_ITEM* itemB = nullptr )
105{
106 PCBEXPR_COMPILER compiler( new PCBEXPR_UNIT_RESOLVER() );
107 PCBEXPR_UCODE ucode;
110 bool ok = true;
111
112 context.SetItems( itemA, itemB );
113
114
115 BOOST_TEST_MESSAGE( "Expr: '" << expr.c_str() << "'" );
116
117 bool error = !compiler.Compile( expr, &ucode, &preflightContext );
118
119 BOOST_CHECK_EQUAL( error, expectError );
120
121 if( error != expectError )
122 {
123 BOOST_TEST_MESSAGE( "Result: FAIL: " << compiler.GetError().message.c_str() <<
124 " (code pos: " << compiler.GetError().srcPos << ")" );
125
126 return false;
127 }
128
129 if( error )
130 return true;
131
133
134 if( ok )
135 {
136 result = ucode.Run( &context );
137 ok = ( result->EqualTo( &context, &expectedResult ) );
138 }
139
140 if( expectedResult.GetType() == LIBEVAL::VT_NUMERIC )
141 {
142 BOOST_CHECK_EQUAL( result->AsDouble(), expectedResult.AsDouble() );
143 }
144 else
145 {
146 BOOST_CHECK_EQUAL( result->AsString(), expectedResult.AsString() );
147 }
148
149 return ok;
150}
151
152
153BOOST_DATA_TEST_CASE( SimpleExpressions, boost::unit_test::data::make( simpleExpressions ), expr )
154{
155 testEvalExpr( expr.expression, expr.expectedResult, expr.expectError );
156}
157
158
159BOOST_AUTO_TEST_CASE( IntrospectedProperties )
160{
162 propMgr.Rebuild();
163
164 BOARD brd;
165
166 const NETINFO_LIST& netInfo = brd.GetNetInfo();
167
168 std::shared_ptr<NETCLASS> netclass1( new NETCLASS( "HV_LINE" ) );
169 std::shared_ptr<NETCLASS> netclass2( new NETCLASS( "otherClass" ) );
170
171 auto net1info = new NETINFO_ITEM( &brd, "net1", 1 );
172 auto net2info = new NETINFO_ITEM( &brd, "net2", 2 );
173
174 net1info->SetNetClass( netclass1 );
175 net2info->SetNetClass( netclass2 );
176
177 PCB_TRACK trackA( &brd );
178 PCB_TRACK trackB( &brd );
179
180 trackA.SetNet( net1info );
181 trackB.SetNet( net2info );
182
183 trackB.SetLayer( F_Cu );
184
185 trackA.SetWidth( pcbIUScale.MilsToIU( 10 ) );
186 trackB.SetWidth( pcbIUScale.MilsToIU( 20 ) );
187
188 for( const auto& expr : introspectionExpressions )
189 {
190 testEvalExpr( expr.expression, expr.expectedResult, expr.expectError, &trackA, &trackB );
191 }
192}
193
194BOOST_AUTO_TEST_CASE( InNetChainClassWildcard )
195{
197 propMgr.Rebuild();
198
199 BOARD brd;
200
201 std::shared_ptr<NET_SETTINGS> netSettings = brd.GetDesignSettings().m_NetSettings;
202 netSettings->SetNetChainClass( wxT( "ChainHS" ), wxT( "HighSpeed" ) );
203
204 auto netUnclassified = new NETINFO_ITEM( &brd, "netA", 1 );
205 auto netClassified = new NETINFO_ITEM( &brd, "netB", 2 );
206 auto netNoChain = new NETINFO_ITEM( &brd, "netC", 3 );
207
208 netUnclassified->SetNetChain( wxT( "ChainOrphan" ) );
209 netClassified->SetNetChain( wxT( "ChainHS" ) );
210
211 PCB_TRACK trackUnclassified( &brd );
212 PCB_TRACK trackClassified( &brd );
213 PCB_TRACK trackNoChain( &brd );
214
215 trackUnclassified.SetNet( netUnclassified );
216 trackClassified.SetNet( netClassified );
217 trackNoChain.SetNet( netNoChain );
218
219 // A chain with no class assignment must not match any inNetChainClass() pattern,
220 // including the '*' wildcard.
221 testEvalExpr( wxT( "A.inNetChainClass('*')" ), VAL( 0.0 ), false, &trackUnclassified,
222 &trackUnclassified );
223 testEvalExpr( wxT( "A.inNetChainClass('HighSpeed')" ), VAL( 0.0 ), false, &trackUnclassified,
224 &trackUnclassified );
225
226 // Net with no chain at all must not match either.
227 testEvalExpr( wxT( "A.inNetChainClass('*')" ), VAL( 0.0 ), false, &trackNoChain,
228 &trackNoChain );
229
230 // Properly classified chain must match both wildcard and exact patterns.
231 testEvalExpr( wxT( "A.inNetChainClass('*')" ), VAL( 1.0 ), false, &trackClassified,
232 &trackClassified );
233 testEvalExpr( wxT( "A.inNetChainClass('HighSpeed')" ), VAL( 1.0 ), false, &trackClassified,
234 &trackClassified );
235 testEvalExpr( wxT( "A.inNetChainClass('High*')" ), VAL( 1.0 ), false, &trackClassified,
236 &trackClassified );
237 testEvalExpr( wxT( "A.inNetChainClass('LowSpeed')" ), VAL( 0.0 ), false, &trackClassified,
238 &trackClassified );
239}
240
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:125
virtual void SetNet(NETINFO_ITEM *aNetInfo)
Set a NET_INFO object for the item.
void SetLayer(PCB_LAYER_ID aLayer) override
Set the layer this item is on.
std::shared_ptr< NET_SETTINGS > m_NetSettings
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:323
const NETINFO_LIST & GetNetInfo() const
Definition board.h:1004
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition board.cpp:1101
const ERROR_STATUS & GetError() const
bool Compile(const wxString &aString, UCODE *aCode, CONTEXT *aPreflightContext)
VALUE * Run(CONTEXT *ctx)
virtual const wxString & AsString() const
virtual double AsDouble() const
VAR_TYPE_T GetType() const
A collection of nets and the parameters used to route or test these nets.
Definition netclass.h:42
Handle the data for a net.
Definition netinfo.h:50
Container for NETINFO_ITEM elements, which are the nets.
Definition netinfo.h:224
void SetItems(BOARD_ITEM *a, BOARD_ITEM *b=nullptr)
virtual void SetWidth(int aWidth)
Definition pcb_track.h:90
Provide class metadata.Helper macro to map type hashes to names.
static PROPERTY_MANAGER & Instance()
void Rebuild()
Rebuild the list of all registered properties.
@ NULL_CONSTRAINT
Definition drc_rule.h:54
@ UNDEFINED_LAYER
Definition layer_ids.h:61
@ F_Cu
Definition layer_ids.h:64
friend std::ostream & operator<<(std::ostream &os, const EXPR_TO_TEST &expr)
LIBEVAL::VALUE expectedResult
BOOST_DATA_TEST_CASE(ConvertToKicadUnit, boost::unit_test::data::make(altium_to_kicad_unit), input_value, expected_result)
Test conversation from Altium internal units into KiCad internal units.
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
static const std::vector< EXPR_TO_TEST > simpleExpressions
static bool testEvalExpr(const wxString &expr, const LIBEVAL::VALUE &expectedResult, bool expectError=false, BOARD_ITEM *itemA=nullptr, BOARD_ITEM *itemB=nullptr)
LIBEVAL::VALUE VAL
static const std::vector< EXPR_TO_TEST > introspectionExpressions
BOOST_AUTO_TEST_CASE(IntrospectedProperties)
BOOST_TEST_MESSAGE("\n=== Real-World Polygon PIP Benchmark ===\n"<< formatTable(table))
wxString result
Test unit parsing edge cases and error handling.
BOOST_CHECK_EQUAL(result, "25.4")