r6127@000-443-371 (orig r9960): rkinyon | 2007-09-20 21:13:08 -0400
[dbsrgits/DBM-Deep.git] / t / 04_array.t
CommitLineData
ffed8b01 1##
2# DBM::Deep Test
3##
4use strict;
807f63a7 5use Test::More tests => 124;
ffed8b01 6use Test::Exception;
fde3db1a 7use t::common qw( new_fh );
ffed8b01 8
9use_ok( 'DBM::Deep' );
10
fde3db1a 11my ($fh, $filename) = new_fh();
ffed8b01 12my $db = DBM::Deep->new(
2a81bf9e 13 file => $filename,
ffed8b01 14 type => DBM::Deep->TYPE_ARRAY
15);
ffed8b01 16
ffed8b01 17##
18# basic put/get/push
19##
20$db->[0] = "elem1";
21$db->push( "elem2" );
22$db->put(2, "elem3");
23$db->store(3, "elem4");
24$db->unshift("elem0");
25
26is( $db->[0], 'elem0', "Array get for shift works" );
27is( $db->[1], 'elem1', "Array get for array set works" );
28is( $db->[2], 'elem2', "Array get for push() works" );
29is( $db->[3], 'elem3', "Array get for put() works" );
30is( $db->[4], 'elem4', "Array get for store() works" );
31
32is( $db->get(0), 'elem0', "get() for shift() works" );
33is( $db->get(1), 'elem1', "get() for array set works" );
34is( $db->get(2), 'elem2', "get() for push() works" );
35is( $db->get(3), 'elem3', "get() for put() works" );
36is( $db->get(4), 'elem4', "get() for store() works" );
37
38is( $db->fetch(0), 'elem0', "fetch() for shift() works" );
39is( $db->fetch(1), 'elem1', "fetch() for array set works" );
40is( $db->fetch(2), 'elem2', "fetch() for push() works" );
41is( $db->fetch(3), 'elem3', "fetch() for put() works" );
42is( $db->fetch(4), 'elem4', "fetch() for store() works" );
43
44is( $db->length, 5, "... and we have five elements" );
45
7f441181 46is( $db->[-1], $db->[4], "-1st index is 4th index" );
47is( $db->[-2], $db->[3], "-2nd index is 3rd index" );
48is( $db->[-3], $db->[2], "-3rd index is 2nd index" );
49is( $db->[-4], $db->[1], "-4th index is 1st index" );
50is( $db->[-5], $db->[0], "-5th index is 0th index" );
c9cec40e 51
52# This is for Perls older than 5.8.0 because of is()'s prototype
53{ my $v = $db->[-6]; is( $v, undef, "-6th index is undef" ); }
54
ffed8b01 55is( $db->length, 5, "... and we have five elements after abortive -6 index lookup" );
56
cb79ec85 57$db->[-1] = 'elem4.1';
58is( $db->[-1], 'elem4.1' );
59is( $db->[4], 'elem4.1' );
60is( $db->get(4), 'elem4.1' );
61is( $db->fetch(4), 'elem4.1' );
62
baa27ab6 63throws_ok {
64 $db->[-6] = 'whoops!';
65} qr/Modification of non-creatable array value attempted, subscript -6/, "Correct error thrown";
66
ffed8b01 67my $popped = $db->pop;
68is( $db->length, 4, "... and we have four after popping" );
69is( $db->[0], 'elem0', "0th element still there after popping" );
70is( $db->[1], 'elem1', "1st element still there after popping" );
71is( $db->[2], 'elem2', "2nd element still there after popping" );
72is( $db->[3], 'elem3', "3rd element still there after popping" );
cb79ec85 73is( $popped, 'elem4.1', "Popped value is correct" );
ffed8b01 74
75my $shifted = $db->shift;
76is( $db->length, 3, "... and we have three after shifting" );
77is( $db->[0], 'elem1', "0th element still there after shifting" );
78is( $db->[1], 'elem2', "1st element still there after shifting" );
79is( $db->[2], 'elem3', "2nd element still there after shifting" );
80is( $shifted, 'elem0', "Shifted value is correct" );
81
82##
83# delete
84##
85my $deleted = $db->delete(0);
86is( $db->length, 3, "... and we still have three after deleting" );
87is( $db->[0], undef, "0th element now undef" );
88is( $db->[1], 'elem2', "1st element still there after deleting" );
89is( $db->[2], 'elem3', "2nd element still there after deleting" );
81d3d316 90is( $deleted, 'elem1', "Deleted value is correct" );
ffed8b01 91
92is( $db->delete(99), undef, 'delete on an element not in the array returns undef' );
93is( $db->length, 3, "... and we still have three after a delete on an out-of-range index" );
94
95is( delete $db->[99], undef, 'DELETE on an element not in the array returns undef' );
96is( $db->length, 3, "... and we still have three after a DELETE on an out-of-range index" );
97
98is( $db->delete(-99), undef, 'delete on an element (neg) not in the array returns undef' );
99is( $db->length, 3, "... and we still have three after a DELETE on an out-of-range negative index" );
100
101is( delete $db->[-99], undef, 'DELETE on an element (neg) not in the array returns undef' );
102is( $db->length, 3, "... and we still have three after a DELETE on an out-of-range negative index" );
103
104$deleted = $db->delete(-2);
105is( $db->length, 3, "... and we still have three after deleting" );
106is( $db->[0], undef, "0th element still undef" );
9281d66b 107is( $db->[1], undef, "1st element now undef" );
ffed8b01 108is( $db->[2], 'elem3', "2nd element still there after deleting" );
9281d66b 109is( $deleted, 'elem2', "Deleted value is correct" );
ffed8b01 110
111$db->[1] = 'elem2';
112
113##
114# exists
115##
116ok( $db->exists(1), "The 1st value exists" );
2120a181 117ok( $db->exists(0), "The 0th value doesn't exist" );
ffed8b01 118ok( !$db->exists(22), "The 22nd value doesn't exists" );
baa27ab6 119ok( $db->exists(-1), "The -1st value does exists" );
ffed8b01 120ok( !$db->exists(-22), "The -22nd value doesn't exists" );
121
122##
123# clear
124##
125ok( $db->clear(), "clear() returns true if the file was ever non-empty" );
126is( $db->length(), 0, "After clear(), no more elements" );
127
128is( $db->pop, undef, "pop on an empty array returns undef" );
129is( $db->length(), 0, "After pop() on empty array, length is still 0" );
130
131is( $db->shift, undef, "shift on an empty array returns undef" );
132is( $db->length(), 0, "After shift() on empty array, length is still 0" );
133
8f6d6ed0 134is( $db->unshift( 1, 2, 3 ), 3, "unshift returns the number of elements in the array" );
135is( $db->unshift( 1, 2, 3 ), 6, "unshift returns the number of elements in the array" );
136is( $db->push( 1, 2, 3 ), 9, "push returns the number of elements in the array" );
137
ffed8b01 138is( $db->length(), 9, "After unshift and push on empty array, length is now 9" );
139
140$db->clear;
141
142##
143# multi-push
144##
145$db->push( 'elem first', "elem middle", "elem last" );
146is( $db->length, 3, "3-element push results in three elements" );
147is($db->[0], "elem first", "First element is 'elem first'");
148is($db->[1], "elem middle", "Second element is 'elem middle'");
149is($db->[2], "elem last", "Third element is 'elem last'");
150
151##
152# splice with length 1
153##
8fec41b9 154my @returned = $db->splice( 1, 1, "middle A", "middle B" );
155is( scalar(@returned), 1, "One element was removed" );
156is( $returned[0], 'elem middle', "... and it was correctly removed" );
ffed8b01 157is($db->length(), 4);
158is($db->[0], "elem first");
159is($db->[1], "middle A");
160is($db->[2], "middle B");
161is($db->[3], "elem last");
162
163##
164# splice with length of 0
165##
8fec41b9 166@returned = $db->splice( -1, 0, "middle C" );
167is( scalar(@returned), 0, "No elements were removed" );
ffed8b01 168is($db->length(), 5);
169is($db->[0], "elem first");
170is($db->[1], "middle A");
171is($db->[2], "middle B");
172is($db->[3], "middle C");
173is($db->[4], "elem last");
174
175##
176# splice with length of 3
177##
8fec41b9 178my $returned = $db->splice( 1, 3, "middle ABC" );
179is( $returned, 'middle C', "Just the last element was returned" );
ffed8b01 180is($db->length(), 3);
181is($db->[0], "elem first");
182is($db->[1], "middle ABC");
183is($db->[2], "elem last");
184
53fdf3d7 185@returned = $db->splice( 1 );
186is($db->length(), 1);
187is($db->[0], "elem first");
188is($returned[0], "middle ABC");
189is($returned[1], "elem last");
190
191$db->push( @returned );
192
193@returned = $db->splice( 1, -1 );
194is($db->length(), 2);
195is($db->[0], "elem first");
196is($db->[1], "elem last");
197is($returned[0], "middle ABC");
198
ffed8b01 199$db->[0] = [ 1 .. 3 ];
200$db->[1] = { a => 'foo' };
994ccd8e 201is( $db->[0]->length, 3, "Reuse of same space with array successful" );
ea2f6d67 202is( $db->[1]->fetch('a'), 'foo', "Reuse of same space with hash successful" );
4b603f25 203
2120a181 204# Test autovivification
4b603f25 205$db->[9999]{bar} = 1;
206ok( $db->[9999] );
207cmp_ok( $db->[9999]{bar}, '==', 1 );
2120a181 208
209# Test failures
210throws_ok {
211 $db->fetch( 'foo' );
212} qr/Cannot use 'foo' as an array index/, "FETCH fails on an illegal key";
213
214throws_ok {
215 $db->fetch();
216} qr/Cannot use an undefined array index/, "FETCH fails on an undefined key";
217
218throws_ok {
219 $db->store( 'foo', 'bar' );
220} qr/Cannot use 'foo' as an array index/, "STORE fails on an illegal key";
221
222throws_ok {
223 $db->store();
224} qr/Cannot use an undefined array index/, "STORE fails on an undefined key";
225
226throws_ok {
227 $db->delete( 'foo' );
228} qr/Cannot use 'foo' as an array index/, "DELETE fails on an illegal key";
229
230throws_ok {
231 $db->delete();
232} qr/Cannot use an undefined array index/, "DELETE fails on an undefined key";
233
234throws_ok {
235 $db->exists( 'foo' );
236} qr/Cannot use 'foo' as an array index/, "EXISTS fails on an illegal key";
237
238throws_ok {
239 $db->exists();
240} qr/Cannot use an undefined array index/, "EXISTS fails on an undefined key";
241
807f63a7 242# Bug reported by Mike Schilli
243{
244 my ($fh, $filename) = new_fh();
245 my $db = DBM::Deep->new(
246 file => $filename,
247 type => DBM::Deep->TYPE_ARRAY
248 );
249
250 push @{$db}, 1, { foo => 1 };
251 lives_ok {
252 shift @{$db};
253 } "Shift doesn't die moving references around";
254 is( $db->[0]{foo}, 1, "Right hashref there" );
255
256 lives_ok {
257 unshift @{$db}, [ 1 .. 3 ];
258 unshift @{$db}, 1;
259 } "Unshift doesn't die moving references around";
260 is( $db->[1][1], 2, "Right arrayref there" );
261 is( $db->[2]{foo}, 1, "Right hashref there" );
262
263 # Add test for splice moving references around
264 lives_ok {
265 splice @{$db}, 0, 0, 1 .. 3;
266 } "Splice doesn't die moving references around";
267 is( $db->[4][1], 2, "Right arrayref there" );
268 is( $db->[5]{foo}, 1, "Right hashref there" );
269}