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