6e6ccee76c330a5fa8551917ccb5f63b6f8a0f84
[dbsrgits/DBM-Deep.git] / t / 03_bighash.t
1 ##
2 # DBM::Deep Test
3 ##
4 use strict;
5 use Test::More;
6
7 plan skip_all => "You must set \$ENV{LONG_TESTS} to run the long tests"
8     unless $ENV{LONG_TESTS};
9
10 use Test::Deep;
11 use t::common qw( new_fh );
12
13 plan tests => 9;
14
15 my $locked = 0;
16
17 use_ok( 'DBM::Deep' );
18
19 diag "This test can take up to a minute to run. Please be patient.";
20
21 my ($fh, $filename) = new_fh();
22 my $db = DBM::Deep->new(
23         file => $filename,
24         type => DBM::Deep->TYPE_HASH,
25 );
26
27 $db->lock_exclusive if $locked;
28
29 $db->{foo} = {};
30 my $foo = $db->{foo};
31
32 ##
33 # put/get many keys
34 ##
35 my $max_keys = 4000;
36
37 warn localtime(time) . ": before put\n";
38 for ( 0 .. $max_keys ) {
39     $foo->put( "hello $_" => "there " . $_ * 2 );
40 }
41 warn localtime(time) . ": after put\n";
42
43 my $count = -1;
44 for ( 0 .. $max_keys ) {
45     $count = $_;
46     unless ( $foo->get( "hello $_" ) eq "there " . $_ * 2 ) {
47         last;
48     };
49 }
50 is( $count, $max_keys, "We read $count keys" );
51 warn localtime(time) . ": after read\n";
52
53 my @keys = sort keys %$foo;
54 warn localtime(time) . ": after keys\n";
55 cmp_ok( scalar(@keys), '==', $max_keys + 1, "Number of keys is correct" );
56 my @control =  sort map { "hello $_" } 0 .. $max_keys;
57 cmp_deeply( \@keys, \@control, "Correct keys are there" );
58
59 warn localtime(time) . ": before exists\n";
60 ok( !exists $foo->{does_not_exist}, "EXISTS works on large hashes for non-existent keys" );
61 is( $foo->{does_not_exist}, undef, "autovivification works on large hashes" );
62 ok( exists $foo->{does_not_exist}, "EXISTS works on large hashes for newly-existent keys" );
63 cmp_ok( scalar(keys %$foo), '==', $max_keys + 2, "Number of keys after autovivify is correct" );
64
65 warn localtime(time) . ": before clear\n";
66 $db->clear;
67 warn localtime(time) . ": after clear\n";
68 cmp_ok( scalar(keys %$db), '==', 0, "Number of keys after clear() is correct" );
69
70 $db->unlock if $locked;