2 # Copyright (C) 2001-2008, Parrot 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 '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")
65 cmp $I0, $N1, 0.000001
66 ok($I0, "stored float has correct value")
69 cmp $I0, $N1, 0.000001
70 ok($I0, "stored float has correct value")
73 cmp $I0, $N1, 0.000001
74 ok($I0, "stored float has correct value")
77 #element access - char, short
78 new $P2, ['ResizablePMCArray']
79 push $P2, .DATATYPE_CHAR
80 push $P2, 2 # 2 elem char array
82 new $P0, ['ManagedStruct'], $P2
85 ok($I1, "ManagedStruct size is at least 2")
88 set $P0[0;1], 258 # must be truncated to 2
90 is($I0, 1, "char val of 1 is correct")
92 is($I0, 2, "char val of 258 retrieved as 2")
93 # now acces that as a short
94 new $P2, ['ResizablePMCArray']
95 push $P2, .DATATYPE_SHORT
99 # should be 2*256+1 or 1*256+2
104 ok($I0, "short val retrieved correctly")
107 .sub named_element_access_int16
108 new $P1, ['OrderedHash']
109 set $P1['x'], .DATATYPE_INT16
113 set $P1['y'], .DATATYPE_INT16
117 # need a ManagedStruct to allocate data memory
118 new $P2, ['ManagedStruct'], $P1
120 # set struct values by name
128 # get struct values by struct item idx
132 is($I2, 2, "'x' value by idx is correct")
133 is($I3, 16, "'y' value by idx is correct")
135 # get struct values by name
139 is($I2, 2, "'x' value by name is correct")
140 is($I3, 16, "'y' value by name is correct")
143 #pasm_output_is( <<'CODE', <<'OUTPUT', "nested struct offsets" );
144 .sub nested_struct_offsets
146 # the nested structure
147 new $P3, ['ResizablePMCArray']
148 push $P3, .DATATYPE_INT
151 push $P3, .DATATYPE_INT
154 new $P4, ['UnManagedStruct'], $P3
156 new $P2, ['ResizablePMCArray']
157 push $P2, .DATATYPE_INT
160 push $P2, .DATATYPE_STRUCT
161 # attach the unmanged struct as property
163 setprop $P1, "_struct", $P4
166 push $P2, .DATATYPE_INT
169 # attach struct initializer
170 new $P5, ['UnManagedStruct'], $P2
174 is($I0, 0, "offset 2 looks good")
176 is($I0, 4, "offset 5 looks good")
178 is($I0, 12, "offset 8 looks good")
181 is($I0, 0, "nested offest 2 looks good")
183 is($I0, 4, "nested offset 5 looks good")
187 is($I0, 16, "struct size looks good")
190 is($I0, 8, "nested struct size looks good")
195 pmc1 = new ['ManagedStruct']
197 does bool1, pmc1, "scalar"
198 is(bool1, 1, "ManagedStruct does scalar")
199 does bool1, pmc1, "no_interface"
200 is(bool1, 0, "ManagedStruct doesn't do no_interface")
207 # vim: expandtab shiftwidth=4 ft=pir: