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