2 # (C) 2007 Jelmer Vernooij <jelmer@samba.org>
3 # Published under the GNU General Public License
7 use Test::More tests => 46;
8 use FindBin qw($RealBin);
11 use Parse::Pidl::Util qw(MyDumper);
12 use Parse::Pidl::NDR qw(GetElementLevelTable ParseElement align_type mapToScalar ParseType can_contain_deferred);
22 'PARENT' => { TYPE => 'STRUCT' },
25 is_deeply(GetElementLevelTable($e, "unique"), [
29 'DATA_TYPE' => 'uint8',
30 'CONTAINS_DEFERRED' => 0,
32 'IS_SURROUNDING' => 0,
36 my $ne = ParseElement($e, "unique");
37 is($ne->{ORIGINAL}, $e);
40 is($ne->{TYPE}, "uint8");
41 is_deeply($ne->{LEVELS}, [
45 'DATA_TYPE' => 'uint8',
46 'CONTAINS_DEFERRED' => 0,
48 'IS_SURROUNDING' => 0,
57 'PROPERTIES' => {"unique" => 1},
59 'PARENT' => { TYPE => 'STRUCT' },
63 is_deeply(GetElementLevelTable($e, "unique"), [
68 POINTER_TYPE => "unique",
75 'DATA_TYPE' => 'uint8',
76 'CONTAINS_DEFERRED' => 0,
78 'IS_SURROUNDING' => 0,
82 # Case 3 : double pointers
87 'PROPERTIES' => {"unique" => 1},
90 'PARENT' => { TYPE => 'STRUCT' },
93 is_deeply(GetElementLevelTable($e, "unique"), [
98 POINTER_TYPE => "unique",
106 POINTER_TYPE => "unique",
113 'DATA_TYPE' => 'uint8',
114 'CONTAINS_DEFERRED' => 0,
116 'IS_SURROUNDING' => 0,
120 # Case 3 : ref pointers
125 'PROPERTIES' => {"ref" => 1},
128 'PARENT' => { TYPE => 'STRUCT' },
131 is_deeply(GetElementLevelTable($e, "unique"), [
136 POINTER_TYPE => "ref",
143 'DATA_TYPE' => 'uint8',
144 'CONTAINS_DEFERRED' => 0,
146 'IS_SURROUNDING' => 0,
150 # Case 3 : ref pointers
155 'PROPERTIES' => {"ref" => 1},
158 'PARENT' => { TYPE => 'STRUCT' },
161 is_deeply(GetElementLevelTable($e, "unique"), [
166 POINTER_TYPE => "ref",
174 POINTER_TYPE => "unique",
182 POINTER_TYPE => "unique",
189 'DATA_TYPE' => 'uint8',
190 'CONTAINS_DEFERRED' => 0,
192 'IS_SURROUNDING' => 0,
196 # Case 3 : ref pointers
201 'PROPERTIES' => {"ref" => 1},
204 'PARENT' => { TYPE => 'STRUCT' },
207 is_deeply(GetElementLevelTable($e, "ref"), [
212 POINTER_TYPE => "ref",
220 POINTER_TYPE => "ref",
228 POINTER_TYPE => "ref",
235 'DATA_TYPE' => 'uint8',
236 'CONTAINS_DEFERRED' => 0,
238 'IS_SURROUNDING' => 0,
242 # Case 4 : top-level ref pointers
247 'PROPERTIES' => {"ref" => 1},
250 'PARENT' => { TYPE => 'FUNCTION' },
253 is_deeply(GetElementLevelTable($e, "unique"), [
258 POINTER_TYPE => "ref",
265 'DATA_TYPE' => 'uint8',
266 'CONTAINS_DEFERRED' => 0,
268 'IS_SURROUNDING' => 0,
272 # Case 4 : top-level ref pointers, triple with pointer_default("unique")
277 'PROPERTIES' => {"ref" => 1},
280 'PARENT' => { TYPE => 'FUNCTION' },
283 is_deeply(GetElementLevelTable($e, "unique"), [
288 POINTER_TYPE => "ref",
296 POINTER_TYPE => "unique",
304 POINTER_TYPE => "unique",
311 'DATA_TYPE' => 'uint8',
312 'CONTAINS_DEFERRED' => 0,
314 'IS_SURROUNDING' => 0,
318 # Case 4 : top-level unique pointers, triple with pointer_default("unique")
323 'PROPERTIES' => {"unique" => 1, "in" => 1},
326 'PARENT' => { TYPE => 'FUNCTION' },
329 is_deeply(GetElementLevelTable($e, "unique"), [
334 POINTER_TYPE => "unique",
342 POINTER_TYPE => "unique",
350 POINTER_TYPE => "unique",
357 'DATA_TYPE' => 'uint8',
358 'CONTAINS_DEFERRED' => 0,
360 'IS_SURROUNDING' => 0,
364 # Case 4 : top-level unique pointers, triple with pointer_default("ref")
369 'PROPERTIES' => {"unique" => 1, "in" => 1},
372 'PARENT' => { TYPE => 'FUNCTION' },
375 is_deeply(GetElementLevelTable($e, "ref"), [
380 POINTER_TYPE => "unique",
388 POINTER_TYPE => "ref",
396 POINTER_TYPE => "ref",
403 'DATA_TYPE' => 'uint8',
404 'CONTAINS_DEFERRED' => 0,
406 'IS_SURROUNDING' => 0,
410 # Case 4 : top-level ref pointers, triple with pointer_default("ref")
415 'PROPERTIES' => {"ref" => 1},
418 'PARENT' => { TYPE => 'FUNCTION' },
421 is_deeply(GetElementLevelTable($e, "ref"), [
426 POINTER_TYPE => "ref",
434 POINTER_TYPE => "ref",
442 POINTER_TYPE => "ref",
449 'DATA_TYPE' => 'uint8',
450 'CONTAINS_DEFERRED' => 0,
452 'IS_SURROUNDING' => 0,
456 # representation_type
460 'PROPERTIES' => { represent_as => "bar" },
463 'PARENT' => { TYPE => 'STRUCT' },
466 $ne = ParseElement($e, undef);
467 is($ne->{REPRESENTATION_TYPE}, "bar");
469 # representation_type
476 'PARENT' => { TYPE => 'STRUCT' },
479 $ne = ParseElement($e, undef);
480 is($ne->{REPRESENTATION_TYPE}, "uint8");
482 is(align_type("hyper"), 8);
483 is(align_type("uint32"), 4);
484 is(align_type("uint16"), 2);
485 is(align_type("uint8"), 1);
486 is(align_type({ TYPE => "STRUCT", "NAME" => "bla",
487 ELEMENTS => [ { TYPE => "uint16" } ] }), 4);
488 is(align_type({ TYPE => "STRUCT",
489 ELEMENTS => [ { TYPE => "hyper" } ] }), 8);
490 is(align_type({ TYPE => "TYPEDEF", DATA => {
492 ELEMENTS => [ { TYPE => "hyper" } ] }}), 8);
493 # typedef of struct without body
494 is(align_type({ TYPE => "TYPEDEF", DATA => {
495 TYPE => "STRUCT", ELEMENTS => undef }}), 4);
496 # struct without body
497 is(align_type({ TYPE => "STRUCT", ELEMENTS => undef }), 4);
499 is(align_type({ TYPE => "STRUCT", ELEMENTS => [] }), 1);
500 is(align_type({ TYPE => "STRUCT", "NAME" => "bla",
501 ELEMENTS => [ { TYPE => "uint8" } ] }), 4);
503 is(mapToScalar("someverymuchnotexistingtype"), undef);
504 is(mapToScalar("uint32"), "uint32");
505 is(mapToScalar({TYPE => "ENUM", PARENT => { PROPERTIES => { enum8bit => 1 } } }), "uint8");
506 is(mapToScalar({TYPE => "BITMAP", PROPERTIES => { bitmap64bit => 1 } }),
508 is(mapToScalar({TYPE => "TYPEDEF", DATA => {TYPE => "ENUM", PARENT => { PROPERTIES => { enum8bit => 1 } } }}), "uint8");
514 SURROUNDING_ELEMENT => undef,
523 is_deeply(ParseType($t->{ORIGINAL}, "ref"), $t);
528 SWITCH_TYPE => "uint32",
537 is_deeply(ParseType($t->{ORIGINAL}, "ref"), $t);
539 ok(not can_contain_deferred("uint32"));
540 ok(can_contain_deferred("some_unknown_type"));
541 ok(can_contain_deferred({ TYPE => "STRUCT",
542 ELEMENTS => [ { TYPE => "uint32", POINTERS => 40 } ]}));
543 ok(can_contain_deferred({ TYPE => "TYPEDEF",
544 DATA => { TYPE => "STRUCT",
545 ELEMENTS => [ { TYPE => "uint32", POINTERS => 40 } ]}}));
546 ok(not can_contain_deferred({ TYPE => "STRUCT",
547 ELEMENTS => [ { TYPE => "uint32" } ]}));
548 ok(not can_contain_deferred({ TYPE => "TYPEDEF",
549 DATA => { TYPE => "STRUCT",
550 ELEMENTS => [ { TYPE => "uint32" } ]}}));
551 ok(can_contain_deferred({ TYPE => "STRUCT",
552 ELEMENTS => [ { TYPE => "someunknowntype" } ]}));
553 # Make sure the elements for a enum without body aren't filled in
554 ok(not defined(ParseType({TYPE => "ENUM", NAME => "foo" }, "ref")->{ELEMENTS}));
555 # Make sure the elements for a bitmap without body aren't filled in
556 ok(not defined(ParseType({TYPE => "BITMAP", NAME => "foo" }, "ref")->{ELEMENTS}));
557 # Make sure the elements for a union without body aren't filled in
558 ok(not defined(ParseType({TYPE => "UNION", NAME => "foo" }, "ref")->{ELEMENTS}));