File: lru.t

package info (click to toggle)
libcache-ref-perl 0.04-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 260 kB
  • sloc: perl: 2,010; makefile: 2
file content (124 lines) | stat: -rw-r--r-- 3,433 bytes parent folder | download | duplicates (2)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;

use ok 'Cache::Ref::LRU';

foreach my $lru ( map { "Cache::Ref::Util::LRU::$_" } qw(Array List) ) {
    use_ok($lru);

    {
        my $c = Cache::Ref::LRU->new( size => 3, lru_class => $lru );

        isa_ok( $c, "Cache::Ref" );

        $c->set( foo => "blah" );
        is( $c->get("foo"), "blah", "foo in cache" );

        $c->set( bar => "lala" );
        is( $c->get("foo"), "blah", "foo still in cache" );
        is( $c->get("bar"), "lala", "bar in cache" );

        $c->set( baz => "blob" );
        is( $c->get("foo"), "blah", "foo still in cache" );
        is( $c->get("bar"), "lala", "bar still in cache" );
        is( $c->get("baz"), "blob", "baz in cache" );

        {
            my $ran = 0;
            $c->compute( baz => sub { $ran++ } );
            ok( !$ran, "did not compute" );
        }

        {
            my $ran = 0;
            $c->compute( zot => sub { $ran++; "quxx" } );
            ok( $ran, "did compute" );
        }

        is( $c->get("foo"), undef, "foo no longer in cache" );
        is( $c->get("bar"), "lala", "bar still in cache" );
        is( $c->get("baz"), "blob", "baz still in cache" );
        is( $c->get("zot"), "quxx", "zot in cache" );

        $c->hit("bar");
        is( $c->_lru->mru, "bar", "mru" );
        is( $c->_lru->lru, "baz", "lru" );

        $c->set( oi => "vey" );
        is( $c->get("foo"), undef, "foo no longer in cache" );
        is( $c->get("bar"), "lala", "bar still in cache" );
        is( $c->get("baz"), undef, "baz no longer in cache" );
        is( $c->get("zot"), "quxx", "zot still in cache" );
        is( $c->get("oi"), "vey", "oi in cache" );

        $c->set( foo => "brrr" );
        $c->set( foo => "bar" );
        $c->set( bar => "baz" );

        is( $c->get("foo"), "bar", "foo in cache" );
        is( $c->get("bar"), "baz", "bar still in cache, new value" );
        is( $c->get("baz"), undef, "baz no longer in cache" );
        is( $c->get("zot"), undef, "zot no longer in cache" );
        is( $c->get("oi"), "vey", "oi still in cache" );

        is_deeply( [ $c->get(qw(foo bar nothere)) ], [ qw(bar baz), undef ], "mget" );

        $c->remove("oi");

        is( $c->get("oi"), undef, "oi removed from cache" );

        is( $c->_index_size, 2, "two elements in cache" );

        $c->expire(1);

        is( $c->_index_size, 1, "expired one entry" );

        $c->clear;

        is( $c->_index_size, 0, "cache is empty" );
    }

    {
        my $c = Cache::Ref::LRU->new( size => 5, lru_class => $lru );

        my ( $hit, $miss ) = ( 0, 0 );

        for ( 1 .. 2000 ) {
            my $key = 1 + int rand 8;

            if ( $c->get($key) ) {
                $hit++;
            } else {
                $miss++;
                $c->set($key => $key);
            }
        }

        cmp_ok( $hit, '>=', $miss, "more cache hits than misses during random access of small sigma ($hit >= $miss)" );

        ( $hit, $miss ) = ( 0, 0 );

        for ( 1 .. 100 ) {
            foreach my $key ( 1 .. 10 ) {
                if ( $c->get($key) ) {
                    $hit++;
                } else {
                    $miss++;
                    $c->set($key => $key);
                }
            }
        }

        cmp_ok( $hit, '<=', $c->size * 3, "no significant hits during linear scans ($hit)" );
    }

}

done_testing;

# ex: set sw=4 et: