Ancient
view release on metacpan or search on metacpan
t/2052-doubly-loop-patterns.t view on Meta::CPAN
#!/usr/bin/perl
use strict;
use warnings;
use Test::More;
use doubly;
# Test doubly linked list with various loop variable patterns
# Note: doubly uses cursor pattern - add() returns cursor to new node
subtest 'for with $item' => sub {
my $list = doubly->new(1); # doubly requires initial data
for my $item (2, 3, 4, 5) {
$list = $list->add($item);
}
is($list->length(), 5, 'doubly add with $item');
};
subtest 'for with $val' => sub {
my $list = doubly->new('a');
my @vals = ('b', 'c');
for my $val (@vals) {
$list = $list->add($val);
}
my @result;
my $node = $list->start();
while ($node) {
push @result, $node->data();
last if $node->is_end();
$node = $node->next();
}
is_deeply(\@result, ['a', 'b', 'c'], 'doubly with $val');
};
subtest 'for with $n numeric' => sub {
my $list = doubly->new(10);
for my $n (20, 30) {
$list = $list->add($n);
}
my $sum = 0;
my $node = $list->start();
while ($node) {
$sum += $node->data();
last if $node->is_end();
$node = $node->next();
}
is($sum, 60, 'doubly sum with $n');
};
subtest 'for with $data hashref' => sub {
my @data = (
{ id => 1, name => 'first' },
{ id => 2, name => 'second' },
);
my $list = doubly->new($data[0]);
for my $data (@data[1..$#data]) {
$list = $list->add($data);
}
my $node = $list->start();
is($node->data()->{id}, 1, 'hashref id 1');
$node = $node->next();
is($node->data()->{name}, 'second', 'hashref name');
};
subtest 'while with $node' => sub {
( run in 0.949 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )