tagged release 0.7.1
[parrot.git] / t / pmc / managedstruct.t
blob7deedb37a60d8ac8307efa59fbffe636a36ae461
1 #! parrot
2 # Copyright (C) 2001-2005, The Perl Foundation.
3 # $Id$
5 =head1 NAME
7 t/pmc/managedstruct.t - Managed C Structure
9 =head1 SYNOPSIS
11     % prove t/pmc/managedstruct.t
13 =head1 DESCRIPTION
15 Tests the ManagedStruct PMC. Checks element access and memory allocation.
17 =cut
19 .sub main :main
20     .include 'include/test_more.pir'
21     plan(24)
23     set_managedstruct_size()
24     element_access()
25     named_element_access_int16()
26     nested_struct_offsets()
27     interface_check()
28 .end
30 .sub set_managedstruct_size
31     new $P0, 'ManagedStruct'
32     set $I0,$P0
33     is($I0, 0, "empty ManagedStruct has size 0")
34     set $P0,1
35     set $I0,$P0
36     is($I0, 1, "non-empty ManagedStruct has correct size")
37     set $P0,2
38     set $I0,$P0
39     is($I0, 2, "non-empty ManagedStruct has correct size")
40 .end
42 #pasm_output_is( <<'CODE', <<'OUTPUT', "element access - float, double" );
43 .sub element_access
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
50     push $P2, 0
51     push $P2, .DATATYPE_DOUBLE
52     push $P2, 0
53     push $P2, 0
54     new $P0, 'ManagedStruct', $P2
55     # must have set size automatically
56     # this is hopefully 2*4+8 everywhere
57     set $I0, $P0
58     is($I0, 16, "ManagedStruct has correct size")
59     set $P0[0;0], 14.1
60     set $N0, $P0[0;0]
61     set $P0[0;1], 14.2
62     set $P0[1], 14.3
63     set $N0, $P0[0;0]
64     is($N0, "14.1", "stored float has correct value")
65     set $N0, $P0[0;1]
66     is($N0, "14.2", "stored float has correct value")
67     set $N0, $P0[1]
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
75     push $P2, 0
76     new $P0, 'ManagedStruct', $P2
77     set $I0, $P0
78     $I1 = ge $I0, 2
79     ok($I1, "ManagedStruct size is at least 2")
81     set $P0[0;0], 1
82     set $P0[0;1], 258   # must be truncated to 2
83     set $I0, $P0[0;0]
84     is($I0, 1, "char val of 1 is correct")
85     set $I0, $P0[0;1]
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
90     push $P2, 1
91     push $P2, 0
92     assign $P0, $P2
93     # should be 2*256+1 or 1*256+2
94     set $I0, $P0[0]
95     $I1 = $I0 == 513
96     $I2 = $I0 == 258
97     $I0 = $I1 | $I2
98     ok($I0, "short val retrieved correctly")
99 .end
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
108     push $P1, 0
109     push $P1, 0
111     set $P1['y'], .DATATYPE_INT16
112     push $P1, 0
113     push $P1, 0
115     # need a ManagedStruct to allocate data memory
116     new $P2, 'ManagedStruct', $P1
118     # calc allocation size
119     set $I0, 0
120     sizeof $I1, .DATATYPE_INT16
121     add $I0, $I1
122     add $I0, $I1
123     # set size
124     set $P2, $I0
126     # set struct values by name
127     set $I0, 2
128     set $P2["x"], $I0
130     set $I1, 16
131     set $S0, "y"
132     set $P2[$S0], $I1
134     # get struct values by struct item idx
135     set $I2, $P2[0]
136     set $I3, $P2[1]
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
142     set $I2, $P2["x"]
143     set $I3, $P2["y"]
145     is($I2, 2, "'x' value by name is correct")
146     is($I3, 16, "'y' value by name is correct")
147 .end
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
155     push $P3, 0
156     push $P3, 0
157     push $P3, .DATATYPE_INT
158     push $P3, 0
159     push $P3, 0
160     new $P4, 'UnManagedStruct', $P3
161     # outer structure
162     new $P2, 'ResizablePMCArray'
163     push $P2, .DATATYPE_INT
164     push $P2, 0
165     push $P2, 0
166     push $P2, .DATATYPE_STRUCT
167     # attach the unmanged struct as property
168     set $P1, $P2[-1]
169     setprop $P1, "_struct", $P4
170     push $P2, 0
171     push $P2, 0
172     push $P2, .DATATYPE_INT
173     push $P2, 0
174     push $P2, 0
175     # attach struct initializer
176     new $P5, 'UnManagedStruct', $P2
178     # now check offsets
179     set $I0, $P2[2]
180     is($I0, 0, "offset 2 looks good")
181     set $I0, $P2[5]
182     is($I0, 4, "offset 5 looks good")
183     set $I0, $P2[8]
184     is($I0, 12, "offset 8 looks good")
185     # nested
186     set $I0, $P3[2]
187     is($I0, 0, "nested offest 2 looks good")
188     set $I0, $P3[5]
189     is($I0, 4, "nested offset 5 looks good")
191     # check struct size
192     set $I0, $P5
193     is($I0, 16, "struct size looks good")
194     # nested
195     set $I0, $P4
196     is($I0, 8, "nested struct size looks good")
197 .end
199 .sub interface_check
200     .local pmc pmc1
201     pmc1 = new 'ManagedStruct'
202     .local int bool1
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")
207 .end
209 # Local Variables:
210 #   mode: pir
211 #   fill-column: 100
212 # End:
213 # vim: expandtab shiftwidth=4 ft=pir: