File: each_loop.t

package info (click to toggle)
libtie-cache-lru-perl 20150301-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 528 kB
  • sloc: perl: 563; makefile: 2
file content (36 lines) | stat: -rw-r--r-- 826 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/perl -w

use strict;
use Test::More 'no_plan';

use Tie::Cache::LRU::Array;
use Tie::Cache::LRU::LinkedList;

my %Test_Hash = qw( a aa b bb );

for my $class (qw(Tie::Cache::LRU::Array Tie::Cache::LRU::LinkedList)) {
    my %cache;
    my $tied = tie %cache, $class, 5;

    foreach my $k (sort { $a cmp $b } keys(%Test_Hash)) {
        $cache{$k} = $Test_Hash{$k};
    }

    my @each;
    while (my($k, $v) = each(%cache)) {
        push @each, $k, $v;
    }

    note("Testing $class");
    is_deeply \@each, [b => 'bb', a => 'aa'], 'each() comes out in LRU order';


    # perldoc says this should be safe.
    my %seen;
    while(my($k, $v) = each(%cache)) {
        $seen{$k} = $v;
        delete $cache{$k};
    }
    is_deeply \%seen, { a => 'aa', b => 'bb' }, 'each() + delete()';
    is keys %cache, 0;
}