Merge cd5303b: RT# 50541: Fix for clear bug. Introduces a speed regression
[dbsrgits/DBM-Deep.git] / t / 02_hash.t
1 ##
2 # DBM::Deep Test
3 ##
4 use strict;
5 use Test::More tests => 53;
6 use Test::Exception;
7 use t::common qw( new_fh );
8
9 use_ok( 'DBM::Deep' );
10
11 my ($fh, $filename) = new_fh();
12 my $db = DBM::Deep->new(
13     file => $filename,
14     fh => $fh,
15 );
16
17 ##
18 # put/get key
19 ##
20 $db->{key1} = "value1";
21 is( $db->get("key1"), "value1", "get() works with hash assignment" );
22 is( $db->fetch("key1"), "value1", "... fetch() works with hash assignment" );
23 is( $db->{key1}, "value1", "... and hash-access also works" );
24
25 $db->put("key2", undef);
26 is( $db->get("key2"), undef, "get() works with put()" );
27 is( $db->fetch("key2"), undef, "... fetch() works with put()" );
28 is( $db->{key2}, undef, "... and hash-access also works" );
29
30 $db->store( "0", "value3" );
31 is( $db->get("0"), "value3", "get() works with store()" );
32 is( $db->fetch("0"), "value3", "... fetch() works with put()" );
33 is( $db->{0}, 'value3', "... and hash-access also works" );
34
35 # Verify that the keyval pairs are still correct.
36 is( $db->{key1}, "value1", "Key1 is still correct" );
37 is( $db->{key2}, undef, "Key2 is still correct" );
38 is( $db->{0}, 'value3', "Key3 is still correct" );
39
40 ok( $db->exists("key1"), "exists() function works" );
41 ok( exists $db->{key2}, "exists() works against tied hash" );
42
43 ok( !exists $db->{key4}, "exists() function works for keys that aren't there" );
44 is( $db->{key4}, undef, "Autovivified key4" );
45 ok( exists $db->{key4}, "Autovivified key4 now exists" );
46
47 delete $db->{key4};
48 ok( !exists $db->{key4}, "And key4 doesn't exists anymore" );
49
50 # Keys will be done via an iterator that keeps a breadcrumb trail of the last
51 # key it provided. There will also be an "edit revision number" on the
52 # reference so that resetting the iterator can be done.
53 #
54 # Q: How do we make sure that the iterator is unique? Is it supposed to be?
55
56 ##
57 # count keys
58 ##
59 is( scalar keys %$db, 3, "keys() works against tied hash" );
60
61 ##
62 # step through keys
63 ##
64 my $temphash = {};
65 while ( my ($key, $value) = each %$db ) {
66     $temphash->{$key} = $value;
67 }
68
69 is( $temphash->{key1}, 'value1', "First key copied successfully using tied interface" );
70 is( $temphash->{key2}, undef, "Second key copied successfully" );
71 is( $temphash->{0}, 'value3', "Third key copied successfully" );
72
73 $temphash = {};
74 my $key = $db->first_key();
75 while (defined $key) {
76     $temphash->{$key} = $db->get($key);
77     $key = $db->next_key($key);
78 }
79
80 is( $temphash->{key1}, 'value1', "First key copied successfully using OO interface" );
81 is( $temphash->{key2}, undef, "Second key copied successfully" );
82 is( $temphash->{0}, 'value3', "Third key copied successfully" );
83
84 ##
85 # delete keys
86 ##
87 is( delete $db->{key2}, undef, "delete through tied inteface works" );
88 is( $db->delete("key1"), 'value1', "delete through OO inteface works" );
89 is( $db->{0}, 'value3', "The other key is still there" );
90 ok( !exists $db->{key1}, "key1 doesn't exist" );
91 ok( !exists $db->{key2}, "key2 doesn't exist" );
92
93 is( scalar keys %$db, 1, "After deleting two keys, 1 remains" );
94
95 ##
96 # delete all keys
97 ##
98 ok( $db->clear(), "clear() returns true" );
99
100 # ~~~ Temporary band-aid until the fix for RT#50541 is merged
101 delete $db->{0};
102
103 is( scalar keys %$db, 0, "After clear(), everything is removed" );
104
105 ##
106 # replace key
107 ##
108 $db->put("key1", "value1");
109 is( $db->get("key1"), "value1", "Assignment still works" );
110
111 $db->put("key1", "value2");
112 is( $db->get("key1"), "value2", "... and replacement works" );
113
114 $db->put("key1", "value222222222222222222222222");
115 is( $db->get("key1"), "value222222222222222222222222", "We set a value before closing the file" );
116
117 ##
118 # Make sure DB still works after closing / opening
119 ##
120 undef $db;
121 open $fh, '+<', $filename;
122 $db = DBM::Deep->new(
123     file => $filename,
124     fh => $fh,
125 );
126 is( $db->get("key1"), "value222222222222222222222222", "The value we set is still there after closure" );
127
128 ##
129 # Make sure keys are still fetchable after replacing values
130 # with smaller ones (bug found by John Cardenas, DBM::Deep 0.93)
131 ##
132 $db->clear();
133 $db->put("key1", "long value here");
134 $db->put("key2", "longer value here");
135
136 $db->put("key1", "short value");
137 $db->put("key2", "shorter v");
138
139 my $first_key = $db->first_key();
140 my $next_key = $db->next_key($first_key);
141
142 ok(
143     (($first_key eq "key1") || ($first_key eq "key2")) && 
144     (($next_key eq "key1") || ($next_key eq "key2")) && 
145     ($first_key ne $next_key)
146     ,"keys() still works if you replace long values with shorter ones"
147 );
148
149 # Make sure we do not trigger a deep recursion warning [RT #53575]
150 {
151     my $w;
152     local $SIG{__WARN__} = sub { $w = shift };
153     my ($fh, $filename) = new_fh();
154     my $db = DBM::Deep->new( file => $filename, fh => $fh, );
155     my $h = {};
156     my $tmp = $h;
157     for(1..100) {
158         %$tmp = ("" => {});
159         $tmp = $$tmp{""};
160     }
161     ok eval {
162         $db->{""} = $h;
163     }, 'deep recursion in hash assignment' or diag $@;
164     is $w, undef, 'no warnings with deep recursion in hash assignment';
165 }
166
167 # Test autovivification
168 $db->{unknown}{bar} = 1;
169 ok( $db->{unknown}, 'Autovivified hash exists' );
170 cmp_ok( $db->{unknown}{bar}, '==', 1, 'And the value stored is there' );
171
172 # Test failures
173 throws_ok {
174     $db->fetch();
175 } qr/Cannot use an undefined hash key/, "FETCH fails on an undefined key";
176
177 throws_ok {
178     $db->fetch(undef);
179 } qr/Cannot use an undefined hash key/, "FETCH fails on an undefined key";
180
181 throws_ok {
182     $db->store();
183 } qr/Cannot use an undefined hash key/, "STORE fails on an undefined key";
184
185 throws_ok {
186     $db->store(undef, undef);
187 } qr/Cannot use an undefined hash key/, "STORE fails on an undefined key";
188
189 throws_ok {
190     $db->delete();
191 } qr/Cannot use an undefined hash key/, "DELETE fails on an undefined key";
192
193 throws_ok {
194     $db->delete(undef);
195 } qr/Cannot use an undefined hash key/, "DELETE fails on an undefined key";
196
197 throws_ok {
198     $db->exists();
199 } qr/Cannot use an undefined hash key/, "EXISTS fails on an undefined key";
200
201 throws_ok {
202     $db->exists(undef);
203 } qr/Cannot use an undefined hash key/, "EXISTS fails on an undefined key";
204
205 {
206     # RT# 50541 (reported by Peter Scott)
207     # clear() leaves one key unless there's only one
208     my ($fh, $filename) = new_fh();
209     my $db = DBM::Deep->new(
210         file => $filename,
211         fh => $fh,
212     );
213
214     $db->{block} = { };
215     $db->{critical} = { };
216     $db->{minor} = { };
217
218     cmp_ok( scalar(keys( %$db )), '==', 3, "Have 3 keys" );
219
220     $db->clear;
221
222     cmp_ok( scalar(keys( %$db )), '==', 0, "clear clears everything" );
223 }