[kdbus] KDBUS_ITEM_PAYLOAD_OFF items are (once again) relative to msg header
[platform/upstream/glib.git] / gio / tests / fileattributematcher.c
1 #include <gio/gio.h>
2
3 static void
4 test_exact (void)
5 {
6   char *exact_matches[] = {
7     "*",
8     "a::*",
9     "a::*,b::*",
10     "a::a,a::b",
11     "a::a,a::b,b::*"
12   };
13
14   GFileAttributeMatcher *matcher;
15   char *s;
16   guint i;
17
18   for (i = 0; i < G_N_ELEMENTS (exact_matches); i++)
19     {
20       matcher = g_file_attribute_matcher_new (exact_matches[i]);
21       s = g_file_attribute_matcher_to_string (matcher);
22       g_assert_cmpstr (exact_matches[i], ==, s);
23       g_free (s);
24       g_file_attribute_matcher_unref (matcher);
25     }
26 }
27
28 static void
29 test_equality (void)
30 {
31   struct {
32     char *expected;
33     char *actual;
34   } equals[] = {
35     /* star makes everything else go away */
36     { "*", "*,*" },
37     { "*", "*,a::*" },
38     { "*", "*,a::b" },
39     { "*", "a::*,*" },
40     { "*", "a::b,*" },
41     { "*", "a::b,*,a::*" },
42     /* a::* makes a::<anything> go away */
43     { "a::*", "a::*,a::*" },
44     { "a::*", "a::*,a::b" },
45     { "a::*", "a::b,a::*" },
46     { "a::*", "a::b,a::*,a::c" },
47     /* a::b does not allow duplicates */
48     { "a::b", "a::b,a::b" },
49     { "a::b,a::c", "a::b,a::c,a::b" },
50     /* stuff gets ordered in registration order */
51     { "a::b,a::c", "a::c,a::b" },
52     { "a::*,b::*", "b::*,a::*" },
53   };
54
55   GFileAttributeMatcher *matcher;
56   char *s;
57   guint i;
58
59   for (i = 0; i < G_N_ELEMENTS (equals); i++)
60     {
61       matcher = g_file_attribute_matcher_new (equals[i].actual);
62       s = g_file_attribute_matcher_to_string (matcher);
63       g_assert_cmpstr (equals[i].expected, ==, s);
64       g_free (s);
65       g_file_attribute_matcher_unref (matcher);
66     }
67 }
68
69 static void
70 test_subtract (void)
71 {
72   struct {
73     char *attributes;
74     char *subtract;
75     char *result;
76   } subtractions[] = {
77     /* * subtracts everything */
78     { "*", "*", NULL },
79     { "a::*", "*", NULL },
80     { "a::b", "*", NULL },
81     { "a::b,a::c", "*", NULL },
82     { "a::*,b::*", "*", NULL },
83     { "a::*,b::c", "*", NULL },
84     { "a::b,b::*", "*", NULL },
85     { "a::b,b::c", "*", NULL },
86     { "a::b,a::c,b::*", "*", NULL },
87     { "a::b,a::c,b::c", "*", NULL },
88     /* a::* subtracts all a's */
89     { "*", "a::*", "*" },
90     { "a::*", "a::*", NULL },
91     { "a::b", "a::*", NULL },
92     { "a::b,a::c", "a::*", NULL },
93     { "a::*,b::*", "a::*", "b::*" },
94     { "a::*,b::c", "a::*", "b::c" },
95     { "a::b,b::*", "a::*", "b::*" },
96     { "a::b,b::c", "a::*", "b::c" },
97     { "a::b,a::c,b::*", "a::*", "b::*" },
98     { "a::b,a::c,b::c", "a::*", "b::c" },
99     /* a::b subtracts exactly that */
100     { "*", "a::b", "*" },
101     { "a::*", "a::b", "a::*" },
102     { "a::b", "a::b", NULL },
103     { "a::b,a::c", "a::b", "a::c" },
104     { "a::*,b::*", "a::b", "a::*,b::*" },
105     { "a::*,b::c", "a::b", "a::*,b::c" },
106     { "a::b,b::*", "a::b", "b::*" },
107     { "a::b,b::c", "a::b", "b::c" },
108     { "a::b,a::c,b::*", "a::b", "a::c,b::*" },
109     { "a::b,a::c,b::c", "a::b", "a::c,b::c" },
110     /* a::b,b::* subtracts both of those */
111     { "*", "a::b,b::*", "*" },
112     { "a::*", "a::b,b::*", "a::*" },
113     { "a::b", "a::b,b::*", NULL },
114     { "a::b,a::c", "a::b,b::*", "a::c" },
115     { "a::*,b::*", "a::b,b::*", "a::*" },
116     { "a::*,b::c", "a::b,b::*", "a::*" },
117     { "a::b,b::*", "a::b,b::*", NULL },
118     { "a::b,b::c", "a::b,b::*", NULL },
119     { "a::b,a::c,b::*", "a::b,b::*", "a::c" },
120     { "a::b,a::c,b::c", "a::b,b::*", "a::c" },
121     /* a::b,b::c should work, too */
122     { "*", "a::b,b::c", "*" },
123     { "a::*", "a::b,b::c", "a::*" },
124     { "a::b", "a::b,b::c", NULL },
125     { "a::b,a::c", "a::b,b::c", "a::c" },
126     { "a::*,b::*", "a::b,b::c", "a::*,b::*" },
127     { "a::*,b::c", "a::b,b::c", "a::*" },
128     { "a::b,b::*", "a::b,b::c", "b::*" },
129     { "a::b,b::c", "a::b,b::c", NULL },
130     { "a::b,a::c,b::*", "a::b,b::c", "a::c,b::*" },
131     { "a::b,a::c,b::c", "a::b,b::c", "a::c" },
132   };
133
134   GFileAttributeMatcher *matcher, *subtract, *result;
135   char *s;
136   guint i;
137
138   for (i = 0; i < G_N_ELEMENTS (subtractions); i++)
139     {
140       matcher = g_file_attribute_matcher_new (subtractions[i].attributes);
141       subtract = g_file_attribute_matcher_new (subtractions[i].subtract);
142       result = g_file_attribute_matcher_subtract (matcher, subtract);
143       s = g_file_attribute_matcher_to_string (result);
144       g_assert_cmpstr (subtractions[i].result, ==, s);
145       g_free (s);
146       g_file_attribute_matcher_unref (matcher);
147       g_file_attribute_matcher_unref (subtract);
148       g_file_attribute_matcher_unref (result);
149     }
150 }
151
152 int
153 main (int argc, char *argv[])
154 {
155   g_test_init (&argc, &argv, NULL);
156
157   g_test_add_func ("/fileattributematcher/exact", test_exact);
158   g_test_add_func ("/fileattributematcher/equality", test_equality);
159   g_test_add_func ("/fileattributematcher/subtract", test_subtract);
160
161   return g_test_run ();
162 }