global reindent, now with uncrustify hook enabled
[oweals/gnunet.git] / src / regex / test_regex_graph_api.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2012 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 /**
21  * @file regex/test_regex_graph_api.c
22  * @brief test for regex_graph.c
23  * @author Maximilian Szengel
24  */
25 #include <regex.h>
26 #include <time.h>
27 #include "platform.h"
28 #include "regex_internal_lib.h"
29 #include "regex_test_lib.h"
30 #include "regex_internal.h"
31
32 #define KEEP_FILES 1
33
34 /**
35  * Check if 'filename' exists and is not empty.
36  *
37  * @param filename name of the file that should be checked
38  *
39  * @return 0 if ok, non 0 on error.
40  */
41 static int
42 filecheck (const char *filename)
43 {
44   int error = 0;
45   FILE *fp;
46
47   /* Check if file was created and delete it again */
48   if (NULL == (fp = fopen (filename, "r")))
49   {
50     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not find graph %s\n", filename);
51     return 1;
52   }
53
54   GNUNET_break (0 == fseek (fp, 0L, SEEK_END));
55   if (1 > ftell (fp))
56   {
57     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
58                 "Graph writing failed, got empty file (%s)!\n", filename);
59     error = 2;
60   }
61
62   GNUNET_assert (0 == fclose (fp));
63
64   if (! KEEP_FILES)
65   {
66     if (0 != unlink (filename))
67       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "unlink", filename);
68   }
69   return error;
70 }
71
72
73 int
74 main (int argc, char *argv[])
75 {
76   int error;
77   struct REGEX_INTERNAL_Automaton *a;
78   unsigned int i;
79   const char *filename = "test_graph.dot";
80
81   const char *regex[12] = {
82     "ab(c|d)+c*(a(b|c)+d)+(bla)+",
83     "(bla)*",
84     "b(lab)*la",
85     "(ab)*",
86     "ab(c|d)+c*(a(b|c)+d)+(bla)(bla)*",
87     "z(abc|def)?xyz",
88     "1*0(0|1)*",
89     "a*b*",
90     "a+X*y+c|p|R|Z*K*y*R+w|Y*6+n+h*k*w+V*F|W*B*e*",
91     "a",
92     "a|b",
93     "PADPADPADPADPADPabcdefghixxxxxxxxxxxxxjklmnop*qstoisdjfguisdfguihsdfgbdsuivggsd"
94   };
95
96   GNUNET_log_setup ("test-regex", "WARNING", NULL);
97   error = 0;
98   for (i = 0; i < 12; i++)
99   {
100     /* Check NFA graph creation */
101     a = REGEX_INTERNAL_construct_nfa (regex[i], strlen (regex[i]));
102     REGEX_TEST_automaton_save_graph (a, filename, REGEX_TEST_GRAPH_DEFAULT);
103     REGEX_INTERNAL_automaton_destroy (a);
104     error += filecheck (filename);
105
106     a = REGEX_INTERNAL_construct_nfa (regex[i], strlen (regex[i]));
107     REGEX_TEST_automaton_save_graph (a, filename,
108                                      REGEX_TEST_GRAPH_DEFAULT
109                                      | REGEX_TEST_GRAPH_VERBOSE);
110     REGEX_INTERNAL_automaton_destroy (a);
111     error += filecheck (filename);
112
113     a = REGEX_INTERNAL_construct_nfa (regex[i], strlen (regex[i]));
114     REGEX_TEST_automaton_save_graph (a, filename,
115                                      REGEX_TEST_GRAPH_DEFAULT
116                                      | REGEX_TEST_GRAPH_COLORING);
117     REGEX_INTERNAL_automaton_destroy (a);
118     error += filecheck (filename);
119
120     a = REGEX_INTERNAL_construct_nfa (regex[i], strlen (regex[i]));
121     REGEX_TEST_automaton_save_graph (a, filename,
122                                      REGEX_TEST_GRAPH_DEFAULT
123                                      | REGEX_TEST_GRAPH_VERBOSE
124                                      | REGEX_TEST_GRAPH_COLORING);
125     REGEX_INTERNAL_automaton_destroy (a);
126     error += filecheck (filename);
127
128
129     /* Check DFA graph creation */
130     a = REGEX_INTERNAL_construct_dfa (regex[i], strlen (regex[i]), 0);
131     REGEX_TEST_automaton_save_graph (a, filename, REGEX_TEST_GRAPH_DEFAULT);
132     REGEX_INTERNAL_automaton_destroy (a);
133     error += filecheck (filename);
134
135     a = REGEX_INTERNAL_construct_dfa (regex[i], strlen (regex[i]), 0);
136     REGEX_TEST_automaton_save_graph (a, filename,
137                                      REGEX_TEST_GRAPH_DEFAULT
138                                      | REGEX_TEST_GRAPH_VERBOSE);
139     REGEX_INTERNAL_automaton_destroy (a);
140     error += filecheck (filename);
141
142     a = REGEX_INTERNAL_construct_dfa (regex[i], strlen (regex[i]), 0);
143     REGEX_TEST_automaton_save_graph (a, filename,
144                                      REGEX_TEST_GRAPH_DEFAULT
145                                      | REGEX_TEST_GRAPH_COLORING);
146     REGEX_INTERNAL_automaton_destroy (a);
147     error += filecheck (filename);
148
149
150     a = REGEX_INTERNAL_construct_dfa (regex[i], strlen (regex[i]), 4);
151     REGEX_TEST_automaton_save_graph (a, filename, REGEX_TEST_GRAPH_DEFAULT);
152     REGEX_INTERNAL_automaton_destroy (a);
153     error += filecheck (filename);
154   }
155
156   return error;
157 }