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>
33#include <pcbnew/pcb_track.h>
34
35BOOST_AUTO_TEST_SUITE( Libeval_Compiler )
36
38{
39 wxString expression;
42
43 friend std::ostream& operator<<( std::ostream& os, const EXPR_TO_TEST& expr )
44 {
45 os << expr.expression;
46 return os;
47 }
48};
49
51
52const static std::vector<EXPR_TO_TEST> simpleExpressions = {
53 { "10mm + 20 mm", false, VAL( 30e6 ) },
54 { "3*(7+8)", false, VAL( 3 * ( 7 + 8 ) ) },
55 { "3*7+8", false, VAL( 3 * 7 + 8 ) },
56 { "(3*7)+8", false, VAL( 3 * 7 + 8 ) },
57 { "10mm + 20)", true, VAL( 0 ) },
58
59 { "1", false, VAL(1) },
60 { "1.5", false, VAL(1.5) },
61 { "1,5", false, VAL(1.5) },
62 { "1mm", false, VAL(1e6) },
63 // Any White-space is OK
64 { " 1 + 2 ", false, VAL(3) },
65 // Decimals are OK in expressions
66 { "1.5 + 0.2 + 0.1", false, VAL(1.8) },
67 // Negatives are OK
68 { "3 - 10", false, VAL(-7) },
69 // Lots of operands
70 { "1 + 2 + 10 + 1000.05", false, VAL(1013.05) },
71 // Operator precedence
72 { "1 + 2 - 4 * 20 / 2", false, VAL(-37) },
73 // Parens
74 { "(1)", false, VAL(1) },
75 // Parens affect precedence
76 { "-(1 + (2 - 4)) * 20.8 / 2", false, VAL(10.4) },
77 // Unary addition is a sign, not a leading operator
78 { "+2 - 1", false, VAL(1) }
79};
80
81
82const static std::vector<EXPR_TO_TEST> introspectionExpressions = {
83 { "A.type == 'Pad' && B.type == 'Pad' && (A.existsOnLayer('F.Cu'))", false, VAL( 0.0 ) },
84 { "A.Width > B.Width", false, VAL( 0.0 ) },
85 { "A.Width + B.Width", false, VAL( pcbIUScale.MilsToIU(10) + pcbIUScale.MilsToIU(20) ) },
86 { "A.Netclass", false, VAL( "HV_LINE" ) },
87 { "(A.Netclass == 'HV_LINE') && (B.netclass == 'otherClass') && (B.netclass != 'F.Cu')", false,
88 VAL( 1.0 ) },
89 { "A.Netclass + 1.0", false, VAL( 1.0 ) },
90 { "A.hasNetclass('HV_LINE')", false, VAL( 1.0 ) },
91 { "A.hasNetclass('HV_*')", false, VAL( 1.0 ) },
92 { "A.type == 'Track' && B.type == 'Track' && A.layer == 'F.Cu'", false, VAL( 1.0 ) },
93 { "(A.type == 'Track') && (B.type == 'Track') && (A.layer == 'F.Cu')", false, VAL( 1.0 ) },
94 { "A.type == 'Via' && A.isMicroVia()", false, VAL(0.0) }
95};
96
97
98static bool testEvalExpr( const wxString& expr, const LIBEVAL::VALUE& expectedResult,
99 bool expectError = false, BOARD_ITEM* itemA = nullptr,
100 BOARD_ITEM* itemB = nullptr )
101{
102 PCBEXPR_COMPILER compiler( new PCBEXPR_UNIT_RESOLVER() );
103 PCBEXPR_UCODE ucode;
106 bool ok = true;
107
108 context.SetItems( itemA, itemB );
109
110
111 BOOST_TEST_MESSAGE( "Expr: '" << expr.c_str() << "'" );
112
113 bool error = !compiler.Compile( expr, &ucode, &preflightContext );
114
115 BOOST_CHECK_EQUAL( error, expectError );
116
117 if( error != expectError )
118 {
119 BOOST_TEST_MESSAGE( "Result: FAIL: " << compiler.GetError().message.c_str() <<
120 " (code pos: " << compiler.GetError().srcPos << ")" );
121
122 return false;
123 }
124
125 if( error )
126 return true;
127
129
130 if( ok )
131 {
132 result = ucode.Run( &context );
133 ok = ( result->EqualTo( &context, &expectedResult ) );
134 }
135
136 if( expectedResult.GetType() == LIBEVAL::VT_NUMERIC )
137 {
138 BOOST_CHECK_EQUAL( result->AsDouble(), expectedResult.AsDouble() );
139 }
140 else
141 {
142 BOOST_CHECK_EQUAL( result->AsString(), expectedResult.AsString() );
143 }
144
145 return ok;
146}
147
148
149BOOST_DATA_TEST_CASE( SimpleExpressions, boost::unit_test::data::make( simpleExpressions ), expr )
150{
151 testEvalExpr( expr.expression, expr.expectedResult, expr.expectError );
152}
153
154
155BOOST_AUTO_TEST_CASE( IntrospectedProperties )
156{
158 propMgr.Rebuild();
159
160 BOARD brd;
161
162 const NETINFO_LIST& netInfo = brd.GetNetInfo();
163
164 std::shared_ptr<NETCLASS> netclass1( new NETCLASS( "HV_LINE" ) );
165 std::shared_ptr<NETCLASS> netclass2( new NETCLASS( "otherClass" ) );
166
167 auto net1info = new NETINFO_ITEM( &brd, "net1", 1 );
168 auto net2info = new NETINFO_ITEM( &brd, "net2", 2 );
169
170 net1info->SetNetClass( netclass1 );
171 net2info->SetNetClass( netclass2 );
172
173 PCB_TRACK trackA( &brd );
174 PCB_TRACK trackB( &brd );
175
176 trackA.SetNet( net1info );
177 trackB.SetNet( net2info );
178
179 trackB.SetLayer( F_Cu );
180
181 trackA.SetWidth( pcbIUScale.MilsToIU( 10 ) );
182 trackB.SetWidth( pcbIUScale.MilsToIU( 20 ) );
183
184 for( const auto& expr : introspectionExpressions )
185 {
186 testEvalExpr( expr.expression, expr.expectedResult, expr.expectError, &trackA, &trackB );
187 }
188}
189
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:112
void SetNet(NETINFO_ITEM *aNetInfo)
Set a NET_INFO object for the item.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:79
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition board_item.h:280
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:317
const NETINFO_LIST & GetNetInfo() const
Definition board.h:933
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:45
Handle the data for a net.
Definition netinfo.h:54
Container for NETINFO_ITEM elements, which are the nets.
Definition netinfo.h:330
void SetItems(BOARD_ITEM *a, BOARD_ITEM *b=nullptr)
virtual void SetWidth(int aWidth)
Definition pcb_track.h:145
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:48
@ 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("Polyline has "<< chain.PointCount()<< " points")
wxString result
Test unit parsing edge cases and error handling.
BOOST_CHECK_EQUAL(result, "25.4")