From 63059c656748615512ec27b9185d2b3cf29bddff Mon Sep 17 00:00:00 2001
From: supermerill <merill@fr.fr>
Date: Tue, 22 Oct 2019 19:09:31 +0200
Subject: [PATCH] some more tests

---
 src/test/libslic3r/test_extrusion_entity.cpp | 97 +++++++++++++++++++-
 src/test/libslic3r/test_flow.cpp             |  8 ++
 src/test/libslic3r/test_print.cpp            | 14 ++-
 3 files changed, 114 insertions(+), 5 deletions(-)

diff --git a/src/test/libslic3r/test_extrusion_entity.cpp b/src/test/libslic3r/test_extrusion_entity.cpp
index 6d07af0ec1..7beae95c40 100644
--- a/src/test/libslic3r/test_extrusion_entity.cpp
+++ b/src/test/libslic3r/test_extrusion_entity.cpp
@@ -1,6 +1,9 @@
 #include <catch.hpp>
+#include "test_data.hpp" // get access to init_print, etc
+#include "Config.hpp"
 #include "ExtrusionEntityCollection.hpp"
 #include "ExtrusionEntity.hpp"
+#include "GCodeReader.hpp"
 #include "Point.hpp"
 #include <cstdlib>
 
@@ -27,12 +30,13 @@ ExtrusionPaths random_paths(size_t count=10, size_t length=20, float LO=-50, flo
 }
 
 using namespace Slic3r;
+using namespace Slic3r::Test;
 
 SCENARIO("ExtrusionEntityCollection: Polygon flattening") {
     srand(0xDEADBEEF); // consistent seed for test reproducibility.
 
     // Generate one specific random path set and save it for later comparison
-    ExtrusionPaths nosort_path_set {random_paths()};
+    ExtrusionPaths nosort_path_set{ random_paths() };
 
     ExtrusionEntityCollection sub_nosort;
     sub_nosort.append(nosort_path_set);
@@ -53,13 +57,13 @@ SCENARIO("ExtrusionEntityCollection: Polygon flattening") {
         WHEN("The EEC is flattened with default options (preserve_order=false)") {
             sample.flatten(&output);
             THEN("The output EEC contains no Extrusion Entity Collections") {
-                CHECK(std::count_if(output.entities.cbegin(), output.entities.cend(), [=](const ExtrusionEntity* e) {return e->is_collection();}) == 0);
+                CHECK(std::count_if(output.entities.cbegin(), output.entities.cend(), [=](const ExtrusionEntity* e) {return e->is_collection(); }) == 0);
             }
         }
         WHEN("The EEC is flattened with preservation (preserve_order=true)") {
             sample.flatten(&output, true);
             THEN("The output EECs contains one EEC.") {
-                CHECK(std::count_if(output.entities.cbegin(), output.entities.cend(), [=](const ExtrusionEntity* e) {return e->is_collection();}) == 1);
+                CHECK(std::count_if(output.entities.cbegin(), output.entities.cend(), [=](const ExtrusionEntity* e) {return e->is_collection(); }) == 1);
             }
             AND_THEN("The ordered EEC contains the same order of elements than the original") {
                 // find the entity in the collection
@@ -77,3 +81,90 @@ SCENARIO("ExtrusionEntityCollection: Polygon flattening") {
         }
     }
 }
+
+SCENARIO("ExtrusionEntityCollection: no sort") {
+    auto config{ Config::new_from_defaults() };
+    config->set("gcode_comments", true);
+    config->set("skirts", "0");
+    
+    Slic3r::Model model;
+    shared_Print print{ Slic3r::Test::init_print({ TestMesh::cube_20x20x20 }, model, config) };
+
+    std::map<double, bool> layers_with_skirt;
+    std::map<double, bool> layers_with_brim;
+
+    print->process();
+    //replace extrusion from sliceing by manual ones
+    print->objects[0]->clear_layers();
+    Layer* customL_layer = print->objects[0]->add_layer(0, 0.2, 0.2, 0.1);
+    LayerRegion* custom_region = customL_layer->add_region(print->regions[0]);
+
+    ExtrusionPath path_peri(ExtrusionRole::erPerimeter);
+    path_peri.polyline.append(Point{ 0,0 });
+    path_peri.polyline.append(Point{ scale_(1),scale_(0) });
+    ExtrusionPath path_fill1(ExtrusionRole::erInternalInfill);
+    path_fill1.polyline.append(Point{ scale_(1),scale_(0) });
+    path_fill1.polyline.append(Point{ scale_(2),scale_(0) });
+    ExtrusionPath path_fill2(ExtrusionRole::erInternalInfill);
+    path_fill2.polyline.append(Point{ scale_(2),scale_(0) });
+    path_fill2.polyline.append(Point{ scale_(3),scale_(0) });
+    ExtrusionEntityCollection coll_fill;
+    coll_fill.append(path_fill2);
+    coll_fill.append(path_fill1);
+    ExtrusionEntityCollection coll_peri;
+    coll_peri.append(path_peri);
+
+
+    WHEN("sort") {
+        custom_region->fills.append(coll_fill);
+        custom_region->perimeters.append(coll_peri);
+        coll_fill.no_sort = false;
+        std::stringstream gcode;
+        Slic3r::Test::gcode(gcode, print);
+        auto parser{ Slic3r::GCodeReader() };
+        std::vector<float> extrude_x;
+        parser.parse_stream(gcode, [](Slic3r::GCodeReader& self, const Slic3r::GCodeReader::GCodeLine& line)
+        {
+            line.extruding();
+            if (self.Z == Approx(0.3) || line.new_Z() == Approx(0.3)) {
+                if (line.extruding() && self.F == Approx(12)) {
+                }
+            }
+        });
+        parser.parse_stream(gcode, [&extrude_x, &config](Slic3r::GCodeReader& self, const Slic3r::GCodeReader::GCodeLine& line)
+        {
+            line.extruding();
+            std::cout << "line.comment='" << line.comment << "'\n";
+            if (line.comment == " infill" || line.comment == " perimeter" || line.comment == " move to first infill point") {
+                extrude_x.push_back(line.new_X());
+            }
+        });
+        REQUIRE(extrude_x.size()==3);
+        REQUIRE(extrude_x[0] == 91);
+        REQUIRE(extrude_x[1] == 92);
+        REQUIRE(extrude_x[2] == 93);
+    }
+
+
+    WHEN("no sort") {
+        coll_fill.no_sort = true;
+        custom_region->fills.append(coll_fill);
+        custom_region->perimeters.append(coll_peri);
+        std::stringstream gcode;
+        Slic3r::Test::gcode(gcode, print);
+        auto parser{ Slic3r::GCodeReader() };
+        std::vector<float> extrude_x;
+        parser.parse_stream(gcode, [&extrude_x, &config](Slic3r::GCodeReader& self, const Slic3r::GCodeReader::GCodeLine& line)
+        {
+            if (line.comment == " infill" || line.comment == " perimeter" || line.comment == " move to first infill point") {
+                extrude_x.push_back(line.new_X());
+            }
+        });
+        REQUIRE(extrude_x.size() == 5);
+        REQUIRE(extrude_x[0] == 91);
+        REQUIRE(extrude_x[1] == 92);
+        REQUIRE(extrude_x[2] == 93);
+        REQUIRE(extrude_x[3] == 91);
+        REQUIRE(extrude_x[4] == 92);
+    }
+}
diff --git a/src/test/libslic3r/test_flow.cpp b/src/test/libslic3r/test_flow.cpp
index 1db8cd5fdf..b2ffe30b40 100644
--- a/src/test/libslic3r/test_flow.cpp
+++ b/src/test/libslic3r/test_flow.cpp
@@ -127,6 +127,10 @@ SCENARIO("Flow: Flow math for non-bridges", "[!mayfail]") {
                 auto flow {Flow::new_from_config_width(frPerimeter, ConfigOptionFloatOrPercent(0, false), nozzle_diameter, layer_height, 0.0f)};
                 REQUIRE(flow.width == Approx(1.4*nozzle_diameter));
             }
+            THEN("Min width is respected") {
+                auto flow{ Flow::new_from_config_width(frPerimeter, ConfigOptionFloatOrPercent{0, false}, nozzle_diameter, layer_height, 0.0f) };
+                REQUIRE(flow.width >= Approx(1.05*nozzle_diameter));
+            }
         }
         WHEN("Layer height is set to 0.2") {
             layer_height = 0.3f;
@@ -134,6 +138,10 @@ SCENARIO("Flow: Flow math for non-bridges", "[!mayfail]") {
                 auto flow {Flow::new_from_config_width(frPerimeter, ConfigOptionFloatOrPercent(0, false), nozzle_diameter, layer_height, 0.0f)};
                 REQUIRE(flow.width == Approx(1.05*nozzle_diameter));
             }
+            THEN("Min width is respected.") {
+                auto flow{ Flow::new_from_config_width(frPerimeter, ConfigOptionFloatOrPercent{0, false}, nozzle_diameter, layer_height, 0.0f) };
+                REQUIRE(flow.width >= Approx(1.05*nozzle_diameter));
+            }
         }
     }
 
diff --git a/src/test/libslic3r/test_print.cpp b/src/test/libslic3r/test_print.cpp
index abfed1a7de..54b71cd4d2 100644
--- a/src/test/libslic3r/test_print.cpp
+++ b/src/test/libslic3r/test_print.cpp
@@ -138,8 +138,18 @@ SCENARIO("Print: Brim generation") {
             config->set("first_layer_extrusion_width", 0.5);
             auto print {Slic3r::Test::init_print({m}, model, config)};
             print->make_brim();
-            THEN("Brim Extrusion collection has 12 loops in it") {
-                REQUIRE(print->brim.items_count() == 12);
+            double nbLoops = 6.0 / print->brim_flow().spacing();
+            THEN("Brim Extrusion collection has " + std::to_string(nbLoops) + " loops in it (flow="+ std::to_string(print->brim_flow().spacing())+")") {
+                REQUIRE(print->brim.items_count() == floor(nbLoops));
+            }
+        }
+        WHEN("Brim ears activated, 3mm") {
+            config->set("brim_width", 3);
+            config->set("brim_ears", true);
+            shared_Print print{ Slic3r::Test::init_print({m}, model, config) };
+            print->process();
+            THEN("Brim ears Extrusion collection has 4 extrusions in it") {
+                REQUIRE(print->brim.items_count() == 4);
             }
         }
     }