Commit | Line | Data |
ffed8b01 |
1 | ## |
2 | # DBM::Deep Test |
3 | ## |
4 | use strict; |
81e8596e |
5 | use Test::More tests => 13; |
98ac82af |
6 | use File::Temp qw( tempfile tempdir ); |
58910373 |
7 | use Fcntl qw( :flock ); |
ffed8b01 |
8 | |
9 | use_ok( 'DBM::Deep' ); |
10 | |
98ac82af |
11 | my $dir = tempdir( CLEANUP => 1 ); |
12 | my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); |
58910373 |
13 | flock $fh, LOCK_UN; |
98ac82af |
14 | my $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 | |
29 | is( $db->{hash1}{subkey1}, 'subvalue1', "Value imported correctly" ); |
30 | is( $db->{hash1}{subkey2}, 'subvalue2', "Value imported correctly" ); |
31 | |
32 | $db->{copy} = $db->{hash1}; |
33 | |
34 | is( $db->{copy}{subkey1}, 'subvalue1', "Value copied correctly" ); |
35 | is( $db->{copy}{subkey2}, 'subvalue2', "Value copied correctly" ); |
36 | |
37 | $db->{copy}{subkey1} = "another value"; |
38 | is( $db->{copy}{subkey1}, 'another value', "New value is set correctly" ); |
39 | is( $db->{hash1}{subkey1}, 'another value', "Old value is set to the new one" ); |
40 | |
41 | is( scalar(keys %{$db->{hash1}}), 2, "Start with 2 keys in the original" ); |
42 | is( scalar(keys %{$db->{copy}}), 2, "Start with 2 keys in the copy" ); |
43 | |
44 | delete $db->{copy}{subkey2}; |
45 | |
46 | is( scalar(keys %{$db->{copy}}), 1, "Now only have 1 key in the copy" ); |
47 | is( scalar(keys %{$db->{hash1}}), 1, "... and only 1 key in the original" ); |
48 | |
49 | $db->{copy} = $db->{hash2}; |
50 | is( $db->{copy}{subkey3}, 'subvalue3', "After the second copy, we're still good" ); |
81e8596e |
51 | |
52 | my $max_keys = 1000; |
53 | |
98ac82af |
54 | my ($fh2, $filename2) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); |
58910373 |
55 | flock $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 | } |