/
callcontext.t
360 lines (272 loc) · 8.2 KB
/
callcontext.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
#!./parrot
# Copyright (C) 2006-2010, Parrot Foundation.
=head1 NAME
t/pmc/callcontext.t - test CallContext PMC
=head1 SYNOPSIS
% prove t/pmc/callcontext.t
=head1 DESCRIPTION
Tests the CallContext PMC.
=cut
.sub 'main' :main
.include 'test_more.pir'
plan(65)
test_instantiate()
test_get_set_attrs()
test_indexed_access()
test_indexed_boxing()
test_keyed_access()
test_shift_access()
test_shift_acess_empty()
test_exists()
test_clone()
test_short_sign()
test_named_args('Int' => 10, 'Num' => 3.14, 'Str' => 'A String', 'Pmc' => 'A String PMC')
.end
.sub 'test_instantiate'
$P0 = new ['CallContext']
ok(1, 'Instantiated CallContext')
.end
.sub 'test_get_set_attrs'
$P0 = new ['CallContext']
$P5 = new 'String'
$P5 = 'moonbomb'
setattribute $P0, 'return_flags', $P5
ok(1, 'set return_flags attribute')
getattribute $P1, $P0, 'return_flags'
is($P5,'moonbomb', 'got return_flags attribute')
$P5 = 'cheese'
setattribute $P0, 'arg_flags', $P5
ok(1, 'set arg_flags attribute')
getattribute $P1, $P0, 'arg_flags'
is($P5,'cheese', 'got arg_flags attribute')
$P5 = 'nothing'
push_eh eh
$I0 = 1
setattribute $P0, 'unexisting_attribute_123', $P5
$I0 = 0
eh:
pop_eh
ok($I0, 'exception when set an unexisting attribute')
.end
.sub 'test_indexed_access'
$P0 = new [ 'CallContext' ]
$P0[0] = 100
$I0 = elements $P0
is( $I0, 1, 'elements after set_*_indexed' )
$P0[1] = 1.11
$I0 = elements $P0
is( $I0, 2, 'elements after set_*_indexed' )
$S0 = '2.22'
$P0[2] = $S0
$I0 = elements $P0
is( $I0, 3, 'elements after set_*_indexed' )
$P1 = new [ 'Float' ]
$P1 = 3.33
$P0[3] = $P1
$I0 = elements $P0
is( $I0, 4, 'elements after set_*_indexed' )
$I1 = $P0[0]
is( $I1, 100, 'set_integer_keyed_int/get_integer_keyed_int pair' )
$N1 = $P0[1]
is( $N1, 1.11, 'set_number_keyed_int/get_number_keyed_int pair' )
$S1 = $P0[2]
is( $S1, '2.22', 'set_string_keyed_int/get_string_keyed_int pair' )
$P1 = $P0[3]
is( $P1, 3.33, 'set_pmc_keyed_int/get_pmc_keyed_int pair' )
$I2 = $P0[4]
is($I2, 0, 'unexisting get_integer_keyed_int')
$N2 = $P0[4]
is($N2, 0.0, 'unexisting get_number_keyed_int')
$S2 = $P0[4]
null $S3
is($S2, $S3, 'unexisting get_string_keyed_int')
$P2 = $P0[4]
null $P3
is($P2, $P3, 'unexisting get_pmc_keyed_int')
.end
.sub 'test_indexed_boxing'
$P0 = new [ 'CallContext' ]
$P0[0] = 100
$P0[1] = 1.11
$S0 = '2.22'
$P0[2] = $S0
$P1 = new [ 'Float' ]
$P1 = 3.33
$P0[3] = $P1
$I0 = $P0[1]
is( $I0, 1, 'indexed float converted to int on get_integer_keyed_int' )
$I0 = $P0[2]
is( $I0, 2, 'indexed string converted to int on get_integer_keyed_int' )
$I0 = $P0[3]
is( $I0, 3, 'indexed PMC converted to int on get_integer_keyed_int' )
$N0 = $P0[0]
is( $N0, 100.0, 'indexed integer converted to num on get_number_keyed_int' )
$N0 = $P0[2]
is( $N0, 2.22, 'indexed string converted to num on get_number_keyed_int' )
$N0 = $P0[3]
is( $N0, 3.33, 'indexed PMC converted to int num get_number_keyed_int' )
$S0 = $P0[0]
is( $S0, '100', 'indexed int converted to string on get_string_keyed_int' )
$S0 = $P0[1]
is( $S0, '1.11', 'indexed num converted to string on get_string_keyed_int' )
$S0 = $P0[3]
is( $S0, '3.33', 'indexed PMC converted to string get_string_keyed_int' )
$P1 = $P0[0]
is( $P1, 100, 'indexed int converted to PMC on get_pmc_keyed_int' )
$P1 = $P0[1]
is( $P1, 1.11, 'indexed float converted to PMC on get_pmc_keyed_int' )
$P1 = $P0[2]
is( $P1, 2.22, 'indexed string converted to PMC on get_pmc_keyed_int' )
.end
.sub 'test_keyed_access'
$P0 = new [ 'CallContext' ]
$P0['foo'] = 100
$S0 = 'bar'
$P0[$S0] = 1.11
$P0['baz'] = '2.22'
$P1 = new [ 'Float' ]
$P1 = 3.33
$P0['qux'] = $P1
$I0 = $P0['foo']
is( $I0, 100, 'set/get_intval_keyed' )
$N0 = $P0[$S0]
is( $N0, 1.11, 'set/get_number_keyed' )
$S0 = $P0['baz']
is( $S0, '2.22', 'set/get_string_keyed' )
$P2 = $P0['qux']
is( $P2, 3.33, 'set/get_pmc_keyed' )
$P1 = getattribute $P0, 'named'
$I0 = elements $P1
is( $I0, 4, 'elements after set_*_keyed' )
$P4 = $P0['baz']
is($P4, '2.22', 'get_pmc_keyed with boxing')
$P3 = new ['CallContext']
$I1 = $P0['unexisting']
is($I1, 0, 'get_integer_keyed whith empty CallContext')
$N1 = $P0['unexisting']
is($N1, 0.0, 'get_number_keyed whith empty CallContext')
$S1 = $P0['unexisting']
null $S2
is($S1, $S2, 'get_string_keyed whith empty CallContext')
$P3 = $P0['unexisting']
null $P4
is($P3, $P4, 'get_pmc_keyed whith empty CallContext')
.end
.sub 'test_exists'
$P0 = new [ 'CallContext' ]
$P0[0] = 111
$P0['foo'] = 100
$I0 = exists $P0[0]
ok( $I0, 'exists_keyed_int' )
$I0 = exists $P0['foo']
ok( $I0, 'exists_keyed_str' )
$I0 = exists $P0[100]
nok( $I0, 'exists_keyed_int -- non-existent' )
$I0 = exists $P0['bar']
nok( $I0, 'exists_keyed_str -- non-existent' )
$P1 = new ['CallContext']
$I0 = exists $P1[1]
nok($I0, 'test exists_keyed_int whith empty CallContext')
$P2 = new ['String']
$P2 = 'unexisting'
$I0 = exists $P1[$P2]
nok($I0, 'test exists_keyed whith empty CallContext')
.end
.sub 'test_clone'
$P0 = new ['CallContext']
$P0[0] = 42
$P0[1] = "Hello Parrot"
$P0['floatval'] = 3.14159
$P0 = 'PISS'
$P2 = new['String']
$P2 = 'return attr'
setattribute $P0, 'return_flags', $P2
$P3 = new['String']
$P3 = 'arg attr'
setattribute $P0, 'arg_flags', $P3
$P1 = clone $P0
$I2 = $P1[0]
is($I2, 42, 'clone - integer positional cloned')
$S2 = $P1[1]
is($S2, "Hello Parrot", 'clone - string positional cloned')
$N2 = $P1['floatval']
is($N2, 3.14159, 'clone - named number cloned')
$S3 = $P0
is($S3, 'PISS', 'clone - with a short signature string')
getattribute $P4, $P0, 'return_flags'
is($P4,'return attr', 'clone - with return_flags attribute')
getattribute $P4, $P0, 'arg_flags'
is($P4,'arg attr', 'clone - with arg_flags attribute')
.end
.sub 'test_shift_access'
$P0 = new ['CallContext']
$P1 = new [ 'String' ]
$P1 = 'derF'
$P3 = new ['String']
$P3 = 'Second'
unshift $P0, $P3
unshift $P0, $P1
$S1 = shift $P0
is($S1, 'derF', 'shift should convert to proper type (PMC -> STRING)')
unshift $P0, $P1
$P2 = shift $P0
is($P2, 'derF', '... but not convert when unneccesary')
$I0 = issame $P1, $P2
ok($I0, '... returning the same item' )
# no unshift_string for now
$P0[0] = 'Fred'
$P2 = shift $P0
is($P2, 'Fred', 'shift should convert to proper type (STRING -> PMC)')
$P0[0] = 'Fred'
$S2 = shift $P0
is($S2, 'Fred', '... but not convert when unnecessary')
.end
.sub 'test_shift_acess_empty'
$P0 = new['CallContext']
$I0 = 1
push_eh first_eh
$P1 = shift $P0
$I0 = 0
first_eh:
pop_eh
ok($I0, 'exception when shift_pmc an empty CallContext')
push_eh second_eh
$I0 = 1
$S0 = shift $P0
$I0 = 0
second_eh:
pop_eh
ok($I0, 'exception when shift_string an empty CallContext')
.end
.sub 'test_short_sign'
$P0 = new ['CallContext']
$P1 = new ['String']
$P1 = 'PMC String'
$S0 = 'String'
$I0 = 123
$N0 = 3.14
$P0[0] = $P1
$P0[1] = $S0
$P0[2] = $I0
$P0[3] = $N0
$S1 = $P0
is($S1, 'PSIN', 'get short signature string from arguments type')
$P0 = 'NISP'
$S2 = $P0
is($S2, 'NISP', 'set short signature')
.end
.sub 'test_named_args'
.param int i :named('Int')
.param num n :named('Num')
.param string s :named('Str')
.param pmc p :named('Pmc')
is( i, 10, 'set/get_intval_keyed_str' )
is( n, 3.14, 'set/get_number_keyed_str' )
is( s, 'A String', 'set/get_string_keyed_str' )
is( p, 'A String PMC', 'set/get_pmc_keyed_str' )
.end
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: