KiCad PCB EDA Suite
libeval_compiler_test.cpp File Reference
#include <wx/wx.h>
#include <cstdio>
#include "class_board.h"
#include "class_track.h"
#include <pcb_expr_evaluator.h>
#include <io_mgr.h>
#include <plugins/kicad/kicad_plugin.h>
#include <unordered_set>
#include <profile.h>

Go to the source code of this file.

Functions

bool testEvalExpr (const std::string expr, LIBEVAL::VALUE expectedResult, bool expectError=false, BOARD_ITEM *itemA=nullptr, BOARD_ITEM *itemB=nullptr)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 54 of file libeval_compiler_test.cpp.

55 {
57  propMgr.Rebuild();
58 
59 
60  using VAL = LIBEVAL::VALUE;
61 
62 
63 
64 /* testEvalExpr( "10mm + 20 mm", VAL(30e6) );
65  testEvalExpr( "3*(7+8)", VAL(3*(7+8)) );
66  testEvalExpr( "3*7+8", VAL(3*7+8) );
67  testEvalExpr( "(3*7)+8", VAL(3*7+8) );
68  testEvalExpr( "10mm + 20)", VAL(0), true );
69  */
70 
71  BOARD brd;
72 
73  NETINFO_LIST& netInfo = brd.GetNetInfo();
74 
75  NETCLASSPTR netclass1( new NETCLASS("HV") );
76  NETCLASSPTR netclass2( new NETCLASS("otherClass" ) );
77 
78  auto net1info = new NETINFO_ITEM( &brd, "net1", 1);
79  auto net2info = new NETINFO_ITEM( &brd, "net2", 2);
80 
81  net1info->SetClass( netclass1 );
82  net2info->SetClass( netclass2 );
83 
84  TRACK trackA(&brd);
85  TRACK trackB(&brd);
86 
87  trackA.SetNet( net1info );
88  trackB.SetNet( net2info );
89 
90  trackB.SetLayer( F_Cu );
91 
92  trackA.SetWidth( Mils2iu( 10 ));
93  trackB.SetWidth( Mils2iu( 20 ));
94 
95  testEvalExpr( "A.fromTo('U1', 'U3') && A.NetClass == 'DDR3_A' ", VAL(0),false, &trackA, &trackB );
96 
97  return 0;
98 
99 // testEvalExpr( "A.onlayer('F.Cu') || A.onlayer('B.Cu')", VAL( 1.0 ), false, &trackA, &trackB );
100  testEvalExpr( "A.type == 'Pad' && B.type == 'Pad' && (A.existsOnLayer('F.Cu'))", VAL( 0.0 ), false, &trackA, &trackB );
101  return 0;
102  testEvalExpr( "A.Width > B.Width", VAL( 0.0 ), false, &trackA, &trackB );
103  testEvalExpr( "A.Width + B.Width", VAL( Mils2iu(10) + Mils2iu(20) ), false, &trackA, &trackB );
104 
105  testEvalExpr( "A.Netclass", VAL( (const char*) trackA.GetNetClassName().c_str() ), false, &trackA, &trackB );
106  testEvalExpr( "(A.Netclass == 'HV') && (B.netclass == 'otherClass') && (B.netclass != 'F.Cu')", VAL( 1.0 ), false, &trackA, &trackB );
107  testEvalExpr( "A.Netclass + 1.0", VAL( 1.0 ), false, &trackA, &trackB );
108  testEvalExpr( "A.type == 'Track' && B.type == 'Track' && A.layer == 'F.Cu'", VAL( 0.0 ), false, &trackA, &trackB );
109  testEvalExpr( "(A.type == 'Track') && (B.type == 'Track') && (A.layer == 'F.Cu')", VAL( 0.0 ), false, &trackA, &trackB );
110 
111  return 0;
112 }
void Rebuild()
Rebuilds the list of all registered properties.
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:64
LIBEVAL::VALUE VAL
const NETINFO_LIST & GetNetInfo() const
Definition: class_board.h:724
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
bool testEvalExpr(const std::string expr, LIBEVAL::VALUE expectedResult, bool expectError=false, BOARD_ITEM *itemA=nullptr, BOARD_ITEM *itemB=nullptr)
Field Value of part, i.e. "3.3K".
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_CONNECTED_ITEM::GetNetClassName(), BOARD::GetNetInfo(), PROPERTY_MANAGER::Instance(), PROPERTY_MANAGER::Rebuild(), BOARD_ITEM::SetLayer(), BOARD_CONNECTED_ITEM::SetNet(), TRACK::SetWidth(), testEvalExpr(), and VALUE.

◆ testEvalExpr()

bool testEvalExpr ( const std::string  expr,
LIBEVAL::VALUE  expectedResult,
bool  expectError = false,
BOARD_ITEM itemA = nullptr,
BOARD_ITEM itemB = nullptr 
)

Definition at line 16 of file libeval_compiler_test.cpp.

17 {
18  PCB_EXPR_COMPILER compiler;
19  PCB_EXPR_UCODE ucode;
20  bool ok = true;
21 
22  PCB_EXPR_CONTEXT context, preflightContext;
23 
24  context.SetItems( itemA, itemB );
25 
26  bool error = !compiler.Compile( expr, &ucode, &preflightContext );
27 
28 
29  if( error )
30  {
31  if ( expectError )
32  {
33  ok = true;
34  return ok;
35  }
36  else
37  {
38  ok = false;
39  }
40  }
41 
42  LIBEVAL::VALUE result;
43 
44  if( ok )
45  {
46  result = *ucode.Run( &context );
47  ok = (result.EqualTo( &expectedResult) );
48  }
49 
50  return ok;
51 }
void SetItems(BOARD_ITEM *a, BOARD_ITEM *b=nullptr)
bool Compile(const wxString &aString, UCODE *aCode, CONTEXT *aPreflightContext)
virtual bool EqualTo(const VALUE *b) const
VALUE * Run(CONTEXT *ctx)

References LIBEVAL::COMPILER::Compile(), LIBEVAL::VALUE::EqualTo(), LIBEVAL::UCODE::Run(), and PCB_EXPR_CONTEXT::SetItems().

Referenced by main().