File: 13.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 (93 lines) | stat: -rw-r--r-- 2,580 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
87
88
89
90
91
92
93
use strict;
use warnings;

use Test::More;
use Data::Dumper;
$Data::Dumper::Deparse = 1;

BEGIN {
  note 'Compression testing';
  use_ok('Cache::FastMmap');
}

my %compressors = (
  lz4    => 'Compress::LZ4',
  snappy => 'Compress::Snappy',
  zlib   => 'Compress::Zlib',
);

for my $compressor ( keys %compressors ) {
  note "  Testing with $compressors{$compressor}";
  # Avoid prototype mismatch warnings
  if ( ! eval "require $compressors{$compressor};" ) {
    note "  Cannot load $compressors{$compressor}: skipping tests: reason: $@";
    next;
  }

  my $FC = Cache::FastMmap->new(
    page_size  => 8192,
    num_pages  => 1,
    init_file  => 1,
    serializer => '',
    compressor => $compressor
  );
  ok( defined $FC,          'create compressing cache' );

  my $FCNC = Cache::FastMmap->new(
    page_size  => 8192,
    num_pages  => 1,
    init_file  => 1,
    serializer => '',
  );
  ok( defined $FCNC,        'create non-compressing cache of same size' );

  my $K1 = rand_str(10);
  my $K2 = rand_str(10);
  my $V = rand_str(10) x 1000;

  ok( $FC->set($K1, $V),    'set() with large value in compressing cache' );
  ok( $FC->set($K2, $V),    'also set() same value with different key' );
  ok( !$FCNC->set($K1, $V), 'cannot set() same value in non-compressing cache' );
  ok( !$FCNC->set($K2, $V), 'also fail to set() with different key' );

  my $CV1 = $FC->get($K1);
  my $CV2 = $FC->get($K2);

  is( $CV1, $V,             'get() same large value from compressing cache' );
  is( $CV2, $V,             'also get() same value with second key used' );

  $CV1 = $FCNC->get($K1);
  $CV2 = $FCNC->get($K2);

  ok( !defined $CV1,        'cannot get() anything from non-compressing cache' );
  ok( !defined $CV2,        'also fail to get() with second key used' );
}

note '  Check support for deprecated `compress` param';
for ( 1, 'Compress::NonExistent', 'Compress::LZ4' ) {
  my $DCNC = Cache::FastMmap->new(
    page_size  => 8192,
    num_pages  => 1,
    init_file  => 1,
    serializer => '',
    compress   => $_,
  );

  ok( defined $DCNC,        'create cache with `compress` param: ' . $_ );

  my $wanted1 = quotemeta('&$uncompress(my $Tmp = shift())');
  # rt115043: older versions of Data::Dumper would output like below.
  my $wanted2 = quotemeta('&$uncompress(my $Tmp = shift @_)');
  my $wanted = qr/$wanted1|$wanted2/;
  my $got = Dumper $DCNC->{uncompress};
  like( $got, $wanted,      'using `Compress::Zlib` as compressor' );
}


done_testing;

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

__END__