KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 "pcb_track.h"
6
7#include <pcbexpr_evaluator.h>
8
9#include <pcb_io/pcb_io_mgr.h>
11
12#include <unordered_set>
13
14#include <core/profile.h>
15
16bool testEvalExpr( const std::string expr, LIBEVAL::VALUE expectedResult, bool expectError = false,
17 BOARD_ITEM* itemA = nullptr, BOARD_ITEM* itemB = nullptr )
18{
19 PCBEXPR_COMPILER compiler( new PCB_UNIT_RESOLVER() );
20 PCBEXPR_UCODE ucode;
21 bool ok = true;
22
23 PCBEXPR_CONTEXT context, preflightContext;
24
25 context.SetItems( itemA, itemB );
26
27 bool error = !compiler.Compile( expr, &ucode, &preflightContext );
28
29
30 if( error )
31 {
32 if ( expectError )
33 {
34 ok = true;
35 return ok;
36 }
37 else
38 {
39 ok = false;
40 }
41 }
42
43 LIBEVAL::VALUE result;
44
45 if( ok )
46 {
47 result = *ucode.Run( &context );
48 ok = (result.EqualTo( &expectedResult) );
49 }
50
51 return ok;
52}
53
54
55int main( int argc, char *argv[] )
56{
58 propMgr.Rebuild();
59
60
61 using VAL = LIBEVAL::VALUE;
62
63
64
65/* testEvalExpr( "10mm + 20 mm", VAL(30e6) );
66 testEvalExpr( "3*(7+8)", VAL(3*(7+8)) );
67 testEvalExpr( "3*7+8", VAL(3*7+8) );
68 testEvalExpr( "(3*7)+8", VAL(3*7+8) );
69 testEvalExpr( "10mm + 20)", VAL(0), true );
70 */
71
72 BOARD brd;
73
74 NETINFO_LIST& netInfo = brd.GetNetInfo();
75
76 NETCLASSPTR netclass1( new NETCLASS("HV") );
77 NETCLASSPTR netclass2( new NETCLASS("otherClass" ) );
78
79 auto net1info = new NETINFO_ITEM( &brd, "net1", 1);
80 auto net2info = new NETINFO_ITEM( &brd, "net2", 2);
81
82 net1info->SetClass( netclass1 );
83 net2info->SetClass( netclass2 );
84
85 PCB_TRACK trackA( &brd );
86 PCB_TRACK trackB( &brd );
87
88 trackA.SetNet( net1info );
89 trackB.SetNet( net2info );
90
91 trackB.SetLayer( F_Cu );
92
93 trackA.SetWidth( Mils2iu( 10 ) );
94 trackB.SetWidth( Mils2iu( 20 ) );
95
96 testEvalExpr( "A.fromTo('U1', 'U3') && A.NetClass == 'DDR3_A' ", VAL(0), false, &trackA, &trackB );
97
98 return 0;
99
100// testEvalExpr( "A.onlayer('F.Cu') || A.onlayer('B.Cu')", VAL( 1.0 ), false, &trackA, &trackB );
101 testEvalExpr( "A.type == 'Pad' && B.type == 'Pad' && (A.existsOnLayer('F.Cu'))", VAL( 0.0 ), false, &trackA, &trackB );
102 return 0;
103 testEvalExpr( "A.Width > B.Width", VAL( 0.0 ), false, &trackA, &trackB );
104 testEvalExpr( "A.Width + B.Width", VAL( Mils2iu(10) + Mils2iu(20) ), false, &trackA, &trackB );
105
106 testEvalExpr( "A.Netclass", VAL( (const char*) trackA.GetNetClassName().c_str() ), false, &trackA, &trackB );
107 testEvalExpr( "(A.Netclass == 'HV') && (B.netclass == 'otherClass') && (B.netclass != 'F.Cu')", VAL( 1.0 ), false, &trackA, &trackB );
108 testEvalExpr( "A.Netclass + 1.0", VAL( 1.0 ), false, &trackA, &trackB );
109 testEvalExpr( "A.type == 'Track' && B.type == 'Track' && A.layer == 'F.Cu'", VAL( 0.0 ), false, &trackA, &trackB );
110 testEvalExpr( "(A.type == 'Track') && (B.type == 'Track') && (A.layer == 'F.Cu')", VAL( 0.0 ), false, &trackA, &trackB );
111
112 return 0;
113}
void SetNet(NETINFO_ITEM *aNetInfo)
Set a NET_INFO object for the item.
wxString GetNetClassName() const
Returns the name of the effective netclass.
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:288
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:290
const NETINFO_LIST & GetNetInfo() const
Definition: board.h:871
bool Compile(const wxString &aString, UCODE *aCode, CONTEXT *aPreflightContext)
VALUE * Run(CONTEXT *ctx)
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:44
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.
main()
@ F_Cu
Definition: layer_ids.h:64
bool testEvalExpr(const std::string expr, LIBEVAL::VALUE expectedResult, bool expectError=false, BOARD_ITEM *itemA=nullptr, BOARD_ITEM *itemB=nullptr)
LIBEVAL::VALUE VAL