Added recursion test, hoisted staleness() to Sector.pm, and refactored to write_bucke...
[dbsrgits/DBM-Deep.git] / t / 40_freespace.t
CommitLineData
2120a181 1use strict;
0e3e3555 2use warnings FATAL => 'all';
3
4use Test::More;
2120a181 5use Test::Exception;
6use t::common qw( new_fh );
7
8use_ok( 'DBM::Deep' );
9
10{
11 my ($fh, $filename) = new_fh();
12 my $db = DBM::Deep->new({
13 file => $filename,
14 autoflush => 1,
15 });
16
17 $db->{foo} = '1234';
18 $db->{foo} = '2345';
19
20 my $size = -s $filename;
21 $db->{foo} = '3456';
22 cmp_ok( $size, '==', -s $filename, "A second overwrite doesn't change size" );
23
24 $size = -s $filename;
25 delete $db->{foo};
26 cmp_ok( $size, '==', -s $filename, "Deleted space isn't released" );
27
28 $db->{bar} = '2345';
29 cmp_ok( $size, '==', -s $filename, "Added a new key after a delete reuses space" );
30
31 $db->{baz} = {};
32 $size = -s $filename;
33
34 delete $db->{baz};
35 $db->{baz} = {};
36
37 cmp_ok( $size, '==', -s $filename, "delete and rewrite reuses space" );
38
39 $db->{baz} = {};
40 $size = -s $filename;
41
42 $db->{baz} = {};
43
44 cmp_ok( $size, '==', -s $filename, "delete and rewrite reuses space" );
45
46 my $x = { foo => 'bar' };
47 $db->{floober} = $x;
48
49 delete $db->{floober};
50
51 ok( !exists $x->{foo}, "Deleting floober makes \$x empty (exists)" );
52 is( $x->{foo}, undef, "Deleting floober makes \$x empty (read)" );
53 is( delete $x->{foo}, undef, "Deleting floober makes \$x empty (delete)" );
54
55 eval { $x->{foo} = 'bar'; };
56 like( $@, qr/Cannot write to a deleted spot in DBM::Deep/, "Exception thrown when writing" );
57
58 cmp_ok( scalar( keys %$x ), '==', 0, "Keys returns nothing after deletion" );
59}
60
61{
62 my ($fh, $filename) = new_fh();
63 my $db = DBM::Deep->new({
64 file => $filename,
65 autoflush => 1,
66 });
67
68 $db->{ $_ } = undef for 1 .. 4;
69 delete $db->{ $_ } for 1 .. 4;
70 cmp_ok( keys %{ $db }, '==', 0, "We added and removed 4 keys" );
71
72 # So far, we've written 4 keys. Let's write 13 more keys. This should -not-
73 # trigger a reindex. This requires knowing how much space is taken. Good thing
74 # we wrote this dreck ...
75 my $size = -s $filename;
45f047f8 76
e9b0b5f0 77 my $data_sector_size = $db->_engine->data_sector_size;
78 my $expected = $size + 9 * ( 2 * $data_sector_size );
2120a181 79
80 $db->{ $_ } = undef for 5 .. 17;
81
82 cmp_ok( $expected, '==', -s $filename, "No reindexing after deletion" );
83}
0e3e3555 84
85done_testing;