630fb89746c379f45b3955b9dd019f44ed5e0059
[src/app-framework-binder.git] / src / tests / globset / test-globset.c
1 /*
2  * Copyright (C) 2018, 2019 "IoT.bzh"
3  * Author: José Bollo <jose.bollo@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #define _GNU_SOURCE
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <errno.h>
23
24 #include "globset.h"
25
26 void process(FILE *in, FILE *out)
27 {
28         int rc;
29         char buffer[1024], *str;
30         const struct globset_handler *gh;
31         struct globset *set;
32
33         set = globset_create();
34         while (fgets(buffer, sizeof buffer, in)) {
35                 str = strchr(buffer,'\n');
36                 if (str) *str = 0;
37                 errno = 0;
38                 switch (buffer[0]) {
39                 case '+':
40                         rc = globset_add(set, &buffer[1], NULL, NULL);
41                         fprintf(out, "add [%s]: %d, %m\n", &buffer[1], rc);
42                         break;
43                 case '-':
44                         rc = globset_del(set, &buffer[1], NULL);
45                         fprintf(out, "del [%s]: %d, %m\n", &buffer[1], rc);
46                         break;
47                 case '?':
48                         gh = globset_search(set, &buffer[1]);
49                         fprintf(out, "search [%s]: %s%s\n", &buffer[1], gh ? "found " : "NOT FOUND", gh ? gh->pattern : "");
50                         break;
51                 default:
52                         gh = globset_match(set, buffer);
53                         fprintf(out, "match [%s]: %s%s\n", buffer, gh ? "found by " : "NOT FOUND", gh ? gh->pattern : "");
54                         break;
55                 }
56         }
57         globset_destroy(set);
58 }
59
60 int compare(FILE *f1, FILE *f2)
61 {
62         int l = 0, n = 0;
63         char b1[1024], b2[1024];
64         char *s1, *s2;
65
66         for(;;) {
67                 l++;
68                 s1 = fgets(b1, sizeof b1, f1);
69                 s2 = fgets(b2, sizeof b2, f2);
70                 if (s1 == NULL || s2 == NULL) {
71                         if (s1 != s2) {
72                                 fprintf(stderr, "Length of outputs differ\n");
73                                 n++;
74                         }
75                         return n;
76                 }
77                 if (strcmp(s1, s2)) {
78                         fprintf(stderr, "Line %d differ\n\t%s\t%s", l, s1, s2);
79                         n++;
80                 }
81         }
82 }
83
84 int main(int ac, char **av)
85 {
86         FILE *in = stdin;
87         FILE *out = stdout;
88         FILE *ref = NULL;
89
90         if (ac >= 2) {
91                 in = fopen(av[1], "r");
92                 if (in == NULL) {
93                         fprintf(stderr, "Can't open file %s: %m\n", av[1]);
94                         return 1;
95                 }
96         }
97
98         if (ac < 3)
99                 setvbuf(stdout, NULL, _IOLBF, 0);
100         else {
101                 ref = fopen(av[2], "r");
102                 if (ref == NULL) {
103                         fprintf(stderr, "Can't open file %s: %m\n", av[2]);
104                         return 1;
105                 }
106                 out = tmpfile();
107                 if (out == NULL) {
108                         fprintf(stderr, "Can't create temporary file: %m\n");
109                         return 1;
110                 }
111         }
112
113         process(in, out);
114
115         if (ref) {
116                 rewind(out);
117                 if (compare(out, ref))
118                         return 1;
119         }
120
121         return 0;
122 }