Ticket #950: gc_perl.t

File gc_perl.t, 7.7 KB (added by jrtayloriv, 12 years ago)
Line 
1#!perl
2# Copyright (C) 2001-2009, Parrot Foundation.
3# $Id: gc.t 40810 2009-08-26 01:46:42Z dukeleto $
4
5use strict;
6use warnings;
7use lib qw( . lib ../lib ../../lib );
8use Test::More;
9use Parrot::Test tests => 11;
10
11=head1 NAME
12
13t/op/gc.t - Garbage Collection
14
15=head1 SYNOPSIS
16
17        % prove t/op/gc_perl.t
18
19=head1 DESCRIPTION
20
21NOTE: These tests are currently being converted to use only PIR
22      and Test::More, and are then being migrated over to gc.t,
23      after which this file will disappear ...
24
25Tests garbage collection with the C<interpinfo> operation and various
26GC related bugs
27
28=cut
29
30pir_output_is( <<'CODE', <<OUTPUT, "vanishing singleton PMC" );
31.sub main :main
32    $P16 = new 'Env'
33    $P16['Foo'] = 'bar'
34    $I16 = 100
35    $I17 = 0
36
37    loop:
38        sweep 1
39        _rand()
40        $I17 += 1
41        if $I17 <= $I16 goto loop
42        say "ok"
43.end
44
45.sub _rand
46    $P16 = new 'Env'
47    $P5 = $P16['Foo']
48    if $P5 != 'bar' goto err
49    .return()
50    err:
51        say "singleton destroyed .Env = ."
52        $P16 = new 'Env'
53        $S16 = typeof $P16
54        say $S16
55.end
56
57CODE
58ok
59OUTPUT
60
61pir_output_is( <<'CODE', <<OUTPUT, "vanishing return continuation in method calls" );
62.sub main :main
63    .local pmc o, cl
64    cl = newclass "Foo"
65
66    new o, "Foo"
67    print "ok\n"
68    end
69.end
70
71.namespace ["Foo"]
72.sub init :vtable :method
73    print "init\n"
74    sweep 1
75    new $P6, 'String'
76    set $P6, "hi"
77    self."do_inc"()
78    sweep 1
79.end
80
81.sub do_inc :method
82    sweep 1
83    inc self
84    sweep 1
85    print "back from _inc\n"
86.end
87
88.sub __increment :method
89    print "inc\n"
90    sweep 1
91.end
92CODE
93init
94inc
95back from _inc
96ok
97OUTPUT
98
99pasm_output_is( <<'CODE', <<OUTPUT, "failing if regsave is not marked" );
100    newclass P9, "Source"
101    newclass P10, "Source::Buffer"
102    new P12, "Source"
103
104    set S20, P12
105    print S20
106    set S20, P12
107    print S20
108    end
109
110.namespace ["Source"]
111.pcc_sub __get_string:  # buffer
112    get_params "0", P2
113    getprop P12, "buffer", P2
114    sweep 1
115    unless_null P12, buffer_ok
116    new P12, "Source::Buffer"
117    new P14, 'String'
118    set P14, "hello\n"
119    setprop P12, "buf", P14
120    setprop P2, "buffer", P12
121buffer_ok:
122    set_returns "0", P12
123    returncc
124
125.namespace ["Source::Buffer"]
126.pcc_sub __get_string:
127    get_params "0", P2
128    sweep 1
129    getprop P12, "buf", P2
130    set S16, P12
131    set_returns "0", S16
132    returncc
133CODE
134hello
135hello
136OUTPUT
137
138# this is a stripped down version of imcc/t/syn/pcc_16
139# s. also src/pmc/retcontinuation.pmc
140pasm_output_is( <<'CODE', <<OUTPUT, "coro context and invalid return continuations" );
141.pcc_sub main:
142    .const 'Sub' P0 = "co1"
143    set I20, 0
144l:
145    get_results ''
146    set_args ''
147    invokecc P0
148    inc I20
149    lt I20, 3, l
150    print "done\n"
151    end
152.pcc_sub co1:
153    get_params ''
154    set P17, P1
155col:
156    print "coro\n"
157    sweep 1
158    yield
159    branch col
160
161CODE
162coro
163coro
164coro
165done
166OUTPUT
167
168pir_output_is( <<'CODE', <<OUTPUT, "Recursion and exceptions" );
169
170# this did segfault with GC_DEBUG
171
172.sub main :main
173    .local pmc n
174    $P0 = getinterp
175    $P0."recursion_limit"(10)
176    newclass $P0, "b"
177    $P0 = new "b"
178    $P1 = new 'Integer'
179    $P1 = 0
180    n = $P0."b11"($P1)
181    print "ok 1\n"
182    print n
183    print "\n"
184.end
185.namespace ["b"]
186.sub b11 :method
187    .param pmc n
188    .local pmc n1
189    # new_pad -1
190    # store_lex -1, "n", n
191    n1 = new 'Integer'
192    n1 = n + 1
193    push_eh catch
194    n = self."b11"(n1)
195    # store_lex -1, "n", n
196    pop_eh
197catch:
198    # n = find_lex "n"
199    .return(n)
200.end
201CODE
202ok 1
2039
204OUTPUT
205
206pasm_output_is( <<'CODE', <<OUTPUT, "write barrier 1" );
207    null I2
208    set I3, 100
209lp3:
210    null I0
211    set I1, 1000
212    new P1, 'ResizablePMCArray'
213lp1:
214    new P2, 'ResizablePMCArray'
215    new P0, 'Integer'
216    set P0, I0
217    set P2[0], P0
218    set P1[I0], P2
219    if I0, not_0
220    needs_destroy P0
221    # force marking past P2[0]
222    sweep 0
223not_0:
224    new P3, 'Undef'
225    new P4, 'Undef'
226    inc I0
227    lt I0, I1, lp1
228
229    null I0
230    # trace 1
231lp2:
232    set P2, P1[I0]
233    set P2, P2[0]
234    eq P2, I0, ok
235    print "nok\n"
236    print "I0: "
237    print I0
238    print " P2: "
239    print P2
240    print " type: "
241    typeof S0, P2
242    print S0
243    print " I2: "
244    print I2
245    print "\n"
246    exit 1
247ok:
248    inc I0
249    lt I0, I1, lp2
250    inc I2
251    lt I2, I3, lp3
252    print "ok\n"
253    end
254CODE
255ok
256OUTPUT
257
258pasm_output_is( <<'CODE', <<OUTPUT, "write barrier 2 - hash" );
259    null I2
260    set I3, 100
261lp3:
262    null I0
263    set I1, 100
264    new P1, 'Hash'
265lp1:
266    new P2, 'Hash'
267    new P0, 'Integer'
268    set P0, I0
269    set S0, I0
270    set P2["first"], P0
271    set P1[S0], P2
272    if I0, not_0
273    new P0, 'Integer'
274    needs_destroy P0
275    null P0
276    # force full sweep
277    sweep 0
278not_0:
279    new P3, 'Undef'
280    new P4, 'Undef'
281    inc I0
282    lt I0, I1, lp1
283
284    null I0
285    # trace 1
286lp2:
287    set S0, I0
288    set P2, P1[S0]
289    set P2, P2["first"]
290    eq P2, I0, ok
291    print "nok\n"
292    print "I0: "
293    print I0
294    print " P2: "
295    print P2
296    print " type: "
297    typeof S0, P2
298    print S0
299    print " I2: "
300    print I2
301    print "\n"
302    exit 1
303ok:
304    inc I0
305    lt I0, I1, lp2
306    inc I2
307    lt I2, I3, lp3
308    print "ok\n"
309    end
310CODE
311ok
312OUTPUT
313
314pir_output_is( <<'CODE', <<'OUTPUT', "verify pmc proxy object marking" );
315.sub main :main
316    .local pmc cl, s, t
317    cl = subclass "String", "X"
318    addattribute cl, "o3"
319    addattribute cl, "o4"
320    s = new "X"
321    $P0 = new 'String'
322    $S0 = "ok" . " 3\n"
323    $P0 = $S0
324    setattribute s, "o3", $P0
325    $P0 = new 'String'
326    $S0 = "ok" . " 4\n"
327    $P0 = $S0
328    setattribute s, "o4", $P0
329    null $P0
330    null $S0
331    null cl
332    sweep 1
333    s = "ok 1\n"
334    print s
335    .local int i
336    i = 0
337lp:
338    t = new "X"
339    inc i
340    if i < 1000 goto lp
341    t = "ok 2\n"
342    print s
343    print t
344    $P0 = getattribute s, "o3"
345    print $P0
346    $P0 = getattribute s, "o4"
347    print $P0
348.end
349CODE
350ok 1
351ok 1
352ok 2
353ok 3
354ok 4
355OUTPUT
356
357pir_output_is( <<'CODE', <<'OUTPUT', "AddrRegistry 1" );
358.sub main :main
359    .local pmc a, reg, nil
360    reg = new 'AddrRegistry'
361    a = new 'String'
362    null nil
363    $I0 = reg[a]
364    if $I0 == 0 goto ok1
365    print "not "
366ok1:
367    print "ok 1\n"
368    reg[a] = nil
369    $I0 = reg[a]
370    if $I0 == 1 goto ok2
371    print "not "
372ok2:
373    print "ok 2\n"
374    reg[a] = nil
375    $I0 = reg[a]
376    if $I0 == 2 goto ok3
377    print "not "
378ok3:
379    print "ok 3\n"
380
381    delete reg[a]
382    $I0 = reg[a]
383    if $I0 == 1 goto ok4
384    print "not "
385ok4:
386    print "ok 4\n"
387    delete reg[a]
388    $I0 = reg[a]
389    if $I0 == 0 goto ok5
390    print "not "
391ok5:
392    print "ok 5\n"
393.end
394CODE
395ok 1
396ok 2
397ok 3
398ok 4
399ok 5
400OUTPUT
401
402pir_output_is( <<'CODE', <<'OUTPUT', "AddrRegistry 2" );
403.sub main :main
404    .local pmc a, b, reg, nil
405    null nil
406    reg = new 'AddrRegistry'
407    a = new 'String'
408    b = new 'String'
409    $I0 = elements reg
410    print $I0
411    reg[a] = nil
412    $I0 = elements reg
413    print $I0
414    reg[a] = nil
415    $I0 = elements reg
416    print $I0
417    reg[b] = nil
418    $I0 = elements reg
419    print $I0
420    print "\n"
421.end
422CODE
4230112
424OUTPUT
425
426pir_output_is( <<'CODE', <<'OUTPUT', "AddrRegistry 2" );
427.sub main :main
428    .local pmc a, b, c, reg, nil, it
429    null nil
430    reg = new 'AddrRegistry'
431    a = new 'String'
432    a = "k1"
433    b = new 'String'
434    b = "k2"
435    c = new 'String'
436    c = "k3"
437    reg[a] = nil
438    reg[b] = nil
439    reg[c] = nil
440
441    $P1 = new ['ResizablePMCArray']
442    it = iter reg
443loop:
444    unless it goto done
445    $P0 = shift it
446    $S0 = $P0
447    push $P1, $S0
448    goto loop
449done:
450    $P1.'sort'()
451    $S1 = join '', $P1
452    print $S1
453    print "\n"
454.end
455CODE
456k1k2k3
457OUTPUT
458
459=head1 SEE ALSO
460
461F<examples/benchmarks/primes.c>,
462F<examples/benchmarks/primes.pasm>,
463F<examples/benchmarks/primes.pl>,
464F<examples/benchmarks/primes2_i.pasm>,
465F<examples/benchmarks/primes2.c>,
466F<examples/benchmarks/primes2.py>.
467
468=cut
469
470# Local Variables:
471#   mode: cperl
472#   cperl-indent-level: 4
473#   fill-column: 100
474# End:
475# vim: expandtab shiftwidth=4: