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