2 # Copyright (C) 2009-2010, Parrot Foundation.
7 t/pmc/packfileconstanttable.t - test the PackfileConstantTable PMC
12 % prove t/pmc/packfileconstanttable.t
16 Tests the PackfileConstantTable PMC.
20 # Having some known data would be helpful, here. For now, just make sure
21 # the values returned from get_type look right, and that the corresponding
22 # fetches for the found types don't crash.
25 .include 't/pmc/testlib/packfile_common.pir'
28 .include 'test_more.pir'
29 .include 'packfile_constants.pasm'
36 'test_get_or_create'()
40 # sanity check we have a PackfileConstantTable
42 .local pmc pbc, pftable
47 pftable = _get_consttable(pbc)
48 isa_ok(pftable, "PackfileConstantTable")
53 report_load_error($P0, "PackfileConstantTable")
58 # PackfileConstantTable.elements
60 .local pmc pf, pftable
65 pftable = _get_consttable(pf)
67 # Make sure the mark vtable is exercised and the content survives
70 size = elements pftable
71 ok(size, "PackfileConstantTable.elements returns non-zero")
76 report_load_error($P0, "PackfileConstantTable.elements returns non-zero")
81 # PackfileConstantTable.get_type and PackfileConstantTable.get_*_keyed_int
83 .local pmc pf, pftable
84 .local int size, this, type
88 pftable = _get_consttable(pf)
89 size = elements pftable
92 type = pftable.'get_type'(this)
93 eq type, .PFC_NONE, next
94 eq type, .PFC_NUMBER, const_num
95 eq type, .PFC_STRING, const_str
96 eq type, .PFC_PMC, const_pmc
97 eq type, .PFC_KEY, const_key
112 $S0 = concat 'constant Key with wrong type: ', $S0
123 ok(1, 'PackfileConstantTable.get_*_int works')
126 ok(0, 'Unknown constant type')
131 report_load_error($P0, 'PackfileConstantTable.get_*_int works')
135 # Test setting constants into PackfileConstantTable
139 ct = new ['PackfileConstantTable']
141 # Initial PackfileConstantTable is empty
143 is(size, 0, "Empty PackfileConstantTable created")
148 is($I0, 1, "String element added")
152 is($I0, 2, "Number elements added")
158 is($I0, 3, "PMC elements added")
160 # Check types of created constants
161 $I0 = ct.'get_type'(0)
162 is($I0, .PFC_STRING, "First element is string")
163 $I0 = ct.'get_type'(1)
164 is($I0, .PFC_NUMBER, "Second element is number")
165 $I0 = ct.'get_type'(2)
166 is($I0, .PFC_PMC, "Third element is PMC")
170 .sub 'test_get_or_create'
172 pfc = new 'PackfileConstantTable'
173 $I1 = pfc.'get_or_create_constant'('foo')
174 $I2 = pfc.'get_or_create_constant'('foo')
175 is($I1, $I2, "get_or_create_constant returs same string value for same key")
177 $I2 = pfc.'get_or_create_constant'('bar')
179 ok($I0, "get_or_create_constant returs different string values for different keys")
182 $I1 = pfc.'get_or_create_constant'(1.0)
183 $I2 = pfc.'get_or_create_constant'(1.0)
184 is($I1, $I2, "get_or_create_constant returs same number value for same key")
186 $I2 = pfc.'get_or_create_constant'(42.1)
188 ok($I0, "get_or_create_constant returs different number values for different keys")
190 $P0 = new ['FixedIntegerArray']
193 $P1 = new ['FixedIntegerArray']
196 $P2 = new ['FixedIntegerArray']
200 $I0 = pfc.'get_or_create_constant'($P0)
201 $I1 = pfc.'get_or_create_constant'($P1)
202 is($I0, $I1, "get_or_create_constant returns same index for equal PMCs")
203 $I2 = pfc.'get_or_create_constant'($P2)
204 isnt($I0, $I2, "get_or_create_constant returns different index for different PMCs")
207 .sub '_get_consttable'
210 dir = pf.'get_directory'()
216 $I0 = isa $P0, 'PackfileConstantTable'
220 die "Can't find ConstantTable in Packfile!"
228 # vim: expandtab shiftwidth=4 ft=pir: