[t][TT #1122] Convert t/op/numbert.t to PIR, mgrimes++
[parrot.git] / t / pmc / packfiledirectory.t
blob15b0718e6b7d19f00bae4b04d93ef136a4572ab0
1 #! parrot
2 # Copyright (C) 2009, Parrot Foundation.
3 # $Id$
5 =head1 NAME
7 t/pmc/packfiledirectory.t - test the PackfileDirectory PMC
10 =head1 SYNOPSIS
12     % prove t/pmc/packfiledirectory.t
14 =head1 DESCRIPTION
16 Tests the PackfileDirectory PMC.
18 =cut
20 .include 't/pmc/testlib/packfile_common.pir'
22 .sub 'main' :main
23 .include 'test_more.pir'
24     plan(20)
26     'test_create'()
27     'test_typeof'()
28     'test_elements'()
29     'test_get_iter'()
30     'test_set_pmc_keyed_str'()
31 .end
33 # Test creation of fresh directory
34 .sub 'test_create'
35     .local pmc dir, seg
36     dir = new 'PackfileDirectory'
37     isa_ok(dir, 'PackfileDirectory')
39     seg = new 'PackfileRawSegment'
40     # We should set owner
41     $P0 = seg.'get_directory'()
42     $I0 = defined $P0
43     $I0 = not $I0
44     ok($I0, "Owner of fresh segment unknown")
46     dir['RAWSEGMENT'] = seg
48     # We should set owner
49     $P0 = seg.'get_directory'()
50     $I0 = defined $P0
51     ok($I0, "Owner of segment set correctly")
52 .end
54 # PackfileDirectory.typeof
55 .sub 'test_typeof'
56     .local pmc pf
57     pf = new ['Packfile']
58     $P1 = pf.'get_directory'()
59     isa_ok($P1, 'PackfileDirectory', 'PackfileDirectory.get_directory')
60 .end
62 # PackfileDirectory.elements
63 .sub 'test_elements'
64     .local pmc pf, pfdir
65     pf    = _pbc()
66     pfdir = pf.'get_directory'()
67     $I0   = elements pfdir
68     is($I0, 4, 'PackfileDirectory.elements')
69 .end
72 # PackfileDirectory.get_iter
73 .sub 'test_get_iter'
74     .local pmc pf, pfdir, it, expected
75     .local string name
77     # expected contains all expected segment "prefixes" with count
78     expected = new 'Hash'
79     expected["BYTECODE"] = 2
80     expected["FIXUP"]    = 1
81     expected["CONSTANT"] = 1
83     pf    = _pbc()
84     pfdir = pf.'get_directory'()
85     $I0   = elements pfdir
86     it    = iter pfdir
87   loop:
88     unless it goto done
89     name = shift it
91     # Get prefix
92     $P0 = split '_', name
93     $S0 = shift $P0
94     $I0 = expected[$S0]
95     ok($I0, $S0)
96     # Decrease expectation count
97     dec $I0
98     expected[$S0] = $I0
100     $P1 = pfdir[name]
101     isa_ok($P1, 'PackfileSegment')
102     $P2 = $P1.'get_directory'()
103     $I0 = defined $P2
104     ok($I0, "Loaded Segment has proper directory")
105     goto loop
106   done:
107     .return ()
108 .end
110 ## PackfileDirectory.set_pmc_keyed_str
111 .sub 'test_set_pmc_keyed_str'
112     .local pmc pf, pfdir, seg
113     pf    = _pbc()
114     pfdir = pf.'get_directory'()
115     seg   = new [ 'PackfileRawSegment' ]
117     # Adding segment with same name replaces old one
118     $I0 = elements pfdir
119     $P0 = iter pfdir
120     $S0 = shift $P0
121     pfdir[$S0] = seg
122     $I1   = elements pfdir
123     is($I0, $I1, "Segment with old name was added")
125     # Add segment with new name
126   add_new:
127     seg = new [ 'PackfileRawSegment' ]
128     $S0 = 'BYTECODE_foo'
129     pfdir[$S0] = seg
130     $I1   = elements pfdir
131     inc $I0
132     is($I0, $I1, "New segment added")
134     # Remove that segment again
135   delete_seg:
136     delete pfdir[$S0]
137     dec $I0
138     $I1 = elements pfdir
139     is($I0, $I1, "segment deleted")
141   done:
142     .return()
143 .end
145 # Local Variables:
146 #   mode: cperl
147 #   cperl-indent-level: 4
148 #   fill-column: 100
149 # End:
150 # vim: expandtab shiftwidth=4: