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