File: 4.t

package info (click to toggle)
libcache-fastmmap-perl 1.58-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 356 kB
  • sloc: ansic: 1,399; perl: 628; makefile: 7
file content (86 lines) | stat: -rw-r--r-- 2,111 bytes parent folder | download | duplicates (4)
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

#########################

use Test::More tests => 9;
BEGIN { use_ok('Cache::FastMmap') };
use strict;

#########################

# Insert your test code below, the Test::More module is use()ed here so read
# its man page ( perldoc Test::More ) for help writing this test script.

# Test a backing store just made of a local hash
my %BackingStore = (
  foo => '123abc',
  bar => '456def'
);

my %WrittenItems = %BackingStore;

my $FC = Cache::FastMmap->new(
  serializer => '',
  init_file => 1,
  num_pages => 3,
  page_size => 32768,
  context => \%BackingStore,
  read_cb => sub { return $_[0]->{$_[1]}; },
  write_cb => sub { $_[0]->{$_[1]} = $_[2]; },
  delete_cb => sub { delete $_[0]->{$_[1]} },
  write_action => 'write_back'
);

ok( defined $FC );

srand(6543);

# Put 100 items in the cache (should be big enough)
for (1 .. 100) {
  my ($Key, $Val) = (RandStr(10), RandStr(100));
  $FC->set($Key, $Val);
  $WrittenItems{$Key} = $Val;
}

# Should only be 2 items in the backing store
ok( scalar(keys %BackingStore) == 2, "items match 1");

# Should flush back all the items to backing store
$FC->empty();

# Get values in cache should be empty
my %CacheItems = map { $_->{key} => $_->{value} } $FC->get_keys(2);
ok( scalar(keys %CacheItems) == 0, "empty cache");

# Backing store should be equal to all items we wrote
ok( eq_hash(\%WrittenItems, \%BackingStore), "items match 1");

# Should be able to read all items
my $Failed = 0;
for (keys %WrittenItems) {
  $Failed++ if $FC->get($_) ne $WrittenItems{$_};
}

ok( $Failed == 0, "got all written items 1" );

# Empty backing store
%BackingStore = ();

# Should still be able to read all items
$Failed = 0;
for (keys %WrittenItems) {
  $Failed++ if $FC->get($_) ne $WrittenItems{$_};
}

ok( $Failed == 0, "got all written items 2" );

# Now there should be nothing left
$FC->clear();

%CacheItems = map { $_->{key} => $_->{value} } $FC->get_keys(2);
ok( scalar(keys %CacheItems) == 0, "empty cache 2");
ok( scalar(keys %BackingStore) == 0, "empty backing store 1");

sub RandStr {
  return join '', map { chr(ord('a') + rand(26)) } (1 .. $_[0]);
}