[t][TT #1122] Convert t/op/numbert.t to PIR, mgrimes++
[parrot.git] / t / pmc / fixedintegerarray.t
blob3479a7a549aba43e1de74836c259ba38ffdac54f
1 #! parrot
2 # Copyright (C) 2001-2008, Parrot Foundation.
3 # $Id$
5 =head1 NAME
7 t/pmc/fixedintegerarray.t - FixedIntegerArray PMC
9 =head1 SYNOPSIS
11     % prove t/pmc/fixedintegerarray.t
13 =head1 DESCRIPTION
15 Tests C<FixedIntegerArray> 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(29)
24     'test_set_size'()       # 2 tests
25     'test_reset_size'()     # 1 test
26     'test_set_first'()      # 3 tests
27     'test_set_second'()     # 3 tests
28     'test_out_of_bounds'()  # 4 tests
29     'test_set_via_pmc'()    # 3 tests
30     'test_get_via_pmc'()    # 4 tests
31     'test_interface_done'() # 4 tests
32     'test_get_iter'()       # 1 test
33     'test_equality'()       # 5 tests
34 .end
36 .sub 'test_set_size'
37     $P0 = new ['FixedIntegerArray']
39     $I0 = $P0
40     is($I0, 0, "Fresh array has 0 elements")
42     $P0 = 42
43     $I0 = $P0
44     is($I0, 42, "Size was set correctly")
45 .end
47 .sub 'test_reset_size'
48     $P0 = new ['FixedIntegerArray']
50     $I0 = 1
51     $P0 = 1
52     push_eh handled
53     $P0 = 2
54     $I0 = 0
55   handled:
56     pop_eh
58     ok($I0, "Can't resize")
59 .end
61 .sub 'test_set_first'
62     $P0 = new ['FixedIntegerArray']
63     $P0 = 1
65     $P0[0] = -7
66     $I0 = $P0[0]
67     is($I0, -7, "First element set to integer properly")
69     $P0[0] = 3.7
70     $I0 = $P0[0]
71     is($I0, 3, "First element set to number properly")
73     $P0[0] = "17"
74     $I0 = $P0[0]
75     is($I0, 17, "First element set to string properly")
76 .end
78 .sub 'test_set_second'
79     $P0 = new ['FixedIntegerArray']
80     $P0 = 2
82     $P0[1] = -7
83     $I0 = $P0[1]
84     is($I0, -7, "Second element set to integer properly")
86     $P0[1] = 3.7
87     $I0 = $P0[1]
88     is($I0, 3, "Second element set to number properly")
90     $P0[1] = "17"
91     $I0 = $P0[1]
92     is($I0, 17, "Second element set to string properly")
93 .end
96 .sub 'test_out_of_bounds'
97     $P0 = new ['FixedIntegerArray']
98     $P0 = 1
100     $I0 = 1
101     push_eh handle_set
102     $P0[2] = 7
103     $I0 = 0
104   handle_set:
105     ok($I0, "Can't set out-of-bounds element")
107     $I0 = 1
108     push_eh handle_set_negative
109     $P0[-42] = 7
110     $I0 = 0
111   handle_set_negative:
112     ok($I0, "Can't set element on negative index")
114     $I0 = 1
115     push_eh handle_get
116     $I1 = $P0[2]
117     $I0 = 0
118   handle_get:
119     ok($I0, "Can't get out-of-bounds element")
121     $I0 = 1
122     push_eh handle_get_negative
123     $I1 = $P0[-1]
124     $I0 = 0
125   handle_get_negative:
126     ok($I0, "Can't get element with negative index")
128 .end
130 # Set via PMC keys, access via INTs
131 .sub 'test_set_via_pmc'
132     $P0 = new ['FixedIntegerArray']
133     $P0 = 3
135     $P1 = new ['Key']
137     $P1 = 0
138     $P0[$P1] = 25
139     $I0 = $P0[0]
140     is($I0, 25, "Set INTVAL via PMC Key works")
142     $P1 = 1
143     $P0[$P1] = 2.5
144     $I0 = $P0[1]
145     is($I0, 2, "Set FLOATVAL via PMC Key works")
147     $P1 = 2
148     $P0[$P1] = "17"
149     $I0 = $P0[2]
150     is($I0, 17, "Set STRING via PMC Key works")
151 .end
153 # Set via INTs, access via PMC Keys
154 .sub 'test_get_via_pmc'
155     $P0 = new ['FixedIntegerArray']
156     $P0 = 1024
158     $P0[25]   = 125
159     $P0[128]  = 10.2
160     $P0[513]  = "17"
162     $P1 = new ['Integer']
163     $P1 = 123456
164     $P0[1023] = $P1
166     $P2 = new ['Key']
167     
168     $P2 = 25
169     $I0 = $P0[$P2]
170     is($I0, 125, "Get INTVAL via Key works")
172     $P2 = 128
173     $N0 = $P0[$P2]
174     is($N0, 10.0, "Get FLOATVAL via Key works")
176     $P2 = 513
177     $S0 = $P0[$P2]
178     is($S0, "17", "Get STRING via Key works")
180     $P2 = 1023
181     $I0 = $P0[$P2]
182     is($I0, 123456, "Get INTVAL for stored PMC via Key works")
184 .end
186 .sub 'test_interface_done'
187     .local pmc pmc1
188     pmc1 = new ['FixedIntegerArray']
189     .local int bool1
190     does bool1, pmc1, "scalar"
191     nok(bool1, "Does not scalar")
192     does bool1, pmc1, "array"
193     ok(bool1, "Does array")
194     does bool1, pmc1, "no_interface"
195     nok(bool1, "Does not no_interface")
196 .end
198 .sub 'test_get_iter'
199     $P0 = new ['FixedIntegerArray']
200     $P0 = 3
201     $P0[0] = 42
202     $P0[1] = 43
203     $P0[2] = 44
204     $S0 = ""
205     $P1 = iter $P0
206   loop:
207     unless $P1 goto loop_end
208     $S2 = shift $P1
209     concat $S0, $S2
210     goto loop
211   loop_end:
212     is($S0, "424344", "Iteration works")
213 .end
215 .sub 'test_equality'
216     .local pmc a1, a2
217     a1 = new ['FixedIntegerArray']
218     a2 = new ['FixedIntegerArray']
220     is(a1, a2, "Empty arrays are equal")
221     
222     a1 = 3
223     isnt(a1, a2, "Different size arrays aren't equal")
225     a2 = 3
227     a1[0] = 42
228     a2[0] = 42
229     is(a1, a2, "Equal with first element set")
231     a1[1] = 84
232     isnt(a1, a2, "Not equal when second element differ")
233     
234     a2[1] = 84
235     is(a1, a2, "Equal when second element same")
236 .end
239 # Local Variables:
240 #   mode: cperl
241 #   cperl-indent-level: 4
242 #   fill-column: 100
243 # End:
244 # vim: expandtab shiftwidth=4: