You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.0 KiB

  1. #include <wx/wx.h>
  2. #include <cstdio>
  3. #include "board.h"
  4. #include "pcb_track.h"
  5. #include <pcb_expr_evaluator.h>
  6. #include <io_mgr.h>
  7. #include <plugins/kicad/pcb_plugin.h>
  8. #include <unordered_set>
  9. #include <profile.h>
  10. bool testEvalExpr( const std::string expr, LIBEVAL::VALUE expectedResult, bool expectError = false, BOARD_ITEM* itemA = nullptr, BOARD_ITEM* itemB = nullptr )
  11. {
  12. PCB_EXPR_COMPILER compiler;
  13. PCB_EXPR_UCODE ucode;
  14. bool ok = true;
  15. PCB_EXPR_CONTEXT context, preflightContext;
  16. context.SetItems( itemA, itemB );
  17. bool error = !compiler.Compile( expr, &ucode, &preflightContext );
  18. if( error )
  19. {
  20. if ( expectError )
  21. {
  22. ok = true;
  23. return ok;
  24. }
  25. else
  26. {
  27. ok = false;
  28. }
  29. }
  30. LIBEVAL::VALUE result;
  31. if( ok )
  32. {
  33. result = *ucode.Run( &context );
  34. ok = (result.EqualTo( &expectedResult) );
  35. }
  36. return ok;
  37. }
  38. int main( int argc, char *argv[] )
  39. {
  40. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  41. propMgr.Rebuild();
  42. using VAL = LIBEVAL::VALUE;
  43. /* testEvalExpr( "10mm + 20 mm", VAL(30e6) );
  44. testEvalExpr( "3*(7+8)", VAL(3*(7+8)) );
  45. testEvalExpr( "3*7+8", VAL(3*7+8) );
  46. testEvalExpr( "(3*7)+8", VAL(3*7+8) );
  47. testEvalExpr( "10mm + 20)", VAL(0), true );
  48. */
  49. BOARD brd;
  50. NETINFO_LIST& netInfo = brd.GetNetInfo();
  51. NETCLASSPTR netclass1( new NETCLASS("HV") );
  52. NETCLASSPTR netclass2( new NETCLASS("otherClass" ) );
  53. auto net1info = new NETINFO_ITEM( &brd, "net1", 1);
  54. auto net2info = new NETINFO_ITEM( &brd, "net2", 2);
  55. net1info->SetClass( netclass1 );
  56. net2info->SetClass( netclass2 );
  57. PCB_TRACK trackA( &brd );
  58. PCB_TRACK trackB( &brd );
  59. trackA.SetNet( net1info );
  60. trackB.SetNet( net2info );
  61. trackB.SetLayer( F_Cu );
  62. trackA.SetWidth( Mils2iu( 10 ) );
  63. trackB.SetWidth( Mils2iu( 20 ) );
  64. testEvalExpr( "A.fromTo('U1', 'U3') && A.NetClass == 'DDR3_A' ", VAL(0), false, &trackA, &trackB );
  65. return 0;
  66. // testEvalExpr( "A.onlayer('F.Cu') || A.onlayer('B.Cu')", VAL( 1.0 ), false, &trackA, &trackB );
  67. testEvalExpr( "A.type == 'Pad' && B.type == 'Pad' && (A.existsOnLayer('F.Cu'))", VAL( 0.0 ), false, &trackA, &trackB );
  68. return 0;
  69. testEvalExpr( "A.Width > B.Width", VAL( 0.0 ), false, &trackA, &trackB );
  70. testEvalExpr( "A.Width + B.Width", VAL( Mils2iu(10) + Mils2iu(20) ), false, &trackA, &trackB );
  71. testEvalExpr( "A.Netclass", VAL( (const char*) trackA.GetNetClassName().c_str() ), false, &trackA, &trackB );
  72. testEvalExpr( "(A.Netclass == 'HV') && (B.netclass == 'otherClass') && (B.netclass != 'F.Cu')", VAL( 1.0 ), false, &trackA, &trackB );
  73. testEvalExpr( "A.Netclass + 1.0", VAL( 1.0 ), false, &trackA, &trackB );
  74. testEvalExpr( "A.type == 'Track' && B.type == 'Track' && A.layer == 'F.Cu'", VAL( 0.0 ), false, &trackA, &trackB );
  75. testEvalExpr( "(A.type == 'Track') && (B.type == 'Track') && (A.layer == 'F.Cu')", VAL( 0.0 ), false, &trackA, &trackB );
  76. return 0;
  77. }