Added a comment as to where an allocation error is occurring that crashes perl
[dbsrgits/DBM-Deep.git] / t / 02_hash.t
CommitLineData
ffed8b01 1##
2# DBM::Deep Test
3##
4use strict;
2120a181 5use Test::More tests => 49;
ffed8b01 6use Test::Exception;
fde3db1a 7use t::common qw( new_fh );
ffed8b01 8
9use_ok( 'DBM::Deep' );
10
fde3db1a 11my ($fh, $filename) = new_fh();
45f047f8 12my $db = DBM::Deep->new(
13 file => $filename,
14 fh => $fh,
15);
ffed8b01 16
17##
18# put/get key
19##
20$db->{key1} = "value1";
21is( $db->get("key1"), "value1", "get() works with hash assignment" );
22is( $db->fetch("key1"), "value1", "... fetch() works with hash assignment" );
23is( $db->{key1}, "value1", "... and hash-access also works" );
2120a181 24
ffed8b01 25$db->put("key2", undef);
26is( $db->get("key2"), undef, "get() works with put()" );
27is( $db->fetch("key2"), undef, "... fetch() works with put()" );
28is( $db->{key2}, undef, "... and hash-access also works" );
29
30$db->store( "key3", "value3" );
31is( $db->get("key3"), "value3", "get() works with store()" );
32is( $db->fetch("key3"), "value3", "... fetch() works with put()" );
33is( $db->{key3}, 'value3', "... and hash-access also works" );
34
2120a181 35# Verify that the keyval pairs are still correct.
36is( $db->{key1}, "value1", "Key1 is still correct" );
37is( $db->{key2}, undef, "Key2 is still correct" );
38is( $db->{key3}, 'value3', "Key3 is still correct" );
39
ffed8b01 40ok( $db->exists("key1"), "exists() function works" );
41ok( exists $db->{key2}, "exists() works against tied hash" );
42
94e8af14 43ok( !exists $db->{key4}, "exists() function works for keys that aren't there" );
44is( $db->{key4}, undef, "Autovivified key4" );
2120a181 45ok( exists $db->{key4}, "Autovivified key4 now exists" );
46
94e8af14 47delete $db->{key4};
a8d2331c 48
94e8af14 49ok( !exists $db->{key4}, "And key4 doesn't exists anymore" );
50
2120a181 51# Keys will be done via an iterator that keeps a breadcrumb trail of the last
52# key it provided. There will also be an "edit revision number" on the
53# reference so that resetting the iterator can be done.
54#
55# Q: How do we make sure that the iterator is unique? Is it supposed to be?
56
ffed8b01 57##
58# count keys
59##
60is( scalar keys %$db, 3, "keys() works against tied hash" );
61
62##
63# step through keys
64##
65my $temphash = {};
66while ( my ($key, $value) = each %$db ) {
ea2f6d67 67 $temphash->{$key} = $value;
ffed8b01 68}
69
70is( $temphash->{key1}, 'value1', "First key copied successfully using tied interface" );
71is( $temphash->{key2}, undef, "Second key copied successfully" );
72is( $temphash->{key3}, 'value3', "Third key copied successfully" );
73
74$temphash = {};
75my $key = $db->first_key();
76while ($key) {
ea2f6d67 77 $temphash->{$key} = $db->get($key);
78 $key = $db->next_key($key);
ffed8b01 79}
80
81is( $temphash->{key1}, 'value1', "First key copied successfully using OO interface" );
82is( $temphash->{key2}, undef, "Second key copied successfully" );
83is( $temphash->{key3}, 'value3', "Third key copied successfully" );
84
85##
86# delete keys
87##
8db25060 88is( delete $db->{key2}, undef, "delete through tied inteface works" );
89is( $db->delete("key1"), 'value1', "delete through OO inteface works" );
90is( $db->{key3}, 'value3', "The other key is still there" );
ea2f6d67 91ok( !exists $db->{key1}, "key1 doesn't exist" );
92ok( !exists $db->{key2}, "key2 doesn't exist" );
ffed8b01 93
94is( scalar keys %$db, 1, "After deleting two keys, 1 remains" );
95
96##
97# delete all keys
98##
99ok( $db->clear(), "clear() returns true" );
100
101is( scalar keys %$db, 0, "After clear(), everything is removed" );
102
103##
104# replace key
105##
106$db->put("key1", "value1");
107is( $db->get("key1"), "value1", "Assignment still works" );
108
109$db->put("key1", "value2");
110is( $db->get("key1"), "value2", "... and replacement works" );
111
112$db->put("key1", "value222222222222222222222222");
ffed8b01 113is( $db->get("key1"), "value222222222222222222222222", "We set a value before closing the file" );
114
115##
116# Make sure DB still works after closing / opening
117##
118undef $db;
45f047f8 119open $fh, '+<', $filename;
120$db = DBM::Deep->new(
121 file => $filename,
122 fh => $fh,
123);
ffed8b01 124is( $db->get("key1"), "value222222222222222222222222", "The value we set is still there after closure" );
125
126##
127# Make sure keys are still fetchable after replacing values
128# with smaller ones (bug found by John Cardenas, DBM::Deep 0.93)
129##
130$db->clear();
131$db->put("key1", "long value here");
132$db->put("key2", "longer value here");
133
134$db->put("key1", "short value");
135$db->put("key2", "shorter v");
136
137my $first_key = $db->first_key();
138my $next_key = $db->next_key($first_key);
139
140ok(
ea2f6d67 141 (($first_key eq "key1") || ($first_key eq "key2")) &&
142 (($next_key eq "key1") || ($next_key eq "key2")) &&
143 ($first_key ne $next_key)
ffed8b01 144 ,"keys() still works if you replace long values with shorter ones"
145);
4b603f25 146
147# Test autovivification
4b603f25 148$db->{unknown}{bar} = 1;
2120a181 149ok( $db->{unknown}, 'Autovivified hash exists' );
129ea236 150cmp_ok( $db->{unknown}{bar}, '==', 1, 'And the value stored is there' );
2120a181 151
152# Test failures
153throws_ok {
154 $db->fetch();
155} qr/Cannot use an undefined hash key/, "FETCH fails on an undefined key";
156
157throws_ok {
158 $db->fetch(undef);
159} qr/Cannot use an undefined hash key/, "FETCH fails on an undefined key";
160
161throws_ok {
162 $db->store();
163} qr/Cannot use an undefined hash key/, "STORE fails on an undefined key";
164
165throws_ok {
166 $db->store(undef, undef);
167} qr/Cannot use an undefined hash key/, "STORE fails on an undefined key";
168
169throws_ok {
170 $db->delete();
171} qr/Cannot use an undefined hash key/, "DELETE fails on an undefined key";
172
173throws_ok {
174 $db->delete(undef);
175} qr/Cannot use an undefined hash key/, "DELETE fails on an undefined key";
176
177throws_ok {
178 $db->exists();
179} qr/Cannot use an undefined hash key/, "EXISTS fails on an undefined key";
180
181throws_ok {
182 $db->exists(undef);
183} qr/Cannot use an undefined hash key/, "EXISTS fails on an undefined key";