Moved _create_tag, _load_tag, and _index_lookup into the engine
[dbsrgits/DBM-Deep.git] / t / 22_internal_copy.t
CommitLineData
ffed8b01 1##
2# DBM::Deep Test
3##
4use strict;
81e8596e 5use Test::More tests => 13;
ffed8b01 6
7use_ok( 'DBM::Deep' );
8
9unlink "t/test.db";
10my $db = DBM::Deep->new( "t/test.db" );
ffed8b01 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
25is( $db->{hash1}{subkey1}, 'subvalue1', "Value imported correctly" );
26is( $db->{hash1}{subkey2}, 'subvalue2', "Value imported correctly" );
27
28$db->{copy} = $db->{hash1};
29
30is( $db->{copy}{subkey1}, 'subvalue1', "Value copied correctly" );
31is( $db->{copy}{subkey2}, 'subvalue2', "Value copied correctly" );
32
33$db->{copy}{subkey1} = "another value";
34is( $db->{copy}{subkey1}, 'another value', "New value is set correctly" );
35is( $db->{hash1}{subkey1}, 'another value', "Old value is set to the new one" );
36
37is( scalar(keys %{$db->{hash1}}), 2, "Start with 2 keys in the original" );
38is( scalar(keys %{$db->{copy}}), 2, "Start with 2 keys in the copy" );
39
40delete $db->{copy}{subkey2};
41
42is( scalar(keys %{$db->{copy}}), 1, "Now only have 1 key in the copy" );
43is( scalar(keys %{$db->{hash1}}), 1, "... and only 1 key in the original" );
44
45$db->{copy} = $db->{hash2};
46is( $db->{copy}{subkey3}, 'subvalue3', "After the second copy, we're still good" );
81e8596e 47
48my $max_keys = 1000;
49
50unlink '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}