Skip to content

Commit

Permalink
completed shape tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lichtkind committed Apr 11, 2024
1 parent 98c27e5 commit b163273
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 50 deletions.
8 changes: 5 additions & 3 deletions lib/Graphics/Toolkit/Color/Space/Shape.pm
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,20 @@ sub delta { # values have to be normalized
}

sub in_range {
my ($self, $values, $range) = @_;
my ($self, $values, $range, $precision) = @_;
return 'color value vector in '.$self->basis->name.' needs '.$self->basis->count.' values'
unless $self->basis->is_array( $values );
$range = $self->_range( $range );
return "got bad range definition" unless ref $range;
$precision = $self->_precision( $precision );
return "bad precision definition, need ARRAY with ints or -1" unless ref $precision;
my @names = $self->basis->keys;
for my $i ($self->basis->iterator){
next unless $self->axis_is_numeric($i);
return $names[$i]." value is below minimum of ".$range->[$i][0] if $values->[$i] < $range->[$i][0];
return $names[$i]." value is above maximum of ".$range->[$i][1] if $values->[$i] > $range->[$i][1];
return $names[$i]." value has to be an integer" if $self->dimension_is_int($i, $range)
and int $values->[$i] != $values->[$i];
return $names[$i]." value is not properly rounded " if $precision->[$i] >= 0
and pround($values->[$i], $precision->[$i]) != $values->[$i];
}
return $values;
}
Expand Down
97 changes: 50 additions & 47 deletions t/03_space_shape.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use v5.12;
use warnings;
use Test::More tests => 107;
use Test::More tests => 116;
use Test::Warn;

BEGIN { unshift @INC, 'lib', '../lib'}
Expand Down Expand Up @@ -135,52 +135,55 @@ is( $tr->[1], 1.1, 'rounded in range value to set precision');
is( $tr->[2], 2.54, 'in range value is kept');


exit 0;
is( ref $shape->in_range([1,2,3]), 'ARRAY', 'all values in range');
is( ref $shape->in_range([1,2]), '', "not enough values");
is( ref $shape->in_range([1,2,3,4]), '', "too many values");
is( ref $shape->in_range([1,22,3]), '', "too big second value");
is( ref $shape->in_range([0,1,3.1]), '', "too many decimals in third value");


my $norm = $shape->normalize([-5, 0, 5]);
is( ref $norm, 'ARRAY', 'normalized values');
is( int @$norm, 3, 'normalized 3 into 3 values');
is( $norm->[0], 0, 'normalized first min value');
is( $norm->[1], 0.5, 'normalized second mid value');
is( $norm->[2], 1, 'normalized third max value');

$norm = $shape->denormalize([0, 0.5 , 1]);
is( @$norm, 3, 'denormalized 3 into 3 values');
is( $norm->[0], -5, 'denormalized min value');
is( $norm->[1], 0, 'denormalized second mid value');
is( $norm->[2], 5, 'denormalized third max value');

$norm = $bshape->normalize([-1, 0, 5]);
is( @$norm, 3, 'normalize bawl coordinates');
is( $norm->[0], 0.4, 'normalized first min value');
is( $norm->[1], 0.5, 'normalized second mid value');
is( $norm->[2], 1, 'normalized third max value');

$norm = $bshape->denormalize([0.4, 0.5, 1]);
is( @$norm, 3, 'denormalized 3 into 3 values');
is( $norm->[0], -1, 'denormalized small value');
is( $norm->[1], 0, 'denormalized mid value');
is( $norm->[2], 5, 'denormalized max value');

$norm = $bshape->denormalize([1, 0, 0.5], [[-10,250],[30,50], [-70,70]]);
is( @$norm, 3, 'denormalized bowl with custom range');
is( $norm->[0], 250, 'denormalized with special ranges max value');
is( $norm->[1], 30, 'denormalized with special ranges min value');
is( $norm->[2], 0, 'denormalized with special ranges mid value');

$norm = $bshape->normalize([250, 30, 0], [[-10,250],[30,50], [-70,70]]);
is( @$norm, 3, 'normalized bowl with custom range');
is( $norm->[0], 1, 'normalized with special ranges max value');
is( $norm->[1], 0, 'normalized with special ranges min value');
is( $norm->[2], 0.5,'normalized with special ranges mid value');

$norm = $shape->denormalize_delta([0, 0.5 , 1]);
is( @$norm, 3, 'denormalized 3 into 3 values');
is( $norm->[0], 0, 'denormalized min delta');
is( $norm->[1], 5, 'denormalized second mid delta');
is( $norm->[2], 10, 'denormalized third max delta');

is( $shape->values_in_range([1,2,3]), undef, 'all values in range');
warning_like {$shape->values_in_range([1,2])} {carped => qr/value vector/}, "not enough values";
warning_like {$shape->values_in_range([1,2,3,4])} {carped => qr/value vector/}, "too much values";
warning_like {$shape->values_in_range([-11,2,3])} {carped => qr/aaa value is below/}, "too small first value";
warning_like {$shape->values_in_range([0,21,3])} {carped => qr/bbb value is above/}, "too large second value";
warning_like {$shape->values_in_range([0,1,3.1])} {carped => qr/be an integer/}, "third value was not int";


my @norm = $shape->normalize([0, 10, 20]);
is( int @norm, 3, 'normalized 3 into 3 values');
is( $norm[0], 0, 'normalized first min value');
is( $norm[1], 0.5, 'normalized second mid value');
is( $norm[2], 1, 'normalized third max value');

@norm = $shape->denormalize([0, 0.5 , 1]);
is( int @norm, 3, 'denormalized 3 into 3 values');
is( $norm[0], 0, 'denormalized min value');
is( $norm[1], 10, 'denormalized second mid value');
is( $norm[2], 20, 'denormalized third max value');

@norm = $bshape->normalize([-1, 0, 5]);
is( int @norm, 3, 'normalize bawl coordinates');
is( $norm[0], 0.4, 'normalized first min value');
is( $norm[1], 0.5, 'normalized second mid value');
is( $norm[2], 1, 'normalized third max value');

@norm = $bshape->denormalize([0.4, 0.5, 1]);
is( int @norm, 3, 'denormalized 3 into 3 values');
is( $norm[0], -1, 'denormalized small value');
is( $norm[1], 0, 'denormalized mid value');
is( $norm[2], 5, 'denormalized max value');

@norm = $bshape->denormalize([1, 0, 0.5], [[-10,250],[30,50], [-70,70]]);
is( int @norm, 3, 'denormalized bowl with custom range');
is( $norm[0], 250, 'denormalized with special ranges max value');
is( $norm[1], 30, 'denormalized with special ranges min value');
is( $norm[2], 0, 'denormalized with special ranges mid value');

@norm = $bshape->normalize([250, 30, 0], [[-10,250],[30,50], [-70,70]]);
is( int @norm, 3, 'normalized bowl with custom range');
is( $norm[0], 1, 'normalized with special ranges max value');
is( $norm[1], 0, 'normalized with special ranges min value');
is( $norm[2], 0.5,'normalized with special ranges mid value');

#denormalize_delta

exit 0;

0 comments on commit b163273

Please sign in to comment.