The header now has its own sector. A lot needs to be moved over to it, but it's there.
[dbsrgits/DBM-Deep.git] / t / 04_array.t
index f652ded..3bfc933 100644 (file)
@@ -2,32 +2,30 @@
 # DBM::Deep Test
 ##
 use strict;
-use Test::More tests => 107;
+use Test::More tests => 128;
 use Test::Exception;
-use File::Temp qw( tempfile tempdir );
+use t::common qw( new_fh );
 
 use_ok( 'DBM::Deep' );
 
-my $dir = tempdir( CLEANUP => 1 );
-my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
+my ($fh, $filename) = new_fh();
 my $db = DBM::Deep->new(
-       file => $filename,
-       type => DBM::Deep->TYPE_ARRAY
+    file => $filename,
+    fh => $fh,
+    type => DBM::Deep->TYPE_ARRAY
 );
 
-TODO: {
-    local $TODO = "How is this test ever supposed to pass?";
-    ok( !$db->clear, "If the file has never been written to, clear() returns false" );
-}
-
 ##
 # basic put/get/push
 ##
 $db->[0] = "elem1";
-$db->push( "elem2" );
-$db->put(2, "elem3");
-$db->store(3, "elem4");
+#$db->push( "elem2" );
+#$db->put(2, "elem3");
+#$db->store(3, "elem4");
+warn $db->_engine->_dump_file;
 $db->unshift("elem0");
+warn $db->_engine->_dump_file;
+__END__
 
 is( $db->[0], 'elem0', "Array get for shift works" );
 is( $db->[1], 'elem1', "Array get for array set works" );
@@ -54,7 +52,10 @@ is( $db->[-2], $db->[3], "-2nd index is 3rd index" );
 is( $db->[-3], $db->[2], "-3rd index is 2nd index" );
 is( $db->[-4], $db->[1], "-4th index is 1st index" );
 is( $db->[-5], $db->[0], "-5th index is 0th index" );
-is( $db->[-6], undef, "-6th index is undef" );
+
+# This is for Perls older than 5.8.0 because of is()'s prototype
+{ my $v = $db->[-6]; is( $v, undef, "-6th index is undef" ); }
+
 is( $db->length, 5, "... and we have five elements after abortive -6 index lookup" );
 
 $db->[-1] = 'elem4.1';
@@ -65,7 +66,7 @@ is( $db->fetch(4), 'elem4.1' );
 
 throws_ok {
     $db->[-6] = 'whoops!';
-} qr/Modification of non-creatable array value attempted, subscript -6/, "Correct error thrown"; 
+} qr/Modification of non-creatable array value attempted, subscript -6/, "Correct error thrown";
 
 my $popped = $db->pop;
 is( $db->length, 4, "... and we have four after popping" );
@@ -80,6 +81,7 @@ is( $db->length, 3, "... and we have three after shifting" );
 is( $db->[0], 'elem1', "0th element still there after shifting" );
 is( $db->[1], 'elem2', "1st element still there after shifting" );
 is( $db->[2], 'elem3', "2nd element still there after shifting" );
+is( $db->[3], undef, "There is no third element now" );
 is( $shifted, 'elem0', "Shifted value is correct" );
 
 ##
@@ -117,7 +119,7 @@ $db->[1] = 'elem2';
 # exists
 ##
 ok( $db->exists(1), "The 1st value exists" );
-ok( !$db->exists(0), "The 0th value doesn't exists" );
+ok( $db->exists(0), "The 0th value doesn't exist" );
 ok( !$db->exists(22), "The 22nd value doesn't exists" );
 ok( $db->exists(-1), "The -1st value does exists" );
 ok( !$db->exists(-22), "The -22nd value doesn't exists" );
@@ -199,7 +201,81 @@ is($db->[0], "elem first");
 is($db->[1], "elem last");
 is($returned[0], "middle ABC");
 
+@returned = $db->splice;
+is( $db->length, 0 );
+is( $returned[0], "elem first" );
+is( $returned[1], "elem last" );
+
 $db->[0] = [ 1 .. 3 ];
 $db->[1] = { a => 'foo' };
 is( $db->[0]->length, 3, "Reuse of same space with array successful" );
 is( $db->[1]->fetch('a'), 'foo', "Reuse of same space with hash successful" );
+
+# Test autovivification
+$db->[9999]{bar} = 1;
+ok( $db->[9999] );
+cmp_ok( $db->[9999]{bar}, '==', 1 );
+
+# Test failures
+throws_ok {
+    $db->fetch( 'foo' );
+} qr/Cannot use 'foo' as an array index/, "FETCH fails on an illegal key";
+
+throws_ok {
+    $db->fetch();
+} qr/Cannot use an undefined array index/, "FETCH fails on an undefined key";
+
+throws_ok {
+    $db->store( 'foo', 'bar' );
+} qr/Cannot use 'foo' as an array index/, "STORE fails on an illegal key";
+
+throws_ok {
+    $db->store();
+} qr/Cannot use an undefined array index/, "STORE fails on an undefined key";
+
+throws_ok {
+    $db->delete( 'foo' );
+} qr/Cannot use 'foo' as an array index/, "DELETE fails on an illegal key";
+
+throws_ok {
+    $db->delete();
+} qr/Cannot use an undefined array index/, "DELETE fails on an undefined key";
+
+throws_ok {
+    $db->exists( 'foo' );
+} qr/Cannot use 'foo' as an array index/, "EXISTS fails on an illegal key";
+
+throws_ok {
+    $db->exists();
+} qr/Cannot use an undefined array index/, "EXISTS fails on an undefined key";
+
+# Bug reported by Mike Schilli
+# Also, RT #29583 reported by HANENKAMP
+{
+    my ($fh, $filename) = new_fh();
+    my $db = DBM::Deep->new(
+        file => $filename,
+        fh => $fh,
+        type => DBM::Deep->TYPE_ARRAY
+    );
+
+    push @{$db}, 3, { foo => 1 };
+    lives_ok {
+        shift @{$db};
+    } "Shift doesn't die moving references around";
+    is( $db->[0]{foo}, 1, "Right hashref there" );
+
+    lives_ok {
+        unshift @{$db}, [ 1 .. 3, [ 1 .. 3 ] ];
+        unshift @{$db}, 1;
+    } "Unshift doesn't die moving references around";
+    is( $db->[1][3][1], 2, "Right arrayref there" );
+    is( $db->[2]{foo}, 1, "Right hashref there" );
+
+    # Add test for splice moving references around
+    lives_ok {
+        splice @{$db}, 0, 0, 1 .. 3;
+    } "Splice doesn't die moving references around";
+    is( $db->[4][3][1], 2, "Right arrayref there" );
+    is( $db->[5]{foo}, 1, "Right hashref there" );
+}