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