1#!/usr/bin/perl
2# (C) 2007 Jelmer Vernooij <jelmer@samba.org>
3# Published under the GNU General Public License
4use strict;
5use warnings;
6
7use Test::More tests => 56;
8use FindBin qw($RealBin);
9use lib "$RealBin";
10use Util;
11use Parse::Pidl::Typelist qw(hasType typeHasBody getType mapTypeName expandAlias
12	mapScalarType addType typeIs is_scalar scalar_is_reference
13	enum_type_fn bitmap_type_fn mapType);
14
15is("foo", expandAlias("foo"));
16is("uint32", expandAlias("DWORD"));
17is("int32", expandAlias("int"));
18is("", expandAlias(""));
19is("int32", expandAlias("int32"));
20
21is("uint32_t", mapScalarType("uint32"));
22is("void", mapScalarType("void"));
23is("uint64_t", mapScalarType("hyper"));
24is("double", mapScalarType("double"));
25
26my $x = { TYPE => "ENUM", NAME => "foo", EXTRADATA => 1 };
27addType($x);
28is_deeply($x, getType("foo"));
29is(undef, getType("bloebla"));
30is_deeply(getType({ TYPE => "STRUCT" }), { TYPE => "STRUCT" });
31is_deeply(getType({ TYPE => "ENUM", NAME => "foo" }), $x);
32is_deeply(getType("uint16"), {
33		NAME => "uint16",
34		BASEFILE => "<builtin>",
35		TYPE => "TYPEDEF",
36		DATA => { NAME => "uint16", TYPE => "SCALAR" }});
37
38is_deeply(getType("double"), {
39		NAME => "double",
40		BASEFILE => "<builtin>",
41		TYPE => "TYPEDEF",
42		DATA => { NAME => "double", TYPE => "SCALAR" }});
43
44is(0, typeIs("someUnknownType", "ENUM"));
45is(0, typeIs("foo", "ENUM"));
46addType({NAME => "mytypedef", TYPE => "TYPEDEF", DATA => { TYPE => "ENUM" }});
47is(1, typeIs("mytypedef", "ENUM"));
48is(0, typeIs("mytypedef", "BITMAP"));
49is(1, typeIs({ TYPE => "ENUM"}, "ENUM"));
50is(0, typeIs({ TYPE => "BITMAP"}, "ENUM"));
51is(1, typeIs("uint32", "SCALAR"));
52is(0, typeIs("uint32", "ENUM"));
53
54is(1, hasType("foo"));
55is(0, hasType("nonexistent"));
56is(0, hasType({TYPE => "ENUM", NAME => "someUnknownType"}));
57is(1, hasType({TYPE => "ENUM", NAME => "foo"}));
58is(1, hasType({TYPE => "ENUM"}));
59is(1, hasType({TYPE => "STRUCT"}));
60
61is(1, is_scalar("uint32"));
62is(0, is_scalar("nonexistent"));
63is(1, is_scalar({TYPE => "ENUM"}));
64is(0, is_scalar({TYPE => "STRUCT"}));
65is(1, is_scalar({TYPE => "TYPEDEF", DATA => {TYPE => "ENUM" }}));
66is(1, is_scalar("mytypedef"));
67
68is(1, scalar_is_reference("string"));
69is(0, scalar_is_reference("uint32"));
70is(0, scalar_is_reference({TYPE => "STRUCT", NAME => "echo_foobar"}));
71
72is("uint8", enum_type_fn({TYPE => "ENUM", PARENT=>{PROPERTIES => {enum8bit => 1}}}));
73is("uint32", enum_type_fn({TYPE => "ENUM", PARENT=>{PROPERTIES => {v1_enum => 1}}}));
74is("uint1632", enum_type_fn({TYPE => "ENUM", PARENT=>{PROPERTIES => {}}}));
75
76is("uint8", bitmap_type_fn({TYPE => "BITMAP", PROPERTIES => {bitmap8bit => 1}}));
77is("uint16", bitmap_type_fn({TYPE => "BITMAP", PROPERTIES => {bitmap16bit => 1}}));
78is("hyper", bitmap_type_fn({TYPE => "BITMAP", PROPERTIES => {bitmap64bit => 1}}));
79is("uint32", bitmap_type_fn({TYPE => "BITMAP", PROPERTIES => {}}));
80
81is("enum foo", mapType({TYPE => "ENUM"}, "foo"));
82is("union foo", mapType({TYPE => "UNION"}, "foo"));
83is("struct foo", mapType({TYPE => "STRUCT"}, "foo"));
84is("uint8_t", mapType({TYPE => "BITMAP", PROPERTIES => {bitmap8bit => 1}}, "foo"));
85is("uint8_t", mapType({TYPE => "SCALAR"}, "uint8"));
86is("uint32_t", mapType({TYPE => "TYPEDEF", DATA => {TYPE => "SCALAR"}}, "uint32"));
87
88is("void", mapTypeName(undef));
89is("uint32_t", mapTypeName("uint32"));
90is("int32_t", mapTypeName("int"));
91
92ok(not typeHasBody({TYPE => "TYPEDEF", DATA => { TYPE => "STRUCT" }}));
93ok(typeHasBody({TYPE => "TYPEDEF", DATA => { TYPE => "STRUCT", ELEMENTS => [] }}));
94