Added huge internal copy test
[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" );
11if ($db->error()) {
12 die "ERROR: " . $db->error();
13}
14
15##
16# Create structure in $db
17##
18$db->import(
19 hash1 => {
20 subkey1 => "subvalue1",
21 subkey2 => "subvalue2",
22 },
23 hash2 => {
24 subkey3 => 'subvalue3',
25 },
26);
27
28is( $db->{hash1}{subkey1}, 'subvalue1', "Value imported correctly" );
29is( $db->{hash1}{subkey2}, 'subvalue2', "Value imported correctly" );
30
31$db->{copy} = $db->{hash1};
32
33is( $db->{copy}{subkey1}, 'subvalue1', "Value copied correctly" );
34is( $db->{copy}{subkey2}, 'subvalue2', "Value copied correctly" );
35
36$db->{copy}{subkey1} = "another value";
37is( $db->{copy}{subkey1}, 'another value', "New value is set correctly" );
38is( $db->{hash1}{subkey1}, 'another value', "Old value is set to the new one" );
39
40is( scalar(keys %{$db->{hash1}}), 2, "Start with 2 keys in the original" );
41is( scalar(keys %{$db->{copy}}), 2, "Start with 2 keys in the copy" );
42
43delete $db->{copy}{subkey2};
44
45is( scalar(keys %{$db->{copy}}), 1, "Now only have 1 key in the copy" );
46is( scalar(keys %{$db->{hash1}}), 1, "... and only 1 key in the original" );
47
48$db->{copy} = $db->{hash2};
49is( $db->{copy}{subkey3}, 'subvalue3', "After the second copy, we're still good" );
81e8596e 50
51my $max_keys = 1000;
52
53unlink 't/test2.db';
54{
55 my $db = DBM::Deep->new( 't/test2.db' );
56
57 $db->{foo} = [ 1 .. 3 ];
58 for ( 0 .. $max_keys ) {
59 $db->{'foo' . $_} = $db->{foo};
60 }
61}
62
63{
64 my $db = DBM::Deep->new( 't/test2.db' );
65
66 my $base_offset = $db->{foo}->_base_offset;
67 my $count = -1;
68 for ( 0 .. $max_keys ) {
69 $count = $_;
70 unless ( $base_offset == $db->{'foo'.$_}->_base_offset ) {
71 last;
72 }
73 }
74 is( $count, $max_keys, "We read $count keys" );
75}