Debian Codegraph @

t/ffi_platypus_type.t

raw
1 use Test2::V0 -no_srand => 1;
2 use FFI::Platypus::Internal;
3 use FFI::Platypus::Type;
4 use Data::Dumper qw( Dumper );
5
6 local $Data::Dumper::Sortkeys = 1;
7
8 my $pointer_size = FFI::Platypus->new->sizeof('opaque');
9
10 subtest 'basic type' => sub {
11
12 my $type = FFI::Platypus::TypeParser->create_type_basic(
13 FFI_PL_TYPE_SINT8,
14 );
15
16 isa_ok $type, 'FFI::Platypus::Type';
17 is $type->type_code, FFI_PL_TYPE_SINT8;
18 is $type->sizeof, 1;
19 is $type->is_record, 0;
20 is $type->is_record_value, 0;
21 is $type->kindof, "scalar";
22 is $type->countof, 1;
23 is $type->unitof, undef;
24 note Dumper($type->meta);
25
26 };
27
28 subtest 'fixed string / record (pass by reference)' => sub {
29
30 my $type = FFI::Platypus::TypeParser->create_type_record(
31 0,
32 22,
33 );
34
35 isa_ok $type, 'FFI::Platypus::Type';
36 is $type->type_code, FFI_PL_TYPE_RECORD;
37 is $type->sizeof, 22;
38 is $type->is_record, 1;
39 is $type->is_record_value, 0;
40 is $type->kindof, "record";
41 is $type->countof, 1;
42 is $type->unitof, undef;
43 note Dumper($type->meta);
44
45 my $custom = FFI::Platypus::TypeParser->_create_type_custom(
46 $type,
47 sub {},
48 sub {},
49 sub {},
50 1,
51 );
52
53 isa_ok $custom, 'FFI::Platypus::Type';
54 is $custom->type_code, FFI_PL_TYPE_RECORD | FFI_PL_SHAPE_CUSTOM_PERL;
55 is $custom->sizeof, 22;
56 is $custom->is_record, 1;
57 is $custom->is_record_value, 0;
58 is $custom->kindof, "record";
59 is $custom->countof, 1;
60 is $type->unitof, undef;
61 note Dumper($custom->meta);
62
63 };
64
65 subtest 'record' => sub {
66
67 {
68 package Foo::Bar;
69 use FFI::Platypus::Record;
70 record_layout(qw(
71 int foo
72 ));
73 }
74
75 subtest 'record class value (pass by value)' => sub {
76
77 my $type = FFI::Platypus::TypeParser->create_type_record(
78 1,
79 Foo::Bar->_ffi_record_size,
80 'Foo::Bar',
81 Foo::Bar->_ffi_meta->ffi_type,
82 );
83
84 isa_ok $type, 'FFI::Platypus::Type';
85 is $type->type_code, FFI_PL_TYPE_RECORD_VALUE;
86 is $type->meta->{ref}, 1;
87 is $type->meta->{class}, 'Foo::Bar';
88 is $type->sizeof, 4;
89 is $type->is_record, 0;
90 is $type->is_record_value, 1;
91 is $type->kindof, "record-value";
92 is $type->countof, 1;
93 is $type->unitof, undef;
94 note Dumper($type->meta);
95
96 my $custom = FFI::Platypus::TypeParser->_create_type_custom(
97 $type,
98 sub {},
99 sub {},
100 sub {},
101 1,
102 );
103
104 isa_ok $custom, 'FFI::Platypus::Type';
105 is $custom->type_code, FFI_PL_TYPE_RECORD_VALUE | FFI_PL_SHAPE_CUSTOM_PERL;
106 is $custom->sizeof, 4;
107 is $custom->is_record, 0;
108 is $custom->is_record_value, 1;
109 is $custom->kindof, "record-value";
110 is $custom->countof, 1;
111 is $type->unitof, undef;
112 note Dumper($custom->meta);
113
114
115 };
116
117 subtest 'record class (pass by reference)' => sub {
118
119 my $type = FFI::Platypus::TypeParser->create_type_record(
120 0,
121 Foo::Bar->_ffi_record_size,
122 'Foo::Bar',
123 );
124
125 isa_ok $type, 'FFI::Platypus::Type';
126 is $type->type_code, FFI_PL_TYPE_RECORD;
127 is $type->meta->{ref}, 1;
128 is $type->sizeof, 4;
129 is $type->is_record, 1;
130 is $type->is_record_value, 0;
131 is $type->kindof, "record";
132 is $type->unitof, undef;
133 is $type->countof, 1;
134 note Dumper($type->meta);
135
136 my $custom = FFI::Platypus::TypeParser->_create_type_custom(
137 $type,
138 sub {},
139 sub {},
140 sub {},
141 1,
142 );
143
144 isa_ok $custom, 'FFI::Platypus::Type';
145 is $custom->type_code, FFI_PL_TYPE_RECORD | FFI_PL_SHAPE_CUSTOM_PERL;
146 is $custom->sizeof, 4;
147 is $custom->is_record, 1;
148 is $custom->is_record_value, 0;
149 is $custom->kindof, "record";
150 is $custom->countof, 1;
151 is $type->unitof, undef;
152 note Dumper($custom->meta);
153
154 };
155 };
156
157 subtest 'string rw' => sub {
158
159 my $type = FFI::Platypus::TypeParser->create_type_string(
160 1,
161 );
162
163 isa_ok $type, 'FFI::Platypus::Type';
164 is $type->type_code, FFI_PL_TYPE_STRING;
165 is $type->meta->{access}, 'rw';
166 is $type->sizeof, $pointer_size;
167 is $type->is_record, 0;
168 is $type->is_record_value, 0;
169 is $type->kindof, "string";
170 is $type->countof, 1;
171 is $type->unitof, undef;
172 note Dumper($type->meta);
173
174 };
175
176 subtest 'string ro' => sub {
177
178 my $type = FFI::Platypus::TypeParser->create_type_string(
179 0,
180 );
181
182 isa_ok $type, 'FFI::Platypus::Type';
183 is $type->type_code, FFI_PL_TYPE_STRING;
184 is $type->meta->{access}, 'ro';
185 is $type->sizeof, $pointer_size;
186 is $type->is_record, 0;
187 is $type->is_record_value,0;
188 is $type->kindof, "string";
189 is $type->countof, 1;
190 is $type->unitof, undef;
191 note Dumper($type->meta);
192
193 };
194
195 subtest 'fixed array' => sub {
196
197 my $type = FFI::Platypus::TypeParser->create_type_array(
198 FFI_PL_TYPE_SINT8,
199 10,
200 );
201
202 isa_ok $type, 'FFI::Platypus::Type';
203 is $type->type_code, FFI_PL_TYPE_SINT8 | FFI_PL_SHAPE_ARRAY;
204 is $type->meta->{size}, 10;
205 is $type->sizeof, 10;
206 is $type->is_record, 0;
207 is $type->is_record_value,0;
208 is $type->kindof, "array";
209 is $type->countof, 10;
210 is $type->unitof, 'sint8';
211 note Dumper($type->meta);
212
213 };
214
215 subtest 'var array' => sub {
216
217 my $type = FFI::Platypus::TypeParser->create_type_array(
218 FFI_PL_TYPE_SINT8,
219 0,
220 );
221
222 isa_ok $type, 'FFI::Platypus::Type';
223 is $type->type_code, FFI_PL_TYPE_SINT8 | FFI_PL_SHAPE_ARRAY;
224 is $type->meta->{size}, 0;
225 is $type->is_record, 0;
226 is $type->is_record_value,0;
227 is $type->kindof, "array";
228 is $type->countof, 0;
229 is $type->unitof, 'sint8';
230 note Dumper($type->meta);
231
232 };
233
234 subtest 'pointer' => sub {
235
236 my $type = FFI::Platypus::TypeParser->create_type_pointer(
237 FFI_PL_TYPE_SINT8,
238 );
239
240 isa_ok $type, 'FFI::Platypus::Type';
241 is $type->type_code, FFI_PL_TYPE_SINT8 | FFI_PL_SHAPE_POINTER;
242 is $type->sizeof, $pointer_size;
243 is $type->is_record, 0;
244 is $type->is_record_value,0;
245 is $type->kindof, "pointer";
246 is $type->countof, 1;
247 is $type->unitof, 'sint8';
248 note Dumper($type->meta);
249
250 };
251
252 #_create_type_custom(self, type, perl_to_native, native_to_perl, perl_to_native_post, argument_count)
253
254 subtest 'custom type' => sub {
255
256 my $basis = FFI::Platypus::TypeParser->create_type_basic(
257 FFI_PL_TYPE_SINT8,
258 );
259
260 my $type = FFI::Platypus::TypeParser->_create_type_custom(
261 $basis,
262 sub {},
263 sub {},
264 sub {},
265 1,
266 );
267
268 isa_ok $type, 'FFI::Platypus::Type';
269 is $type->type_code, FFI_PL_TYPE_SINT8 | FFI_PL_SHAPE_CUSTOM_PERL;
270 is $type->sizeof, 1;
271 is $type->is_record, 0;
272 is $type->is_record_value,0;
273 is $type->kindof, "scalar";
274 is $type->countof, 1;
275 is $type->unitof, undef;
276 note Dumper($type->meta);
277
278 };
279
280 done_testing;