Merge branch 'master' of ssh://gnunet.org/gnunet
[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 /**
19  * @file regex/test_regex_graph_api.c
20  * @brief test for regex_graph.c
21  * @author Maximilian Szengel
22  */
23 #include <regex.h>
24 #include <time.h>
25 #include "platform.h"
26 #include "regex_internal_lib.h"
27 #include "regex_test_lib.h"
28 #include "regex_internal.h"
29
30 #define KEEP_FILES 1
31
32 /**
33  * Check if 'filename' exists and is not empty.
34  *
35  * @param filename name of the file that should be checked
36  *
37  * @return 0 if ok, non 0 on error.
38  */
39 static int
40 filecheck (const char *filename)
41 {
42   int error = 0;
43   FILE *fp;
44
45   /* Check if file was created and delete it again */
46   if (NULL == (fp = fopen (filename, "r")))
47   {
48     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not find graph %s\n", filename);
49     return 1;
50   }
51
52   GNUNET_break (0 == fseek (fp, 0L, SEEK_END));
53   if (1 > ftell (fp))
54   {
55     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
56                 "Graph writing failed, got empty file (%s)!\n", filename);
57     error = 2;
58   }
59
60   GNUNET_assert (0 == fclose (fp));
61
62   if (!KEEP_FILES)
63   {
64     if (0 != unlink (filename))
65       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "unlink", filename);
66   }
67   return error;
68 }
69
70
71 int
72 main (int argc, char *argv[])
73 {
74   int error;
75   struct REGEX_INTERNAL_Automaton *a;
76   unsigned int i;
77   const char *filename = "test_graph.dot";
78
79   const char *regex[12] = {
80     "ab(c|d)+c*(a(b|c)+d)+(bla)+",
81     "(bla)*",
82     "b(lab)*la",
83     "(ab)*",
84     "ab(c|d)+c*(a(b|c)+d)+(bla)(bla)*",
85     "z(abc|def)?xyz",
86     "1*0(0|1)*",
87     "a*b*",
88     "a+X*y+c|p|R|Z*K*y*R+w|Y*6+n+h*k*w+V*F|W*B*e*",
89     "a",
90     "a|b",
91     "PADPADPADPADPADPabcdefghixxxxxxxxxxxxxjklmnop*qstoisdjfguisdfguihsdfgbdsuivggsd"
92   };
93
94   GNUNET_log_setup ("test-regex", "WARNING", NULL);
95   error = 0;
96   for (i = 0; i < 12; i++)
97   {
98     /* Check NFA graph creation */
99     a = REGEX_INTERNAL_construct_nfa (regex[i], strlen (regex[i]));
100     REGEX_TEST_automaton_save_graph (a, filename, REGEX_TEST_GRAPH_DEFAULT);
101     REGEX_INTERNAL_automaton_destroy (a);
102     error += filecheck (filename);
103
104     a = REGEX_INTERNAL_construct_nfa (regex[i], strlen (regex[i]));
105     REGEX_TEST_automaton_save_graph (a, filename,
106                                        REGEX_TEST_GRAPH_DEFAULT |
107                                        REGEX_TEST_GRAPH_VERBOSE);
108     REGEX_INTERNAL_automaton_destroy (a);
109     error += filecheck (filename);
110
111     a = REGEX_INTERNAL_construct_nfa (regex[i], strlen (regex[i]));
112     REGEX_TEST_automaton_save_graph (a, filename,
113                                        REGEX_TEST_GRAPH_DEFAULT |
114                                        REGEX_TEST_GRAPH_COLORING);
115     REGEX_INTERNAL_automaton_destroy (a);
116     error += filecheck (filename);
117
118     a = REGEX_INTERNAL_construct_nfa (regex[i], strlen (regex[i]));
119     REGEX_TEST_automaton_save_graph (a, filename,
120                                        REGEX_TEST_GRAPH_DEFAULT |
121                                        REGEX_TEST_GRAPH_VERBOSE |
122                                        REGEX_TEST_GRAPH_COLORING);
123     REGEX_INTERNAL_automaton_destroy (a);
124     error += filecheck (filename);
125
126
127     /* Check DFA graph creation */
128     a = REGEX_INTERNAL_construct_dfa (regex[i], strlen (regex[i]), 0);
129     REGEX_TEST_automaton_save_graph (a, filename, REGEX_TEST_GRAPH_DEFAULT);
130     REGEX_INTERNAL_automaton_destroy (a);
131     error += filecheck (filename);
132
133     a = REGEX_INTERNAL_construct_dfa (regex[i], strlen (regex[i]), 0);
134     REGEX_TEST_automaton_save_graph (a, filename,
135                                        REGEX_TEST_GRAPH_DEFAULT |
136                                        REGEX_TEST_GRAPH_VERBOSE);
137     REGEX_INTERNAL_automaton_destroy (a);
138     error += filecheck (filename);
139
140     a = REGEX_INTERNAL_construct_dfa (regex[i], strlen (regex[i]), 0);
141     REGEX_TEST_automaton_save_graph (a, filename,
142                                        REGEX_TEST_GRAPH_DEFAULT |
143                                        REGEX_TEST_GRAPH_COLORING);
144     REGEX_INTERNAL_automaton_destroy (a);
145     error += filecheck (filename);
146
147
148     a = REGEX_INTERNAL_construct_dfa (regex[i], strlen (regex[i]), 4);
149     REGEX_TEST_automaton_save_graph (a, filename, REGEX_TEST_GRAPH_DEFAULT);
150     REGEX_INTERNAL_automaton_destroy (a);
151     error += filecheck (filename);
152
153   }
154
155   return error;
156 }