-
Notifications
You must be signed in to change notification settings - Fork 138
/
integer.t
423 lines (354 loc) · 6.43 KB
/
integer.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#!parrot
# Copyright (C) 2001-2009, Parrot Foundation.
# $Id$
=head1 NAME
t/pmc/integer.t - Integer basic type
=head1 SYNOPSIS
% prove t/pmc/integer.t
=head1 DESCRIPTION
Tests the Integer PMC.
=cut
.sub 'test' :main
.include 'test_more.pir'
plan(62)
test_init()
test_basic_math()
test_truthiness_and_definedness()
test_set_string_native()
test_isa()
test_interface()
test_ne()
test_gt()
test_ge()
test_istrue_isfalse()
test_if_unless()
test_add()
test_arithmetic()
test_get_as_base()
test_get_as_base10()
test_get_as_base_various()
test_get_as_base_bounds_check()
test_cmp_subclass()
test_cmp_RT59336()
.end
.sub test_init
.local pmc i1, i2
i1 = new ['Integer']
is(i1, 0, "Default value of Integer is 0")
i1 = 42
i2 = new ['Integer'], i1
is(i2, 42, "Initialize with argument set correct value")
.end
.sub test_get_as_base_bounds_check
throws_substring(<<'CODE', 'get_as_base: base out of bounds', 'get_as_base lower bound check')
.sub main
$P0 = new ['Integer']
$P0 = 42
$S0 = $P0.'get_as_base'(1)
say $S0
.end
CODE
throws_substring(<<'CODE', 'get_as_base: base out of bounds', 'get_as_base upper bound check')
.sub main
$P0 = new ['Integer']
$P0 = 42
$S0 = $P0.'get_as_base'(37)
say $S0
.end
CODE
.end
.sub test_basic_math
.local pmc int_1
int_1 = new ['Integer']
is(int_1,0)
int_1 = 1
is(int_1,1)
int_1 += 777777
int_1 -= 777776
is(int_1,2)
int_1 *= -333333
int_1 /= -222222
is(int_1,3)
inc int_1
inc int_1
dec int_1
is(int_1,4)
neg int_1
dec int_1
neg int_1
is(int_1,5)
.end
.sub test_truthiness_and_definedness
.local pmc int_1
int_1 = new ['Integer']
nok(int_1, "A newly created Integer is not true")
.local int is_defined
is_defined = defined int_1
nok(int_1, "A newly created Integer is not defined")
int_1 = -999999999
ok(int_1, "-999999999 is true")
is_defined = defined int_1
ok(int_1, "-999999999 is defined")
.end
.sub test_set_string_native
.local pmc pmc1
pmc1 = new ['Integer']
pmc1 = "-123456789"
is(pmc1, -123456789)
.end
.sub test_isa
.local pmc pmc1
pmc1 = new ['Integer']
.local int pmc1_is_a
pmc1_is_a = isa pmc1, "Integer"
isa_ok(pmc1, "Integer")
.end
.sub test_interface
.local pmc pmc1
pmc1 = new ['Integer']
.local int bool1
does bool1, pmc1, "scalar"
is(bool1,1)
does bool1, pmc1, "integer"
is(bool1,1)
does bool1, pmc1, "no_interface"
is(bool1,0)
.end
.sub test_ne
.local pmc pmc1
pmc1 = new ['Integer']
.local int int1
pmc1 = 10
int1 = 20
ne pmc1, int1, OK1
ok(0)
goto next_test
OK1:
ok(1)
next_test:
int1 = 10
ne pmc1, int1, BAD2
branch OK2
BAD2:
ok(0)
goto fin
OK2:
ok(1)
fin:
.end
.sub test_gt
.local pmc pmc1
pmc1 = new ['Integer']
.local int int1
pmc1 = 10
int1 = 5
gt pmc1, int1, OK1
ok(0)
goto next_test1
OK1:
ok(1)
next_test1:
int1 = 10
gt pmc1, int1, BAD2
branch OK2
BAD2:
ok(0)
OK2:
ok(1)
next_test2:
int1 = 20
gt pmc1, int1, BAD3
branch OK3
BAD3:
ok(0)
goto fin
OK3:
ok(1)
fin:
.end
.sub test_ge
.local pmc pmc1
pmc1 = new ['Integer']
.local int int1
pmc1 = 10
int1 = 5
ge pmc1, int1, OK1
ok(0)
goto next_test1
OK1:
ok(1)
int1 = 10
next_test1:
ge pmc1, int1, OK2
ok(0)
goto next_test2
OK2:
ok(1)
int1 = 20
next_test2:
ge pmc1, int1, BAD3
branch OK3
BAD3:
ok(0)
goto fin
OK3:
ok(1)
fin:
.end
.sub test_istrue_isfalse
.local pmc pmc1
pmc1 = new ['Integer']
.local int int1
pmc1 = 10
istrue int1, pmc1
is(1,int1)
isfalse int1, pmc1
is(0,int1)
pmc1 = 0
istrue int1, pmc1
is(0,int1)
isfalse int1, pmc1
is(1,int1)
.end
.sub test_if_unless
new $P0, ['Integer']
set $P0, 10
if $P0, OK1
ok(0)
goto test1
OK1:
ok(1)
test1:
unless $P0, BAD2
branch OK2
BAD2:
ok(0)
goto test2
OK2:
ok(1)
set $P0, 0
test2:
if $P0, BAD3
branch OK3
BAD3:
ok(0)
goto test3
OK3:
ok(1)
test3:
unless $P0, OK4
ok(0)
goto fin
OK4:
ok(1)
fin:
.end
.sub test_add
new $P0, ['Integer']
set $P0, 5
new $P1, ['Integer']
set $P1, 10
new $P2, ['Integer']
add $P2, $P0, $P1
set $S0, $P2
is($S0,15)
set $P0, "20"
set $P1, "30"
add $P2, $P1, $P0
set $S0, $P2
is($S0,50)
.end
.sub test_arithmetic
$P0 = new ['Integer']
$P1 = new ['Integer']
set $P0, 6
set $P1, 2
add $P2, $P0, $P1
is($P2,8)
$P2 = add $P0, $P1
is($P2,8)
sub $P2, $P0, $P1
is($P2,4)
mul $P2, $P0, $P1
is($P2,12)
div $P2, $P0, $P1
is($P2,3)
mod $P2, $P0, $P1
is($P2,0)
pow $P2, $P0, $P1
is($P2,36)
.end
.sub test_get_as_base
$P0 = new ['Integer']
$P0 = 42
$I0 = can $P0, 'get_as_base'
ok($I0,'Integers can get_as_base')
.end
.sub test_get_as_base10
$P0 = new ['Integer']
$P0 = 42
$S0 = $P0.'get_as_base'(10)
is($S0,42)
.end
.sub test_get_as_base_various
$P0 = new ['Integer']
$P0 = 42
$S0 = $P0.'get_as_base'(2)
is($S0,101010)
$S0 = $P0.'get_as_base'(3)
is($S0,1120)
$S0 = $P0.'get_as_base'(5)
is($S0,132)
$S0 = $P0.'get_as_base'(7)
is($S0,60)
$S0 = $P0.'get_as_base'(11)
is($S0,39)
$S0 = $P0.'get_as_base'(13)
is($S0,33)
$S0 = $P0.'get_as_base'(17)
is($S0,28)
$S0 = $P0.'get_as_base'(19)
is($S0,24)
$S0 = $P0.'get_as_base'(23)
is($S0,'1j')
$S0 = $P0.'get_as_base'(29)
is($S0,'1d')
$S0 = $P0.'get_as_base'(31)
is($S0,'1b')
.end
.sub test_cmp_subclass
$P0 = subclass 'Integer', 'Int'
$P1 = new ['Int']
$P1 = 1
$P2 = new ['Int']
$P2 = 2
$I0 = cmp $P1, $P2
is($I0,-1)
$I0 = cmp $P1, $P1
is($I0,0)
$I0 = cmp $P2, $P1
is($I0,1)
.end
.sub test_cmp_RT59336
$P0 = new ['Integer']
$P0 = 2147483600
test_10:
if $P0 > -10 goto pass
ok(0)
goto test_1000
pass:
ok(1)
test_1000:
if $P0 > -1000 goto pass2
ok(0)
goto fin
pass2:
ok(1)
fin:
.end
# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4: