2 # Copyright (C) 2006-2010, Parrot Foundation.
7 t/pmc/packfile.t - test the Packfile PMC
13 % prove t/pmc/packfile.t
17 Tests the Packfile PMC.
21 .include 't/pmc/testlib/packfile_common.pir'
22 .include 'except_types.pasm'
25 .include 'test_more.pir'
29 'test_set_string_native'()
32 'test_get_integer_keyed_str'()
33 'test_set_integer_keyed_str'()
34 'test_get_directory'()
36 'test_pack_fresh_packfile'()
39 skip(2, "test_synonyms crash on many platforms. See TT #545")
45 # Packfile constructor
52 # Make sure the mark vtable function is exercised
55 .tailcall _check_header(pf)
59 .sub 'test_set_string_native'
63 eh = new ['ExceptionHandler']
64 eh.'handle_types'(.EXCEPTION_MALFORMED_PACKFILE)
68 pf = 'This is not data with a valid packfile format'
75 is(result, 1, 'set_string_native with invalid data throws')
79 .sub 'test_get_string'
83 ok(1, 'get_string(uuid)')
85 # Requesting unknown key should throw exception
86 eh = new ['ExceptionHandler']
87 eh.'handle_types'(.EXCEPTION_KEY_NOT_FOUND)
88 set_label eh, unknown_key
92 ok(0, "get_string_keyed_int return unknown key")
97 ok(1, "get_string_keyed_int handle unknown key properly")
101 .sub 'test_set_string'
103 pf = new ['Packfile']
104 pf["uuid"] = "fe9ab64082e0f6bbbd7b1e8264127908"
105 ok(1, 'set_string(uuid)')
107 # Special check for 0
110 is($I0, 1, "Length is 1")
114 is($I1, 1, "Fetched length is 1")
116 # Requesting unknown key should throw exception
118 pf["foo"] = "fe9ab64082e0f6bbbd7b1e8264127908"
120 ok(0, "set_string_keyed_int set unknown key")
125 ok(1, "set_string_keyed_int handle unknown key properly")
129 # Compose the message for the given key
133 msg = 'get_integer_keyed_str('
134 msg = concat msg, key
135 msg = concat msg, ')'
138 .sub 'set_keyed_str_msg'
141 msg = 'set_integer_keyed_str('
142 msg = concat msg, key
143 msg = concat msg, ')'
147 # Check the given key in the Packfile pf
148 .sub 'do_get_integer_keyed_str'
153 msg = 'keyed_str_msg'(key)
167 # Create a list of the keys for the integer attributes
170 keys = new ['ResizableStringArray']
171 push keys, 'wordsize'
172 push keys, 'byteorder'
174 push keys, 'version_major'
175 push keys, 'version_minor'
176 push keys, 'version_patch'
177 push keys, 'bytecode_major'
178 push keys, 'bytecode_minor'
179 push keys, 'uuid_type'
183 # Some keys are still not handled in set_integer_keyed_str
184 # Use this list for its test
185 .sub 'integer_keys_s'
187 keys = new ['ResizableStringArray']
188 push keys, 'version_major'
189 push keys, 'version_minor'
190 push keys, 'version_patch'
191 push keys, 'uuid_type'
196 .sub 'test_get_integer_keyed_str'
200 keys = 'integer_keys'()
201 nkeys = elements keys
210 do_get_integer_keyed_str(pf, $S0)
212 if i < nkeys goto nextkey
214 # Requesting unknown key should throw exception
217 ok(0, "get_integer_keyed_str return unknown key")
222 ok(1, "get_integer_keyed_str handle unknown key properly")
225 # On load error report a failure for each test
233 $S0 = keyed_str_msg($S0)
234 report_load_error($P0, $S0)
236 if i < nkeys goto nexterr
238 report_load_error($P0, "get_integer_keyed_str unknown key")
243 .sub 'test_set_integer_keyed_str'
244 .local pmc pf, keys, saved
245 .local int nkeys, i, value, check
246 .local string skey, msg
247 keys = 'integer_keys_s'()
248 nkeys = elements keys
249 pf = new ['Packfile']
250 saved = new ['FixedIntegerArray'], nkeys
252 # For each key get its value, set it modified and save the new value
253 # The modified value may be invalid, but we are not going to pack it,
254 # so it shouldn't fail here.
263 if i < nkeys goto set_next
265 # Read new values and compare with the saved ones
271 msg = 'set_keyed_str_msg'(skey)
272 is(value, check, msg)
274 if i < nkeys goto get_next
284 is(i, 1, "set_integer_keyed_str handle unknown key properly")
289 # Packfile.get_directory
290 .sub 'test_get_directory'
292 pf = new ['Packfile']
293 $P0 = pf.'get_directory'()
294 isa_ok($P0, 'PackfileDirectory')
298 # PackfileSegment.pack (via subclass PackfileDirectory)
299 .sub 'test_get_directory'
304 pfdir = pf.'get_directory'()
308 ok($I1, 'get_directory')
313 report_load_error($P0, 'get_directory')
318 # Packfile.set_string_native
319 # Check that packfile was loaded properly and set various attributes
326 .tailcall _check_header(pf)
330 report_load_error($P0, "Wordsize set")
331 report_load_error($P0, "version_major set")
332 report_load_error($P0, "bytecode_major set")
336 # Helper sub to check fields in Packfile header
340 # wordsize always greater than 0
342 ok($I0, "Wordsize set")
344 # We are living in post-1.0 era.
345 $I0 = pf["version_major"]
346 ok($I0, "version_major set")
348 $I0 = pf["bytecode_major"]
349 ok($I0, "bytecode_major set")
352 # Create very simple Packfile and pack it
353 .sub 'test_pack_fresh_packfile'
356 pfdir = pf.'get_directory'()
357 #$P0 = new 'PackfileConstantTable'
359 $P0 = new 'PackfileFixupTable'
360 $P1 = new 'PackfileFixupEntry'
365 pfdir["FIXUP_t/pmc/packfile.t"] = $P0
367 $P1 = new 'PackfileRawSegment'
368 pfdir["BYTECODE_t/pmc/packfile.t"] = $P1
370 $P2 = new 'PackfileConstantTable'
379 pfdir["CONSTANTS_t/pmc/packfile.t"] = $P2
386 ok(1, "PackFile packed")
388 #$P1 = open "/tmp/1.pbc", "w"
394 ok(1, "PackFile unpacked after pack")
396 $I0 = pf['uuid_type']
397 is($I0, 1, "uuid_type preserved")
399 # Check that FixupTable contains our Entry.
400 $P0 = _get_fixup_table(pf)
402 is($I1, 1, "FixupTable contains one element")
404 isa_ok($P1, "PackfileFixupEntry")
406 is($I0, 42, "FixupEntry offset preserved")
408 is($S0, "The fixup", "FixupEntry name preserved")
410 # Check unpacked ConstTable
411 $P0 = _find_segment_by_type(pf, "PackfileConstantTable")
413 ok($I0, "ConstantTable unpacked")
415 is($I0, 4, " and contains 4 elements")
417 is($N0, 42.0, " first is number")
419 is($S0, "42", " second is string")
421 isa_ok($P1, "Integer")
423 is($I0, 42, " with proper value")
429 # Check that unpack-pack produce correct result.
431 .local string filename, orig
434 $P0 = new ['FileHandle']
437 orig = $P0.'readall'()
440 packfile = new 'Packfile'
444 # Loaded packfile can be from different platform/config,
445 # packing and unpacking again to avoid that differences.
446 .local string first, second
449 .local pmc packfilesecond
450 packfilesecond = new 'Packfile'
451 packfilesecond = first
452 second = packfilesecond
454 is(first, second, 'pack produced same result twice: TT #1614')
459 report_load_error($P0, 'pack produced same result twice')
463 # Test pack/set_string unpack/get_string equivalency
474 is($I0, 0, "pack and get_string are synonyms")
476 # Unpack data in two ways
477 $P0 = new ['Packfile']
479 $P1 = new ['Packfile']
485 is($I0, 0, "unpack and set_string are synonyms")
490 report_load_error($P0, "pack and get_string are synonyms")
491 report_load_error($P0, "unpack and set_string are synonyms")
500 # vim: expandtab shiftwidth=4 ft=pir: