2 # Copyright (C) 2001-2005, The Perl Foundation.
7 t/pmc/managedstruct.t - Managed C Structure
11 % prove t/pmc/managedstruct.t
15 Tests the ManagedStruct PMC. Checks element access and memory allocation.
20 .include 'include/test_more.pir'
23 set_managedstruct_size()
25 named_element_access_int16()
26 nested_struct_offsets()
30 .sub set_managedstruct_size
31 new $P0, 'ManagedStruct'
33 is($I0, 0, "empty ManagedStruct has size 0")
36 is($I0, 1, "non-empty ManagedStruct has correct size")
39 is($I0, 2, "non-empty ManagedStruct has correct size")
42 #pasm_output_is( <<'CODE', <<'OUTPUT', "element access - float, double" );
45 #element access - float, double
46 new $P2, 'ResizablePMCArray'
47 .include "datatypes.pasm"
48 push $P2, .DATATYPE_FLOAT
49 push $P2, 2 # 2 elem array
51 push $P2, .DATATYPE_DOUBLE
54 new $P0, 'ManagedStruct', $P2
55 # must have set size automatically
56 # this is hopefully 2*4+8 everywhere
58 is($I0, 16, "ManagedStruct has correct size")
64 is($N0, "14.1", "stored float has correct value")
66 is($N0, "14.2", "stored float has correct value")
68 is($N0, "14.3", "stored float has correct value")
71 #element access - char, short
72 new $P2, 'ResizablePMCArray'
73 push $P2, .DATATYPE_CHAR
74 push $P2, 2 # 2 elem char array
76 new $P0, 'ManagedStruct', $P2
79 ok($I1, "ManagedStruct size is at least 2")
82 set $P0[0;1], 258 # must be truncated to 2
84 is($I0, 1, "char val of 1 is correct")
86 is($I0, 2, "char val of 258 retrieved as 2")
87 # now acces that as a short
88 new $P2, 'ResizablePMCArray'
89 push $P2, .DATATYPE_SHORT
93 # should be 2*256+1 or 1*256+2
98 ok($I0, "short val retrieved correctly")
101 #pir_output_is( <<'CODE', <<'OUTPUT', "named element access int16" );
103 #.include "datatypes.pasm"
105 .sub named_element_access_int16
106 new $P1, 'OrderedHash'
107 set $P1['x'], .DATATYPE_INT16
111 set $P1['y'], .DATATYPE_INT16
115 # need a ManagedStruct to allocate data memory
116 new $P2, 'ManagedStruct', $P1
118 # calc allocation size
120 sizeof $I1, .DATATYPE_INT16
126 # set struct values by name
134 # get struct values by struct item idx
138 is($I2, 2, "'x' value by idx is correct")
139 is($I3, 16, "'y' value by idx is correct")
141 # get struct values by name
145 is($I2, 2, "'x' value by name is correct")
146 is($I3, 16, "'y' value by name is correct")
149 #pasm_output_is( <<'CODE', <<'OUTPUT', "nested struct offsets" );
150 .sub nested_struct_offsets
152 # the nested structure
153 new $P3, 'ResizablePMCArray'
154 push $P3, .DATATYPE_INT
157 push $P3, .DATATYPE_INT
160 new $P4, 'UnManagedStruct', $P3
162 new $P2, 'ResizablePMCArray'
163 push $P2, .DATATYPE_INT
166 push $P2, .DATATYPE_STRUCT
167 # attach the unmanged struct as property
169 setprop $P1, "_struct", $P4
172 push $P2, .DATATYPE_INT
175 # attach struct initializer
176 new $P5, 'UnManagedStruct', $P2
180 is($I0, 0, "offset 2 looks good")
182 is($I0, 4, "offset 5 looks good")
184 is($I0, 12, "offset 8 looks good")
187 is($I0, 0, "nested offest 2 looks good")
189 is($I0, 4, "nested offset 5 looks good")
193 is($I0, 16, "struct size looks good")
196 is($I0, 8, "nested struct size looks good")
201 pmc1 = new 'ManagedStruct'
203 does bool1, pmc1, "scalar"
204 is(bool1, 1, "ManagedStruct does scalar")
205 does bool1, pmc1, "no_interface"
206 is(bool1, 0, "ManagedStruct doesn't do no_interface")
213 # vim: expandtab shiftwidth=4 ft=pir: