000519f86c773d4095c171364ee41139e0267023
[dbsrgits/DBM-Deep.git] / t / 22_internal_copy.t
1 ##
2 # DBM::Deep Test
3 ##
4 use strict;
5 use Test::More tests => 13;
6
7 use_ok( 'DBM::Deep' );
8
9 unlink "t/test.db";
10 my $db = DBM::Deep->new( "t/test.db" );
11
12 ##
13 # Create structure in $db
14 ##
15 $db->import(
16         hash1 => {
17                 subkey1 => "subvalue1",
18                 subkey2 => "subvalue2",
19         },
20     hash2 => {
21         subkey3 => 'subvalue3',
22     },
23 );
24
25 is( $db->{hash1}{subkey1}, 'subvalue1', "Value imported correctly" );
26 is( $db->{hash1}{subkey2}, 'subvalue2', "Value imported correctly" );
27
28 $db->{copy} = $db->{hash1};
29
30 is( $db->{copy}{subkey1}, 'subvalue1', "Value copied correctly" );
31 is( $db->{copy}{subkey2}, 'subvalue2', "Value copied correctly" );
32
33 $db->{copy}{subkey1} = "another value";
34 is( $db->{copy}{subkey1}, 'another value', "New value is set correctly" );
35 is( $db->{hash1}{subkey1}, 'another value', "Old value is set to the new one" );
36
37 is( scalar(keys %{$db->{hash1}}), 2, "Start with 2 keys in the original" );
38 is( scalar(keys %{$db->{copy}}), 2, "Start with 2 keys in the copy" );
39
40 delete $db->{copy}{subkey2};
41
42 is( scalar(keys %{$db->{copy}}), 1, "Now only have 1 key in the copy" );
43 is( scalar(keys %{$db->{hash1}}), 1, "... and only 1 key in the original" );
44
45 $db->{copy} = $db->{hash2};
46 is( $db->{copy}{subkey3}, 'subvalue3', "After the second copy, we're still good" );
47
48 my $max_keys = 1000;
49
50 unlink 't/test2.db';
51 {
52     my $db = DBM::Deep->new( 't/test2.db' );
53
54     $db->{foo} = [ 1 .. 3 ];
55     for ( 0 .. $max_keys ) {
56         $db->{'foo' . $_} = $db->{foo};
57     }
58 }
59
60 {
61     my $db = DBM::Deep->new( 't/test2.db' );
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 }