[cage] Fix pgegrep, which was merely an innocent bystander in the Great Namespace...
[parrot.git] / t / pmc / fixedpmcarray.t
blob23ac712c8ce613eb8eca9a00531f9fc1c1aedec0
1 #! parrot
2 # Copyright (C) 2001-2009, Parrot Foundation.
3 # $Id$
5 =head1 NAME
7 t/pmc/fixedpmcarray.t - FixedPMCArray PMC
9 =head1 SYNOPSIS
11     % prove t/pmc/fixedpmcarray.t
13 =head1 DESCRIPTION
15 Tests C<FixedPMCArray> PMC. Checks size, sets various elements, including
16 out-of-bounds test. Checks INT and PMC keys.
18 =cut
20 .sub main :main
21     .include 'test_more.pir'
22     plan(76)
23     test_setting_array_size()
24     test_assign_from_another()
25     test_assign_self()
26     test_assign_non_array()
27     test_resize_exception()
28     test_truthiness()
29     test_tt991()
30     test_setting_first_elem()
31     test_setting_second_elem()
32     test_negative_index()
33     test_oob_elem()
34     test_set_pmc_keys_access_ints()
35     test_set_ints_access_pmc_keys()
36     test_interface()
37     test_get_uninitialized()
38     test_get_null_elem()
39     test_definedness()
40     test_splice_oob()
41     test_get_repr()
42     test_elements()
43     test_equality()
44     test_multi_keys()
45     test_splice()
46     test_sort()
47     test_exists()
48 .end
50 .sub test_exists
51     .local pmc fpa
52     fpa = new ['FixedPMCArray']
53     fpa = 5
54     $I0 = exists fpa[3]
55     nok($I0,'FixedPMCArray element existence')
56     fpa[2] = 42
57     $I0 = exists fpa[2]
58     ok($I0,'FixedPMCArray element existence')
60     new $P1, ['Key']
61     set $P1, 0
62     fpa[$P1] = 99
63     $I0 = exists fpa[$P1]
64     ok($I0,'FixedPMCArray element existence')
65 .end
67 .sub test_sort
68      .local pmc compares, cmp_fun
69      # TT #1317 doesnt work wit prederef of JIT
70      bounds 1
71      compares = new ['Integer']
72      compares = 0
73      set_global "compares", compares
74      cmp_fun = get_global "cmp_fun"
75      sort_ar()
76      sort_ar(cmp_fun)
77 .end
79 # this is used by test_sort
80 .sub sort_ar
81     .param pmc cmp_fun :optional
82     .local pmc compares
83     compares = get_global "compares"
84     compares = 0
85     .local pmc array
86     array = new ['FixedPMCArray']
87     array = 5
88     array[0] = 10
89     array[1] = 2
90     array[2] = 5
91     array[3] = 9
92     array[4] = 1
93     array."sort"(cmp_fun)
94     ok(1,'call sort on FixedPMCArray')
96     .local pmc test1
97     test1 = new ['FixedPMCArray']
98     test1 = 5
99     test1[0] = 1
100     test1[1] = 2
101     test1[2] = 5
102     test1[3] = 9
103     test1[4] = 10
105     is_deeply( array, test1 )
107 .end
109 # this is used by test_sort
110 .sub cmp_fun
111     .param pmc a
112     .param pmc b
113     $I0 = cmp a, b
114     .local pmc compares
115     compares = get_global "compares"
116     inc compares
117     .begin_return
118     .set_return $I0
119     .end_return
120 .end
122 .sub test_splice
123     .local pmc one
124     .local pmc test1, test2, test3
125     one = new ['Integer']
126     one = 1
128     .local pmc fpa
129     fpa = new ['FixedPMCArray']
130     fpa = 5
132     splice fpa, one, 0, 5
133     test1 = new ['FixedPMCArray']
134     test1 = 5
135     test1[0] = 1
136     test1[1] = 1
137     test1[2] = 1
138     test1[3] = 1
139     test1[4] = 1
141     is_deeply(fpa, test1 )
143     .local pmc two
144     two = new ['Integer']
145     two = 2
147     splice fpa, two, 1, 3
148     test2 = new ['FixedPMCArray']
149     test2 = 5
150     test2[0] = 1
151     test2[1] = 2
152     test2[2] = 2
153     test2[3] = 2
154     test2[4] = 1
155     is_deeply(fpa, test2 )
157     .local pmc three
158     three = new ['Integer']
159     three = 3
161     splice fpa, three, 2, 3
162     test3 = new ['FixedPMCArray']
163     test3 = 5
164     test3[0] = 1
165     test3[1] = 2
166     test3[2] = 3
167     test3[3] = 3
168     test3[4] = 3
169     is_deeply(fpa, test3 )
170 .end
172 .sub test_multi_keys
173     .local pmc    matrix, row
174     .local pmc    elem_in_pmc
175     .local pmc    elem_out_pmc
176     .local int    elem_out_int
177     .local num    elem_out_num
178     .local string elem_out_string
180     matrix = new ['FixedPMCArray']
181     matrix = 1
182     row = new ['FixedPMCArray']
183     row = 4           # set the size by assigning an integer, number or pmc
184     matrix[0] = row
185     matrix[0;0] = 128
186     matrix[0;1] = 128.128
187     elem_in_pmc = new ['Integer']
188     elem_in_pmc = 256
189     matrix[0;2] = elem_in_pmc
190     matrix[0;3] = "asdf"
192     elem_out_int = matrix[0;0]
193     is(elem_out_int,128)
195     elem_out_pmc = matrix[0;0]
196     is(elem_out_pmc,128)
198     elem_out_num = matrix[0;0]
199     is(elem_out_num,128)
201     elem_out_string = matrix[0;0]
202     is(elem_out_string,128)
204     elem_out_pmc = matrix[0;1]
205     is(elem_out_pmc,"128.128")
207     elem_out_num = matrix[0;1]
208     is(elem_out_num,"128.128")
210     elem_out_string = matrix[0;1]
211     is(elem_out_string,"128.128")
213     elem_out_int = matrix[0;2]
214     is(elem_out_int,256)
216     elem_out_pmc = matrix[0;2]
217     is(elem_out_pmc,256)
219     elem_out_num = matrix[0;2]
220     is(elem_out_num,256)
222     elem_out_string = matrix[0;2]
223     is(elem_out_string,256)
225     elem_out_int = matrix[0;0]
226     is(elem_out_int,128)
228     elem_out_pmc = matrix[0;0]
229     is(elem_out_pmc,128)
231     elem_out_num = matrix[0;0]
232     is(elem_out_num,128)
234     elem_out_string = matrix[0;0]
235     is(elem_out_string,128)
237 .end
239 .sub test_equality
240     .local pmc fpa1, fpa2, p1, p2
241     .local int i
242     fpa1 = new ['FixedPMCArray']
243     fpa2 = new ['FixedPMCArray']
245     is(fpa1,fpa2)
247     fpa1 = 3
248     isnt(fpa1,fpa2)
250     fpa2 = 3
252     p1 = new ['String']
253     p1 = "foobarx"
254     p2 = new ['String']
255     p2 = "foobarx"
257     fpa1[0] = p1
258     fpa2[0] = p2
260     is(fpa1,fpa2)
262     p1 = new ['String']
263     p2 = new ['String']
264     p1 = ''
265     p2 = ''
267     fpa1[1] = p1
269     isnt(fpa1,fpa2)
271     fpa2[1] = p2
273     is(fpa1,fpa2)
275 .end
277 .sub test_elements
278     .local pmc arr1
279     .local int elems_i
280     .local num elems_f
281     arr1 = new ['FixedPMCArray']
282     arr1 = 0
283     elems_i = elements arr1
284     is(elems_i,0)
286     elems_i = arr1
287     is(elems_i,0)
289     elems_f = arr1
290     is(elems_f,0)
292     arr1 = new ['FixedPMCArray']
293     arr1 = 2048
294     elems_i = elements arr1
295     is(elems_i,2048)
297     elems_i = arr1
298     is(elems_i,2048)
300     elems_f = arr1
301     is(elems_f,2048)
302 .end
304 .sub test_get_repr
305     .local string s, aux
306     s = get_repr_fpa_n(0)
307     aux = get_repr_fpa_n(1)
308     concat s, aux
309     aux = get_repr_fpa_n(2)
310     concat s, aux
311     aux = get_repr_fpa_n(3)
312     concat s, aux
313     substring(s,'()(0)(0, 1)(0, 1, 2)','get_repr')
314 .end
316 .sub get_repr_fpa_n
317     .param int n
318     .local int i
319     .local pmc fpa, p
320     .local string s
321     fpa = new ['FixedPMCArray']
322     fpa = n
323     i = 0
324 next:
325     if i == n goto done
326     p = box i
327     fpa[i] = p
328     inc i
329     goto next
330 done:
331     s = get_repr fpa
332     .return(s)
333 .end
335 .sub test_splice_oob
336     throws_substring(<<'CODE','FixedPMCArray: index out of bounds','splice oob, offset 0')
337     .sub main
338         .local pmc fpa
339         fpa = new ['FixedPMCArray']
340         fpa = 5
342         .local pmc nil
343         nil = new ['Undef']
345         splice fpa, nil, 0, 6
346     .end
347 CODE
348     throws_substring(<<'CODE','FixedPMCArray: index out of bounds','splice oob, big offset')
349     .sub main
350         .local pmc fpa
351         fpa = new ['FixedPMCArray']
352         fpa = 5
354         .local pmc nil
355         nil = new ['Undef']
357         splice fpa, nil, 6, 0
358     .end
359 CODE
360 .end
362 .sub test_definedness
363     .local pmc arr1
364     arr1 = new ['FixedPMCArray']
365     arr1 = 2005
366     .local int defined_elem_1956
367     defined_elem_1956 = defined arr1[1956]
368     is(defined_elem_1956,0,'definedness')
369     arr1[1956] = 42
370     defined_elem_1956 = defined arr1[1956]
371     is(defined_elem_1956,1,'definedness')
372     .local pmc val
373     null val
374     arr1[1956] = val
375     defined_elem_1956 = defined arr1[1956]
376     is(defined_elem_1956,0,'definedness')
377 .end
379 .sub test_get_null_elem
380   .local pmc arr1, n
381   .local int i
382   .local string s
383   arr1 = new ['FixedPMCArray']
384   arr1 = 1
385   arr1[0] = n
386   i = arr1[0]
387   is(i,0,'null int is 0')
388   s = arr1[0]
389   is(s,"",'null string is empty string')
390 .end
392 .sub test_get_uninitialized
393     throws_substring(<<'CODE','Null PMC access in name','get uninitialized')
394     .sub main
395         .local pmc arr1
396         arr1 = new ['FixedPMCArray']
397         arr1 = 2005
398         .local pmc elem_1956
399         elem_1956 = arr1[1956]
400         .local string type_1956
401         type_1956 = typeof elem_1956
402         print type_1956
403     .end
404 CODE
405 .end
407 .sub test_interface
408     .local pmc pmc1
409     pmc1 = new ['FixedPMCArray']
410     .local int bool1
411     does bool1, pmc1, "scalar"
412     nok(bool1,'FixedPMCArray does not scalar')
413     does bool1, pmc1, "array"
414     ok(bool1,'FixedPMCArray does array')
415     does bool1, pmc1, "no_interface"
416     nok(bool1,'no interface')
417 .end
418 .sub test_set_ints_access_pmc_keys
419      new $P0, ['FixedPMCArray']
420      set $P0, 1024
422      set $P0[25], 125
423      set $P0[128], 10.2
424      set $P0[513], "cow"
425      new $P1, ['Integer']
426      set $P1, 123456
427      set $P0[1023], $P1
429      new $P2, ['Key']
430      set $P2, 25
431      set $I0, $P0[$P2]
432      is($I0, 125,'got int with pmc key')
434      set $P2, 128
435      set $N0, $P0[$P2]
436      is($N0,10.2,'got float with pmc key',0.00001)
438      set $P2, 513
439      set $S0, $P0[$P2]
440      is($S0, "cow", 'got string with pmc key')
442      set $P2, 1023
443      set $P3, $P0[$P2]
444      set $I1, $P3
445      is($I1, 123456, 'got another int with pmc key')
446 .end
448 .sub test_set_pmc_keys_access_ints
449      new $P0, ['FixedPMCArray']
450      set $P0, 3
451      new $P1, ['Key']
453      set $P1, 0
454      set $P0[$P1], 25
456      set $P1, 1
457      set $P0[$P1], 2.5
459      set $P1, 2
460      set $P0[$P1], "bleep"
462      set $I0, $P0[0]
463      is($I0, 25,'got integer with int lookup')
464      set $N0, $P0[1]
465      is($N0,2.5,'got float with int lookup',0.00001)
467      set $S0, $P0[2]
468      is($S0, "bleep",'got string with int lookup')
469 .end
471 .sub test_oob_elem
472     throws_substring(<<'CODE','FixedPMCArray: index out of bounds!','set out-of-bounds index')
473         .sub main
474             new $P0, ['FixedPMCArray']
475             set $P0, 1
476             set $P0[1], -7
477         .end
478 CODE
479     throws_substring(<<'CODE','FixedPMCArray: index out of bounds!','set out-of-bounds index')
480         .sub main
481             new $P0, ['FixedPMCArray']
482             set $P0, 1
483             set $I0, $P0[1]
484         .end
485 CODE
487 .end
489 .sub test_negative_index
490     throws_substring(<<'CODE','FixedPMCArray: index out of bounds!','set negative index')
491 .sub main
492     new $P0, ['FixedPMCArray']
493     set $P0, 1
494     set $P0[-1], -7
495 .end
496 CODE
497     throws_substring(<<'CODE','FixedPMCArray: index out of bounds!','get negative index')
498 .sub main
499     new $P0, ['FixedPMCArray']
500     set $P0, 1
501     set $I0, $P0[-1]
502 .end
503 CODE
505 .end
507 .sub test_setting_second_elem
508     new $P0, ['FixedPMCArray']
509     set $P0, 2
511     set $P0[1],-7
512     set $I0,$P0[1]
513     is($I0,-7,'set second elem to int')
515     set $P0[1],3.7
516     set $N0,$P0[1]
517     is($N0,3.7,'set second elem to float')
519     set $P0[1],"muwhahaha"
520     set $S0,$P0[1]
521     is($S0,"muwhahaha",'set second elem to string')
522 .end
524 .sub test_setting_first_elem
525     new $P0, ['FixedPMCArray']
526     set $P0, 1
528     set $P0[0],-7
529     set $I0,$P0[0]
530     is($I0,-7,'set first elem to int')
532     set $P0[0],3.7
533     set $N0,$P0[0]
534     is($N0,3.7,'set first elem to float')
536     set $P0[0],"muwhahaha"
537     set $S0,$P0[0]
538     is($S0,"muwhahaha",'set first elem to string')
539 .end
541 .sub test_truthiness
542     new $P0, ['FixedPMCArray']
543     set $P0, 0
544     nok($P0,'length 0 FixedPMCArray is falsey')
545     set $P0, 1
546     ok($P0, 'length 1 FixedPMCArray is truthy')
547 .end
549 .sub test_tt991
550     throws_substring(<<'CODE','FixedPMCArray: Cannot set array size to a negative number','cannot create a negative length array')
551         .sub main
552             new $P0, ['FixedPMCArray']
553             set $P0, -1
554         .end
555 CODE
556 .end
558 .sub test_resize_exception
559     throws_substring(<<'CODE',"FixedPMCArray: Can't resize",'cannot resize FixedPMCArray')
560         .sub main
561             new $P0, ['FixedPMCArray']
562             set $I0,$P0
563             set $P0,1
564             set $P0,2
565         .end
566 CODE
568     throws_substring(<<'CODE',"set_number_native() not implemented in class 'FixedPMCArray'", 'cannot use float as length to FixedPMCArray')
569         .sub main
570             new $P0, ['FixedPMCArray']
571             set $P0, 42.0
572         .end
573 CODE
575     throws_substring(<<'CODE',"set_string_native() not implemented in class 'FixedPMCArray'", 'cannot use string as length to FixedPMCArray')
576         .sub main
577             new $P0, ['FixedPMCArray']
578             set $P0,"GIGO"
579         .end
580 CODE
581 .end
583 .sub test_assign_non_array
584     throws_substring(<<'CODE', "Can't set self from this type",'assign from non-array')
585     .sub main
586         .local pmc arr, other
587         .local int n
588         arr = new ['FixedPMCArray']
589         other = new ['Integer']
590         assign arr, other
591     .end
592 CODE
593 .end
595 .sub test_assign_self
596     .local pmc arr
597     arr = new ['FixedPMCArray']
598     assign arr, arr
599     ok(1, 'Can assign FixedPMCArray to itself')
600 .end
602 .sub test_assign_from_another
603     .local pmc arr1, arr2
604     .local int n
605     arr1 = new ['FixedPMCArray']
606     arr1 = 32
607     arr2 = new ['FixedPMCArray']
608     arr2 = 15
609     assign arr1, arr2
610     n = arr1
611     is(n,15,'assigning to FixedPMCArray from another FixedPMCArray')
612 .end
614 .sub test_setting_array_size
615     new $P0, ['FixedPMCArray']
617     set $I0, $P0
618     is($I0,0,'size of new FixedPMCArray is 0')
620     set $P0, 1
621     set $I0, $P0
623     is($I0,1,'size of FixedPMCArray is 1')
624 .end
626 # Local Variables:
627 #   mode: cperl
628 #   cperl-indent-level: 4
629 #   fill-column: 100
630 # End:
631 # vim: expandtab shiftwidth=4: