-
Notifications
You must be signed in to change notification settings - Fork 138
/
resizableintegerarray.t
614 lines (521 loc) · 11.6 KB
/
resizableintegerarray.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
#!./parrot
# Copyright (C) 2001-2009, Parrot Foundation.
=head1 NAME
t/pmc/resizableintegerarray.t - Tests for the ResizableIntegerArray PMC
=head1 SYNOPSIS
% prove t/pmc/resizableintegerarray.t
=head1 DESCRIPTION
This tests the C<ResizableIntegerArray> PMC. It checks size, sets various
elements, including out-of-bounds test as well as INT and PMC keys.
=cut
=for notes
Coverage plan:
* Get & Set Size
* Get & Set Element
* Type of value (int, num, string, pmc)
* Type of index (int, pmc)
* index negative/in-range/beyond-end
* Set doesn't clobber other elements
* Delete
* Push/Unshift, Pop/Shift
* Correct values
* Correct sequence
* Correctly resized
* Iterator
* Doesn't change array size
* Multiple concurrent iterators don't interfere
=cut
.sub main :main
.include 'test_more.pir'
plan(47)
test_does_interfaces()
test_get_size()
test_resize()
test_distinct_storage()
test_cant_set_negative()
test_cant_get_negative()
test_set_beyond_end()
test_get_beyond_end()
test_delete()
test_conversion()
test_conversion_overflow()
test_set_pmc_index()
test_get_pmc_index()
test_push()
test_pop()
test_pop_many()
test_push_many()
test_push_pop()
test_cant_pop_empty()
test_shift()
test_unshift()
test_cant_shift_empty()
test_iterator()
test_clone()
test_freeze()
.end
.sub test_does_interfaces
$P0 = new ['ResizableIntegerArray']
ok( 1, 'Instantiated ResizableIntegerArray PMC' )
$I0 = does $P0, 'array'
ok( $I0, 'Interface does "array"' )
$I0 = does $P0, 'scalar'
is( $I0, 0, 'Interface does not do "scalar"' )
$I0 = does $P0, 'no_interface'
is( $I0, 0, 'Interface does not do "no_interface"' )
.end
.sub test_get_size
$P0 = new ['ResizableIntegerArray']
$I0 = $P0
is( $I0, 0, 'Initial array size is 0' )
$I1 = elements $P0
is( $I0, $I1, '... and "elements" opcode agrees' )
.end
.sub test_resize
$P0 = new ['ResizableIntegerArray']
$I1 = 0
$P0 = 1
$I0 = $P0
ne $I0, 1, X1
inc $I1
$P0 = 9
$I0 = $P0
ne $I0, 9, X1
inc $I1
$P0 = 5
$I0 = $P0
ne $I0, 5, X1
inc $I1
$P0 = 99999
$I0 = $P0
ne $I0, 99999, X1
inc $I1
$P0 = 0
$I0 = $P0
ne $I0, 0, X1
inc $I1
$P0 = 77
$I0 = $P0
ne $I0, 77, X1
inc $I1
X1:
is( $I1, 6, 'Setting array size (four different values, including 0)' )
$I2 = elements $P0
is( $I0, $I2, '... and "elements" opcode still agrees' )
push_eh E
$I1 = 1
$P0 = -4
$I1 = 0
E:
pop_eh
ok( $I1, 'Setting negative size should throw an exception' )
.end
.sub test_distinct_storage
# Walk the array in pseudo-random order
# Pick a sample size $I4 and another number $I2, such that
############################################################
##### Plase rewrite this with ascii chars, it got unreadable
##### by editing with mixed charsets.
# n: n > 0 $I2 % $I4 = 1 n % $I4 = 0
############################################################
$I4 = 17
$I2 = 3
# Create and fill array in random order
$P0 = new ['ResizableIntegerArray']
$P0 = $I4
# say '\n ... checking that pseudo-random sequence is exhaustive ...'
$I0 = 1
L1:
# say $I0
$I0 = mul $I0, $I2
$I0 = mod $I0, $I4
$P0[$I0] = $I0
gt $I0, 1, L1
$P0[0] = 0
# say 0
# Read back array and check values match
$I0 = 0
L2:
$I1 = $P0[$I0]
ne $I1, $I0, X1
inc $I0
lt $I0, $I4, L2
X1:
is( $I0, $I4, 'All array elements stored separately' )
.end
.sub test_cant_set_negative
$P0 = new ['ResizableIntegerArray']
$P0 = 1
$I0 = 1
push_eh eh
$P0[-1] = -7
$I0 = 0
eh:
pop_eh
ok( $I0, 'Setting with negative index should throw an exception' )
.end
.sub test_cant_get_negative
$P0 = new ['ResizableIntegerArray']
$P0 = 1
$I0 = 1
push_eh eh
$I0 = $P0[-1]
$I0 = 0
eh:
pop_eh
ok( $I0, 'Getting with negative index should throw an exception' )
.end
.sub test_set_beyond_end
$P0 = new ['ResizableIntegerArray']
$P0 = 1
$I0 = 0
push_eh eh
$P0[1] = -7
$I0 = 1
eh:
pop_eh
ok( $I0, 'Setting with too-big index should not throw an exception' )
$I0 = $P0
is( $I0, 2, '... and should extend array' )
.end
.sub test_get_beyond_end
$P0 = new ['ResizableIntegerArray']
$P0 = 1
$I0 = 1
push_eh eh
$I1 = $P0[1]
$I0 = 1
eh:
pop_eh
ok( $I0, 'Getting with too-big index should not throw an exception' )
is( $I1, 0, '... and result should be 0' )
$I0 = $P0
is( $I0, 1, '... and should not extend array' )
.end
.sub test_delete
$P0 = new ['ResizableIntegerArray'], 3
$P0[0] = 9
$P0[1] = 8
$P0[2] = 7
delete $P0[1]
$I0 = elements $P0
is( $I0, 2, 'delete one element dec size')
$I0 = $P0[1]
is( $I0, 7, 'deleted move back the remaining part')
push_eh caught
delete $P0[2]
pop_eh
ok(0, 'delete ouf of bound should throw')
goto end
caught:
pop_eh
ok(1, 'delete ouf of bound throws')
end:
.end
.sub test_conversion
$P0 = new ['ResizableIntegerArray']
$P0 = 6
$P0[0] = -7
$P0[1] = 3.7
$P0[2] = '17'
$P1 = new ['Integer']
$P1 = 123456
$P0[3] = $P1
$P2 = new ['Float']
$P2 = 7.3
$P0[4] = $P2
$P3 = new ['String']
$P3 = '987654321'
$P0[5] = $P3
$I0 = $P0[0]
is( $I0, -7, 'Setting element to integer' )
$N0 = $P0[1]
is( $N0, 3.0, 'Setting element to float (gets truncated)' )
$S0 = $P0[2]
is( $S0, '17', 'Setting element to string (gets converted to int and back)' )
$I0 = $P0[3]
is( $I0, 123456, 'Setting element to boxed integer' )
$N0 = $P0[4]
is( $N0, 7.0, 'Setting element to boxed float (gets truncated)' )
$S0 = $P0[5]
is( $S0, '987654321', 'Setting element to boxed string (gets converted to int and back)' )
.end
.sub test_conversion_overflow
$P0 = new ['ResizableIntegerArray']
$P0 = 1
$S0 = '12345678901234567890123456789012345678901234567890123456789012345678901234567890'
push_eh eh0
$I1 = 1
$P0[0] = $S0
$I0 = $P0[0]
$I1 = 0
eh0:
pop_eh
ok( $I1, 'Throw exception when setting element to too-large digit-string' )
.end
.sub test_set_pmc_index
$P0 = new ['ResizableIntegerArray']
$P1 = new ['Key']
$P1 = 0
$P0[$P1] = 25
$P1 = 1
$P0[$P1] = 2.5
$P1 = 2
$P0[$P1] = '17'
$I1 = 0
$I0 = $P0[0]
ne $I0, 25, X1
inc $I1
$N0 = $P0[1]
ne $N0, 2.0, X1
inc $I1
$S0 = $P0[2]
ne $S0, '17', X1
inc $I1
X1:
is( $I1, 3, 'Setting via PMC key (3 different types)' )
.end
.sub test_get_pmc_index
$P0 = new ['ResizableIntegerArray']
$P0 = 1
$P0[25] = 125
$P0[128] = 10.2
$P0[513] = '17'
$P0[1023] = 123456
$I1 = 0
$P2 = new ['Key']
$P2 = 25
$I0 = $P0[$P2]
ne $I0, 125, X1
inc $I1
$P2 = 128
$N0 = $P0[$P2]
ne $N0, 10.0, X1
inc $I1
$P2 = 513
$S0 = $P0[$P2]
ne $S0, '17', X1
inc $I1
$P2 = 1023
$I2 = $P0[$P2]
ne $I2, 123456, X1
inc $I1
X1:
is( $I1, 4, 'Getting via PMC key (4 different types)' )
.end
.sub test_push
$P0 = new ['ResizableIntegerArray']
$P0[9999] = 0
push $P0, 12345
$I0 = $P0
is( $I0, 10001, 'Push increases number of elements by one' )
$I0 = $P0[10000]
is( $I0, 12345, '... and stores correct value' )
.end
.sub test_pop
$P0 = new ['ResizableIntegerArray']
$P0[0] = 4
$P0[1] = 8
$P0[2] = 16
$I0 = $P0
$I0 = pop $P0
is( $I0, 16, 'Pop retrieves correct value' )
$I0 = $P0
is( $I0, 2, '... and reduces number of elements by one' )
.end
.sub test_pop_many
$P0 = new ['ResizableIntegerArray']
$I0 = 0
l1:
$P0[$I0] = $I0
inc $I0
lt $I0, 100000, l1
l2:
le $I0, 0, e2
dec $I0
$I1 = pop $P0
eq $I0, $I1, l2
e2:
is( $I0, $I1, 'Pop many times retrieves correct values' )
$I0 = $P0
is( $I0, 0, '... and leaves array empty' )
.end
.sub test_push_many
$P0 = new ['ResizableIntegerArray']
$I0 = 0
l1:
push $P0, $I0
inc $I0
lt $I0, 100000, l1
$I1 = $P0
is( $I1, 100000, 'Push many values fills array to correct size' )
l2:
le $I0, 0, e2
dec $I0
$I1 = $P0[$I0]
eq $I0, $I1, l2
e2:
is( $I0, $I1, '... and stores correct values')
.end
.sub test_push_pop
$P0 = new ['ResizableIntegerArray']
$I1 = 0
push $P0, 2
$I0 = $P0
ne $I0, 1, X1
inc $I1
push $P0, 4
$I0 = $P0
ne $I0, 2, X1
inc $I1
push $P0, 6
$I0 = $P0
ne $I0, 3, X1
inc $I1
$I0 = pop $P0
ne $I0, 6, X1
inc $I1
$I0 = $P0
ne $I0, 2, X1
inc $I1
$I0 = pop $P0
ne $I0, 4, X1
inc $I1
$I0 = $P0
ne $I0, 1, X1
inc $I1
$I0 = pop $P0
ne $I0, 2, X1
inc $I1
$I0 = $P0
ne $I0, 0, X1
inc $I1
X1:
is( $I1, 9, 'Push-then-Pop retrieves values in reverse order' )
.end
.sub test_cant_pop_empty
$P0 = new ['ResizableIntegerArray']
$I0 = 1
push_eh eh
$I0 = pop $P0
$I0 = 0
eh:
pop_eh
ok( $I0, 'Pop from empty array should throw an exception' )
.end
# .sub test_cant_pop_empty
# # test_pass( 'pop from empty array should throw exception' )
# throws_like( <<'CODE', 'Can\'t pop from an empty array!', 'pop from empty array should throw exception' )
# .sub main
# $P0 = new ['ResizableIntegerArray']
# $I0 = pop $P0
# .end
# CODE
# # test_test( 'pop from empty array should throw exception' )
# .end
.sub test_shift
$P0 = new ['ResizableIntegerArray']
$P0[0] = 10
$P0[1] = 20
$I1 = 0
$I0 = $P0
ne $I0, 2, X1
inc $I1
$I0 = shift $P0
ne $I0, 10, X1
inc $I1
$I0 = $P0
ne $I0, 1, X1
inc $I1
$I0 = shift $P0
ne $I0, 20, X1
inc $I1
X1:
is( $I1, 4, 'Shift returns values in correct order' )
$I0 = $P0
is( $I0, 0, '... and removes correct number of elements' )
.end
.sub test_unshift
$P0 = new ['ResizableIntegerArray']
unshift $P0, 10
unshift $P0, 20
$I0 = $P0
is( $I0, 2, 'Unshift adds correct number of elements' )
$I1 = 0
$I0 = $P0[0]
ne $I0, 20, X1
inc $I1
$I0 = $P0[1]
ne $I0, 10, X1
inc $I1
X1:
is( $I1, 2, '... and stores values in correct order' )
.end
.sub test_cant_shift_empty
$P0 = new ['ResizableIntegerArray']
$I0 = 1
push_eh eh
$I0 = shift $P0
$I0 = 0
eh:
pop_eh
ok( $I0, 'Shift from empty array should throw an exception' )
.end
.sub test_iterator
$P0 = new ['ResizableIntegerArray']
push_eh k0
$P0[0] = 42
$P0[1] = 43
$P0[2] = 44
push $P0, 999
$I0 = 0
$P1 = iter $P0
$I2 = shift $P1
inc $I0
eq $I2, 42, k3
dec $I0
say 'Missing 42'
k3:
$I2 = shift $P1
inc $I0
eq $I2, 43, k2
dec $I0
say 'Missing 43'
k2:
$I2 = shift $P1
inc $I0
eq $I2, 44, k1
dec $I0
say 'Missing 44'
k1:
$I2 = shift $P1
inc $I0
eq $I2, 999, k0
dec $I0
say 'Missing 999'
k0:
pop_eh
is( $I0, 4, 'get_iter: iterator returns all values in correct sequence' )
.end
.sub test_clone
$P0 = new ['ResizableIntegerArray']
push $P0, 1
$P1 = clone $P0
$I0 = iseq $P0, $P1
is( $I0, 1, 'cloned is equal to original')
.end
.sub test_freeze
.local pmc ria, th
.local string s
ria = new ['ResizableIntegerArray']
push ria, 1
push ria, 0x1FFFF
s = freeze ria
th = thaw s
is( ria, th, 'freeze/thaw copy is equal to original' )
.end
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: