Added fix for RT#17947 and fixed a bug from last checkin
[dbsrgits/DBM-Deep.git] / t / 04_array.t
CommitLineData
ffed8b01 1##
2# DBM::Deep Test
3##
4use strict;
53fdf3d7 5use Test::More tests => 107;
ffed8b01 6use Test::Exception;
2a81bf9e 7use File::Temp qw( tempfile tempdir );
ffed8b01 8
9use_ok( 'DBM::Deep' );
10
2a81bf9e 11my $dir = tempdir( CLEANUP => 1 );
12my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
ffed8b01 13my $db = DBM::Deep->new(
2a81bf9e 14 file => $filename,
ffed8b01 15 type => DBM::Deep->TYPE_ARRAY
16);
ffed8b01 17
18TODO: {
19 local $TODO = "How is this test ever supposed to pass?";
20 ok( !$db->clear, "If the file has never been written to, clear() returns false" );
21}
22
23##
24# basic put/get/push
25##
26$db->[0] = "elem1";
27$db->push( "elem2" );
28$db->put(2, "elem3");
29$db->store(3, "elem4");
30$db->unshift("elem0");
31
32is( $db->[0], 'elem0', "Array get for shift works" );
33is( $db->[1], 'elem1', "Array get for array set works" );
34is( $db->[2], 'elem2', "Array get for push() works" );
35is( $db->[3], 'elem3', "Array get for put() works" );
36is( $db->[4], 'elem4', "Array get for store() works" );
37
38is( $db->get(0), 'elem0', "get() for shift() works" );
39is( $db->get(1), 'elem1', "get() for array set works" );
40is( $db->get(2), 'elem2', "get() for push() works" );
41is( $db->get(3), 'elem3', "get() for put() works" );
42is( $db->get(4), 'elem4', "get() for store() works" );
43
44is( $db->fetch(0), 'elem0', "fetch() for shift() works" );
45is( $db->fetch(1), 'elem1', "fetch() for array set works" );
46is( $db->fetch(2), 'elem2', "fetch() for push() works" );
47is( $db->fetch(3), 'elem3', "fetch() for put() works" );
48is( $db->fetch(4), 'elem4', "fetch() for store() works" );
49
50is( $db->length, 5, "... and we have five elements" );
51
7f441181 52is( $db->[-1], $db->[4], "-1st index is 4th index" );
53is( $db->[-2], $db->[3], "-2nd index is 3rd index" );
54is( $db->[-3], $db->[2], "-3rd index is 2nd index" );
55is( $db->[-4], $db->[1], "-4th index is 1st index" );
56is( $db->[-5], $db->[0], "-5th index is 0th index" );
c9cec40e 57
58# This is for Perls older than 5.8.0 because of is()'s prototype
59{ my $v = $db->[-6]; is( $v, undef, "-6th index is undef" ); }
60
ffed8b01 61is( $db->length, 5, "... and we have five elements after abortive -6 index lookup" );
62
cb79ec85 63$db->[-1] = 'elem4.1';
64is( $db->[-1], 'elem4.1' );
65is( $db->[4], 'elem4.1' );
66is( $db->get(4), 'elem4.1' );
67is( $db->fetch(4), 'elem4.1' );
68
baa27ab6 69throws_ok {
70 $db->[-6] = 'whoops!';
71} qr/Modification of non-creatable array value attempted, subscript -6/, "Correct error thrown";
72
ffed8b01 73my $popped = $db->pop;
74is( $db->length, 4, "... and we have four after popping" );
75is( $db->[0], 'elem0', "0th element still there after popping" );
76is( $db->[1], 'elem1', "1st element still there after popping" );
77is( $db->[2], 'elem2', "2nd element still there after popping" );
78is( $db->[3], 'elem3', "3rd element still there after popping" );
cb79ec85 79is( $popped, 'elem4.1', "Popped value is correct" );
ffed8b01 80
81my $shifted = $db->shift;
82is( $db->length, 3, "... and we have three after shifting" );
83is( $db->[0], 'elem1', "0th element still there after shifting" );
84is( $db->[1], 'elem2', "1st element still there after shifting" );
85is( $db->[2], 'elem3', "2nd element still there after shifting" );
86is( $shifted, 'elem0', "Shifted value is correct" );
87
88##
89# delete
90##
91my $deleted = $db->delete(0);
92is( $db->length, 3, "... and we still have three after deleting" );
93is( $db->[0], undef, "0th element now undef" );
94is( $db->[1], 'elem2', "1st element still there after deleting" );
95is( $db->[2], 'elem3', "2nd element still there after deleting" );
81d3d316 96is( $deleted, 'elem1', "Deleted value is correct" );
ffed8b01 97
98is( $db->delete(99), undef, 'delete on an element not in the array returns undef' );
99is( $db->length, 3, "... and we still have three after a delete on an out-of-range index" );
100
101is( delete $db->[99], undef, 'DELETE on an element not in the array returns undef' );
102is( $db->length, 3, "... and we still have three after a DELETE on an out-of-range index" );
103
104is( $db->delete(-99), undef, 'delete on an element (neg) not in the array returns undef' );
105is( $db->length, 3, "... and we still have three after a DELETE on an out-of-range negative index" );
106
107is( delete $db->[-99], undef, 'DELETE on an element (neg) not in the array returns undef' );
108is( $db->length, 3, "... and we still have three after a DELETE on an out-of-range negative index" );
109
110$deleted = $db->delete(-2);
111is( $db->length, 3, "... and we still have three after deleting" );
112is( $db->[0], undef, "0th element still undef" );
9281d66b 113is( $db->[1], undef, "1st element now undef" );
ffed8b01 114is( $db->[2], 'elem3', "2nd element still there after deleting" );
9281d66b 115is( $deleted, 'elem2', "Deleted value is correct" );
ffed8b01 116
117$db->[1] = 'elem2';
118
119##
120# exists
121##
122ok( $db->exists(1), "The 1st value exists" );
123ok( !$db->exists(0), "The 0th value doesn't exists" );
124ok( !$db->exists(22), "The 22nd value doesn't exists" );
baa27ab6 125ok( $db->exists(-1), "The -1st value does exists" );
ffed8b01 126ok( !$db->exists(-22), "The -22nd value doesn't exists" );
127
128##
129# clear
130##
131ok( $db->clear(), "clear() returns true if the file was ever non-empty" );
132is( $db->length(), 0, "After clear(), no more elements" );
133
134is( $db->pop, undef, "pop on an empty array returns undef" );
135is( $db->length(), 0, "After pop() on empty array, length is still 0" );
136
137is( $db->shift, undef, "shift on an empty array returns undef" );
138is( $db->length(), 0, "After shift() on empty array, length is still 0" );
139
8f6d6ed0 140is( $db->unshift( 1, 2, 3 ), 3, "unshift returns the number of elements in the array" );
141is( $db->unshift( 1, 2, 3 ), 6, "unshift returns the number of elements in the array" );
142is( $db->push( 1, 2, 3 ), 9, "push returns the number of elements in the array" );
143
ffed8b01 144is( $db->length(), 9, "After unshift and push on empty array, length is now 9" );
145
146$db->clear;
147
148##
149# multi-push
150##
151$db->push( 'elem first', "elem middle", "elem last" );
152is( $db->length, 3, "3-element push results in three elements" );
153is($db->[0], "elem first", "First element is 'elem first'");
154is($db->[1], "elem middle", "Second element is 'elem middle'");
155is($db->[2], "elem last", "Third element is 'elem last'");
156
157##
158# splice with length 1
159##
8fec41b9 160my @returned = $db->splice( 1, 1, "middle A", "middle B" );
161is( scalar(@returned), 1, "One element was removed" );
162is( $returned[0], 'elem middle', "... and it was correctly removed" );
ffed8b01 163is($db->length(), 4);
164is($db->[0], "elem first");
165is($db->[1], "middle A");
166is($db->[2], "middle B");
167is($db->[3], "elem last");
168
169##
170# splice with length of 0
171##
8fec41b9 172@returned = $db->splice( -1, 0, "middle C" );
173is( scalar(@returned), 0, "No elements were removed" );
ffed8b01 174is($db->length(), 5);
175is($db->[0], "elem first");
176is($db->[1], "middle A");
177is($db->[2], "middle B");
178is($db->[3], "middle C");
179is($db->[4], "elem last");
180
181##
182# splice with length of 3
183##
8fec41b9 184my $returned = $db->splice( 1, 3, "middle ABC" );
185is( $returned, 'middle C', "Just the last element was returned" );
ffed8b01 186is($db->length(), 3);
187is($db->[0], "elem first");
188is($db->[1], "middle ABC");
189is($db->[2], "elem last");
190
53fdf3d7 191@returned = $db->splice( 1 );
192is($db->length(), 1);
193is($db->[0], "elem first");
194is($returned[0], "middle ABC");
195is($returned[1], "elem last");
196
197$db->push( @returned );
198
199@returned = $db->splice( 1, -1 );
200is($db->length(), 2);
201is($db->[0], "elem first");
202is($db->[1], "elem last");
203is($returned[0], "middle ABC");
204
ffed8b01 205$db->[0] = [ 1 .. 3 ];
206$db->[1] = { a => 'foo' };
ffed8b01 207is( $db->[1]->fetch('a'), 'foo', "Reuse of same space with hash successful" );
994ccd8e 208is( $db->[0]->length, 3, "Reuse of same space with array successful" );