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" ) },
87 { "(A.Netclass == 'HV') && (B.netclass == 'otherClass') && (B.netclass != 'F.Cu')", false,
88 VAL( 1.0 ) },
89 { "A.Netclass + 1.0", false, VAL( 1.0 ) },
90 { "A.type == 'Track' && B.type == 'Track' && A.layer == 'F.Cu'", false, VAL( 1.0 ) },
91 { "(A.type == 'Track') && (B.type == 'Track') && (A.layer == 'F.Cu')", false, VAL( 1.0 ) },
92 { "A.type == 'Via' && A.isMicroVia()", false, VAL(0.0) }
93};
94
95
96static bool testEvalExpr( const wxString& expr, const LIBEVAL::VALUE& expectedResult,
97 bool expectError = false, BOARD_ITEM* itemA = nullptr,
98 BOARD_ITEM* itemB = nullptr )
99{
100 PCBEXPR_COMPILER compiler( new PCBEXPR_UNIT_RESOLVER() );
101 PCBEXPR_UCODE ucode;
104 bool ok = true;
105
106 context.SetItems( itemA, itemB );
107
108
109 BOOST_TEST_MESSAGE( "Expr: '" << expr.c_str() << "'" );
110
111 bool error = !compiler.Compile( expr, &ucode, &preflightContext );
112
113 BOOST_CHECK_EQUAL( error, expectError );
114
115 if( error != expectError )
116 {
117 BOOST_TEST_MESSAGE( "Result: FAIL: " << compiler.GetError().message.c_str() <<
118 " (code pos: " << compiler.GetError().srcPos << ")" );
119
120 return false;
121 }
122
123 if( error )
124 return true;
125
126 LIBEVAL::VALUE* result;
127
128 if( ok )
129 {
130 result = ucode.Run( &context );
131 ok = ( result->EqualTo( &context, &expectedResult ) );
132 }
133
134 if( expectedResult.GetType() == LIBEVAL::VT_NUMERIC )
135 {
136 BOOST_CHECK_EQUAL( result->AsDouble(), expectedResult.AsDouble() );
137 }
138 else
139 {
140 BOOST_CHECK_EQUAL( result->AsString(), expectedResult.AsString() );
141 }
142
143 return ok;
144}
145
146
147BOOST_DATA_TEST_CASE( SimpleExpressions, boost::unit_test::data::make( simpleExpressions ), expr )
148{
149 testEvalExpr( expr.expression, expr.expectedResult, expr.expectError );
150}
151
152
153BOOST_AUTO_TEST_CASE( IntrospectedProperties )
154{
156 propMgr.Rebuild();
157
158 BOARD brd;
159
160 const NETINFO_LIST& netInfo = brd.GetNetInfo();
161
162 std::shared_ptr<NETCLASS> netclass1( new NETCLASS( "HV" ) );
163 std::shared_ptr<NETCLASS> netclass2( new NETCLASS( "otherClass" ) );
164
165 auto net1info = new NETINFO_ITEM( &brd, "net1", 1 );
166 auto net2info = new NETINFO_ITEM( &brd, "net2", 2 );
167
168 net1info->SetNetClass( netclass1 );
169 net2info->SetNetClass( netclass2 );
170
171 PCB_TRACK trackA( &brd );
172 PCB_TRACK trackB( &brd );
173
174 trackA.SetNet( net1info );
175 trackB.SetNet( net2info );
176
177 trackB.SetLayer( F_Cu );
178
179 trackA.SetWidth( pcbIUScale.MilsToIU( 10 ) );
180 trackB.SetWidth( pcbIUScale.MilsToIU( 20 ) );
181
182 for( const auto& expr : introspectionExpressions )
183 {
184 testEvalExpr( expr.expression, expr.expectedResult, expr.expectError, &trackA, &trackB );
185 }
186}
187
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:108
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:290
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:295
const NETINFO_LIST & GetNetInfo() const
Definition: board.h:879
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
virtual bool EqualTo(CONTEXT *aCtx, const VALUE *b) 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:56
Container for NETINFO_ITEM elements, which are the nets.
Definition: netinfo.h:346
void SetItems(BOARD_ITEM *a, BOARD_ITEM *b=nullptr)
virtual void SetWidth(int aWidth)
Definition: pcb_track.h:115
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:85
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:87
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
constexpr int MilsToIU(int mils) const
Definition: base_units.h:93
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_CHECK_EQUAL(ret, c.m_exp_result)
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")