Refactored to _descend to fix the recursion bug
[dbsrgits/DBM-Deep.git] / t / 22_internal_copy.t
1 use strict;
2 use warnings FATAL => 'all';
3
4 use Test::More;
5 use t::common qw( new_dbm new_fh );
6
7 use_ok( 'DBM::Deep' );
8
9 my $dbm_factory = new_dbm();
10 while ( my $dbm_maker = $dbm_factory->() ) {
11     my $db = $dbm_maker->();
12
13     $db->import({
14         hash1 => {
15             subkey1 => "subvalue1",
16             subkey2 => "subvalue2",
17         },
18         hash2 => {
19             subkey3 => 'subvalue3',
20         },
21     });
22
23     is( $db->{hash1}{subkey1}, 'subvalue1', "Value imported correctly" );
24     is( $db->{hash1}{subkey2}, 'subvalue2', "Value imported correctly" );
25
26     $db->{copy} = $db->{hash1};
27
28     is( $db->{copy}{subkey1}, 'subvalue1', "Value copied correctly" );
29     is( $db->{copy}{subkey2}, 'subvalue2', "Value copied correctly" );
30
31     $db->{copy}{subkey1} = "another value";
32     is( $db->{copy}{subkey1}, 'another value', "New value is set correctly" );
33     is( $db->{hash1}{subkey1}, 'another value', "Old value is set to the new one" );
34
35     is( scalar(keys %{$db->{hash1}}), 2, "Start with 2 keys in the original" );
36     is( scalar(keys %{$db->{copy}}), 2, "Start with 2 keys in the copy" );
37
38     delete $db->{copy}{subkey2};
39
40     is( scalar(keys %{$db->{copy}}), 1, "Now only have 1 key in the copy" );
41     is( scalar(keys %{$db->{hash1}}), 1, "... and only 1 key in the original" );
42
43     $db->{copy} = $db->{hash2};
44     is( $db->{copy}{subkey3}, 'subvalue3', "After the second copy, we're still good" );
45 }
46
47 {
48     my $max_keys = 1000;
49     my $dbm_factory = new_dbm();
50     while ( my $dbm_maker = $dbm_factory->() ) {
51         {
52             my $db = $dbm_maker->();
53
54             $db->{foo} = [ 1 .. 3 ];
55             for ( 0 .. $max_keys ) {
56                 $db->{'foo' . $_} = $db->{foo};
57             }
58         }
59
60         {
61             my $db = $dbm_maker->();
62
63             my $base_offset = $db->{foo}->_base_offset;
64             my $count = -1;
65             for ( 0 .. $max_keys ) {
66                 $count = $_;
67                 unless ( $base_offset == $db->{'foo'.$_}->_base_offset ) {
68                     last;
69                 }
70             }
71             is( $count, $max_keys, "We read $count keys" );
72         }
73     }
74 }
75
76 done_testing;