[t][TT#1507] Add tests for VTABLE_init_int with a key constant
[parrot.git] / t / pmc / fixedpmcarray.t
blobf89a09627b8de8bace8bdcdefbbd36cb7de7ae3a
1 #! parrot
2 # Copyright (C) 2001-2010, 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(80)
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_tt1039()
31     test_setting_first_elem()
32     test_setting_second_elem()
33     test_negative_index()
34     test_oob_elem()
35     test_set_pmc_keys_access_ints()
36     test_set_ints_access_pmc_keys()
37     test_interface()
38     test_get_uninitialized()
39     test_get_null_elem()
40     test_definedness()
41     test_splice_oob()
42     test_get_repr()
43     test_elements()
44     test_equality()
45     test_multi_keys()
46     test_splice()
47     test_sort()
48     test_exists()
49     test_new_style_init()
50 .end
52 .sub test_exists
53     .local pmc fpa
54     fpa = new ['FixedPMCArray']
55     fpa = 5
56     $I0 = exists fpa[3]
57     nok($I0,'FixedPMCArray element existence')
58     fpa[2] = 42
59     $I0 = exists fpa[2]
60     ok($I0,'FixedPMCArray element existence')
62     new $P1, ['Key']
63     set $P1, 0
64     fpa[$P1] = 99
65     $I0 = exists fpa[$P1]
66     ok($I0,'FixedPMCArray element existence')
67 .end
69 .sub test_sort
70      .local pmc compares, cmp_fun
71      # TT #1317 doesnt work wit prederef of JIT
72      bounds 1
73      compares = new ['Integer']
74      compares = 0
75      set_global "compares", compares
76      cmp_fun = get_global "cmp_fun"
77      sort_ar()
78      sort_ar(cmp_fun)
79 .end
81 # this is used by test_sort
82 .sub sort_ar
83     .param pmc cmp_fun :optional
84     .local pmc compares
85     compares = get_global "compares"
86     compares = 0
87     .local pmc array
88     array = new ['FixedPMCArray']
89     array = 5
90     array[0] = 10
91     array[1] = 2
92     array[2] = 5
93     array[3] = 9
94     array[4] = 1
95     array."sort"(cmp_fun)
96     ok(1,'call sort on FixedPMCArray')
98     .local pmc test1
99     test1 = new ['FixedPMCArray']
100     test1 = 5
101     test1[0] = 1
102     test1[1] = 2
103     test1[2] = 5
104     test1[3] = 9
105     test1[4] = 10
107     is_deeply( array, test1 )
109 .end
111 # this is used by test_sort
112 .sub cmp_fun
113     .param pmc a
114     .param pmc b
115     $I0 = cmp a, b
116     .local pmc compares
117     compares = get_global "compares"
118     inc compares
119     .begin_return
120     .set_return $I0
121     .end_return
122 .end
124 .sub test_splice
125     .local pmc one
126     .local pmc test1, test2, test3
127     one = new ['Integer']
128     one = 1
130     .local pmc fpa
131     fpa = new ['FixedPMCArray']
132     fpa = 5
134     splice fpa, one, 0, 5
135     test1 = new ['FixedPMCArray']
136     test1 = 5
137     test1[0] = 1
138     test1[1] = 1
139     test1[2] = 1
140     test1[3] = 1
141     test1[4] = 1
143     is_deeply(fpa, test1 )
145     .local pmc two
146     two = new ['Integer']
147     two = 2
149     splice fpa, two, 1, 3
150     test2 = new ['FixedPMCArray']
151     test2 = 5
152     test2[0] = 1
153     test2[1] = 2
154     test2[2] = 2
155     test2[3] = 2
156     test2[4] = 1
157     is_deeply(fpa, test2 )
159     .local pmc three
160     three = new ['Integer']
161     three = 3
163     splice fpa, three, 2, 3
164     test3 = new ['FixedPMCArray']
165     test3 = 5
166     test3[0] = 1
167     test3[1] = 2
168     test3[2] = 3
169     test3[3] = 3
170     test3[4] = 3
171     is_deeply(fpa, test3 )
172 .end
174 .sub test_multi_keys
175     .local pmc    matrix, row
176     .local pmc    elem_in_pmc
177     .local pmc    elem_out_pmc
178     .local int    elem_out_int
179     .local num    elem_out_num
180     .local string elem_out_string
182     matrix = new ['FixedPMCArray']
183     matrix = 1
184     row = new ['FixedPMCArray']
185     row = 4           # set the size by assigning an integer, number or pmc
186     matrix[0] = row
187     matrix[0;0] = 128
188     matrix[0;1] = 128.128
189     elem_in_pmc = new ['Integer']
190     elem_in_pmc = 256
191     matrix[0;2] = elem_in_pmc
192     matrix[0;3] = "asdf"
194     elem_out_int = matrix[0;0]
195     is(elem_out_int,128)
197     elem_out_pmc = matrix[0;0]
198     is(elem_out_pmc,128)
200     elem_out_num = matrix[0;0]
201     is(elem_out_num,128)
203     elem_out_string = matrix[0;0]
204     is(elem_out_string,128)
206     elem_out_pmc = matrix[0;1]
207     is(elem_out_pmc,"128.128")
209     elem_out_num = matrix[0;1]
210     is(elem_out_num,"128.128")
212     elem_out_string = matrix[0;1]
213     is(elem_out_string,"128.128")
215     elem_out_int = matrix[0;2]
216     is(elem_out_int,256)
218     elem_out_pmc = matrix[0;2]
219     is(elem_out_pmc,256)
221     elem_out_num = matrix[0;2]
222     is(elem_out_num,256)
224     elem_out_string = matrix[0;2]
225     is(elem_out_string,256)
227     elem_out_int = matrix[0;0]
228     is(elem_out_int,128)
230     elem_out_pmc = matrix[0;0]
231     is(elem_out_pmc,128)
233     elem_out_num = matrix[0;0]
234     is(elem_out_num,128)
236     elem_out_string = matrix[0;0]
237     is(elem_out_string,128)
239 .end
241 .sub test_equality
242     .local pmc fpa1, fpa2, p1, p2
243     .local int i
244     fpa1 = new ['FixedPMCArray']
245     fpa2 = new ['FixedPMCArray']
247     is(fpa1,fpa2)
249     fpa1 = 3
250     isnt(fpa1,fpa2)
252     fpa2 = 3
254     p1 = new ['String']
255     p1 = "foobarx"
256     p2 = new ['String']
257     p2 = "foobarx"
259     fpa1[0] = p1
260     fpa2[0] = p2
262     is(fpa1,fpa2)
264     p1 = new ['String']
265     p2 = new ['String']
266     p1 = ''
267     p2 = ''
269     fpa1[1] = p1
271     isnt(fpa1,fpa2)
273     fpa2[1] = p2
275     is(fpa1,fpa2)
277 .end
279 .sub test_elements
280     .local pmc arr1
281     .local int elems_i
282     .local num elems_f
283     arr1 = new ['FixedPMCArray']
284     arr1 = 0
285     elems_i = elements arr1
286     is(elems_i,0)
288     elems_i = arr1
289     is(elems_i,0)
291     elems_f = arr1
292     is(elems_f,0)
294     arr1 = new ['FixedPMCArray']
295     arr1 = 2048
296     elems_i = elements arr1
297     is(elems_i,2048)
299     elems_i = arr1
300     is(elems_i,2048)
302     elems_f = arr1
303     is(elems_f,2048)
304 .end
306 .sub test_get_repr
307     .local string s, aux
308     s = get_repr_fpa_n(0)
309     aux = get_repr_fpa_n(1)
310     concat s, aux
311     aux = get_repr_fpa_n(2)
312     concat s, aux
313     aux = get_repr_fpa_n(3)
314     concat s, aux
315     substring(s,'()(0)(0, 1)(0, 1, 2)','get_repr')
316 .end
318 .sub get_repr_fpa_n
319     .param int n
320     .local int i
321     .local pmc fpa, p
322     .local string s
323     fpa = new ['FixedPMCArray']
324     fpa = n
325     i = 0
326 next:
327     if i == n goto done
328     p = box i
329     fpa[i] = p
330     inc i
331     goto next
332 done:
333     s = get_repr fpa
334     .return(s)
335 .end
337 .sub test_splice_oob
338     throws_substring(<<'CODE','FixedPMCArray: index out of bounds','splice oob, offset 0')
339     .sub main
340         .local pmc fpa
341         fpa = new ['FixedPMCArray']
342         fpa = 5
344         .local pmc nil
345         nil = new ['Undef']
347         splice fpa, nil, 0, 6
348     .end
349 CODE
350     throws_substring(<<'CODE','FixedPMCArray: index out of bounds','splice oob, big offset')
351     .sub main
352         .local pmc fpa
353         fpa = new ['FixedPMCArray']
354         fpa = 5
356         .local pmc nil
357         nil = new ['Undef']
359         splice fpa, nil, 6, 0
360     .end
361 CODE
362 .end
364 .sub test_definedness
365     .local pmc arr1
366     arr1 = new ['FixedPMCArray']
367     arr1 = 2005
368     .local int defined_elem_1956
369     defined_elem_1956 = defined arr1[1956]
370     is(defined_elem_1956,0,'definedness')
371     arr1[1956] = 42
372     defined_elem_1956 = defined arr1[1956]
373     is(defined_elem_1956,1,'definedness')
374     .local pmc val
375     null val
376     arr1[1956] = val
377     defined_elem_1956 = defined arr1[1956]
378     is(defined_elem_1956,0,'definedness')
379 .end
381 .sub test_get_null_elem
382   .local pmc arr1, n
383   .local int i
384   .local string s
385   arr1 = new ['FixedPMCArray']
386   arr1 = 1
387   arr1[0] = n
388   i = arr1[0]
389   is(i,0,'null int is 0')
390   s = arr1[0]
391   is(s,"",'null string is empty string')
392 .end
394 .sub test_get_uninitialized
395     throws_substring(<<'CODE','Null PMC access in name','get uninitialized')
396     .sub main
397         .local pmc arr1
398         arr1 = new ['FixedPMCArray']
399         arr1 = 2005
400         .local pmc elem_1956
401         elem_1956 = arr1[1956]
402         .local string type_1956
403         type_1956 = typeof elem_1956
404         print type_1956
405     .end
406 CODE
407 .end
409 .sub test_interface
410     .local pmc pmc1
411     pmc1 = new ['FixedPMCArray']
412     .local int bool1
413     does bool1, pmc1, "scalar"
414     nok(bool1,'FixedPMCArray does not scalar')
415     does bool1, pmc1, "array"
416     ok(bool1,'FixedPMCArray does array')
417     does bool1, pmc1, "no_interface"
418     nok(bool1,'no interface')
419 .end
420 .sub test_set_ints_access_pmc_keys
421      new $P0, ['FixedPMCArray']
422      set $P0, 1024
424      set $P0[25], 125
425      set $P0[128], 10.2
426      set $P0[513], "cow"
427      new $P1, ['Integer']
428      set $P1, 123456
429      set $P0[1023], $P1
431      new $P2, ['Key']
432      set $P2, 25
433      set $I0, $P0[$P2]
434      is($I0, 125,'got int with pmc key')
436      set $P2, 128
437      set $N0, $P0[$P2]
438      is($N0,10.2,'got float with pmc key',0.00001)
440      set $P2, 513
441      set $S0, $P0[$P2]
442      is($S0, "cow", 'got string with pmc key')
444      set $P2, 1023
445      set $P3, $P0[$P2]
446      set $I1, $P3
447      is($I1, 123456, 'got another int with pmc key')
448 .end
450 .sub test_set_pmc_keys_access_ints
451      new $P0, ['FixedPMCArray']
452      set $P0, 3
453      new $P1, ['Key']
455      set $P1, 0
456      set $P0[$P1], 25
458      set $P1, 1
459      set $P0[$P1], 2.5
461      set $P1, 2
462      set $P0[$P1], "bleep"
464      set $I0, $P0[0]
465      is($I0, 25,'got integer with int lookup')
466      set $N0, $P0[1]
467      is($N0,2.5,'got float with int lookup',0.00001)
469      set $S0, $P0[2]
470      is($S0, "bleep",'got string with int lookup')
471 .end
473 .sub test_oob_elem
474     throws_substring(<<'CODE','FixedPMCArray: index out of bounds!','set out-of-bounds index')
475         .sub main
476             new $P0, ['FixedPMCArray']
477             set $P0, 1
478             set $P0[1], -7
479         .end
480 CODE
481     throws_substring(<<'CODE','FixedPMCArray: index out of bounds!','set out-of-bounds index')
482         .sub main
483             new $P0, ['FixedPMCArray']
484             set $P0, 1
485             set $I0, $P0[1]
486         .end
487 CODE
489 .end
491 .sub test_negative_index
492     throws_substring(<<'CODE','FixedPMCArray: index out of bounds!','set negative index')
493 .sub main
494     new $P0, ['FixedPMCArray']
495     set $P0, 1
496     set $P0[-1], -7
497 .end
498 CODE
499     throws_substring(<<'CODE','FixedPMCArray: index out of bounds!','get negative index')
500 .sub main
501     new $P0, ['FixedPMCArray']
502     set $P0, 1
503     set $I0, $P0[-1]
504 .end
505 CODE
507 .end
509 .sub test_setting_second_elem
510     new $P0, ['FixedPMCArray']
511     set $P0, 2
513     set $P0[1],-7
514     set $I0,$P0[1]
515     is($I0,-7,'set second elem to int')
517     set $P0[1],3.7
518     set $N0,$P0[1]
519     is($N0,3.7,'set second elem to float')
521     set $P0[1],"muwhahaha"
522     set $S0,$P0[1]
523     is($S0,"muwhahaha",'set second elem to string')
524 .end
526 .sub test_setting_first_elem
527     new $P0, ['FixedPMCArray']
528     set $P0, 1
530     set $P0[0],-7
531     set $I0,$P0[0]
532     is($I0,-7,'set first elem to int')
534     set $P0[0],3.7
535     set $N0,$P0[0]
536     is($N0,3.7,'set first elem to float')
538     set $P0[0],"muwhahaha"
539     set $S0,$P0[0]
540     is($S0,"muwhahaha",'set first elem to string')
541 .end
543 .sub test_truthiness
544     new $P0, ['FixedPMCArray']
545     set $P0, 0
546     nok($P0,'length 0 FixedPMCArray is falsey')
547     set $P0, 1
548     ok($P0, 'length 1 FixedPMCArray is truthy')
549 .end
551 .sub test_tt991
552     throws_substring(<<'CODE','FixedPMCArray: Cannot set array size to a negative number','cannot create a negative length array')
553         .sub main
554             new $P0, ['FixedPMCArray']
555             set $P0, -1
556         .end
557 CODE
558 .end
560 .sub test_tt1039
561     .local pmc arr
562     arr = new 'FixedPMCArray'
563     arr = 4
564     arr[0] = 'just'
565     arr[1] = 'another'
566     arr[2] = 'perl'
567     arr[3] = 'hacker'
569     .local pmc sorted_arr
570     sorted_arr = new 'FixedPMCArray'
571     sorted_arr = 4
572     sorted_arr[0] = 'another'
573     sorted_arr[1] = 'hacker'
574     sorted_arr[2] = 'just'
575     sorted_arr[3] = 'perl'
577     $P0 = get_global 'cmpfn1'
578     $P1 = clone arr
579     $P1.'sort'($P0)
580     is_deeply($P1, sorted_arr, 'fpa.sort called with normal Sub')
582     $P0 = get_global 'cmpfn2'
583     $P1 = clone arr
584     $P1.'sort'($P0)
585     is_deeply($P1, sorted_arr, 'fpa.sort called with MultiSub')
586 .end
588 .sub 'cmpfn1'
589     .param pmc a
590     .param pmc b
591     $I0 = cmp_str a, b
592     .return ($I0)
593 .end
595 .sub 'cmpfn2' :multi(_, _)
596     .param pmc a
597     .param pmc b
598     $I0 = cmp_str a, b
599     .return ($I0)
600 .end
602 .sub test_resize_exception
603     throws_substring(<<'CODE',"FixedPMCArray: Can't resize",'cannot resize FixedPMCArray')
604         .sub main
605             new $P0, ['FixedPMCArray']
606             set $I0,$P0
607             set $P0,1
608             set $P0,2
609         .end
610 CODE
612     throws_substring(<<'CODE',"set_number_native() not implemented in class 'FixedPMCArray'", 'cannot use float as length to FixedPMCArray')
613         .sub main
614             new $P0, ['FixedPMCArray']
615             set $P0, 42.0
616         .end
617 CODE
619     throws_substring(<<'CODE',"set_string_native() not implemented in class 'FixedPMCArray'", 'cannot use string as length to FixedPMCArray')
620         .sub main
621             new $P0, ['FixedPMCArray']
622             set $P0,"GIGO"
623         .end
624 CODE
625 .end
627 .sub test_assign_non_array
628     throws_substring(<<'CODE', "Can't set self from this type",'assign from non-array')
629     .sub main
630         .local pmc arr, other
631         .local int n
632         arr = new ['FixedPMCArray']
633         other = new ['Integer']
634         assign arr, other
635     .end
636 CODE
637 .end
639 .sub test_assign_self
640     .local pmc arr
641     arr = new ['FixedPMCArray']
642     assign arr, arr
643     ok(1, 'Can assign FixedPMCArray to itself')
644 .end
646 .sub test_assign_from_another
647     .local pmc arr1, arr2
648     .local int n
649     arr1 = new ['FixedPMCArray']
650     arr1 = 32
651     arr2 = new ['FixedPMCArray']
652     arr2 = 15
653     assign arr1, arr2
654     n = arr1
655     is(n,15,'assigning to FixedPMCArray from another FixedPMCArray')
656 .end
658 .sub test_setting_array_size
659     new $P0, ['FixedPMCArray']
661     set $I0, $P0
662     is($I0,0,'size of new FixedPMCArray is 0')
664     set $P0, 1
665     set $I0, $P0
667     is($I0,1,'size of FixedPMCArray is 1')
668 .end
670 .sub 'test_new_style_init'
671     $P0 = new 'FixedPMCArray', 10
673     $I0 = $P0
674     is($I0, 10, "New style init creates the correct # of elements")
676     $P0 = new ['FixedPMCArray'], 10
678     $I0 = $P0
679     is($I0, 10, "New style init creates the correct # of elements for a key constant")
680 .end
682 # Local Variables:
683 #   mode: pir
684 #   fill-column: 100
685 # End:
686 # vim: expandtab shiftwidth=4 ft=pir: