Speed up clear()
[dbsrgits/DBM-Deep.git] / t / 22_internal_copy.t
CommitLineData
ffed8b01 1##
2# DBM::Deep Test
3##
4use strict;
1cff45d7 5use Test::More tests => 13;
fde3db1a 6use t::common qw( new_fh );
ffed8b01 7
8use_ok( 'DBM::Deep' );
9
fde3db1a 10my ($fh, $filename) = new_fh();
45f047f8 11my $db = DBM::Deep->new( file => $filename, fh => $fh, );
ffed8b01 12
13##
14# Create structure in $db
15##
e00d0eb3 16$db->import({
45f047f8 17 hash1 => {
18 subkey1 => "subvalue1",
19 subkey2 => "subvalue2",
20 },
ffed8b01 21 hash2 => {
22 subkey3 => 'subvalue3',
23 },
e00d0eb3 24});
ffed8b01 25
26is( $db->{hash1}{subkey1}, 'subvalue1', "Value imported correctly" );
27is( $db->{hash1}{subkey2}, 'subvalue2', "Value imported correctly" );
28
29$db->{copy} = $db->{hash1};
30
31is( $db->{copy}{subkey1}, 'subvalue1', "Value copied correctly" );
32is( $db->{copy}{subkey2}, 'subvalue2', "Value copied correctly" );
33
34$db->{copy}{subkey1} = "another value";
35is( $db->{copy}{subkey1}, 'another value', "New value is set correctly" );
36is( $db->{hash1}{subkey1}, 'another value', "Old value is set to the new one" );
37
38is( scalar(keys %{$db->{hash1}}), 2, "Start with 2 keys in the original" );
39is( scalar(keys %{$db->{copy}}), 2, "Start with 2 keys in the copy" );
40
41delete $db->{copy}{subkey2};
42
43is( scalar(keys %{$db->{copy}}), 1, "Now only have 1 key in the copy" );
44is( scalar(keys %{$db->{hash1}}), 1, "... and only 1 key in the original" );
45
46$db->{copy} = $db->{hash2};
47is( $db->{copy}{subkey3}, 'subvalue3', "After the second copy, we're still good" );
81e8596e 48my $max_keys = 1000;
49
fde3db1a 50my ($fh2, $filename2) = new_fh();
81e8596e 51{
45f047f8 52 my $db = DBM::Deep->new( file => $filename2, fh => $fh2, );
81e8596e 53
54 $db->{foo} = [ 1 .. 3 ];
55 for ( 0 .. $max_keys ) {
56 $db->{'foo' . $_} = $db->{foo};
57 }
45f047f8 58 ## Rewind handle otherwise the signature is not recognised below.
59 ## The signature check should probably rewind the fh?
f1879fdc 60 seek $db->_get_self->_engine->storage->{fh}, 0, 0;
81e8596e 61}
62
63{
45f047f8 64 my $db = DBM::Deep->new( fh => $fh2, );
81e8596e 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}