/ unit_tests / interp / test_interp_block.cc
test_interp_block.cc
  1  #include "catch.hpp"
  2  
  3  #include <interp_testing_util.hh> // For core interp stuff and extra REQUIRE macros/ setup
  4  #include <rs274ngc_interp.hh>
  5  #include <interp_inspection.hh>
  6  #include <interp_return.hh>
  7  #include <saicanon.hh>
  8  
  9  #include <interp_parameter_def.hh>
 10  using namespace interp_param_global;
 11  
 12  /** Reads and parses a G-code command (and checks for errors along the way). */
 13  static block_struct read_and_parse_command(Interp &interp, const char *command)
 14  {
 15    char rawline[1024]="";
 16    char blocktext[1024]="";
 17    int line_length = 0;
 18    REQUIRE_INTERP_OK(interp.read_text(
 19                        command,
 20                        nullptr,
 21                        rawline,
 22                        blocktext,
 23                        &line_length));
 24  
 25    // Ensure it actually does something
 26    if (command && command[0]) {
 27      REQUIRE(line_length);
 28    }
 29  
 30    block_struct eblock{};
 31    REQUIRE_INTERP_OK(interp.init_block(&eblock));
 32    REQUIRE_INTERP_OK(interp.read_items(&eblock, blocktext, interp._setup.parameters));
 33    return eblock;
 34  }
 35  
 36  SCENARIO("Movement commands")
 37  {
 38    GIVEN("Linear Feed along multiple axes ")
 39    {
 40      DECL_INIT_TEST_INTERP();
 41  
 42      THEN("Block should have G1 word and axis words") {
 43        const char *command = "G1 X1 Y2 Z3 A4 C6";
 44  
 45        block_struct eblock = read_and_parse_command(test_interp, command);
 46        REQUIRE(eblock.g_modes[GM_MOTION] == G_1);
 47        REQUIRE(eblock.x_flag);
 48        REQUIRE(eblock.y_flag);
 49        REQUIRE(eblock.z_flag);
 50        REQUIRE(eblock.a_flag);
 51        REQUIRE(!eblock.b_flag);
 52        REQUIRE(eblock.c_flag);
 53  
 54        REQUIRE_FUZZ(eblock.x_number, 1);
 55        REQUIRE_FUZZ(eblock.y_number, 2);
 56        REQUIRE_FUZZ(eblock.z_number, 3);
 57        REQUIRE_FUZZ(eblock.a_number, 4);
 58        REQUIRE_FUZZ(eblock.c_number, 6);
 59      }
 60    }
 61    GIVEN("Rapid along multiple axes ")
 62    {
 63      DECL_INIT_TEST_INTERP();
 64  
 65      THEN("Block should have G0 word and axis words") {
 66        const char *command = "G0 X1 Y2 Z3 A4 C6";
 67  
 68        block_struct eblock = read_and_parse_command(test_interp, command);
 69        REQUIRE(eblock.g_modes[GM_MOTION] == G_0);
 70        REQUIRE(eblock.x_flag);
 71        REQUIRE(eblock.y_flag);
 72        REQUIRE(eblock.z_flag);
 73        REQUIRE(eblock.a_flag);
 74        REQUIRE(!eblock.b_flag);
 75        REQUIRE(eblock.c_flag);
 76  
 77        REQUIRE_FUZZ(eblock.x_number, 1);
 78        REQUIRE_FUZZ(eblock.y_number, 2);
 79        REQUIRE_FUZZ(eblock.z_number, 3);
 80        REQUIRE_FUZZ(eblock.a_number, 4);
 81        REQUIRE_FUZZ(eblock.c_number, 6);
 82      }
 83    }
 84    GIVEN("Clockwise circular motion commanded")
 85    {
 86      DECL_INIT_TEST_INTERP();
 87      const char *command = "G2 X1 Y2 Z3 R1";
 88      block_struct eblock = read_and_parse_command(test_interp, command);
 89  
 90      THEN("Block should have G2 / G3word and axis words") {
 91  
 92        REQUIRE(eblock.g_modes[GM_MOTION] == G_2);
 93        REQUIRE(eblock.x_flag);
 94        REQUIRE(eblock.y_flag);
 95        REQUIRE(eblock.z_flag);
 96        REQUIRE(eblock.r_flag);
 97        REQUIRE(!eblock.a_flag);
 98        REQUIRE(!eblock.b_flag);
 99        REQUIRE(!eblock.c_flag);
100  
101        REQUIRE_FUZZ(eblock.x_number, 1);
102        REQUIRE_FUZZ(eblock.y_number, 2);
103        REQUIRE_FUZZ(eblock.z_number, 3);
104        REQUIRE_FUZZ(eblock.r_number, 1);
105      }
106    }
107  }