KiCad PCB EDA Suite
test_libeval_compiler.cpp File Reference

Go to the source code of this file.

Classes

struct  EXPR_TO_TEST
 

Typedefs

using VAL = LIBEVAL::VALUE
 

Functions

static bool testEvalExpr (const wxString &expr, LIBEVAL::VALUE expectedResult, bool expectError=false, BOARD_ITEM *itemA=nullptr, BOARD_ITEM *itemB=nullptr)
 
 BOOST_AUTO_TEST_CASE (SimpleExpressions)
 
 BOOST_AUTO_TEST_CASE (IntrospectedProperties)
 

Variables

static const std::vector< EXPR_TO_TESTsimpleExpressions
 
static const std::vector< EXPR_TO_TESTintrospectionExpressions
 

Typedef Documentation

◆ VAL

Definition at line 43 of file test_libeval_compiler.cpp.

Function Documentation

◆ BOOST_AUTO_TEST_CASE() [1/2]

BOOST_AUTO_TEST_CASE ( SimpleExpressions  )

Definition at line 138 of file test_libeval_compiler.cpp.

139 {
140  for( const auto& expr : simpleExpressions )
141  {
142  bool ok = testEvalExpr( expr.expression, expr.expectedResult, expr.expectError );
143  }
144 }
static bool testEvalExpr(const wxString &expr, LIBEVAL::VALUE expectedResult, bool expectError=false, BOARD_ITEM *itemA=nullptr, BOARD_ITEM *itemB=nullptr)
static const std::vector< EXPR_TO_TEST > simpleExpressions

References simpleExpressions, and testEvalExpr().

◆ BOOST_AUTO_TEST_CASE() [2/2]

BOOST_AUTO_TEST_CASE ( IntrospectedProperties  )

Definition at line 146 of file test_libeval_compiler.cpp.

147 {
149  propMgr.Rebuild();
150 
151  BOARD brd;
152 
153  NETINFO_LIST& netInfo = brd.GetNetInfo();
154 
155  NETCLASSPTR netclass1( new NETCLASS("HV") );
156  NETCLASSPTR netclass2( new NETCLASS("otherClass" ) );
157 
158  auto net1info = new NETINFO_ITEM( &brd, "net1", 1);
159  auto net2info = new NETINFO_ITEM( &brd, "net2", 2);
160 
161  net1info->SetClass( netclass1 );
162  net2info->SetClass( netclass2 );
163 
164  TRACK trackA(&brd);
165  TRACK trackB(&brd);
166 
167  trackA.SetNet( net1info );
168  trackB.SetNet( net2info );
169 
170  trackB.SetLayer( F_Cu );
171 
172  trackA.SetWidth( Mils2iu( 10 ));
173  trackB.SetWidth( Mils2iu( 20 ));
174 
175  for( const auto& expr : introspectionExpressions )
176  {
177  bool ok = testEvalExpr( expr.expression, expr.expectedResult, expr.expectError, &trackA, &trackB );
178  }
179 }
void Rebuild()
Rebuilds the list of all registered properties.
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:64
const NETINFO_LIST & GetNetInfo() const
Definition: class_board.h:724
static bool testEvalExpr(const wxString &expr, LIBEVAL::VALUE expectedResult, bool expectError=false, BOARD_ITEM *itemA=nullptr, BOARD_ITEM *itemB=nullptr)
static const std::vector< EXPR_TO_TEST > introspectionExpressions
NETINFO_LIST is a container class for NETINFO_ITEM elements, which are the nets.
Definition: netinfo.h:422
NETCLASS handles a collection of nets and the parameters used to route or test these nets.
Definition: netclass.h:49
NETINFO_ITEM handles the data for a net.
Definition: netinfo.h:65
Information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:186
Provides class metadata.
Definition: property_mgr.h:61

References F_Cu, BOARD::GetNetInfo(), PROPERTY_MANAGER::Instance(), introspectionExpressions, PROPERTY_MANAGER::Rebuild(), BOARD_ITEM::SetLayer(), BOARD_CONNECTED_ITEM::SetNet(), TRACK::SetWidth(), and testEvalExpr().

◆ testEvalExpr()

static bool testEvalExpr ( const wxString &  expr,
LIBEVAL::VALUE  expectedResult,
bool  expectError = false,
BOARD_ITEM itemA = nullptr,
BOARD_ITEM itemB = nullptr 
)
static

Definition at line 88 of file test_libeval_compiler.cpp.

90 {
91  PCB_EXPR_COMPILER compiler;
92  PCB_EXPR_UCODE ucode;
93  PCB_EXPR_CONTEXT context, preflightContext;
94  bool ok = true;
95 
96  context.SetItems( itemA, itemB );
97 
98 
99  BOOST_TEST_MESSAGE("Expr: '" << expr.c_str() << "'");
100 
101  bool error = !compiler.Compile( expr, &ucode, &preflightContext );
102 
103  BOOST_CHECK_EQUAL( error, expectError );
104 
105  if( error != expectError )
106  {
107  BOOST_TEST_MESSAGE( "Result: FAIL: " << compiler.GetError().message.c_str() <<
108  " (code pos: " << compiler.GetError().srcPos << ")" );
109 
110  return false;
111  }
112 
113  if( error )
114  return true;
115 
116  LIBEVAL::VALUE result;
117 
118  if( ok )
119  {
120  result = *ucode.Run( &context );
121  ok = ( result.EqualTo( &expectedResult ) );
122  }
123 
124 
125  if( expectedResult.GetType() == LIBEVAL::VT_NUMERIC )
126  {
127  BOOST_CHECK_EQUAL( result.AsDouble(), expectedResult.AsDouble() );
128  }
129  else
130  {
131  BOOST_CHECK_EQUAL( result.AsString(), expectedResult.AsString() );
132  }
133 
134 
135  return ok;
136 }
void SetItems(BOARD_ITEM *a, BOARD_ITEM *b=nullptr)
bool Compile(const wxString &aString, UCODE *aCode, CONTEXT *aPreflightContext)
VAR_TYPE_T GetType() const
virtual bool EqualTo(const VALUE *b) const
const ERROR_STATUS & GetError() const
virtual double AsDouble() const
virtual const wxString & AsString() const
VALUE * Run(CONTEXT *ctx)

References LIBEVAL::VALUE::AsDouble(), LIBEVAL::VALUE::AsString(), LIBEVAL::COMPILER::Compile(), LIBEVAL::VALUE::EqualTo(), LIBEVAL::COMPILER::GetError(), LIBEVAL::VALUE::GetType(), LIBEVAL::ERROR_STATUS::message, LIBEVAL::UCODE::Run(), PCB_EXPR_CONTEXT::SetItems(), LIBEVAL::ERROR_STATUS::srcPos, and LIBEVAL::VT_NUMERIC.

Referenced by BOOST_AUTO_TEST_CASE().

Variable Documentation

◆ introspectionExpressions

const std::vector<EXPR_TO_TEST> introspectionExpressions
static
Initial value:
= {
{ "A.type == 'Pad' && B.type == 'Pad' && (A.existsOnLayer('F.Cu'))", false, VAL( 0.0 ) },
{ "A.Width > B.Width", false, VAL( 0.0 ) },
{ "A.Width + B.Width", false, VAL( Mils2iu(10) + Mils2iu(20) ) },
{ "A.Netclass", false, VAL( "HV" ) },
{ "(A.Netclass == 'HV') && (B.netclass == 'otherClass') && (B.netclass != 'F.Cu')", false, VAL( 1.0 ) },
{ "A.Netclass + 1.0", false, VAL( 1.0 ) },
{ "A.type == 'Track' && B.type == 'Track' && A.layer == 'F.Cu'", false, VAL( 1.0 ) },
{ "(A.type == 'Track') && (B.type == 'Track') && (A.layer == 'F.Cu')", false, VAL( 1.0 ) },
{ "A.type == 'Via' && A.isMicroVia()", false, VAL(0.0) }
}
LIBEVAL::VALUE VAL

Definition at line 75 of file test_libeval_compiler.cpp.

Referenced by BOOST_AUTO_TEST_CASE().

◆ simpleExpressions

const std::vector<EXPR_TO_TEST> simpleExpressions
static
Initial value:
= {
{ "10mm + 20 mm", false, VAL( 30e6 ) },
{ "3*(7+8)", false, VAL( 3 * ( 7 + 8 ) ) },
{ "3*7+8", false, VAL( 3 * 7 + 8 ) },
{ "(3*7)+8", false, VAL( 3 * 7 + 8 ) },
{ "10mm + 20)", true, VAL( 0 ) },
{ "1", false, VAL(1) },
{ "1.5", false, VAL(1.5) },
{ "1,5", false, VAL(1.5) },
{ "1mm", false, VAL(1e6) },
{ " 1 + 2 ", false, VAL(3) },
{ "1.5 + 0.2 + 0.1", false, VAL(1.8) },
{ "3 - 10", false, VAL(-7) },
{ "1 + 2 + 10 + 1000.05", false, VAL(1013.05) },
{ "1 + 2 - 4 * 20 / 2", false, VAL(-37) },
{ "(1)", false, VAL(1) },
{ "-(1 + (2 - 4)) * 20.8 / 2", false, VAL(10.4) },
{ "+2 - 1", false, VAL(1) }
}
LIBEVAL::VALUE VAL

Definition at line 45 of file test_libeval_compiler.cpp.

Referenced by BOOST_AUTO_TEST_CASE().