VERSION: Disable GIT_SNAPSHOT for the 4.16.0rc3 release.
[Samba.git] / pidl / tests / util.pl
blobcb77f34c5101899b87e48c7ae7ef29ed44f2cf99
1 #!/usr/bin/perl
2 # (C) 2007 Jelmer Vernooij <jelmer@samba.org>
3 # Published under the GNU General Public License
4 use strict;
5 use warnings;
7 use Test::More tests => 72;
8 use FindBin qw($RealBin);
9 use lib "$RealBin";
10 use Util;
11 use Parse::Pidl qw(error);
12 use Parse::Pidl::Util;
14 # has_property()
15 is(undef, has_property({}, "foo"));
16 is(undef, has_property({PROPERTIES => {}}, "foo"));
17 is("data", has_property({PROPERTIES => {foo => "data"}}, "foo"));
18 is(undef, has_property({PROPERTIES => {foo => undef}}, "foo"));
20 # is_constant()
21 ok(is_constant("2"));
22 ok(is_constant("256"));
23 ok(is_constant("0x400"));
24 ok(is_constant("0x4BC"));
25 ok(not is_constant("0x4BGC"));
26 ok(not is_constant("str"));
27 ok(not is_constant("2 * expr"));
29 # make_str()
30 is("\"bla\"", make_str("bla"));
31 is("\"bla\"", make_str("\"bla\""));
32 is("\"\"bla\"\"", make_str("\"\"bla\"\""));
33 is("\"bla\"\"", make_str("bla\""));
34 is("\"foo\"bar\"", make_str("foo\"bar"));
36 is("bla", unmake_str("\"bla\""));
37 is("\"bla\"", unmake_str("\"\"bla\"\""));
39 # print_uuid()
40 is(undef, print_uuid("invalid"));
41 is("{0x12345778,0x1234,0xabcd,{0xef,0x00},{0x01,0x23,0x45,0x67,0x89,0xac}}",
42 print_uuid("12345778-1234-abcd-ef00-0123456789ac"));
43 is("{0x12345778,0x1234,0xabcd,{0xef,0x00},{0x01,0x23,0x45,0x67,0x89,0xac}}",
44 print_uuid("\"12345778-1234-abcd-ef00-0123456789ac\""));
46 # property_matches()
47 # missing property
48 ok(not property_matches({PROPERTIES => {}}, "x", "data"));
49 # data not matching
50 ok(not property_matches({PROPERTIES => {x => "bar"}}, "x", "data"));
51 # data matching exactly
52 ok(property_matches({PROPERTIES => {x => "data"}}, "x", "data"));
53 # regex matching
54 ok(property_matches({PROPERTIES => {x => "data"}}, "x", "^([dat]+)\$"));
56 # ParseExpr()
57 is(undef, ParseExpr("", {}, undef));
58 is("a", ParseExpr("a", {"b" => "2"}, undef));
59 is("2", ParseExpr("a", {"a" => "2"}, undef));
60 is("2 * 2", ParseExpr("a*a", {"a" => "2"}, undef));
61 is("r->length + r->length",
62 ParseExpr("length+length", {"length" => "r->length"}, undef));
63 is("2 / 2 * (r->length)",
64 ParseExpr("constant/constant*(len)", {"constant" => "2",
65 "len" => "r->length"}, undef));
66 is("2 + 2 - r->length",
67 ParseExpr("constant+constant-len", {"constant" => "2",
68 "len" => "r->length"}, undef));
69 is("*r->length", ParseExpr("*len", { "len" => "r->length"}, undef));
70 is("**r->length", ParseExpr("**len", { "len" => "r->length"}, undef));
71 is("r->length & 2", ParseExpr("len&2", { "len" => "r->length"}, undef));
72 is("&r->length", ParseExpr("&len", { "len" => "r->length"}, undef));
73 is("calc()", ParseExpr("calc()", { "foo" => "2"}, undef));
74 is("calc(2 * 2)", ParseExpr("calc(foo * 2)", { "foo" => "2"}, undef));
75 is("strlen(\"data\")", ParseExpr("strlen(foo)", { "foo" => "\"data\""}, undef));
76 is("strlen(\"data\", 4)", ParseExpr("strlen(foo, 4)", { "foo" => "\"data\""}, undef));
77 is("foo / bar", ParseExpr("foo / bar", { "bla" => "\"data\""}, undef));
78 is("r->length % 2", ParseExpr("len%2", { "len" => "r->length"}, undef));
79 is("r->length == 2", ParseExpr("len==2", { "len" => "r->length"}, undef));
80 is("r->length != 2", ParseExpr("len!=2", { "len" => "r->length"}, undef));
81 is("pr->length", ParseExpr("pr->length", { "p" => "r"}, undef));
82 is("r->length", ParseExpr("p->length", { "p" => "r"}, undef));
83 is("_foo / bla32", ParseExpr("_foo / bla32", { "bla" => "\"data\""}, undef));
84 is("foo.bar.blah", ParseExpr("foo.blah", { "foo" => "foo.bar"}, undef));
85 is("\"bla\"", ParseExpr("\"bla\"", {}, undef));
86 is("1 << 2", ParseExpr("1 << 2", {}, undef));
87 is("1 >> 2", ParseExpr("1 >> 2", {}, undef));
88 is("0x200", ParseExpr("0x200", {}, undef));
89 is("2?3:0", ParseExpr("2?3:0", {}, undef));
90 is("~0", ParseExpr("~0", {}, undef));
91 is("b->a->a", ParseExpr("a->a->a", {"a" => "b"}, undef));
92 is("b.a.a", ParseExpr("a.a.a", {"a" => "b"}, undef));
94 test_errors("nofile:0: Parse error in `~' near `~'\n", sub {
95 is(undef, ParseExpr("~", {}, {FILE => "nofile", LINE => 0})); });
97 test_errors("nofile:0: Got pointer, expected integer\n", sub {
98 is(undef, ParseExprExt("foo", {}, {FILE => "nofile", LINE => 0},
99 undef, sub { my $x = shift;
100 error({FILE => "nofile", LINE => 0},
101 "Got pointer, expected integer");
102 return undef; }))});
104 is("b.a.a", ParseExpr("b.a.a", {"a" => "b"}, undef));
105 is("((rr_type) == NBT_QTYPE_NETBIOS)", ParseExpr("((rr_type)==NBT_QTYPE_NETBIOS)", {}, undef));
106 is("talloc_check_name", ParseExpr("talloc_check_name", {}, undef));
107 is("talloc_check_name()", ParseExpr("talloc_check_name()", {}, undef));
108 is("talloc_check_name(ndr)", ParseExpr("talloc_check_name(ndr)", {}, undef));
109 is("talloc_check_name(ndr, 1)", ParseExpr("talloc_check_name(ndr,1)", {}, undef));
110 is("talloc_check_name(ndr, \"struct ndr_push\")", ParseExpr("talloc_check_name(ndr,\"struct ndr_push\")", {}, undef));
111 is("((rr_type) == NBT_QTYPE_NETBIOS) && talloc_check_name(ndr, \"struct ndr_push\")", ParseExpr("((rr_type)==NBT_QTYPE_NETBIOS)&&talloc_check_name(ndr,\"struct ndr_push\")", {}, undef));
112 is("(rdata).data.length", ParseExpr("(rdata).data.length", {}, undef));
113 is("((rdata).data.length == 2)", ParseExpr("((rdata).data.length==2)", {}, undef));
114 is("((rdata).data.length == 2)?0:rr_type", ParseExpr("((rdata).data.length==2)?0:rr_type", {}, undef));
115 is("((((rr_type) == NBT_QTYPE_NETBIOS) && talloc_check_name(ndr, \"struct ndr_push\") && ((rdata).data.length == 2))?0:rr_type)", ParseExpr("((((rr_type)==NBT_QTYPE_NETBIOS)&&talloc_check_name(ndr,\"struct ndr_push\")&&((rdata).data.length==2))?0:rr_type)", {}, undef));