tagged release 0.7.1
[parrot.git] / t / library / dumper.t
blob750d39be7cd640b3d8dc86c1eeae624e81b9f08a
1 #!perl
2 # Copyright (C) 2001-2006, The Perl Foundation.
3 # $Id$
5 use strict;
6 use warnings;
7 use lib qw( t . lib ../lib ../../lib );
9 use Test::More;
10 use Parrot::Test tests => 26;
12 =head1 NAME
14 t/library/dumper.t - test dumping of data
16 =head1 SYNOPSIS
18     % prove t/library/dumper.t
20 =head1 DESCRIPTION
22 Tests data dumping.
24 =cut
26 # no. 1
27 pir_output_is( <<'CODE', <<'OUT', "dumping array of sorted numbers" );
29 .include "library/dumper.pir"
30 .sub test :main
31     .local pmc array
33     new array, 'ResizablePMCArray'
34     push array, 0
35     push array, 1
36     push array, 2
37     push array, 3
38     push array, 4
39     push array, 5
40     push array, 6
41     push array, 7
42     push array, 8
43     push array, 9
45     _dumper( array, "array" )
46 .end
47 CODE
48 "array" => ResizablePMCArray (size:10) [
49     0,
50     1,
51     2,
52     3,
53     4,
54     5,
55     6,
56     7,
57     8,
58     9
60 OUT
62 # no. 2
64 pir_output_is( <<'CODE', <<'OUT', "dumping unsorted numbers" );
65 .include "library/dumper.pir"
66 .sub test :main
67     .local pmc array
69     new array, 'ResizablePMCArray'
70     push array, 6
71     push array, 1
72     push array, 8
73     push array, 3
74     push array, 2
75     push array, 9
76     push array, 7
77     push array, 4
78     push array, 0
79     push array, 5
81     _dumper( array, "array" )
82 .end
83 CODE
84 "array" => ResizablePMCArray (size:10) [
85     6,
86     1,
87     8,
88     3,
89     2,
90     9,
91     7,
92     4,
93     0,
94     5
96 OUT
98 # no. 3
99 pir_output_is( <<'CODE', <<'OUT', "dumping sorted strings" );
101 .include "library/dumper.pir"
102 .sub test :main
103     .local pmc array
105     new array, 'ResizablePMCArray'
106     push array, "alpha"
107     push array, "bravo"
108     push array, "charlie"
109     push array, "delta"
110     push array, "echo"
111     push array, "foxtrot"
112     push array, "golf"
113     push array, "hotel"
115     _dumper( array, "strings" )
116 .end
117 CODE
118 "strings" => ResizablePMCArray (size:8) [
119     "alpha",
120     "bravo",
121     "charlie",
122     "delta",
123     "echo",
124     "foxtrot",
125     "golf",
126     "hotel"
130 # no. 4
131 pir_output_is( <<'CODE', <<'OUT', "sorting unsorted strings" );
133 .sub test :main
134     .local pmc array
136     new array, 'ResizablePMCArray'
137     push array, "charlie"
138     push array, "hotel"
139     push array, "alpha"
140     push array, "delta"
141     push array, "foxtrot"
142     push array, "golf"
143     push array, "bravo"
144     push array, "echo"
146     _dumper( array, "strings" )
147 .end
148 .include "library/dumper.pir"
149 CODE
150 "strings" => ResizablePMCArray (size:8) [
151     "charlie",
152     "hotel",
153     "alpha",
154     "delta",
155     "foxtrot",
156     "golf",
157     "bravo",
158     "echo"
162 # no. 5
163 pir_output_is( <<'CODE', <<'OUT', "dumping different types" );
165 .sub test :main
166     .local pmc array
168     new array, 'ResizablePMCArray'
169     push array, 0.1
170     push array, "charlie"
171     push array, 2
172     push array, "hotel"
173     push array, 5
174     push array, "alpha"
175     push array, 0.2
176     push array, "delta"
177     push array, 4
178     push array, "foxtrot"
179     push array, 0.5
180     push array, 0.4
181     push array, 1
182     push array, "golf"
183     push array, 0.3
184     push array, 3
185     push array, "bravo"
186     push array, 0.0
187     push array, 0
188     push array, "echo"
190     _dumper( array, "array" )
191 .end
192 .include "library/dumper.pir"
193 CODE
194 "array" => ResizablePMCArray (size:20) [
195     0.1,
196     "charlie",
197     2,
198     "hotel",
199     5,
200     "alpha",
201     0.2,
202     "delta",
203     4,
204     "foxtrot",
205     0.5,
206     0.4,
207     1,
208     "golf",
209     0.3,
210     3,
211     "bravo",
212     0,
213     0,
214     "echo"
218 # no. 6
219 pir_output_is( <<'CODE', <<'OUT', "dumping complex data" );
221 .sub test :main
222     .local pmc hash1
223     .local pmc hash2
224     .local pmc hash3
225     .local pmc array1
226     .local pmc array2
228     new hash1, 'Hash'
229     new hash2, 'Hash'
230     new hash3, 'Hash'
231     new array1, 'ResizablePMCArray'
232     new array2, 'ResizablePMCArray'
234     _dumper( hash1,"hash1" )
236     S0 = "hello"
237     S1 = "world"
238     set hash1[S0], S1
240     _dumper( hash1,"hash1" )
242     S0 = "hello2"
243     S1 = "world2"
244     set hash1[S0], S1
246     _dumper( hash1,"hash1" )
248     S0 = "hash2"
249     set hash1[S0], hash2
251     _dumper( hash1,"hash1" )
253     S0 = "hello3"
254     S1 = "world3"
255     set hash2[S0], S1
257     _dumper( hash1,"hash1" )
259     S0 = "name"
260     S1 = "parrot"
261     set hash3[S0], S1
262     S0 = "is"
263     S1 = "cool"
264     set hash3[S0], S1
266     push array1, "this"
267     push array1, "is"
268     push array1, "a"
269     push array1, "test"
270     push array1, hash3
272     S0 = "array1"
273     set hash2[S0], array1
275     _dumper( hash1,"hash1" )
277 .end
278 .include "library/dumper.pir"
279 CODE
280 "hash1" => Hash {
282 "hash1" => Hash {
283     "hello" => "world"
285 "hash1" => Hash {
286     "hello" => "world",
287     "hello2" => "world2"
289 "hash1" => Hash {
290     "hash2" => Hash {
291     },
292     "hello" => "world",
293     "hello2" => "world2"
295 "hash1" => Hash {
296     "hash2" => Hash {
297         "hello3" => "world3"
298     },
299     "hello" => "world",
300     "hello2" => "world2"
302 "hash1" => Hash {
303     "hash2" => Hash {
304         "array1" => ResizablePMCArray (size:5) [
305             "this",
306             "is",
307             "a",
308             "test",
309             Hash {
310                 "is" => "cool",
311                 "name" => "parrot"
312             }
313         ],
314         "hello3" => "world3"
315     },
316     "hello" => "world",
317     "hello2" => "world2"
321 # no.7
322 pir_output_is( <<'CODE', <<'OUT', "properties" );
324 .sub test :main
325     .local pmc str
326     .local pmc array
328     new array, 'ResizablePMCArray'
329     push array, "test1"
330     push array, "test2"
332     new str, 'String'
333     set str, "value1"
334     setprop array, "key1", str
336     new str, 'String'
337     set str, "value2"
338     setprop array, "key2", str
340     _dumper( array )
342 .end
343 .include "library/dumper.pir"
344 CODE
345 "VAR1" => ResizablePMCArray (size:2) [
346     "test1",
347     "test2"
348 ] with-properties: Hash {
349     "key1" => "value1",
350     "key2" => "value2"
354 # no. 8
355 pir_output_is( <<'CODE', <<'OUT', "indent string" );
357 .sub test :main
358     .local pmc hash1
359     .local pmc hash2
360     .local pmc array1
361     .local pmc array2
362     .local string name
363     .local string indent
365     new hash1, 'Hash'
366     new hash2, 'Hash'
367     new array1, 'ResizablePMCArray'
368     new array2, 'ResizablePMCArray'
370     set hash1["hash2"], hash2
371     set hash2["array"], array1
372     set hash1["test1"], "test1"
373     set hash2["test2"], "test2"
374     push array1, 1
375     push array1, array2
376     push array2, "test"
377     setprop hash1, "array2", array2
378     name = "hash"
379     indent = "|  "
380     _dumper( hash1, name, indent )
381     _dumper( hash1, name, indent )
382     print "name = '"
383     print name
384     print "'\nindent = '"
385     print indent
386     print "'\n"
387 .end
388 .include "library/dumper.pir"
389 CODE
390 "hash" => Hash {
391 |  "hash2" => Hash {
392 |  |  "array" => ResizablePMCArray (size:2) [
393 |  |  |  1,
394 |  |  |  ResizablePMCArray (size:1) [
395 |  |  |  |  "test"
396 |  |  |  ]
397 |  |  ],
398 |  |  "test2" => "test2"
399 |  },
400 |  "test1" => "test1"
401 } with-properties: Hash {
402 |  "array2" => \hash["hash2"]["array"][1]
404 "hash" => Hash {
405 |  "hash2" => Hash {
406 |  |  "array" => ResizablePMCArray (size:2) [
407 |  |  |  1,
408 |  |  |  ResizablePMCArray (size:1) [
409 |  |  |  |  "test"
410 |  |  |  ]
411 |  |  ],
412 |  |  "test2" => "test2"
413 |  },
414 |  "test1" => "test1"
415 } with-properties: Hash {
416 |  "array2" => \hash["hash2"]["array"][1]
418 name = 'hash'
419 indent = '|  '
422 # no. 9
423 pir_output_is( <<'CODE', <<'OUT', "back-referencing properties" );
425 .sub test :main
426     .local pmc hash
428     new hash, 'Hash'
430     set hash["hello"], "world"
431     setprop hash, "backref", hash
432     _dumper( hash )
433 .end
434 .include "library/dumper.pir"
435 CODE
436 "VAR1" => Hash {
437     "hello" => "world"
438 } with-properties: Hash {
439     "backref" => \VAR1
443 # no. 10
444 pir_output_is( <<'CODE', <<'OUT', "self-referential properties (1)" );
446 .sub test :main
447     .local pmc hash
448     .local pmc prop
450     new hash, 'Hash'
452     set hash["hello"], "world"
453     setprop hash, "self", hash
454     prophash prop, hash
455     setprop hash, "self", prop
456     _dumper( hash )
457 .end
458 .include "library/dumper.pir"
459 CODE
460 "VAR1" => Hash {
461     "hello" => "world"
462 } with-properties: Hash {
463     "self" => \VAR1.properties()
467 # no. 11
468 pir_output_is( <<'CODE', <<'OUT', "self-referential properties (2)" );
470 .sub test :main
471     .local pmc array
472     .local pmc hash1
473     .local pmc hash2
474     .local pmc prop
476     new array, 'ResizablePMCArray'
477     new hash1, 'Hash'
478     new hash2, 'Hash'
480     set hash1["hello1"], "world1"
481     set hash2["hello2"], "world2"
482     setprop hash1, "das leben", hash2
483     prophash prop, hash1
484     set prop["das leben"], "ist schoen"
485     setprop hash2, "hash1prop", prop
486     push array, hash1
487     push array, hash2
488     push array, prop
489     prophash prop, hash2
490     push array, prop
491     _dumper( array )
492 .end
493 .include "library/dumper.pir"
494 CODE
495 "VAR1" => ResizablePMCArray (size:4) [
496     Hash {
497         "hello1" => "world1"
498     } with-properties: Hash {
499         "das leben" => "ist schoen"
500     },
501     Hash {
502         "hello2" => "world2"
503     } with-properties: Hash {
504         "hash1prop" => \VAR1[0].properties()
505     },
506     \VAR1[0].properties(),
507     \VAR1[1].properties()
511 # no. 12
512 pir_output_is( <<'CODE', <<'OUT', "dumping objects" );
514 .sub test :main
515     .local pmc temp
516     .local pmc array
518     newclass temp, "TestClass"
520     new array, 'ResizablePMCArray'
521     temp = new "TestClass"
522     push array, temp
523     $P0 = get_class 'TestClass'
524     temp = new $P0
525     push array, temp
527     _dumper( array )
528 .end
530 .namespace ["TestClass"]
532 .sub __dump :method
533     .param pmc dumper
534     .param string dname
535     .local string subindent
536     .local string indent
537     .local string name
539     (subindent, indent) = dumper."newIndent"()
540     print "{\n"
542     print subindent
543     print "this is\n"
545     print subindent
546     print "_"
547     typeof name, self
548     print name
549     print "::__dump\n"
551     print indent
552     print "}"
554     dumper."deleteIndent"()
556     .begin_return
557     .end_return
558 .end
559 .namespace []
560 .include "library/dumper.pir"
561 CODE
562 "VAR1" => ResizablePMCArray (size:2) [
563     PMC 'TestClass' {
564         this is
565         _TestClass::__dump
566     },
567     PMC 'TestClass' {
568         this is
569         _TestClass::__dump
570     }
574 # no. 13
575 pir_output_is( <<'CODE', <<'OUT', "dumping 'null'" );
577 .sub test :main
578     .local pmc array
579     .local pmc temp
581     new array, 'ResizablePMCArray'
583     push array, 0
584     push array, "0"
586     null temp
587     push array, temp
589     new temp, 'Integer'
590     set temp, 0
591     push array, temp
593     new temp, 'String'
594     set temp, "0"
595     push array, temp
597     _dumper( array, "array" )
598 .end
599 .include "library/dumper.pir"
600 CODE
601 "array" => ResizablePMCArray (size:5) [
602     0,
603     "0",
604     null,
605     0,
606     "0"
610 # no. 14
611 pir_output_is( << 'CODE', << 'OUT', "dumping strings" );
613 .include "library/dumper.pir"
614 .sub _test :main
615     .local pmc array
616     array = new 'ResizablePMCArray'
618     .local pmc pmc_string, pmc_perl_string
619     .local string string_1
621     pmc_string = new 'String'
622     pmc_string = "This is a String PMC"
623     push array, pmc_string
625     pmc_perl_string = new 'String'
626     pmc_perl_string = "This is a String PMC"
627     push array, pmc_perl_string
629     string_1 = "This is a String"
630     push array, string_1
632     _dumper( array, "array of various strings" )
633 .end
634 CODE
635 "array of various strings" => ResizablePMCArray (size:3) [
636     "This is a String PMC",
637     "This is a String PMC",
638     "This is a String"
642 # no. 15
643 pir_output_is( <<'CODE', <<'OUT', "dumping complex data in Hash" );
645 .sub test :main
646     .local pmc hash1
647     .local pmc hash2
648     .local pmc hash3
649     .local pmc array1
651     new hash1, 'Hash'
652     new hash2, 'Hash'
653     new hash3, 'Hash'
654     new array1, 'ResizablePMCArray'
656     _dumper( hash1,"hash1" )
658     S0 = "hello"
659     S1 = "world"
660     set hash1[S0], S1
662     _dumper( hash1,"hash1" )
664     S0 = "hello2"
665     S1 = "world2"
666     set hash1[S0], S1
668     _dumper( hash1,"hash1" )
670     S0 = "hash2"
671     set hash1[S0], hash2
673     _dumper( hash1,"hash1" )
675     S0 = "hello3"
676     S1 = "world3"
677     set hash2[S0], S1
679     _dumper( hash1,"hash1" )
681     S0 = "name"
682     S1 = "parrot"
683     set hash3[S0], S1
684     S0 = "is"
685     S1 = "cool"
686     set hash3[S0], S1
688     array1 = 5
689     array1[0] = "this"
690     array1[1] = "is"
691     array1[2] = "a"
692     array1[3] = "test"
693     array1[4] = hash3
695     S0 = "array1"
696     set hash2[S0], array1
698     _dumper( hash1,"hash1" )
700 .end
701 .include "library/dumper.pir"
702 CODE
703 "hash1" => Hash {
705 "hash1" => Hash {
706     "hello" => "world"
708 "hash1" => Hash {
709     "hello" => "world",
710     "hello2" => "world2"
712 "hash1" => Hash {
713     "hash2" => Hash {
714     },
715     "hello" => "world",
716     "hello2" => "world2"
718 "hash1" => Hash {
719     "hash2" => Hash {
720         "hello3" => "world3"
721     },
722     "hello" => "world",
723     "hello2" => "world2"
725 "hash1" => Hash {
726     "hash2" => Hash {
727         "array1" => ResizablePMCArray (size:5) [
728             "this",
729             "is",
730             "a",
731             "test",
732             Hash {
733                 "is" => "cool",
734                 "name" => "parrot"
735             }
736         ],
737         "hello3" => "world3"
738     },
739     "hello" => "world",
740     "hello2" => "world2"
744 # no. 16
745 pir_output_is( <<'CODE', <<'OUTPUT', "dumping Integer PMC" );
747 .sub test :main
748     .local pmc int1
750     new int1, 'Integer'
751     int1 = 12345
752     _dumper( int1, "Int:" )
753 .end
754 .include "library/dumper.pir"
755 CODE
756 "Int:" => 12345
757 OUTPUT
759 # no. 17
760 pir_output_is( <<'CODE', <<'OUTPUT', "dumping Float PMC" );
762 .sub test :main
763     .local pmc float1
765     new float1, 'Float'
766     float1 = 12345.678
767     _dumper( float1, "Float:" )
768 .end
769 .include "library/dumper.pir"
770 CODE
771 "Float:" => 12345.7
772 OUTPUT
774 # no. 18
775 pir_output_is( <<'CODE', <<'OUTPUT', "dumping ResizablePMCArray PMC" );
776 .sub test :main
777     .local pmc array
779     new array, 'ResizablePMCArray'
780     push array, 12345
781     push array, "hello"
782     _dumper( array, "array:" )
783 .end
784 .include "library/dumper.pir"
785 CODE
786 "array:" => ResizablePMCArray (size:2) [
787     12345,
788     "hello"
790 OUTPUT
792 # no. 19
793 pir_output_is( <<'CODE', <<'OUTPUT', "dumping ResizableStringArray PMC" );
794 .sub test :main
795     .local pmc array
797     new array, 'ResizableStringArray'
798     push array, "hello"
799     push array, "world"
800     _dumper( array, "array:" )
801 .end
802 .include "library/dumper.pir"
803 CODE
804 "array:" => ResizableStringArray (size:2) [
805     "hello",
806     "world"
808 OUTPUT
810 # no. 20
811 pir_output_is( <<'CODE', <<'OUTPUT', "dumping ResizableIntegerArray PMC" );
812 .sub test :main
813     .local pmc array
815     new array, 'ResizableIntegerArray'
816     push array, 12345
817     push array, 67890
818     _dumper( array, "array:" )
819 .end
820 .include "library/dumper.pir"
821 CODE
822 "array:" => ResizableIntegerArray (size:2) [
823     12345,
824     67890
826 OUTPUT
828 # no. 21
829 pir_output_is( <<'CODE', <<'OUTPUT', "dumping ResizableFloatArray PMC" );
830 .sub test :main
831     .local pmc array
833     new array, 'ResizableFloatArray'
834     push array, 123.45
835     push array, 67.89
836     _dumper( array, "array:" )
837 .end
838 .include "library/dumper.pir"
839 CODE
840 "array:" => ResizableFloatArray (size:2) [
841     123.45,
842     67.89
844 OUTPUT
846 # no. 22
847 pir_output_is( <<'CODE', <<'OUTPUT', "dumping FixedPMCArray PMC" );
848 .sub test :main
849     .local pmc array
851     new array, 'FixedPMCArray'
852     array = 2
853     array[0] = 12345
854     array[1] = "hello"
855     _dumper( array, "array:" )
856 .end
857 .include "library/dumper.pir"
858 CODE
859 "array:" => FixedPMCArray (size:2) [
860     12345,
861     "hello"
863 OUTPUT
865 # no. 23
866 pir_output_is( <<'CODE', <<'OUTPUT', "dumping FixedStringArray PMC" );
867 .sub test :main
868     .local pmc array
870     new array, 'FixedStringArray'
871     array = 2
872     array[0] = "hello"
873     array[1] = "world"
874     _dumper( array, "array:" )
875 .end
876 .include "library/dumper.pir"
877 CODE
878 "array:" => FixedStringArray (size:2) [
879     "hello",
880     "world"
882 OUTPUT
884 # no. 24
885 pir_output_is( <<'CODE', <<'OUTPUT', "dumping FixedIntegerArray PMC" );
886 .sub test :main
887     .local pmc array
889     new array, 'FixedIntegerArray'
890     array = 2
891     array[0] = 12345
892     array[1] = 67890
893     _dumper( array, "array:" )
894 .end
895 .include "library/dumper.pir"
896 CODE
897 "array:" => FixedIntegerArray (size:2) [
898     12345,
899     67890
901 OUTPUT
903 # no. 25
904 pir_output_is( <<'CODE', <<'OUTPUT', "dumping FixedFloatArray PMC" );
905 .sub test :main
906     .local pmc array
908     new array, 'FixedFloatArray'
909     array = 2
910     array[0] = 123.45
911     array[1] = 67.89
912     _dumper( array, "array:" )
913 .end
914 .include "library/dumper.pir"
915 CODE
916 "array:" => FixedFloatArray (size:2) [
917     123.45,
918     67.89
920 OUTPUT
922 # no. 26 - Deleted --leo
924 # no. 27
925 pir_output_is( <<'CODE', <<'OUTPUT', "custom dumper" );
926 .sub main :main
927     .local pmc o, cl
928     cl = subclass 'ResizablePMCArray', 'bar'
929     o = new cl
930     _dumper(o)
931 .end
933 .namespace ["bar"]
934 .sub init :vtable :method
935     .local pmc ar
936     ar = getattribute self, ['ResizablePMCArray'], 'proxy'
937     push ar, 1
938     push ar, 2
939 .end
941 .sub __dump :method
942     .param pmc dumper
943     .param string label
944     print " __value => {\n"
945     .local pmc ar
946     ar = getattribute self, ['ResizablePMCArray'], 'proxy'
947     dumper.'dump'('attr', ar)
948     print "\n}"
949 .end
950 .namespace []
951 .include 'library/dumper.pir'
953 CODE
954 "VAR1" => PMC 'bar'  __value => {
955 ResizablePMCArray (size:2) [
956     1,
957     2
960 OUTPUT
962 # pir_output_is(<<'CODE', <<'OUTPUT', "dumping IntegerArray PMC");
963 # pir_output_is(<<'CODE', <<'OUTPUT', "dumping FloatValArray PMC");
965 # Local Variables:
966 #   mode: cperl
967 #   cperl-indent-level: 4
968 #   fill-column: 100
969 # End:
970 # vim: expandtab shiftwidth=4: