Added unflocks to all tests so that the tests run on OSX
[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;
98ac82af 6use File::Temp qw( tempfile tempdir );
58910373 7use Fcntl qw( :flock );
ffed8b01 8
9use_ok( 'DBM::Deep' );
10
98ac82af 11my $dir = tempdir( CLEANUP => 1 );
12my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
58910373 13flock $fh, LOCK_UN;
98ac82af 14my $db = DBM::Deep->new( $filename );
ffed8b01 15
16##
17# Create structure in $db
18##
19$db->import(
20 hash1 => {
21 subkey1 => "subvalue1",
22 subkey2 => "subvalue2",
23 },
24 hash2 => {
25 subkey3 => 'subvalue3',
26 },
27);
28
29is( $db->{hash1}{subkey1}, 'subvalue1', "Value imported correctly" );
30is( $db->{hash1}{subkey2}, 'subvalue2', "Value imported correctly" );
31
32$db->{copy} = $db->{hash1};
33
34is( $db->{copy}{subkey1}, 'subvalue1', "Value copied correctly" );
35is( $db->{copy}{subkey2}, 'subvalue2', "Value copied correctly" );
36
37$db->{copy}{subkey1} = "another value";
38is( $db->{copy}{subkey1}, 'another value', "New value is set correctly" );
39is( $db->{hash1}{subkey1}, 'another value', "Old value is set to the new one" );
40
41is( scalar(keys %{$db->{hash1}}), 2, "Start with 2 keys in the original" );
42is( scalar(keys %{$db->{copy}}), 2, "Start with 2 keys in the copy" );
43
44delete $db->{copy}{subkey2};
45
46is( scalar(keys %{$db->{copy}}), 1, "Now only have 1 key in the copy" );
47is( scalar(keys %{$db->{hash1}}), 1, "... and only 1 key in the original" );
48
49$db->{copy} = $db->{hash2};
50is( $db->{copy}{subkey3}, 'subvalue3', "After the second copy, we're still good" );
81e8596e 51
52my $max_keys = 1000;
53
98ac82af 54my ($fh2, $filename2) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
58910373 55flock $fh2, LOCK_UN;
81e8596e 56{
98ac82af 57 my $db = DBM::Deep->new( $filename2 );
81e8596e 58
59 $db->{foo} = [ 1 .. 3 ];
60 for ( 0 .. $max_keys ) {
61 $db->{'foo' . $_} = $db->{foo};
62 }
63}
64
65{
98ac82af 66 my $db = DBM::Deep->new( $filename2 );
81e8596e 67
68 my $base_offset = $db->{foo}->_base_offset;
69 my $count = -1;
70 for ( 0 .. $max_keys ) {
71 $count = $_;
72 unless ( $base_offset == $db->{'foo'.$_}->_base_offset ) {
73 last;
74 }
75 }
76 is( $count, $max_keys, "We read $count keys" );
77}