Merged with master and am ready to merge back
[dbsrgits/DBM-Deep.git] / t / 03_bighash.t
index b7d46f7..1735a97 100644 (file)
@@ -1,36 +1,55 @@
-##
-# DBM::Deep Test
-##
 use strict;
-use Test::More tests => 4;
-use t::common qw( new_fh );
+use warnings FATAL => 'all';
+
+use Test::More;
+
+plan skip_all => "You must set \$ENV{LONG_TESTS} to run the long tests"
+    unless $ENV{LONG_TESTS};
+
+use Test::Deep;
+use t::common qw( new_dbm );
 
 use_ok( 'DBM::Deep' );
 
-my ($fh, $filename) = new_fh();
-my $db = DBM::Deep->new(
-       file => $filename,
-       type => DBM::Deep->TYPE_HASH
-);
+diag "This test can take up to several minutes to run. Please be patient.";
 
-##
-# put/get many keys
-##
-my $max_keys = 4000;
+my $dbm_factory = new_dbm( type => DBM::Deep->TYPE_HASH );
+while ( my $dbm_maker = $dbm_factory->() ) {
+    my $db = $dbm_maker->();
 
-for ( 0 .. $max_keys ) {
-    $db->put( "hello $_" => "there " . $_ * 2 );
-}
+    $db->{foo} = {};
+    my $foo = $db->{foo};
+
+    ##
+    # put/get many keys
+    ##
+    my $max_keys = 4000;
+
+    for ( 0 .. $max_keys ) {
+        $foo->put( "hello $_" => "there " . $_ * 2 );
+    }
+
+    my $count = -1;
+    for ( 0 .. $max_keys ) {
+        $count = $_;
+        unless ( $foo->get( "hello $_" ) eq "there " . $_ * 2 ) {
+            last;
+        };
+    }
+    is( $count, $max_keys, "We read $count keys" );
+
+    my @keys = sort keys %$foo;
+    cmp_ok( scalar(@keys), '==', $max_keys + 1, "Number of keys is correct" );
+    my @control =  sort map { "hello $_" } 0 .. $max_keys;
+    cmp_deeply( \@keys, \@control, "Correct keys are there" );
+
+    ok( !exists $foo->{does_not_exist}, "EXISTS works on large hashes for non-existent keys" );
+    is( $foo->{does_not_exist}, undef, "autovivification works on large hashes" );
+    ok( exists $foo->{does_not_exist}, "EXISTS works on large hashes for newly-existent keys" );
+    cmp_ok( scalar(keys %$foo), '==', $max_keys + 2, "Number of keys after autovivify is correct" );
 
-my $count = -1;
-for ( 0 .. $max_keys ) {
-    $count = $_;
-    unless ( $db->get( "hello $_" ) eq "there " . $_ * 2 ) {
-        last;
-    };
+    $db->clear;
+    cmp_ok( scalar(keys %$db), '==', 0, "Number of keys after clear() is correct" );
 }
-is( $count, $max_keys, "We read $count keys" );
 
-cmp_ok( scalar(keys %$db), '==', $max_keys + 1, "Number of keys is correct" );
-$db->clear;
-cmp_ok( scalar(keys %$db), '==', 0, "Number of keys after clear() is correct" );
+done_testing;