Algorithm-Odometer-Tiny

 view release on metacpan or  search on metacpan

t/algorithm_odometer_tiny.t  view on Meta::CPAN

#!/usr/bin/env perl
use warnings;
use strict;

=head1 Synopsis

Tests for the Perl distribution Algorithm-Odometer-Tiny.

=head1 Author, Copyright, and License

Copyright (c) 2019 Hauke Daempfling (haukex@zero-g.net).

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl 5 itself.

For more information see the L<Perl Artistic License|perlartistic>,
which should have been distributed with your copy of Perl.
Try the command C<perldoc perlartistic> or see
L<http://perldoc.perl.org/perlartistic.html>.

=cut

use FindBin ();
use lib $FindBin::Bin;
use Algorithm_Odometer_Tiny_Testlib;
use List::Util qw/reduce/;

use constant TESTCOUNT => 16; ## no critic (ProhibitConstantPragma)
use Test::More tests => TESTCOUNT;

BEGIN {
	diag "This is Perl $] at $^X on $^O";
	use_ok('Algorithm::Odometer::Tiny') or BAIL_OUT("failed to use Algorithm::Odometer::Tiny");
	use_ok('Algorithm::Odometer::Gray') or BAIL_OUT("failed to use Algorithm::Odometer::Gray");
}
is $Algorithm::Odometer::Tiny::VERSION, '0.04', 'Algorithm::Odometer::Tiny version matches tests';
is $Algorithm::Odometer::Gray::VERSION, '0.04', 'Algorithm::Odometer::Gray version matches tests';

{
	my $odo = Algorithm::Odometer::Tiny->new( ['foo','bar'], '-', [3..5] );
	my @o;
	while (<$odo>) { push @o, $_ }
	while (my @c = $odo->()) { push @o, \@c } # re-start odometer
	is_deeply \@o, ["foo-3", "foo-4", "foo-5", "bar-3", "bar-4", "bar-5",
		["foo","-","3"], ["foo","-","4"], ["foo","-","5"], ["bar","-","3"], ["bar","-","4"], ["bar","-","5"] ],
		'basic test 1/2';
}
subtest 'basic tests 2/2' => sub {  ## no critic (RequireTestLabels)
	plan tests=>28;
	my $odo1 = new_ok('Algorithm::Odometer::Tiny' => [ [qw/a b c/], [qw/1 2/] ]);
	is $odo1->(), 'a1';
	is $odo1->(), 'a2';
	is <$odo1>, 'b1';
	is_deeply [$odo1->()], ['b','2'];
	is $odo1->(), 'c1';
	is_deeply [$odo1->()], ['c','2'];
	is $odo1->(), undef;
	is $odo1->(), 'a1';
	my $odo2 = new_ok('Algorithm::Odometer::Tiny' => [ 'r', [qw/X Y/], [qw/9 0/], [qw/- _/] ]);
	is $odo2->(), 'rX9-';
	is $odo2->(), 'rX9_';
	is <$odo2>, 'rX0-';
	is <$odo2>, 'rX0_';
	is <$odo2>, 'rY9-';
	is_deeply [$odo2->()], ['r','Y','9','_'];
	is_deeply [$odo2->()], ['r','Y','0','-'];
	is $odo2->(), 'rY0_';
	is_deeply [$odo2->()], [];
	is $odo2->(), 'rX9-';
	my $odo3 = new_ok('Algorithm::Odometer::Tiny' => [ [0..9],[0..9],[0..9] ]);
	my @res;
	push @res, $_ while <$odo3>;
	is_deeply \@res, [map {sprintf '%03d', $_} 0..999];
	my $odo4 = new_ok('Algorithm::Odometer::Tiny' => [ [qw/a b/], [undef, {x=>3}] ]);
	is <$odo4>, 'a';
	like <$odo4>, qr/^aHASH\(0x[a-fA-F0-9]+\)$/;
	is_deeply [$odo4->()], ['b', undef];
	is_deeply [$odo4->()], ['b', {x=>3}];
	is <$odo4>, undef;
};

{
	my $odo = Algorithm::Odometer::Tiny->new( (['0'..'9','a'..'z']) x 4 );
	my ($cnt,@o)=0;



( run in 1.374 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )