KiCad PCB EDA Suite
libeval_compiler_test.cpp
Go to the documentation of this file.
1 #include <wx/wx.h>
2 #include <cstdio>
3 
4 #include "board.h"
5 #include "track.h"
6 
7 #include <pcb_expr_evaluator.h>
8 
9 #include <io_mgr.h>
11 
12 #include <unordered_set>
13 
14 #include <profile.h>
15 
16 bool testEvalExpr( const std::string expr, LIBEVAL::VALUE expectedResult, bool expectError = false, BOARD_ITEM* itemA = nullptr, BOARD_ITEM* itemB = nullptr )
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 }
52 
53 
54 int main( int argc, char *argv[] )
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.
void SetItems(BOARD_ITEM *a, BOARD_ITEM *b=nullptr)
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:64
LIBEVAL::VALUE VAL
virtual void SetLayer(PCB_LAYER_ID aLayer)
Function SetLayer sets the layer this item is on.
Definition: board_item.h:206
BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
Definition: board_item.h:86
const NETINFO_LIST & GetNetInfo() const
Definition: board.h:719
virtual wxString GetNetClassName() const
Function GetNetClassName returns a pointer to the netclass of the zone.
bool Compile(const wxString &aString, UCODE *aCode, CONTEXT *aPreflightContext)
void SetWidth(int aWidth)
Definition: track.h:109
virtual bool EqualTo(const VALUE *b) const
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)
NETINFO_ITEM handles the data for a net.
Definition: netinfo.h:65
void SetNet(NETINFO_ITEM *aNetInfo)
Function SetNet Sets a NET_INFO object for the item.
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:186
Provides class metadata.
Definition: property_mgr.h:61
VALUE * Run(CONTEXT *ctx)
int main(int argc, char *argv[])
Definition: track.h:83