tagged release 0.6.4
[parrot.git] / t / pmc / freeze.t
blob8c423197b7a1092e7136e6f7180429d2af7044d3
1 #! perl
2 # Copyright (C) 2001-2006, The Perl Foundation.
3 # $Id$
5 use strict;
6 use warnings;
7 use lib qw( . lib ../lib ../../lib );
9 use Test::More;
10 use Parrot::Test tests => 25;
12 =head1 NAME
14 t/pmc/freeze.t - Archiving
16 =head1 SYNOPSIS
18     % prove t/pmc/freeze.t
20 =head1 DESCRIPTION
22 Tests the freeze/thaw archiving subsystem.
24 =cut
26 END { unlink "temp.fpmc"; }
28 pasm_output_is( <<'CODE', <<'OUTPUT', "freeze/thaw a Integer" );
29     new P1, 'Integer'
30     set P1, 777
31     freeze S0, P1
33     thaw P10, S0
34     typeof S10, P10
35     print S10
36     print " "
37     print P10
38     print "\n"
39     end
40 CODE
41 Integer 777
42 OUTPUT
44 pasm_output_is( <<'CODE', <<'OUTPUT', "freeze/thaw a String" );
45     new P1, 'String'
46     set P1, "foo"
47     freeze S0, P1
49     thaw P10, S0
50     typeof S10, P10
51     print S10
52     print " "
53     print P10
54     print "\n"
55     end
56 CODE
57 String foo
58 OUTPUT
60 pasm_output_is( <<'CODE', <<'OUTPUT', "freeze/thaw a Float" );
61     new P1, 'Float'
62     set P1, 3.14159
63     freeze S0, P1
65     thaw P10, S0
66     typeof S10, P10
67     print S10
68     print " "
69     print P10
70     print "\n"
71     end
72 CODE
73 Float 3.14159
74 OUTPUT
76 pasm_output_is( <<'CODE', <<'OUTPUT', "freeze/thaw a Hash" );
77     new P1, 'Integer'
78     set P1, 666
79     new P0, 'Hash'
80     set P0["k1"], P1
81     new P1, 'Integer'
82     set P1, 777
83     set P0["k2"], P1
84     freeze S0, P0
86     thaw P10, S0
87     typeof S10, P10
88     print S10
89     print " "
90     set I11, P10
91     print I11
92     print "\n"
93     set P12, P10["k1"]
94     print P12
95     print "\n"
96     set P12, P10["k2"]
97     print P12
98     print "\n"
99     end
100 CODE
101 Hash 2
104 OUTPUT
106 pasm_output_is( <<'CODE', <<'OUTPUT', "freeze/thaw a Hash" );
107     new P1, 'Integer'
108     set P1, 666
109     new P0, 'Hash'
110     set P0["k1"], P1
111     new P1, 'Integer'
112     set P1, 777
113     set P0["k2"], P1
114     freeze S0, P0
116     thaw P10, S0
117     typeof S10, P10
118     print S10
119     print " "
120     set I11, P10
121     print I11
122     print "\n"
123     set P12, P10["k1"]
124     print P12
125     print "\n"
126     set P12, P10["k2"]
127     print P12
128     print "\n"
129     end
130 CODE
131 Hash 2
134 OUTPUT
136 pasm_output_is( <<'CODE', <<'OUTPUT', "freeze/thaw a Integer with prop" );
137     new P1, 'Integer'
138     set P1, 666
139     new P2, 'Integer'
140     set P2, 42
141     setprop P1, "answer", P2
142     freeze S0, P1
144     thaw P10, S0
145     typeof S10, P10
146     print S10
147     print " "
148     set I11, P10
149     print I11
150     print "\n"
151     getprop P12, "answer", P10
152     print P12
153     print "\n"
154     end
155 CODE
156 Integer 666
158 OUTPUT
160 pasm_output_is( <<'CODE', <<'OUTPUT', "freeze/thaw Array w Integer with prop" );
161     new P0, 'ResizablePMCArray'
162     new P1, 'Integer'
163     set P1, 666
164     push P0, P1
165     new P2, 'Integer'
166     set P2, 777
167     push P0, P2
168     new P3, 'Integer'
169     set P3, 42
170     setprop P1, "answer", P3
172     freeze S0, P0
174     thaw P10, S0
175     typeof S10, P10
176     print S10
177     print " "
178     set I11, P10
179     print I11
180     print "\n"
181     set P12, P10[0]
182     print P12
183     print "\n"
184     set P13, P10[1]
185     print P13
186     print "\n"
187     getprop P12, "answer", P12
188     print P12
189     print "\n"
190     end
191 CODE
192 ResizablePMCArray 2
196 OUTPUT
198 pasm_output_is( <<'CODE', <<'OUTPUT', "freeze/thaw a NULL pmc" );
199     null P0
200     freeze S0, P0
201     thaw P10, S0
202     defined I0, P10
203     unless I0, ok
204     print "not "
205 ok: print "ok\n"
206     end
207 CODE
209 OUTPUT
211 pasm_output_is( <<'CODE', <<'OUTPUT', "freeze/thaw array w NULL pmc" );
212     new P0, 'ResizablePMCArray'
213     null P1
214     push P0, P1
215     new P1, 'Integer'
216     set P1, 10
217     push P0, P1
219     freeze S0, P0
220     thaw P10, S0
222     typeof S10, P10
223     print S10
224     print " "
225     set I11, P10
226     print I11
227     print "\n"
228     set P11, P10[0]
229     defined I0, P11
230     unless I0, ok
231     print "not "
232 ok: print "ok\n"
233     set P11, P10[1]
234     print P11
235     print "\n"
236     end
237 CODE
238 ResizablePMCArray 2
241 OUTPUT
243 pasm_output_is( <<'CODE', <<'OUTPUT', "freeze/thaw a Sub" );
244     get_global P1, "_foo"
245     freeze S0, P1
247     thaw P0, S0
248     typeof S10, P0
249     print S10
250     print "\n"
251     invokecc P0
252     print "back\n"
253     end
254 .pcc_sub _foo:
255     print "in sub _foo\n"
256     returncc
257 CODE
259 in sub _foo
260 back
261 OUTPUT
263 pasm_output_is( <<'CODE', <<'OUTPUT', "freeze/thaw a FixedPMCArray" );
264     new P0, 'FixedPMCArray'
265     set P0, 3
266     new P1, 'Integer'
267     set P1, 666
268     set P0[0], P1
269     new P2, 'Integer'
270     set P2, 777
271     set P0[1], P2
272     new P1, 'Integer'
273     set P1, 666
274     set P0[2], P1
275     freeze S0, P0
277     thaw P10, S0
278     typeof S10, P10     # type
279     print S10
280     print " "
281     set I11, P10        # elements
282     print I11
283     print "\n"
284     set P12, P10[0]
285     print P12
286     print "\n"
287     set P13, P10[1]
288     print P13
289     print "\n"
290     set P14, P10[2]
291     print P14
292     print "\n"
293     ne_addr P12, P14, ok
294     print "not "
295 ok: print "ok diff\n"
296     end
297 CODE
298 FixedPMCArray 3
302 ok diff
303 OUTPUT
305 pasm_output_is( <<'CODE', <<'OUTPUT', "freeze/thaw a FixedPMCArray" );
306     new P0, 'FixedPMCArray'
307     set P0, 3
308     new P1, 'Integer'
309     set P1, 666
310     set P0[0], P1
311     new P2, 'Integer'
312     set P2, 777
313     set P0[1], P2
314     set P0[2], P1
315     freeze S0, P0
317     thaw P10, S0
318     typeof S10, P10     # type
319     print S10
320     print " "
321     set I11, P10        # elements
322     print I11
323     print "\n"
324     set P12, P10[0]
325     print P12
326     print "\n"
327     set P13, P10[1]
328     print P13
329     print "\n"
330     set P14, P10[2]
331     print P14
332     print "\n"
333     eq_addr P12, P14, ok
334     print "not "
335 ok: print "ok same\n"
336     end
337 CODE
338 FixedPMCArray 3
342 ok same
343 OUTPUT
345 pasm_output_is( <<'CODE', <<'OUTPUT', "freeze class" );
346     newclass P10, "Foo"
347     set S10, P10
348     print S10
349     print "\n"
350     freeze S11, P10
351     print "ok 1\n"
352     open P3, "temp.fpmc", ">"
353     print P3, S11
354     close P3
355     print "ok 2\n"
356     end
357 CODE
359 ok 1
360 ok 2
361 OUTPUT
363 pasm_output_is( <<'CODE', <<'OUTPUT', "thaw class into new interpreter" );
364     set S3, "temp.fpmc"
365     .include "stat.pasm"
366     stat I0, S3, .STAT_FILESIZE
367     gt I0, 1, ok1
368     print "stat failed\n"
369     exit 1
370 ok1:
371     open P3, S3, "<"
372     read S3, P3, I0
373     close P3
374     print "ok 1\n"
375     thaw P4, S3
376     print "ok 2\n"
377     set S10, P4
378     print S10
379     print "\n"
380     end
381 CODE
382 ok 1
383 ok 2
385 OUTPUT
387 pasm_output_is( <<'CODE', <<'OUTPUT', "freeze/thaw simple class" );
388     newclass P10, "Foo"
389     set S10, P10
390     print S10
391     print "\n"
392     freeze S11, P10
394     thaw P4, S11
395     print "ok\n"
396     set S12, P4
397     print S12
398     print "\n"
399     end
400 CODE
404 OUTPUT
406 pasm_output_is( <<'CODE', <<'OUTPUT', "freeze class w attr" );
407     newclass P10, "Foo"
408     addattribute P10, ".aa"
409     set S10, P10
410     print S10
411     print "\n"
412     freeze S11, P10
413     print "ok 1\n"
414     open P3, "temp.fpmc", ">"
415     print P3, S11
416     close P3
417     print "ok 2\n"
418     end
419 CODE
421 ok 1
422 ok 2
423 OUTPUT
425 pasm_output_is( <<'CODE', <<'OUTPUT', "thaw class w attr into new interpreter" );
426     set S3, "temp.fpmc"
427     .include "stat.pasm"
428     stat I0, S3, .STAT_FILESIZE
429     gt I0, 1, ok1
430     print "stat failed\n"
431     exit 1
432 ok1:
433     open P3, S3, "<"
434     read S3, P3, I0
435     close P3
436     # print S3
437     # print "\n"
438     print "ok 1\n"
439     thaw P4, S3
440     print "ok 2\n"
441     set S10, P4
442     print S10
443     print "\n"
445     new P5, S10
446     print "ok 3\n"
447     new P6, 'String'
448     set P6, "ok 5\n"
449     setattribute P5, '.aa', P6
450     print "ok 4\n"
451     getattribute P7, P5, '.aa' 
452     print P7
453     end
454 CODE
455 ok 1
456 ok 2
458 ok 3
459 ok 4
460 ok 5
461 OUTPUT
463 pasm_output_is( <<'CODE', <<'OUTPUT', "thaw class w attr same interp" );
464     newclass P10, "Foo"
465     addattribute P10, ".aa"
466     addattribute P10, ".bb"
467     set S10, P10
468     print S10
469     print "\n"
470     freeze S3, P10
471     open P3, "temp.fpmc", ">"
472     print P3, S3
473     close P3
475     # print S3
476     # print "\n"
477     print "ok 1\n"
478     thaw P4, S3
479     print "ok 2\n"
480     set S10, P4
481     print S10
482     print "\n"
484     new P5, S10
485     print "ok 3\n"
486     new P6, 'String'
487     set P6, "ok 5\n"
488     setattribute P5, ["Foo"], ".aa", P6
489     new P6, 'String'
490     set P6, "ok 6\n"
491     setattribute P5, ["Foo"], ".bb", P6
492     print "ok 4\n"
493     getattribute P7, P5, ".aa"
494     print P7
495     getattribute P7, P5, ".bb"
496     print P7
497     end
500 CODE
502 ok 1
503 ok 2
505 ok 3
506 ok 4
507 ok 5
508 ok 6
509 OUTPUT
511 pasm_output_is( <<'CODE', <<'OUTPUT', "thaw object w attr into same interpreter" );
512     newclass P10, "Foo"
513     addattribute P10, ".aa"
514     addattribute P10, ".bb"
515     new P10, "Foo"
516     print S10
517     freeze S3, P10
518     open P3, "temp.fpmc", ">"
519     print P3, S3
520     close P3
521     print "ok 1\n"
523     thaw P5, S3
524     print "ok 2\n"
525     typeof S10, P5
526     print S10
527     print "\n"
529     print "ok 3\n"
530     new P6, 'String'
531     set P6, "ok 5\n"
532     setattribute P5, ["Foo"], ".aa", P6
533     new P6, 'String'
534     set P6, "ok 6\n"
535     setattribute P5, ["Foo"], ".bb", P6
536     print "ok 4\n"
537     getattribute P7, P5, ".aa"
538     print P7
539     getattribute P7, P5, ".bb"
540     print P7
541     end
542 CODE
543 ok 1
544 ok 2
546 ok 3
547 ok 4
548 ok 5
549 ok 6
550 OUTPUT
552 pasm_output_is( <<'CODE', <<'OUTPUT', "thaw object w attr into new interpreter" );
553     set S3, "temp.fpmc"
554     .include "stat.pasm"
555     stat I0, S3, .STAT_FILESIZE
556     gt I0, 1, ok1
557     print "stat failed\n"
558     exit 1
559 ok1:
560     open P3, S3, "<"
561     read S3, P3, I0
562     close P3
564     thaw P5, S3
565     print "ok 2\n"
566     typeof S10, P5
567     print S10
568     print "\n"
570     print "ok 3\n"
571     new P6, 'String'
572     set P6, "ok 5\n"
573     setattribute P5, ["Foo"], ".aa", P6
574     new P6, 'String'
575     set P6, "ok 6\n"
576     setattribute P5, ["Foo"], ".bb", P6
577     print "ok 4\n"
578     getattribute P7, P5, ".aa"
579     print P7
580     getattribute P7, P5, ".bb"
581     print P7
582     end
583 CODE
584 ok 2
586 ok 3
587 ok 4
588 ok 5
589 ok 6
590 OUTPUT
592 pasm_output_is( <<'CODE', <<'OUTPUT', "freeze Key" );
593     new P0, 'Hash'
594     new P1, 'FixedPMCArray'
595     set P1, 2
596     set P1[1], P0
597     set P0["foo"], "ok\n"
598     set S0, P1[1; "foo"]
599     print S0
601     new P3, 'Key'
602     set P3, 1
603     new P4, 'Key'
604     set P4, "foo"
605     push P3, P4
607     set S0, P1[P3]
608     print S0
610     freeze S0, P3
611     print "ok 1\n"
612     thaw P5, S0
613     print "ok 2\n"
615     set S0, P1[P5]
616     print S0
617     end
618 CODE
621 ok 1
622 ok 2
624 OUTPUT
626 pir_output_is( <<'CODE', <<'OUTPUT', "freeze/thaw a ResizableBooleanArray" );
627 .sub test :main
628     .local pmc original_arr, thawed_arr
629     .local string frozen_arr
630     original_arr = new 'ResizableBooleanArray'
631     set original_arr, 666
632     original_arr[555] = 777
634     # Dump some data before freezing
635     print "Before freezing:\n"
636     typeof S10, original_arr    # type
637     print S10
638     print "\n"
639     set I12, original_arr       # elements
640     print I12
641     print "\n"
642     I12 = original_arr[554]
643     print I12
644     print "\n"
645     I12 = original_arr[555]
646     print I12
647     print "\n"
648     I12 = original_arr[556]
649     print I12
650     print "\n"
652     frozen_arr = freeze original_arr
653     thawed_arr = thaw frozen_arr
655     # Dump the same data after freeze/thaw
656     print "\nAfter freeze/thaw:\n"
657     typeof S10, thawed_arr      # type
658     print S10
659     print "\n"
660     set I12, thawed_arr # elements
661     print I12
662     print "\n"
663     I12 = thawed_arr[554]
664     print I12
665     print "\n"
666     I12 = thawed_arr[555]
667     print I12
668     print "\n"
669     I12 = thawed_arr[556]
670     print I12
671     print "\n"
673 .end
674 CODE
675 Before freezing:
676 ResizableBooleanArray
682 After freeze/thaw:
683 ResizableBooleanArray
688 OUTPUT
690 pir_output_is( <<'CODE', <<'OUTPUT', "freeze/thaw a ResizablePMCArray" );
691 .sub test :main
692     .local pmc original_arr, thawed_arr
693     .local string frozen_arr
694     original_arr = new 'ResizablePMCArray'
695     original_arr[0] = 1
696     original_arr[1] = 2.72
697     original_arr[2] = "three.14"
699     # Dump some data before freezing
700     print "Before freezing:\n"
701     typeof S10, original_arr    # type
702     print S10
703     print "\n"
704     set I12, original_arr       # elements
705     print I12
706     print "\n"
707     I12 = original_arr[0]
708     print I12
709     print "\n"
710     N12 = original_arr[1]
711     print N12
712     print "\n"
713     S12 = original_arr[2]
714     print S12
715     print "\n"
717     frozen_arr = freeze original_arr
718     thawed_arr = thaw frozen_arr
720     # Dump the same data after freeze/thaw
721     print "\nAfter freeze/thaw:\n"
722     typeof S10, thawed_arr      # type
723     print S10
724     print "\n"
725     set I12, thawed_arr # elements
726     print I12
727     print "\n"
728     I12 = thawed_arr[0]
729     print I12
730     print "\n"
731     N12 = thawed_arr[1]
732     print N12
733     print "\n"
734     S12 = thawed_arr[2]
735     print S12
736     print "\n"
738 .end
739 CODE
740 Before freezing:
741 ResizablePMCArray
744 2.720000
745 three.14
747 After freeze/thaw:
748 ResizablePMCArray
751 2.720000
752 three.14
753 OUTPUT
755 pir_output_is( <<'CODE', <<'OUTPUT', "freeze/thaw a Conure" );
756 .sub main :main
757     .local pmc cl, o
758     cl = newclass 'Conure'
759     addattribute cl, 'temperature'
760     o = new 'Conure'
761     $S0 = freeze o
762     $P1 = thaw $S0
763     $P2 = getattribute $P1, 'temperature'
764     say $P2
765 .end
767 .namespace ['Conure']
768 .sub __init :method
769     $P0 = new 'Integer'
770     $P0 = 37
771     setattribute self, 'temperature', $P0
772 .end
773 CODE
775 OUTPUT
777 pir_output_is( <<'CODE', <<'OUTPUT', "freeze/thaw obj of class w Hash attrs" );
778 .sub main :main
779     .local pmc cl, o
780     #cl = subclass 'Hash', 'OPTable'
781     cl = newclass 'OPTable'
782     addattribute cl, '%!key'
783     addattribute cl, '%!klen'
784     addattribute cl, '&!ws'
785     o = new 'OPTable'
786     o."init"()
787     o."test"()
788     $S0 = freeze o
789     $P1 = thaw $S0
790     $P1."test"()
791 .end
793 .namespace [ "OPTable" ]
795 .sub "__init" :method
796     .local pmc keytable, klentable
797     keytable = new 'Hash'
798     klentable = new 'Hash'
799     setattribute self, '%!key', keytable
800     setattribute self, '%!klen', klentable
801 .end
803 .sub "init" :method
804     .local pmc keytable, klentable
805     keytable = getattribute self, '%!key'
806     keytable['bar'] = 1
807     keytable['foobar'] = 2
808     klentable = getattribute self, '%!klen'
809     klentable['bar'] = 3
810     klentable['foobar'] = 6
811 .end
813 .sub "test" :method
814     .local pmc keytable, klentable
815     keytable = getattribute self, "%!key"
816     $I0 = keytable['bar']
817     print $I0
818     print ' '
819     $I0 = keytable['foobar']
820     print $I0
821     print ' '
822     klentable = getattribute self, "%!klen"
823     $I0 = klentable['bar']
824     print $I0
825     print ' '
826     $I0 = klentable['foobar']
827     say $I0
828 .end
829 CODE
830 1 2 3 6
831 1 2 3 6
832 OUTPUT
834 # Local Variables:
835 #   mode: cperl
836 #   cperl-indent-level: 4
837 #   fill-column: 100
838 # End:
839 # vim: expandtab shiftwidth=4: