Rewrite layers.t to c++

This commit is contained in:
Martin Šach 2023-12-15 11:42:58 +01:00 committed by SachCZ
parent 6e871a874a
commit 3e28ea3379
3 changed files with 105 additions and 80 deletions

View File

@ -1,79 +0,0 @@
use Test::More tests => 5;
use strict;
use warnings;
BEGIN {
use FindBin;
use lib "$FindBin::Bin/../lib";
use local::lib "$FindBin::Bin/../local-lib";
}
use List::Util qw(first);
use Slic3r;
use Slic3r::Test qw(_eq);
{
my $config = Slic3r::Config::new_from_defaults;
my $test = sub {
my ($conf) = @_;
$conf ||= $config;
my $print = Slic3r::Test::init_print('20mm_cube', config => $conf);
my @z = ();
my @increments = ();
Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
my ($self, $cmd, $args, $info) = @_;
if ($info->{dist_Z}) {
push @z, 1*$args->{Z};
push @increments, $info->{dist_Z};
}
});
fail 'wrong first layer height'
if $z[0] ne $config->get_value('first_layer_height') + $config->z_offset;
fail 'wrong second layer height'
if $z[1] ne $config->get_value('first_layer_height') + $config->get_value('layer_height') + $config->z_offset;
fail 'wrong layer height'
if first { !_eq($_, $config->layer_height) } @increments[1..$#increments];
1;
};
$config->set('start_gcode', ''); # to avoid dealing with the nozzle lift in start G-code
$config->set('layer_height', 0.3);
$config->set('first_layer_height', 0.2);
ok $test->(), "absolute first layer height";
$config->set('first_layer_height', 0.6 * $config->layer_height);
ok $test->(), "relative first layer height";
$config->set('z_offset', 0.9);
ok $test->(), "positive Z offset";
$config->set('z_offset', -0.8);
ok $test->(), "negative Z offset";
}
{
my $config = Slic3r::Config->new;
$config->set('fill_density', 0); # just for making the test faster
$config->set('binary_gcode', 0);
my $print = Slic3r::Test::init_print('20mm_cube', config => $config, scale => 2);
my @z = ();
Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
my ($self, $cmd, $args, $info) = @_;
if ($info->{dist_Z}) {
push @z, 1*$args->{Z};
}
});
ok $z[-1] > 20*1.8 && $z[-1] < 20*2.2, 'resulting G-code has reasonable height';
}
__END__

View File

@ -17,13 +17,14 @@ add_executable(${_TEST_NAME}_tests
test_gcode_layer_changes.cpp
test_gcodefindreplace.cpp
test_gcodewriter.cpp
test_retraction.cpp
test_layers.cpp
test_model.cpp
test_multi.cpp
test_perimeters.cpp
test_print.cpp
test_printgcode.cpp
test_printobject.cpp
test_retraction.cpp
test_shells.cpp
test_skirt_brim.cpp
test_support_material.cpp

View File

@ -0,0 +1,103 @@
/**
* Ported from t/layers.t
*/
#include <catch2/catch.hpp>
#include "test_data.hpp"
using namespace Slic3r;
using namespace Slic3r::Test;
void check_layers(const DynamicPrintConfig& config) {
GCodeReader parser;
std::string gcode = Slic3r::Test::slice({TestMesh::cube_20x20x20}, config);
std::vector<double> z;
std::vector<double> increments;
parser.parse_buffer(gcode, [&] (Slic3r::GCodeReader &self, const Slic3r::GCodeReader::GCodeLine &line) {
if (line.has_z()) {
z.emplace_back(line.z());
increments.emplace_back(line.dist_Z(self));
}
});
const double first_layer_height = config.opt_float("first_layer_height");
const double z_offset = config.opt_float("z_offset");
const double layer_height = config.opt_float("layer_height");
INFO("Correct first layer height.");
CHECK(z.at(0) == Approx(first_layer_height + z_offset));
INFO("Correct second layer height")
CHECK(z.at(1) == Approx(first_layer_height + layer_height + z_offset));
INFO("Correct layer height")
for (const double increment : tcb::span{increments}.subspan(1)) {
CHECK(increment == Approx(layer_height));
}
}
TEST_CASE("Layer heights are correct", "[Layers]") {
DynamicPrintConfig config = Slic3r::DynamicPrintConfig::full_print_config();
config.set_deserialize_strict({
{ "start_gcode", "" },
{ "layer_height", 0.3 },
{ "first_layer_height", 0.2 },
});
SECTION("Absolute first layer height") {
check_layers(config);
}
SECTION("Relative layer height") {
const double layer_height = config.opt_float("layer_height");
config.set_deserialize_strict({
{ "first_layer_height", 0.6 * layer_height },
});
check_layers(config);
}
SECTION("Positive z offset") {
config.set_deserialize_strict({
{ "z_offset", 0.9 },
});
check_layers(config);
}
SECTION("Negative z offset") {
config.set_deserialize_strict({
{ "z_offset", -0.8 },
});
check_layers(config);
}
}
TEST_CASE("GCode has reasonable height", "[Layers]") {
DynamicPrintConfig config = Slic3r::DynamicPrintConfig::full_print_config();
config.set_deserialize_strict({
{ "fill_density", 0 },
{ "gcode_binary", 0 },
});
Print print;
Model model;
TriangleMesh test_mesh{mesh(TestMesh::cube_20x20x20)};
test_mesh.scale(2);
Test::init_print({test_mesh}, print, model, config);
const std::string gcode{Test::gcode(print)};
std::vector<double> z;
GCodeReader parser;
parser.parse_buffer(gcode, [&] (Slic3r::GCodeReader &self, const Slic3r::GCodeReader::GCodeLine &line) {
if (line.dist_Z(self) != Approx(0)) {
z.emplace_back(line.z());
}
});
REQUIRE(!z.empty());
INFO("Last Z is: " + std::to_string(z.back()));
CHECK((z.back() > 20*1.8 && z.back() < 20*2.2));
}