1 | Index: t/pmc/resizableintegerarray.t |
---|
2 | =================================================================== |
---|
3 | --- t/pmc/resizableintegerarray.t (revision 41731) |
---|
4 | +++ t/pmc/resizableintegerarray.t (working copy) |
---|
5 | @@ -1,20 +1,14 @@ |
---|
6 | -#! perl |
---|
7 | -# Copyright (C) 2001-2007, Parrot Foundation. |
---|
8 | +#! parrot |
---|
9 | +# Copyright © 2001-2007, Parrot Foundation. |
---|
10 | # $Id$ |
---|
11 | |
---|
12 | -use strict; |
---|
13 | -use warnings; |
---|
14 | -use lib qw( . lib ../lib ../../lib ); |
---|
15 | -use Test::More; |
---|
16 | -use Parrot::Test tests => 18; |
---|
17 | - |
---|
18 | =head1 NAME |
---|
19 | |
---|
20 | t/pmc/resizableintegerarray.t - ResizableIntegerArray PMC |
---|
21 | |
---|
22 | =head1 SYNOPSIS |
---|
23 | |
---|
24 | - % prove t/pmc/ResizableIntegerArray.t |
---|
25 | + % prove t/pmc/resizableintegerarray.t |
---|
26 | |
---|
27 | =head1 DESCRIPTION |
---|
28 | |
---|
29 | @@ -23,433 +17,539 @@ |
---|
30 | |
---|
31 | =cut |
---|
32 | |
---|
33 | -pasm_output_is( <<'CODE', <<'OUTPUT', "Setting array size" ); |
---|
34 | - new P0, ['ResizableIntegerArray'] |
---|
35 | +=for notes |
---|
36 | |
---|
37 | - set I0,P0 |
---|
38 | - eq I0,0,OK_1 |
---|
39 | - print "not " |
---|
40 | -OK_1: print "ok 1\n" |
---|
41 | +Coverage plan: |
---|
42 | |
---|
43 | - set P0,1 |
---|
44 | - set I0,P0 |
---|
45 | - eq I0,1,OK_2 |
---|
46 | - print "not " |
---|
47 | -OK_2: print "ok 2\n" |
---|
48 | + * Get & Set Size |
---|
49 | |
---|
50 | - set P0,5 |
---|
51 | - set I0,P0 |
---|
52 | - eq I0,5,OK_3 |
---|
53 | - print "not " |
---|
54 | -OK_3: print "ok 3\n" |
---|
55 | + * Get & Set Element |
---|
56 | + * Type of value (int, num, string, pmc) |
---|
57 | + * Type of index (int, pmc) |
---|
58 | + * index negative/in-range/beyond-end |
---|
59 | + * Set doesn't clobber other elements |
---|
60 | |
---|
61 | - set P0,9 |
---|
62 | - set I0,P0 |
---|
63 | - eq I0,9,OK_4 |
---|
64 | - print "not " |
---|
65 | -OK_4: print "ok 4\n" |
---|
66 | + * Push/Unshift, Pop/Shift |
---|
67 | + * Correct values |
---|
68 | + * Correct sequence |
---|
69 | + * Correctly resized |
---|
70 | |
---|
71 | - set P0,7 |
---|
72 | - set I0,P0 |
---|
73 | - eq I0,7,OK_5 |
---|
74 | - print "not " |
---|
75 | -OK_5: print "ok 5\n" |
---|
76 | + * Iterator |
---|
77 | + * Doesn't change array size |
---|
78 | + * Multiple concurrent iterators don't interfere |
---|
79 | |
---|
80 | - end |
---|
81 | -CODE |
---|
82 | -ok 1 |
---|
83 | -ok 2 |
---|
84 | -ok 3 |
---|
85 | -ok 4 |
---|
86 | -ok 5 |
---|
87 | -OUTPUT |
---|
88 | +=cut |
---|
89 | |
---|
90 | -pasm_output_is( <<'CODE', <<'OUTPUT', "Setting first element" ); |
---|
91 | - new P0, ['ResizableIntegerArray'] |
---|
92 | - set P0, 1 |
---|
93 | +.sub main :main |
---|
94 | + .include 'test_more.pir' |
---|
95 | + plan(41) |
---|
96 | |
---|
97 | - set P0[0],-7 |
---|
98 | - set I0,P0[0] |
---|
99 | - eq I0,-7,OK_1 |
---|
100 | - print "not " |
---|
101 | -OK_1: print "ok 1\n" |
---|
102 | + test_does_interfaces() # 4 tests |
---|
103 | |
---|
104 | - set P0[0],3.7 |
---|
105 | - set N0,P0[0] |
---|
106 | - eq N0,3.0,OK_2 |
---|
107 | - print "not " |
---|
108 | -OK_2: print "ok 2\n" |
---|
109 | + test_get_size() # 2 tests |
---|
110 | + test_resize() # 3 tests |
---|
111 | + test_distinct_storage() # 1 test |
---|
112 | |
---|
113 | - set P0[0],"17" |
---|
114 | - set S0,P0[0] |
---|
115 | - eq S0,"17",OK_3 |
---|
116 | - print "not " |
---|
117 | -OK_3: print "ok 3\n" |
---|
118 | + test_cant_set_negative() # 1 test |
---|
119 | + test_cant_get_negative() # 1 test |
---|
120 | + test_set_beyond_end() # 2 test |
---|
121 | + test_get_beyond_end() # 3 tests |
---|
122 | |
---|
123 | - end |
---|
124 | -CODE |
---|
125 | -ok 1 |
---|
126 | -ok 2 |
---|
127 | -ok 3 |
---|
128 | -OUTPUT |
---|
129 | + test_conversion() # 6 tests |
---|
130 | + test_conversion_overflow() # 1 test |
---|
131 | |
---|
132 | -pasm_output_is( <<'CODE', <<'OUTPUT', "Setting second element" ); |
---|
133 | - new P0, ['ResizableIntegerArray'] |
---|
134 | + test_set_pmc_index() # 1 tests |
---|
135 | + test_get_pmc_index() # 1 tests |
---|
136 | |
---|
137 | - set P0[1], -7 |
---|
138 | - set I0, P0[1] |
---|
139 | - eq I0,-7,OK_1 |
---|
140 | - print "not " |
---|
141 | -OK_1: print "ok 1\n" |
---|
142 | + test_push() # 2 tests |
---|
143 | + test_pop() # 2 tests |
---|
144 | + test_pop_many() # 2 test |
---|
145 | + test_push_many() # 2 test |
---|
146 | + test_push_pop() # 1 tests |
---|
147 | + test_cant_pop_empty() # 1 test |
---|
148 | + test_shift() # 2 tests |
---|
149 | + test_unshift() # 2 tests |
---|
150 | + test_iterator() # 1 test |
---|
151 | +.end |
---|
152 | |
---|
153 | - set P0[1], 3.7 |
---|
154 | - set N0, P0[1] |
---|
155 | - eq N0,3.0,OK_2 |
---|
156 | - print "not " |
---|
157 | -OK_2: print "ok 2\n" |
---|
158 | +.sub test_does_interfaces |
---|
159 | + $P0 = new ['ResizableIntegerArray'] |
---|
160 | + ok( 1, 'Instantiated ResizableIntegerArray PMC' ) |
---|
161 | + $I0 = does $P0, 'array' |
---|
162 | + ok( $I0, 'Interface does "array"' ) |
---|
163 | + $I0 = does $P0, 'scalar' |
---|
164 | + is( $I0, 0, 'Interface does not do "scalar"' ) |
---|
165 | + $I0 = does $P0, 'no_interface' |
---|
166 | + is( $I0, 0, 'Interface does not do "no_interface"' ) |
---|
167 | +.end |
---|
168 | |
---|
169 | - set P0[1],"17" |
---|
170 | - set S0, P0[1] |
---|
171 | - eq S0,"17",OK_3 |
---|
172 | - print "not " |
---|
173 | -OK_3: print "ok 3\n" |
---|
174 | +.sub test_get_size |
---|
175 | + $P0 = new ['ResizableIntegerArray'] |
---|
176 | + $I0 = $P0 |
---|
177 | + is( $I0, 0, 'Initial array size is 0' ) |
---|
178 | + $I1 = elements $P0 |
---|
179 | + is( $I0, $I1, '... and "elements" opcode agrees' ) |
---|
180 | +.end |
---|
181 | |
---|
182 | - end |
---|
183 | -CODE |
---|
184 | -ok 1 |
---|
185 | -ok 2 |
---|
186 | -ok 3 |
---|
187 | -OUTPUT |
---|
188 | +.sub test_resize |
---|
189 | + $P0 = new ['ResizableIntegerArray'] |
---|
190 | + $I1 = 0 |
---|
191 | |
---|
192 | -pasm_output_is( <<'CODE', <<'OUTPUT', "Setting negatively indexed elements" ); |
---|
193 | - new P0, ['ResizableIntegerArray'] |
---|
194 | - set P0, 1 |
---|
195 | + $P0 = 1 |
---|
196 | + $I0 = $P0 |
---|
197 | + ne $I0, 1, X1 |
---|
198 | + inc $I1 |
---|
199 | |
---|
200 | + $P0 = 9 |
---|
201 | + $I0 = $P0 |
---|
202 | + ne $I0, 9, X1 |
---|
203 | + inc $I1 |
---|
204 | + |
---|
205 | + $P0 = 5 |
---|
206 | + $I0 = $P0 |
---|
207 | + ne $I0, 5, X1 |
---|
208 | + inc $I1 |
---|
209 | + |
---|
210 | + $P0 = 99999 |
---|
211 | + $I0 = $P0 |
---|
212 | + ne $I0, 99999, X1 |
---|
213 | + inc $I1 |
---|
214 | + |
---|
215 | + $P0 = 0 |
---|
216 | + $I0 = $P0 |
---|
217 | + ne $I0, 0, X1 |
---|
218 | + inc $I1 |
---|
219 | + |
---|
220 | + $P0 = 77 |
---|
221 | + $I0 = $P0 |
---|
222 | + ne $I0, 77, X1 |
---|
223 | + inc $I1 |
---|
224 | + |
---|
225 | +X1: |
---|
226 | + is( $I1, 6, 'Setting array size (four different values, including 0)' ) |
---|
227 | + |
---|
228 | + $I2 = elements $P0 |
---|
229 | + is( $I0, $I2, '... and "elements" opcode still agrees' ) |
---|
230 | + |
---|
231 | + push_eh E |
---|
232 | + $I1 = 1 |
---|
233 | + $P0 = -4 |
---|
234 | + $I1 = 0 |
---|
235 | +E: |
---|
236 | + pop_eh |
---|
237 | + ok( $I1, 'Setting negative size should throw an exception' ) |
---|
238 | +.end |
---|
239 | + |
---|
240 | +.sub test_distinct_storage |
---|
241 | + # Walk the array in pseudo-random order |
---|
242 | + # Pick a sample size $I4 and another number $I2, such that |
---|
243 | + # ân: n > 0 ⧠$I2 â¿ % $I4 = 1 â n % $I4 = 0 |
---|
244 | + $I4 = 17 |
---|
245 | + $I2 = 3 |
---|
246 | + # Create and fill array in random order |
---|
247 | + $P0 = new ['ResizableIntegerArray'] |
---|
248 | + $P0 = $I4 |
---|
249 | +# say '\n ... checking that pseudo-random sequence is exhaustive ...' |
---|
250 | + $I0 = 1 |
---|
251 | +L1: |
---|
252 | +# say $I0 |
---|
253 | + $I0 = mul $I0, $I2 |
---|
254 | + $I0 = mod $I0, $I4 |
---|
255 | + $P0[$I0] = $I0 |
---|
256 | + gt $I0, 1, L1 |
---|
257 | + $P0[0] = 0 |
---|
258 | +# say 0 |
---|
259 | + # Read back array and check values match |
---|
260 | + $I0 = 0 |
---|
261 | +L2: |
---|
262 | + $I1 = $P0[$I0] |
---|
263 | + ne $I1, $I0, X1 |
---|
264 | + inc $I0 |
---|
265 | + lt $I0, $I4, L2 |
---|
266 | +X1: |
---|
267 | + is( $I0, $I4, 'All array elements stored separately' ) |
---|
268 | +.end |
---|
269 | + |
---|
270 | +.sub test_cant_set_negative |
---|
271 | + $P0 = new ['ResizableIntegerArray'] |
---|
272 | + $P0 = 1 |
---|
273 | + $I0 = 1 |
---|
274 | push_eh eh |
---|
275 | - set P0[-1], -7 |
---|
276 | + $P0[-1] = -7 |
---|
277 | + $I0 = 0 |
---|
278 | +eh: |
---|
279 | pop_eh |
---|
280 | - print "no ex\n" |
---|
281 | - end |
---|
282 | + ok( $I0, 'Setting with negative index should throw an exception' ) |
---|
283 | +.end |
---|
284 | + |
---|
285 | +.sub test_cant_get_negative |
---|
286 | + $P0 = new ['ResizableIntegerArray'] |
---|
287 | + $P0 = 1 |
---|
288 | + $I0 = 1 |
---|
289 | + push_eh eh |
---|
290 | + $I0 = $P0[-1] |
---|
291 | + $I0 = 0 |
---|
292 | eh: |
---|
293 | - say "got an ex" |
---|
294 | - end |
---|
295 | -CODE |
---|
296 | -got an ex |
---|
297 | -OUTPUT |
---|
298 | + pop_eh |
---|
299 | + ok( $I0, 'Getting with negative index should throw an exception' ) |
---|
300 | +.end |
---|
301 | |
---|
302 | -pasm_output_is( <<'CODE', <<'OUTPUT', "Getting negatively indexed elements" ); |
---|
303 | - new P0, ['ResizableIntegerArray'] |
---|
304 | - set P0, 1 |
---|
305 | +.sub test_set_beyond_end |
---|
306 | + $P0 = new ['ResizableIntegerArray'] |
---|
307 | + $P0 = 1 |
---|
308 | + $I0 = 0 |
---|
309 | + push_eh eh |
---|
310 | + $P0[1] = -7 |
---|
311 | + $I0 = 1 |
---|
312 | +eh: |
---|
313 | + pop_eh |
---|
314 | + ok( $I0, 'Setting with too-big index should not throw an exception' ) |
---|
315 | |
---|
316 | + $I0 = $P0 |
---|
317 | + is( $I0, 2, '... and should extend array' ) |
---|
318 | +.end |
---|
319 | + |
---|
320 | +.sub test_get_beyond_end |
---|
321 | + $P0 = new ['ResizableIntegerArray'] |
---|
322 | + $P0 = 1 |
---|
323 | + $I0 = 1 |
---|
324 | push_eh eh |
---|
325 | - set I0, P0[-1] |
---|
326 | + $I1 = $P0[1] |
---|
327 | + $I0 = 1 |
---|
328 | +eh: |
---|
329 | pop_eh |
---|
330 | - print "no ex\n" |
---|
331 | - end |
---|
332 | -eh: |
---|
333 | - say "got an ex" |
---|
334 | - end |
---|
335 | -CODE |
---|
336 | -got an ex |
---|
337 | -OUTPUT |
---|
338 | + ok( $I0, 'Getting with too-big index should not throw an exception' ) |
---|
339 | + is( $I1, 0, '... and result should be 0' ) |
---|
340 | |
---|
341 | -pasm_output_is( <<'CODE', <<'OUTPUT', "Setting out-of-bounds elements" ); |
---|
342 | - new P0, ['ResizableIntegerArray'] |
---|
343 | - set P0, 1 |
---|
344 | + $I0 = $P0 |
---|
345 | + is( $I0, 1, '... and should not extend array' ) |
---|
346 | +.end |
---|
347 | |
---|
348 | - set P0[1], -7 |
---|
349 | - print "ok 1\n" |
---|
350 | +.sub test_conversion |
---|
351 | + $P0 = new ['ResizableIntegerArray'] |
---|
352 | + $P0 = 6 |
---|
353 | + $P0[0] = -7 |
---|
354 | + $P0[1] = 3.7 |
---|
355 | + $P0[2] = '17' |
---|
356 | + $P1 = new ['Integer'] |
---|
357 | + $P1 = 123456 |
---|
358 | + $P0[3] = $P1 |
---|
359 | + $P2 = new ['Float'] |
---|
360 | + $P2 = 7.3 |
---|
361 | + $P0[4] = $P2 |
---|
362 | + $P3 = new ['String'] |
---|
363 | + $P3 = '987654321' |
---|
364 | + $P0[5] = $P3 |
---|
365 | + $I0 = $P0[0] |
---|
366 | + is( $I0, -7, 'Setting element to integer' ) |
---|
367 | + $N0 = $P0[1] |
---|
368 | + is( $N0, 3.0, 'Setting element to float (gets truncated)' ) |
---|
369 | + $S0 = $P0[2] |
---|
370 | + is( $S0, '17', 'Setting element to string (gets converted to int and back)' ) |
---|
371 | + $I0 = $P0[3] |
---|
372 | + is( $I0, 123456, 'Setting element to boxed integer' ) |
---|
373 | + $N0 = $P0[4] |
---|
374 | + is( $N0, 7.0, 'Setting element to boxed float (gets truncated)' ) |
---|
375 | + $S0 = $P0[5] |
---|
376 | + is( $S0, '987654321', 'Setting element to boxed string (gets converted to int and back)' ) |
---|
377 | +.end |
---|
378 | |
---|
379 | - end |
---|
380 | -CODE |
---|
381 | -ok 1 |
---|
382 | -OUTPUT |
---|
383 | +.sub test_conversion_overflow |
---|
384 | + $P0 = new ['ResizableIntegerArray'] |
---|
385 | + $P0 = 1 |
---|
386 | |
---|
387 | -pasm_output_is( <<'CODE', <<'OUTPUT', "Getting out-of-bounds elements" ); |
---|
388 | - new P0, ['ResizableIntegerArray'] |
---|
389 | - set P0, 1 |
---|
390 | + $S0 = '12345678901234567890123456789012345678901234567890123456789012345678901234567890' |
---|
391 | |
---|
392 | - set I0, P0[1] |
---|
393 | - print "ok 1\n" |
---|
394 | - end |
---|
395 | -CODE |
---|
396 | -ok 1 |
---|
397 | -OUTPUT |
---|
398 | + push_eh eh0 |
---|
399 | + $I1 = 1 |
---|
400 | + $P0[0] = $S0 |
---|
401 | + $I0 = $P0[0] |
---|
402 | + $I1 = 0 |
---|
403 | +eh0: |
---|
404 | + pop_eh |
---|
405 | + ok( $I1, 'Throw exception when setting element to too-large digit-string' ) |
---|
406 | |
---|
407 | -pasm_output_is( <<"CODE", <<'OUTPUT', "Set via PMC keys, access via INTs" ); |
---|
408 | - .include 'fp_equality.pasm' |
---|
409 | - new P0, ['ResizableIntegerArray'] |
---|
410 | - new P1, ['Key'] |
---|
411 | +.end |
---|
412 | |
---|
413 | - set P1, 0 |
---|
414 | - set P0[P1], 25 |
---|
415 | +.sub test_set_pmc_index |
---|
416 | + $P0 = new ['ResizableIntegerArray'] |
---|
417 | + $P1 = new ['Key'] |
---|
418 | + $P1 = 0 |
---|
419 | + $P0[$P1] = 25 |
---|
420 | + $P1 = 1 |
---|
421 | + $P0[$P1] = 2.5 |
---|
422 | + $P1 = 2 |
---|
423 | + $P0[$P1] = '17' |
---|
424 | |
---|
425 | - set P1, 1 |
---|
426 | - set P0[P1], 2.5 |
---|
427 | + $I1 = 0 |
---|
428 | |
---|
429 | - set P1, 2 |
---|
430 | - set P0[P1], "17" |
---|
431 | + $I0 = $P0[0] |
---|
432 | + ne $I0, 25, X1 |
---|
433 | + inc $I1 |
---|
434 | |
---|
435 | - set I0, P0[0] |
---|
436 | - eq I0, 25, OK1 |
---|
437 | - print "not " |
---|
438 | -OK1: print "ok 1\\n" |
---|
439 | + $N0 = $P0[1] |
---|
440 | + ne $N0, 2.0, X1 |
---|
441 | + inc $I1 |
---|
442 | |
---|
443 | - set N0, P0[1] |
---|
444 | - .fp_eq_pasm(N0, 2.0, OK2) |
---|
445 | - print "not " |
---|
446 | -OK2: print "ok 2\\n" |
---|
447 | + $S0 = $P0[2] |
---|
448 | + ne $S0, '17', X1 |
---|
449 | + inc $I1 |
---|
450 | +X1: |
---|
451 | + is( $I1, 3, 'Setting via PMC key (3 different types)' ) |
---|
452 | +.end |
---|
453 | |
---|
454 | - set S0, P0[2] |
---|
455 | - eq S0, "17", OK3 |
---|
456 | - print "not " |
---|
457 | -OK3: print "ok 3\\n" |
---|
458 | +.sub test_get_pmc_index |
---|
459 | + $P0 = new ['ResizableIntegerArray'] |
---|
460 | + $P0 = 1 |
---|
461 | + $P0[25] = 125 |
---|
462 | + $P0[128] = 10.2 |
---|
463 | + $P0[513] = '17' |
---|
464 | + $P0[1023] = 123456 |
---|
465 | |
---|
466 | - end |
---|
467 | -CODE |
---|
468 | -ok 1 |
---|
469 | -ok 2 |
---|
470 | -ok 3 |
---|
471 | -OUTPUT |
---|
472 | + $I1 = 0 |
---|
473 | |
---|
474 | -pasm_output_is( <<"CODE", <<'OUTPUT', "Set via INTs, access via PMC Keys" ); |
---|
475 | - .include 'fp_equality.pasm' |
---|
476 | - new P0, ['ResizableIntegerArray'] |
---|
477 | - set P0, 1 |
---|
478 | + $P2 = new ['Key'] |
---|
479 | |
---|
480 | - set P0[25], 125 |
---|
481 | - set P0[128], 10.2 |
---|
482 | - set P0[513], "17" |
---|
483 | - new P1, ['Integer'] |
---|
484 | - set P1, 123456 |
---|
485 | - set P0[1023], P1 |
---|
486 | + $P2 = 25 |
---|
487 | + $I0 = $P0[$P2] |
---|
488 | + ne $I0, 125, X1 |
---|
489 | + inc $I1 |
---|
490 | |
---|
491 | - new P2, ['Key'] |
---|
492 | - set P2, 25 |
---|
493 | - set I0, P0[P2] |
---|
494 | - eq I0, 125, OK1 |
---|
495 | - print "not " |
---|
496 | -OK1: print "ok 1\\n" |
---|
497 | + $P2 = 128 |
---|
498 | + $N0 = $P0[$P2] |
---|
499 | + ne $N0, 10.0, X1 |
---|
500 | + inc $I1 |
---|
501 | |
---|
502 | - set P2, 128 |
---|
503 | - set N0, P0[P2] |
---|
504 | - .fp_eq_pasm(N0, 10.0, OK2) |
---|
505 | - print "not " |
---|
506 | -OK2: print "ok 2\\n" |
---|
507 | + $P2 = 513 |
---|
508 | + $S0 = $P0[$P2] |
---|
509 | + ne $S0, '17', X1 |
---|
510 | + inc $I1 |
---|
511 | |
---|
512 | - set P2, 513 |
---|
513 | - set S0, P0[P2] |
---|
514 | - eq S0, "17", OK3 |
---|
515 | - print "not " |
---|
516 | -OK3: print "ok 3\\n" |
---|
517 | + $P2 = 1023 |
---|
518 | + $I2 = $P0[$P2] |
---|
519 | + ne $I2, 123456, X1 |
---|
520 | + inc $I1 |
---|
521 | +X1: |
---|
522 | + is( $I1, 4, 'Getting via PMC key (4 different types)' ) |
---|
523 | +.end |
---|
524 | |
---|
525 | - set P2, 1023 |
---|
526 | - set P3, P0[P2] |
---|
527 | - set I1, P3 |
---|
528 | - eq I1, 123456, OK4 |
---|
529 | - print "not " |
---|
530 | -OK4: print "ok 4\\n" |
---|
531 | +.sub test_push |
---|
532 | + $P0 = new ['ResizableIntegerArray'] |
---|
533 | + $P0[9999] = 0 |
---|
534 | + push $P0, 12345 |
---|
535 | + $I0 = $P0 |
---|
536 | + is( $I0, 10001, 'Push increases number of elements by one' ) |
---|
537 | + $I0 = $P0[10000] |
---|
538 | + is( $I0, 12345, '... and stores correct value' ) |
---|
539 | +.end |
---|
540 | |
---|
541 | - end |
---|
542 | -CODE |
---|
543 | -ok 1 |
---|
544 | -ok 2 |
---|
545 | -ok 3 |
---|
546 | -ok 4 |
---|
547 | -OUTPUT |
---|
548 | +.sub test_pop |
---|
549 | + $P0 = new ['ResizableIntegerArray'] |
---|
550 | + $P0[0] = 4 |
---|
551 | + $P0[1] = 8 |
---|
552 | + $P0[2] = 16 |
---|
553 | + $I0 = $P0 |
---|
554 | + $I0 = pop $P0 |
---|
555 | + is( $I0, 16, 'Pop retrieves correct value' ) |
---|
556 | + $I0 = $P0 |
---|
557 | + is( $I0, 2, '... and reduces number of elements by one' ) |
---|
558 | +.end |
---|
559 | |
---|
560 | -pir_output_is( << 'CODE', << 'OUTPUT', "check whether interface is done" ); |
---|
561 | +.sub test_pop_many |
---|
562 | + $P0 = new ['ResizableIntegerArray'] |
---|
563 | + $I0 = 0 |
---|
564 | +l1: |
---|
565 | + $P0[$I0] = $I0 |
---|
566 | + inc $I0 |
---|
567 | + lt $I0, 100000, l1 |
---|
568 | +l2: |
---|
569 | + le $I0, 0, e2 |
---|
570 | + dec $I0 |
---|
571 | + $I1 = pop $P0 |
---|
572 | + eq $I0, $I1, l2 |
---|
573 | +e2: |
---|
574 | + is( $I0, $I1, 'Pop many times retrieves correct values' ) |
---|
575 | + $I0 = $P0 |
---|
576 | + is( $I0, 0, '... and leaves array empty' ) |
---|
577 | +.end |
---|
578 | |
---|
579 | -.sub test :main |
---|
580 | - .local pmc pmc1 |
---|
581 | - pmc1 = new ['ResizableIntegerArray'] |
---|
582 | - .local int bool1 |
---|
583 | - does bool1, pmc1, "scalar" |
---|
584 | - print bool1 |
---|
585 | - print "\n" |
---|
586 | - does bool1, pmc1, "array" |
---|
587 | - print bool1 |
---|
588 | - print "\n" |
---|
589 | - does bool1, pmc1, "no_interface" |
---|
590 | - print bool1 |
---|
591 | - print "\n" |
---|
592 | - end |
---|
593 | +.sub test_push_many |
---|
594 | + $P0 = new ['ResizableIntegerArray'] |
---|
595 | + $I0 = 0 |
---|
596 | +l1: |
---|
597 | + push $P0, $I0 |
---|
598 | + inc $I0 |
---|
599 | + lt $I0, 100000, l1 |
---|
600 | + $I1 = $P0 |
---|
601 | + is( $I1, 100000, 'Push many values fills array to correct size' ) |
---|
602 | +l2: |
---|
603 | + le $I0, 0, e2 |
---|
604 | + dec $I0 |
---|
605 | + $I1 = $P0[$I0] |
---|
606 | + eq $I0, $I1, l2 |
---|
607 | +e2: |
---|
608 | + is( $I0, $I1, '... and stores correct values') |
---|
609 | .end |
---|
610 | -CODE |
---|
611 | -0 |
---|
612 | -1 |
---|
613 | -0 |
---|
614 | -OUTPUT |
---|
615 | |
---|
616 | -pir_output_is( << 'CODE', << 'OUTPUT', "push integer" ); |
---|
617 | +.sub test_push_pop |
---|
618 | + $P0 = new ['ResizableIntegerArray'] |
---|
619 | + $I1 = 0 |
---|
620 | |
---|
621 | -.sub test :main |
---|
622 | - .local pmc pmc1 |
---|
623 | - pmc1 = new ['ResizableIntegerArray'] |
---|
624 | - pmc1[9999] = 0 |
---|
625 | - push pmc1, 10001 |
---|
626 | - .local int elements |
---|
627 | - elements = pmc1 |
---|
628 | - print elements |
---|
629 | - print "\n" |
---|
630 | - .local string last |
---|
631 | - last = pmc1[10000] |
---|
632 | - print last |
---|
633 | - print "\n" |
---|
634 | - end |
---|
635 | + push $P0, 2 |
---|
636 | + $I0 = $P0 |
---|
637 | + ne $I0, 1, X1 |
---|
638 | + inc $I1 |
---|
639 | + |
---|
640 | + push $P0, 4 |
---|
641 | + $I0 = $P0 |
---|
642 | + ne $I0, 2, X1 |
---|
643 | + inc $I1 |
---|
644 | + |
---|
645 | + push $P0, 6 |
---|
646 | + $I0 = $P0 |
---|
647 | + ne $I0, 3, X1 |
---|
648 | + inc $I1 |
---|
649 | + |
---|
650 | + $I0 = pop $P0 |
---|
651 | + ne $I0, 6, X1 |
---|
652 | + inc $I1 |
---|
653 | + |
---|
654 | + $I0 = $P0 |
---|
655 | + ne $I0, 2, X1 |
---|
656 | + inc $I1 |
---|
657 | + |
---|
658 | + $I0 = pop $P0 |
---|
659 | + ne $I0, 4, X1 |
---|
660 | + inc $I1 |
---|
661 | + |
---|
662 | + $I0 = $P0 |
---|
663 | + ne $I0, 1, X1 |
---|
664 | + inc $I1 |
---|
665 | + |
---|
666 | + $I0 = pop $P0 |
---|
667 | + ne $I0, 2, X1 |
---|
668 | + inc $I1 |
---|
669 | + |
---|
670 | + $I0 = $P0 |
---|
671 | + ne $I0, 0, X1 |
---|
672 | + inc $I1 |
---|
673 | + |
---|
674 | +X1: |
---|
675 | + is( $I1, 9, 'Push-then-Pop retrieves values in reverse order' ) |
---|
676 | .end |
---|
677 | -CODE |
---|
678 | -10001 |
---|
679 | -10001 |
---|
680 | -OUTPUT |
---|
681 | |
---|
682 | -pasm_output_is( <<'CODE', <<'OUTPUT', 'basic pop' ); |
---|
683 | - new P0, ['ResizableIntegerArray'] |
---|
684 | - set P0[0], 4 |
---|
685 | - set P0[1], 8 |
---|
686 | - set P0[2], 16 |
---|
687 | - pop I0, P0 |
---|
688 | - eq I0, 16, OK1 |
---|
689 | - print "not " |
---|
690 | - print I0 |
---|
691 | -OK1: print "ok 1\n" |
---|
692 | +.sub test_cant_pop_empty |
---|
693 | + $P0 = new ['ResizableIntegerArray'] |
---|
694 | + $I0 = 1 |
---|
695 | + push_eh eh |
---|
696 | + $I0 = pop $P0 |
---|
697 | + $I0 = 0 |
---|
698 | +eh: |
---|
699 | + pop_eh |
---|
700 | + ok( $I0, 'Pop from empty array should throw an exception' ) |
---|
701 | +.end |
---|
702 | |
---|
703 | - pop I0, P0 |
---|
704 | - eq I0, 8, OK2 |
---|
705 | - print "not " |
---|
706 | - print I0 |
---|
707 | -OK2: print "ok 2\n" |
---|
708 | +# .sub test_cant_pop_empty |
---|
709 | +# # test_pass( 'pop from empty array should throw exception' ) |
---|
710 | +# throws_like( <<'CODE', 'Can\'t pop from an empty array!', 'pop from empty array should throw exception' ) |
---|
711 | +# .sub main |
---|
712 | +# $P0 = new ['ResizableIntegerArray'] |
---|
713 | +# $I0 = pop $P0 |
---|
714 | +# .end |
---|
715 | +# CODE |
---|
716 | +# # test_test( 'pop from empty array should throw exception' ) |
---|
717 | +# .end |
---|
718 | |
---|
719 | - pop I0, P0 |
---|
720 | - eq I0, 4, OK3 |
---|
721 | - print "not " |
---|
722 | - print I0 |
---|
723 | -OK3: print "ok 3\n" |
---|
724 | - end |
---|
725 | -CODE |
---|
726 | -ok 1 |
---|
727 | -ok 2 |
---|
728 | -ok 3 |
---|
729 | -OUTPUT |
---|
730 | +.sub test_shift |
---|
731 | + $P0 = new ['ResizableIntegerArray'] |
---|
732 | + $P0[0] = 10 |
---|
733 | + $P0[1] = 20 |
---|
734 | |
---|
735 | -pasm_output_is( <<'CODE', <<'OUTPUT', 'pop many values' ); |
---|
736 | - new P0, ['ResizableIntegerArray'] |
---|
737 | - set I0, 0 |
---|
738 | -L1: set P0[I0], I0 |
---|
739 | - inc I0 |
---|
740 | - lt I0, 100000, L1 |
---|
741 | + $I1 = 0 |
---|
742 | |
---|
743 | -L2: dec I0 |
---|
744 | - pop I1, P0 |
---|
745 | - eq I0, I1, OK |
---|
746 | - branch NOT_OK |
---|
747 | -OK: gt I0, 0, L2 |
---|
748 | - print "ok\n" |
---|
749 | - end |
---|
750 | + $I0 = $P0 |
---|
751 | + ne $I0, 2, X1 |
---|
752 | + inc $I1 |
---|
753 | |
---|
754 | -NOT_OK: |
---|
755 | - print I0 |
---|
756 | - print "\n" |
---|
757 | - print I1 |
---|
758 | - print "\n" |
---|
759 | - end |
---|
760 | -CODE |
---|
761 | -ok |
---|
762 | -OUTPUT |
---|
763 | + $I0 = shift $P0 |
---|
764 | + ne $I0, 10, X1 |
---|
765 | + inc $I1 |
---|
766 | |
---|
767 | -pasm_output_is( <<'CODE', <<'OUTPUT', 'push/pop' ); |
---|
768 | - new P0, ['ResizableIntegerArray'] |
---|
769 | - push P0, 2 |
---|
770 | - push P0, 4 |
---|
771 | - push P0, 6 |
---|
772 | - pop I0, P0 |
---|
773 | - eq I0, 6, OK1 |
---|
774 | - print "not " |
---|
775 | -OK1: print "ok 1\n" |
---|
776 | - end |
---|
777 | -CODE |
---|
778 | -ok 1 |
---|
779 | -OUTPUT |
---|
780 | + $I0 = $P0 |
---|
781 | + ne $I0, 1, X1 |
---|
782 | + inc $I1 |
---|
783 | |
---|
784 | -pasm_error_output_like( <<'CODE', <<'OUTPUT', 'pop from empty array' ); |
---|
785 | - new P0, ['ResizableIntegerArray'] |
---|
786 | - pop I0, P0 |
---|
787 | - end |
---|
788 | -CODE |
---|
789 | -/ResizableIntegerArray: Can't pop from an empty array!/ |
---|
790 | -OUTPUT |
---|
791 | + $I0 = shift $P0 |
---|
792 | + ne $I0, 20, X1 |
---|
793 | + inc $I1 |
---|
794 | |
---|
795 | -#' |
---|
796 | -pir_output_is( << 'CODE', << 'OUTPUT', "shift integer" ); |
---|
797 | -.sub test :main |
---|
798 | - .local pmc ar |
---|
799 | - ar = new ['ResizableIntegerArray'] |
---|
800 | - ar[0] = 10 |
---|
801 | - ar[1] = 20 |
---|
802 | - $I0 = elements ar |
---|
803 | - print $I0 |
---|
804 | - print ' ' |
---|
805 | - $I0 = shift ar |
---|
806 | - print $I0 |
---|
807 | - print ' ' |
---|
808 | - $I0 = elements ar |
---|
809 | - print $I0 |
---|
810 | - print ' ' |
---|
811 | - $I0 = shift ar |
---|
812 | - print $I0 |
---|
813 | - print ' ' |
---|
814 | - $I0 = elements ar |
---|
815 | - say $I0 |
---|
816 | +X1: |
---|
817 | + is( $I1, 4, 'Shift returns values in correct order' ) |
---|
818 | + |
---|
819 | + $I0 = $P0 |
---|
820 | + is( $I0, 0, '... and removes correct number of elements' ) |
---|
821 | .end |
---|
822 | -CODE |
---|
823 | -2 10 1 20 0 |
---|
824 | -OUTPUT |
---|
825 | |
---|
826 | -pir_output_is( << 'CODE', << 'OUTPUT', "unshift integer" ); |
---|
827 | -.sub test :main |
---|
828 | - .local pmc ar |
---|
829 | - ar = new ['ResizableIntegerArray'] |
---|
830 | - unshift ar, 10 |
---|
831 | - unshift ar, 20 |
---|
832 | - $I0 = elements ar |
---|
833 | - print $I0 |
---|
834 | - print ' ' |
---|
835 | - $I0 = ar[0] |
---|
836 | - print $I0 |
---|
837 | - print ' ' |
---|
838 | - $I0 = ar[1] |
---|
839 | - say $I0 |
---|
840 | +.sub test_unshift |
---|
841 | + $P0 = new ['ResizableIntegerArray'] |
---|
842 | + unshift $P0, 10 |
---|
843 | + unshift $P0, 20 |
---|
844 | + |
---|
845 | + $I0 = $P0 |
---|
846 | + is( $I0, 2, 'Unshift adds correct number of elements' ) |
---|
847 | + |
---|
848 | + $I1 = 0 |
---|
849 | + |
---|
850 | + $I0 = $P0[0] |
---|
851 | + ne $I0, 20, X1 |
---|
852 | + inc $I1 |
---|
853 | + $I0 = $P0[1] |
---|
854 | + ne $I0, 10, X1 |
---|
855 | + inc $I1 |
---|
856 | + |
---|
857 | +X1: |
---|
858 | + is( $I1, 2, '... and stores values in correct order' ) |
---|
859 | .end |
---|
860 | -CODE |
---|
861 | -2 20 10 |
---|
862 | -OUTPUT |
---|
863 | |
---|
864 | -pir_output_is( <<'CODE', <<'OUTPUT', "get_iter" ); |
---|
865 | -.sub 'main' :main |
---|
866 | +.sub test_iterator |
---|
867 | $P0 = new ['ResizableIntegerArray'] |
---|
868 | + push_eh k0 |
---|
869 | $P0[0] = 42 |
---|
870 | $P0[1] = 43 |
---|
871 | $P0[2] = 44 |
---|
872 | push $P0, 999 |
---|
873 | + $I0 = 0 |
---|
874 | $P1 = iter $P0 |
---|
875 | -loop: |
---|
876 | - unless $P1 goto loop_end |
---|
877 | - $S2 = shift $P1 |
---|
878 | - say $S2 |
---|
879 | - goto loop |
---|
880 | -loop_end: |
---|
881 | + $I2 = shift $P1 |
---|
882 | + inc $I0 |
---|
883 | + eq $I2, 42, k3 |
---|
884 | + dec $I0 |
---|
885 | + say 'Missing 42' |
---|
886 | +k3: |
---|
887 | + $I2 = shift $P1 |
---|
888 | + inc $I0 |
---|
889 | + eq $I2, 43, k2 |
---|
890 | + dec $I0 |
---|
891 | + say 'Missing 43' |
---|
892 | +k2: |
---|
893 | + $I2 = shift $P1 |
---|
894 | + inc $I0 |
---|
895 | + eq $I2, 44, k1 |
---|
896 | + dec $I0 |
---|
897 | + say 'Missing 44' |
---|
898 | +k1: |
---|
899 | + $I2 = shift $P1 |
---|
900 | + inc $I0 |
---|
901 | + eq $I2, 999, k0 |
---|
902 | + dec $I0 |
---|
903 | + say 'Missing 999' |
---|
904 | +k0: |
---|
905 | + pop_eh |
---|
906 | + is( $I0, 4, 'get_iter: iterator returns all values in correct sequence' ) |
---|
907 | .end |
---|
908 | -CODE |
---|
909 | -42 |
---|
910 | -43 |
---|
911 | -44 |
---|
912 | -999 |
---|
913 | -OUTPUT |
---|
914 | |
---|
915 | - |
---|
916 | # Local Variables: |
---|
917 | -# mode: cperl |
---|
918 | -# cperl-indent-level: 4 |
---|
919 | +# mode: pir |
---|
920 | # fill-column: 100 |
---|
921 | # End: |
---|
922 | -# vim: expandtab shiftwidth=4: |
---|
923 | +# vim: expandtab shiftwidth=4 ft=pir: |
---|