File: random.t

package info (click to toggle)
libcache-ref-perl 0.04-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 260 kB
  • sloc: perl: 2,010; makefile: 2
file content (67 lines) | stat: -rw-r--r-- 1,206 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;

use ok 'Cache::Ref::Random';

my $c = Cache::Ref::Random->new( size => 5 );

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 .. 8 ) {
        if ( $c->get($key) ) {
            $hit++;
        } else {
            $miss++;
            $c->set($key => $key);
        }
    }
}

cmp_ok( $hit, '>=', $miss / 3, "hit rate in linear scans($hit >= $miss / 3)" );

{
    # Tests shouldn't use a private method, but right now I can't think
    # of other way to test that the expire method is actually removing
    # the desired number of elements

    my $c = Cache::Ref::Random->new( size => 100 );

    for (1..10) {

        $c->set( $_ => $_ ) for (1..100);

        is $c->_index_size, 100;

        $c->expire(50);

        is $c->_index_size, 50;

        $c->clear;
    }

}

done_testing;

# ex: set sw=4 et: