Return objects by reference instead of always cloning
This commit is contained in:
parent
1741301973
commit
c0789506e4
30 changed files with 158 additions and 54 deletions
|
@ -28,20 +28,19 @@ is ref($expolygon->pp), 'ARRAY', 'expolygon pp is unblessed';
|
|||
is_deeply $expolygon->pp, [$square, $hole_in_square], 'expolygon roundtrip';
|
||||
|
||||
is ref($expolygon->arrayref), 'ARRAY', 'expolygon arrayref is unblessed';
|
||||
isa_ok $expolygon->[0], 'Slic3r::Polygon', 'expolygon polygon is blessed';
|
||||
isa_ok $expolygon->contour, 'Slic3r::Polygon', 'expolygon contour is blessed';
|
||||
isa_ok $expolygon->holes->[0], 'Slic3r::Polygon', 'expolygon hole is blessed';
|
||||
isa_ok $expolygon->[0][0], 'Slic3r::Point', 'expolygon point is blessed';
|
||||
isa_ok $expolygon->[0], 'Slic3r::Polygon::Ref', 'expolygon polygon is blessed';
|
||||
isa_ok $expolygon->contour, 'Slic3r::Polygon::Ref', 'expolygon contour is blessed';
|
||||
isa_ok $expolygon->holes->[0], 'Slic3r::Polygon::Ref', 'expolygon hole is blessed';
|
||||
isa_ok $expolygon->[0][0], 'Slic3r::Point::Ref', 'expolygon point is blessed';
|
||||
|
||||
{
|
||||
my $polygon = $expolygon->[0];
|
||||
my $expolygon2 = $expolygon->clone;
|
||||
my $polygon = $expolygon2->[0];
|
||||
$polygon->scale(2);
|
||||
isnt $expolygon->[0][0][0], $polygon->[0][0], 'a copy of polygons is returned';
|
||||
is $expolygon2->[0][0][0], $polygon->[0][0], 'polygons are returned by reference';
|
||||
}
|
||||
|
||||
is_deeply $expolygon->clone->pp, [$square, $hole_in_square], 'clone';
|
||||
# The following tests implicitely check that modifying clones
|
||||
# does not modify the original one.
|
||||
|
||||
is $expolygon->area, 100*100-20*20, 'area';
|
||||
|
||||
|
@ -100,7 +99,7 @@ is $expolygon->area, 100*100-20*20, 'area';
|
|||
|
||||
my $exp = $collection->[0];
|
||||
$exp->scale(3);
|
||||
isnt $collection->[0][0][0][0], $exp->[0][0][0], 'collection items are not returned by reference';
|
||||
is $collection->[0][0][0][0], $exp->[0][0][0], 'collection items are returned by reference';
|
||||
|
||||
is_deeply $collection->[0]->clone->pp, $collection->[0]->pp, 'clone collection item';
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ use strict;
|
|||
use warnings;
|
||||
|
||||
use Slic3r::XS;
|
||||
use Test::More tests => 13;
|
||||
use Test::More tests => 14;
|
||||
|
||||
my $square = [ # ccw
|
||||
[100, 100],
|
||||
|
@ -18,7 +18,7 @@ ok $polygon->is_valid, 'is_valid';
|
|||
is_deeply $polygon->pp, $square, 'polygon roundtrip';
|
||||
|
||||
is ref($polygon->arrayref), 'ARRAY', 'polygon arrayref is unblessed';
|
||||
isa_ok $polygon->[0], 'Slic3r::Point', 'polygon point is blessed';
|
||||
isa_ok $polygon->[0], 'Slic3r::Point::Ref', 'polygon point is blessed';
|
||||
|
||||
my $lines = $polygon->lines;
|
||||
is_deeply [ map $_->pp, @$lines ], [
|
||||
|
@ -44,4 +44,16 @@ ok $polygon->is_counter_clockwise, 'is_counter_clockwise';
|
|||
ok $clone->is_counter_clockwise, 'make_counter_clockwise';
|
||||
}
|
||||
|
||||
isa_ok $polygon->first_point, 'Slic3r::Point::Ref', 'first_point';
|
||||
|
||||
# this is not a test: this just demonstrates bad usage, where $polygon->clone gets
|
||||
# DESTROY'ed before the derived object ($point), causing bad memory access
|
||||
if (0) {
|
||||
my $point;
|
||||
{
|
||||
$point = $polygon->clone->[0];
|
||||
}
|
||||
$point->scale(2);
|
||||
}
|
||||
|
||||
__END__
|
||||
|
|
|
@ -16,7 +16,7 @@ my $polyline = Slic3r::Polyline->new(@$points);
|
|||
is_deeply $polyline->pp, $points, 'polyline roundtrip';
|
||||
|
||||
is ref($polyline->arrayref), 'ARRAY', 'polyline arrayref is unblessed';
|
||||
isa_ok $polyline->[0], 'Slic3r::Point', 'polyline point is blessed';
|
||||
isa_ok $polyline->[0], 'Slic3r::Point::Ref', 'polyline point is blessed';
|
||||
|
||||
my $lines = $polyline->lines;
|
||||
is_deeply [ map $_->pp, @$lines ], [
|
||||
|
|
|
@ -15,7 +15,7 @@ my $line = Slic3r::Line->new(@$points);
|
|||
is_deeply $line->pp, $points, 'line roundtrip';
|
||||
|
||||
is ref($line->arrayref), 'ARRAY', 'line arrayref is unblessed';
|
||||
isa_ok $line->[0], 'Slic3r::Point', 'line point is blessed';
|
||||
isa_ok $line->[0], 'Slic3r::Point::Ref', 'line point is blessed';
|
||||
|
||||
{
|
||||
my $clone = $line->clone;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue