/
fixedbooleanarray.t
376 lines (298 loc) · 7.64 KB
/
fixedbooleanarray.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
#!./parrot
# Copyright (C) 2001-2010, Parrot Foundation.
=head1 NAME
t/pmc/fixedbooleanarray.t - FixedBooleanArray PMC
=head1 SYNOPSIS
% prove t/pmc/fixedbooleanarray.t
=head1 DESCRIPTION
Tests C<FixedBooleanArray> PMC. Checks size, sets various elements, including
out-of-bounds test. Checks INT and PMC keys.
=cut
.sub 'test' :main
.include 'test_more.pir'
plan(42)
setting_array_size()
resizing_not_allowed()
setting_first_element()
setting_second_element()
setting_out_of_bounds()
getting_out_of_bounds()
set_pmc_access_int()
set_int_access_pmc()
interface()
truth()
pmc_keys_and_values()
freeze_thaw()
test_clone()
get_iter()
fill()
test_new_style_init()
test_invalid_init_tt1509()
test_get_string()
.end
.sub 'setting_array_size'
$P0 = new ['FixedBooleanArray']
$I0 = $P0
is($I0, 0, 'size is initially zero')
$P0 = 1
$I0 = $P0
is($I0, 1, 'size set to 1')
# Make sure destroy is exercised
null $P0
sweep 1
.end
.sub 'resizing_not_allowed'
$P0 = new ['FixedBooleanArray']
push_eh resizing_not_allowed_handler
$P0 = 1
$P0 = 2
nok(1, 'resizing should not have succeeded')
pop_eh
.return()
resizing_not_allowed_handler:
pop_eh
ok(1, 'resizing does not work on a fixed-size array')
.end
.sub 'setting_first_element'
$P0 = new ['FixedBooleanArray']
$P0 = 1
$P0[0] = -7
$I0 = $P0[0]
is($I0, 1, 'setting first element to a true int value')
$P0[0] = 3.7
$N0 = $P0[0]
is($N0, 1.0, 'setting first element to a true num value')
$P0[0] = "17"
$S0 = $P0[0]
is($S0, "1", 'setting first element to a true string value')
.end
.sub 'setting_second_element'
$P0 = new ['FixedBooleanArray']
$P0 = 2
$P0[1] = -7
$I0 = $P0[1]
is($I0, 1, 'setting second element to a true int value')
$P0[1] = 3.7
$N0 = $P0[1]
is($N0, 1.0, 'setting second element to a true num value')
$P0[1] = "17"
$S0 = $P0[1]
is($S0, "1", 'setting second element to a true string value')
.end
.sub 'setting_out_of_bounds'
$P0 = new ['FixedBooleanArray']
$P0 = 1
push_eh setting_out_of_bounds_handler
$P0[1] = -7
pop_eh
nok(1, "Setting out-of-bounds element wrongly succeeded")
.return()
setting_out_of_bounds_handler:
pop_eh
ok(1, "Setting out-of-bounds element did not succeed")
.end
.sub 'getting_out_of_bounds'
$P0 = new ['FixedBooleanArray']
$P0 = 1
push_eh getting_out_of_bounds_handler
$I0 = $P0[1]
pop_eh
nok(1, "Getting out-of-bounds element wrongly succeeded")
.return()
getting_out_of_bounds_handler:
pop_eh
ok(1, "Getting out-of-bounds element does not succeed")
.end
.sub 'set_pmc_access_int'
$P0 = new ['FixedBooleanArray']
$P0 = 3
$P1 = new ['Key']
$P1 = 0
$P0[$P1] = 25
$P1 = 1
$P0[$P1] = 2.5
$P1 = 2
$P0[$P1] = "17"
$I0 = $P0[0]
is($I0, 1, "Set via PMC keys, access via INTs: int value")
$N0 = $P0[1]
is($N0, 1.0, "Set via PMC keys, access via INTs: num value")
$S0 = $P0[0]
is($S0, "1", "Set via PMC keys, access via INTs: string value")
.end
.sub 'set_int_access_pmc'
$P0 = new ['FixedBooleanArray']
$P0 = 1024
$P0[25] = 125
$P0[128] = 10.2
$P0[513] = "17"
$P1 = new ['Integer']
$P1 = 123456
$P0[1023] = $P1
$P2 = new ['Key']
$P2 = 25
$I0 = $P0[$P2]
is($I0, 1, 'Set via INTs, access via PMC Keys: int value')
$P2 = 128
$N0 = $P0[$P2]
is($N0, 1.0, 'Set via INTs, access via PMC Keys: num value')
$P2 = 513
$S0 = $P0[$P2]
is($S0, '1', 'Set via INTs, access via PMC Keys: string value')
$P2 = 1023
$P3 = $P0[$P2]
is($P3, 1, 'Set via INTs, access via PMC Keys: PMC value')
.end
.sub 'interface'
$P0 = new ['FixedBooleanArray']
$I0 = does $P0, 'scalar'
nok($I0, 'FixedBooleanArray does not scalar')
$I0 = does $P0, 'array'
ok($I0, 'FixedBooleanArray does array')
$I0 = does $P0, 'no_interface'
nok($I0, 'FixedBooleanArray does not no_interface')
.end
.sub 'truth'
$P0 = new ['FixedBooleanArray']
nok($P0, 'Empty FixedBooleanArray is false')
$P0 = 1
ok($P0, 'Non-empty FixedBooleanArray is true')
$P0[0] = 0
ok($P0, 'FixedBooleanArray is true, no matter what its values are')
.end
.sub 'pmc_keys_and_values'
$P0 = new ['FixedBooleanArray']
$P0 = 2
$P1 = new ['Key']
$P1 = 1
$P2 = new ['Integer']
$P2 = 1
$P0[$P1] = $P2
$I0 = $P0[$P1]
is($I0, 1, 'PMC keys & values')
.end
.sub 'freeze_thaw'
.local pmc fba
.local int i
.local string s
fba = new ['FixedBooleanArray']
fba = 17
fba[1] = 1
fba[4] = 1
fba[8] = 1
fba[12] = 1
fba[15] = 1
$S0 = fba
is($S0, '01001000100010010', 'FixedBooleanArray before freeze')
s = freeze fba
fba.'fill'(0)
fba = thaw s
$S0 = fba
is($S0, '01001000100010010', 'FixedBooleanArray after thaw')
.end
.sub test_clone
.local pmc fba1, fba2
.local int i
.local string s
fba1 = new ['FixedBooleanArray']
fba1 = 17
fba1[1] = 1
fba1[4] = 1
fba1[8] = 1
fba1[12] = 1
fba1[15] = 1
$S0 = fba1
is($S0, '01001000100010010', 'FixedBooleanArray before clone')
fba2 = clone fba1
$S1 = fba2
is($S0, $S1, "clones have the same string representation")
.end
.sub 'get_iter'
$P0 = new ['FixedBooleanArray']
$P0 = 3
$P0[0] = 1
$P0[1] = 0
$P0[2] = 1
$P1 = iter $P0
$I2 = shift $P1
is($I2, 1, 'get_iter: first element')
$I2 = shift $P1
is($I2, 0, 'get_iter: second element')
$I2 = shift $P1
is($I2, 1, 'get_iter: third element')
nok($P1, 'iterator exhausted')
.end
.sub 'fill'
$P0 = new ['FixedBooleanArray']
$P0.'fill'(0)
ok(1, 'Filling empty array')
.local int result, i, size
size = 1564
$P0 = size
# Fresh array is empty.
i = 0
result = 0
initial_false_loop:
unless i < size goto initial_false_end
$I0 = $P0[i]
result = or result, $I0
inc i
goto initial_false_loop
initial_false_end:
nok(result, "Fresh array filled with 0")
$P0.'fill'(1)
i = 0
result = 1
$I1 = 0
fill_true_loop:
unless i < size goto fill_true_end
$I0 = $P0[i]
result = and result, $I0
inc i
goto fill_true_loop
fill_true_end:
ok(result, "Fill with 1")
$P0.'fill'(0)
i = 0
result = 0
$I1 = 0
fill_false_loop:
unless i < size goto fill_false_end
$I0 = $P0[i]
result = or result, $I0
inc i
goto fill_false_loop
fill_false_end:
nok(result, "Fill with 0")
.end
.sub test_new_style_init
$P0 = new 'FixedBooleanArray', 10
$I0 = $P0
is($I0, 10, "New style init creates the correct # of elements")
$P0 = new ['FixedBooleanArray'], 10
$I0 = $P0
is($I0, 10, "New style init creates the correct # of elements for a key constant")
.end
.sub test_invalid_init_tt1509
throws_substring(<<'CODE', 'FixedBooleanArray: Cannot set array size to a negative number (-10)', 'New style init does not dump core for negative array lengths')
.sub main :main
$P0 = new ['FixedBooleanArray'], -10
.end
CODE
throws_substring(<<'CODE', 'FixedBooleanArray: Cannot set array size to a negative number (-10)', 'New style init (key constant) does not dump core for negative array lengths')
.sub main :main
$P0 = new 'FixedBooleanArray', -10
.end
CODE
.end
.sub test_get_string
$P0 = new 'FixedBooleanArray', 2
$P0[1] = 1
$S0 = $P0
is($S0, '01', 'converts to string')
.end
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: