Improve test coverage for GSubprocess
[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       if (! g_str_equal (exact_matches[i], s))
23         {
24           g_test_fail ();
25           g_test_message ("matcher should be %s, but is %s", exact_matches[i], s);
26         }
27       g_free (s);
28       g_file_attribute_matcher_unref (matcher);
29     }
30 }
31
32 static void
33 test_equality (void)
34 {
35   struct {
36     char *expected;
37     char *actual;
38   } equals[] = {
39     /* star makes everything else go away */
40     { "*", "*,*" },
41     { "*", "*,a::*" },
42     { "*", "*,a::b" },
43     { "*", "a::*,*" },
44     { "*", "a::b,*" },
45     { "*", "a::b,*,a::*" },
46     /* a::* makes a::<anything> go away */
47     { "a::*", "a::*,a::*" },
48     { "a::*", "a::*,a::b" },
49     { "a::*", "a::b,a::*" },
50     { "a::*", "a::b,a::*,a::c" },
51     /* a::b does not allow duplicates */
52     { "a::b", "a::b,a::b" },
53     { "a::b,a::c", "a::b,a::c,a::b" },
54     /* stuff gets ordered in registration order */
55     { "a::b,a::c", "a::c,a::b" },
56     { "a::*,b::*", "b::*,a::*" },
57   };
58
59   GFileAttributeMatcher *matcher;
60   char *s;
61   guint i;
62
63   for (i = 0; i < G_N_ELEMENTS (equals); i++)
64     {
65       matcher = g_file_attribute_matcher_new (equals[i].actual);
66       s = g_file_attribute_matcher_to_string (matcher);
67       if (! g_str_equal (equals[i].expected, s))
68         {
69           g_test_fail ();
70           g_test_message ("matcher for %s should be %s, but is %s", equals[i].actual, equals[i].expected, s);
71         }
72       g_free (s);
73       g_file_attribute_matcher_unref (matcher);
74     }
75 }
76
77 static void
78 test_subtract (void)
79 {
80   struct {
81     char *attributes;
82     char *subtract;
83     char *result;
84   } subtractions[] = {
85     /* * subtracts everything */
86     { "*", "*", NULL },
87     { "a::*", "*", NULL },
88     { "a::b", "*", NULL },
89     { "a::b,a::c", "*", NULL },
90     { "a::*,b::*", "*", NULL },
91     { "a::*,b::c", "*", NULL },
92     { "a::b,b::*", "*", NULL },
93     { "a::b,b::c", "*", NULL },
94     { "a::b,a::c,b::*", "*", NULL },
95     { "a::b,a::c,b::c", "*", NULL },
96     /* a::* subtracts all a's */
97     { "*", "a::*", "*" },
98     { "a::*", "a::*", NULL },
99     { "a::b", "a::*", NULL },
100     { "a::b,a::c", "a::*", NULL },
101     { "a::*,b::*", "a::*", "b::*" },
102     { "a::*,b::c", "a::*", "b::c" },
103     { "a::b,b::*", "a::*", "b::*" },
104     { "a::b,b::c", "a::*", "b::c" },
105     { "a::b,a::c,b::*", "a::*", "b::*" },
106     { "a::b,a::c,b::c", "a::*", "b::c" },
107     /* a::b subtracts exactly that */
108     { "*", "a::b", "*" },
109     { "a::*", "a::b", "a::*" },
110     { "a::b", "a::b", NULL },
111     { "a::b,a::c", "a::b", "a::c" },
112     { "a::*,b::*", "a::b", "a::*,b::*" },
113     { "a::*,b::c", "a::b", "a::*,b::c" },
114     { "a::b,b::*", "a::b", "b::*" },
115     { "a::b,b::c", "a::b", "b::c" },
116     { "a::b,a::c,b::*", "a::b", "a::c,b::*" },
117     { "a::b,a::c,b::c", "a::b", "a::c,b::c" },
118     /* a::b,b::* subtracts both of those */
119     { "*", "a::b,b::*", "*" },
120     { "a::*", "a::b,b::*", "a::*" },
121     { "a::b", "a::b,b::*", NULL },
122     { "a::b,a::c", "a::b,b::*", "a::c" },
123     { "a::*,b::*", "a::b,b::*", "a::*" },
124     { "a::*,b::c", "a::b,b::*", "a::*" },
125     { "a::b,b::*", "a::b,b::*", NULL },
126     { "a::b,b::c", "a::b,b::*", NULL },
127     { "a::b,a::c,b::*", "a::b,b::*", "a::c" },
128     { "a::b,a::c,b::c", "a::b,b::*", "a::c" },
129     /* a::b,b::c should work, too */
130     { "*", "a::b,b::c", "*" },
131     { "a::*", "a::b,b::c", "a::*" },
132     { "a::b", "a::b,b::c", NULL },
133     { "a::b,a::c", "a::b,b::c", "a::c" },
134     { "a::*,b::*", "a::b,b::c", "a::*,b::*" },
135     { "a::*,b::c", "a::b,b::c", "a::*" },
136     { "a::b,b::*", "a::b,b::c", "b::*" },
137     { "a::b,b::c", "a::b,b::c", NULL },
138     { "a::b,a::c,b::*", "a::b,b::c", "a::c,b::*" },
139     { "a::b,a::c,b::c", "a::b,b::c", "a::c" },
140   };
141
142   GFileAttributeMatcher *matcher, *subtract, *result;
143   char *s;
144   guint i;
145
146   for (i = 0; i < G_N_ELEMENTS (subtractions); i++)
147     {
148       matcher = g_file_attribute_matcher_new (subtractions[i].attributes);
149       subtract = g_file_attribute_matcher_new (subtractions[i].subtract);
150       result = g_file_attribute_matcher_subtract (matcher, subtract);
151       s = g_file_attribute_matcher_to_string (result);
152       if (g_strcmp0 (subtractions[i].result, s))
153         {
154           g_test_fail ();
155           g_test_message ("matcher for %s - %s should be %s, but is %s", 
156                           subtractions[i].attributes, subtractions[i].subtract,
157                           subtractions[i].result, s);
158         }
159       g_free (s);
160       g_file_attribute_matcher_unref (matcher);
161       g_file_attribute_matcher_unref (subtract);
162       g_file_attribute_matcher_unref (result);
163     }
164 }
165
166 int
167 main (int argc, char *argv[])
168 {
169   g_test_init (&argc, &argv, NULL);
170
171   g_test_add_func ("/fileattributematcher/exact", test_exact);
172   g_test_add_func ("/fileattributematcher/equality", test_equality);
173   g_test_add_func ("/fileattributematcher/subtract", test_subtract);
174
175   return g_test_run ();
176 }