test_gcodewriter.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include <catch2/catch.hpp>
  2. #include <memory>
  3. #include "libslic3r/GCodeWriter.hpp"
  4. using namespace Slic3r;
  5. SCENARIO("lift() is not ignored after unlift() at normal values of Z", "[GCodeWriter]") {
  6. GIVEN("A config from a file and a single extruder.") {
  7. GCodeWriter writer;
  8. GCodeConfig &config = writer.config;
  9. config.load(std::string(TEST_DATA_DIR) + "/fff_print_tests/test_gcodewriter/config_lift_unlift.ini", ForwardCompatibilitySubstitutionRule::Disable);
  10. std::vector<unsigned int> extruder_ids {0};
  11. writer.set_extruders(extruder_ids);
  12. writer.set_extruder(0);
  13. WHEN("Z is set to 203") {
  14. double trouble_Z = 203;
  15. writer.travel_to_z(trouble_Z);
  16. AND_WHEN("GcodeWriter::Lift() is called") {
  17. REQUIRE(writer.lift().size() > 0);
  18. AND_WHEN("Z is moved post-lift to the same delta as the config Z lift") {
  19. REQUIRE(writer.travel_to_z(trouble_Z + config.retract_lift.values[0]).size() == 0);
  20. AND_WHEN("GCodeWriter::Unlift() is called") {
  21. REQUIRE(writer.unlift().size() == 0); // we're the same height so no additional move happens.
  22. THEN("GCodeWriter::Lift() emits gcode.") {
  23. REQUIRE(writer.lift().size() > 0);
  24. }
  25. }
  26. }
  27. }
  28. }
  29. WHEN("Z is set to 500003") {
  30. double trouble_Z = 500003;
  31. writer.travel_to_z(trouble_Z);
  32. AND_WHEN("GcodeWriter::Lift() is called") {
  33. REQUIRE(writer.lift().size() > 0);
  34. AND_WHEN("Z is moved post-lift to the same delta as the config Z lift") {
  35. REQUIRE(writer.travel_to_z(trouble_Z + config.retract_lift.values[0]).size() == 0);
  36. AND_WHEN("GCodeWriter::Unlift() is called") {
  37. REQUIRE(writer.unlift().size() == 0); // we're the same height so no additional move happens.
  38. THEN("GCodeWriter::Lift() emits gcode.") {
  39. REQUIRE(writer.lift().size() > 0);
  40. }
  41. }
  42. }
  43. }
  44. }
  45. WHEN("Z is set to 10.3") {
  46. double trouble_Z = 10.3;
  47. writer.travel_to_z(trouble_Z);
  48. AND_WHEN("GcodeWriter::Lift() is called") {
  49. REQUIRE(writer.lift().size() > 0);
  50. AND_WHEN("Z is moved post-lift to the same delta as the config Z lift") {
  51. REQUIRE(writer.travel_to_z(trouble_Z + config.retract_lift.values[0]).size() == 0);
  52. AND_WHEN("GCodeWriter::Unlift() is called") {
  53. REQUIRE(writer.unlift().size() == 0); // we're the same height so no additional move happens.
  54. THEN("GCodeWriter::Lift() emits gcode.") {
  55. REQUIRE(writer.lift().size() > 0);
  56. }
  57. }
  58. }
  59. }
  60. }
  61. // The test above will fail for trouble_Z == 9007199254740992, where trouble_Z + 1.5 will be rounded to trouble_Z + 2.0 due to double mantisa overflow.
  62. }
  63. }
  64. SCENARIO("set_speed emits values with fixed-point output.", "[GCodeWriter]") {
  65. GIVEN("GCodeWriter instance") {
  66. GCodeWriter writer;
  67. WHEN("set_speed is called to set speed to 99999.123") {
  68. THEN("Output string is G1 F99999.123") {
  69. REQUIRE_THAT(writer.set_speed(99999.123), Catch::Equals("G1 F99999.123\n"));
  70. }
  71. }
  72. WHEN("set_speed is called to set speed to 1") {
  73. THEN("Output string is G1 F1") {
  74. REQUIRE_THAT(writer.set_speed(1.0), Catch::Equals("G1 F1\n"));
  75. }
  76. }
  77. WHEN("set_speed is called to set speed to 203.200022") {
  78. THEN("Output string is G1 F203.2") {
  79. REQUIRE_THAT(writer.set_speed(203.200022), Catch::Equals("G1 F203.2\n"));
  80. }
  81. }
  82. WHEN("set_speed is called to set speed to 203.200522") {
  83. THEN("Output string is G1 F203.201") {
  84. REQUIRE_THAT(writer.set_speed(203.200522), Catch::Equals("G1 F203.201\n"));
  85. }
  86. }
  87. }
  88. }