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