19cb90c08082fa450932caf7babfccd5c952f80c
[gd/samba-autobuild/.git] / source4 / pidl / tests / util.pl
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;
6
7 use Test::More tests => 56;
8 use FindBin qw($RealBin);
9 use lib "$RealBin";
10 use Util;
11 use Parse::Pidl::Util;
12
13 # has_property()
14 is(undef, has_property({}, "foo"));
15 is(undef, has_property({PROPERTIES => {}}, "foo"));
16 is("data", has_property({PROPERTIES => {foo => "data"}}, "foo"));
17 is(undef, has_property({PROPERTIES => {foo => undef}}, "foo"));
18
19 # is_constant()
20 ok(is_constant("2"));
21 ok(is_constant("256"));
22 ok(is_constant("0x400"));
23 ok(is_constant("0x4BC"));
24 ok(not is_constant("0x4BGC"));
25 ok(not is_constant("str"));
26 ok(not is_constant("2 * expr"));
27
28 # make_str()
29 is("\"bla\"", make_str("bla"));
30 is("\"bla\"", make_str("\"bla\""));
31 is("\"\"bla\"\"", make_str("\"\"bla\"\""));
32 is("\"bla\"\"", make_str("bla\""));
33 is("\"foo\"bar\"", make_str("foo\"bar"));
34
35 # print_uuid()
36 is(undef, print_uuid("invalid"));
37 is("{0x12345778,0x1234,0xabcd,{0xef,0x00},{0x01,0x23,0x45,0x67,0x89,0xac}}", 
38    print_uuid("12345778-1234-abcd-ef00-0123456789ac"));
39 is("{0x12345778,0x1234,0xabcd,{0xef,0x00},{0x01,0x23,0x45,0x67,0x89,0xac}}", 
40    print_uuid("\"12345778-1234-abcd-ef00-0123456789ac\""));
41
42 # property_matches()
43 # missing property
44 ok(not property_matches({PROPERTIES => {}}, "x", "data"));
45 # data not matching
46 ok(not property_matches({PROPERTIES => {x => "bar"}}, "x", "data"));
47 # data matching exactly
48 ok(property_matches({PROPERTIES => {x => "data"}}, "x", "data"));
49 # regex matching
50 ok(property_matches({PROPERTIES => {x => "data"}}, "x", "^([dat]+)\$"));
51
52 # ParseExpr()
53 is(undef, ParseExpr("", {}, undef));
54 is("a", ParseExpr("a", {"b" => "2"}, undef));
55 is("2", ParseExpr("a", {"a" => "2"}, undef));
56 is("2 * 2", ParseExpr("a*a", {"a" => "2"}, undef));
57 is("r->length + r->length", 
58    ParseExpr("length+length", {"length" => "r->length"}, undef));
59 is("2 / 2 * (r->length)", 
60         ParseExpr("constant/constant*(len)", {"constant" => "2", 
61                                                       "len" => "r->length"}, undef));
62 is("2 + 2 - r->length", 
63         ParseExpr("constant+constant-len", {"constant" => "2", 
64                                                       "len" => "r->length"}, undef));
65 is("*r->length", ParseExpr("*len", { "len" => "r->length"}, undef));
66 is("**r->length", ParseExpr("**len", { "len" => "r->length"}, undef));
67 is("r->length & 2", ParseExpr("len&2", { "len" => "r->length"}, undef));
68 is("&r->length", ParseExpr("&len", { "len" => "r->length"}, undef));
69 is("calc()", ParseExpr("calc()", { "foo" => "2"}, undef));
70 is("calc(2 * 2)", ParseExpr("calc(foo * 2)", { "foo" => "2"}, undef));
71 is("strlen(\"data\")", ParseExpr("strlen(foo)", { "foo" => "\"data\""}, undef));
72 is("strlen(\"data\", 4)", ParseExpr("strlen(foo, 4)", { "foo" => "\"data\""}, undef));
73 is("foo / bar", ParseExpr("foo / bar", { "bla" => "\"data\""}, undef));
74 is("r->length % 2", ParseExpr("len%2", { "len" => "r->length"}, undef));
75 is("r->length == 2", ParseExpr("len==2", { "len" => "r->length"}, undef));
76 is("r->length != 2", ParseExpr("len!=2", { "len" => "r->length"}, undef));
77 is("pr->length", ParseExpr("pr->length", { "p" => "r"}, undef));
78 is("r->length", ParseExpr("p->length", { "p" => "r"}, undef));
79 is("_foo / bla32", ParseExpr("_foo / bla32", { "bla" => "\"data\""}, undef));
80 is("foo.bar.blah", ParseExpr("foo.blah", { "foo" => "foo.bar"}, undef));
81 is("\"bla\"", ParseExpr("\"bla\"", {}, undef));
82 is("1 << 2", ParseExpr("1 << 2", {}, undef));
83 is("1 >> 2", ParseExpr("1 >> 2", {}, undef));
84 is("0x200", ParseExpr("0x200", {}, undef));
85 is("2?3:0", ParseExpr("2?3:0", {}, undef));
86 is("~0", ParseExpr("~0", {}, undef));
87 is("b->a->a", ParseExpr("a->a->a", {"a" => "b"}, undef));
88 is("b.a.a", ParseExpr("a.a.a", {"a" => "b"}, undef));
89
90 test_errors("nofile:0: Parse error in `~' near `~'\n", sub {
91         is(undef, ParseExpr("~", {}, {FILE => "nofile", LINE => 0})); });