continuing refactoring of regex library structure, disambiguating symbol names betwee...
[oweals/gnunet.git] / src / regex / test_regex_graph_api.c
1 /*
2      This file is part of GNUnet
3      (C) 2012 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
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_internal.h"
30
31 #define KEEP_FILES 1
32
33 /**
34  * Check if 'filename' exists and is not empty.
35  *
36  * @param filename name of the file that should be checked
37  *
38  * @return 0 if ok, non 0 on error.
39  */
40 static int
41 filecheck (const char *filename)
42 {
43   int error = 0;
44   FILE *fp;
45
46   /* Check if file was created and delete it again */
47   if (NULL == (fp = fopen (filename, "r")))
48   {
49     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not find graph %s\n", filename);
50     return 1;
51   }
52
53   GNUNET_break (0 == fseek (fp, 0L, SEEK_END));
54   if (1 > ftell (fp))
55   {
56     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
57                 "Graph writing failed, got empty file (%s)!\n", filename);
58     error = 2;
59   }
60
61   GNUNET_assert (0 == fclose (fp));
62
63   if (!KEEP_FILES)
64   {
65     if (0 != unlink (filename))
66       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "unlink", filename);
67   }
68   return error;
69 }
70
71
72 int
73 main (int argc, char *argv[])
74 {
75   int error;
76   struct REGEX_ITERNAL_Automaton *a;
77   unsigned int i;
78   const char *filename = "test_graph.dot";
79
80   const char *regex[12] = {
81     "ab(c|d)+c*(a(b|c)+d)+(bla)+",
82     "(bla)*",
83     "b(lab)*la",
84     "(ab)*",
85     "ab(c|d)+c*(a(b|c)+d)+(bla)(bla)*",
86     "z(abc|def)?xyz",
87     "1*0(0|1)*",
88     "a*b*",
89     "a+X*y+c|p|R|Z*K*y*R+w|Y*6+n+h*k*w+V*F|W*B*e*",
90     "a",
91     "a|b",
92     "PADPADPADPADPADPabcdefghixxxxxxxxxxxxxjklmnop*qstoisdjfguisdfguihsdfgbdsuivggsd"
93   };
94
95   GNUNET_log_setup ("test-regex", "WARNING", NULL);
96   error = 0;
97   for (i = 0; i < 12; i++)
98   {
99     /* Check NFA graph creation */
100     a = REGEX_ITERNAL_construct_nfa (regex[i], strlen (regex[i]));
101     REGEX_ITERNAL_automaton_save_graph (a, filename, REGEX_ITERNAL_GRAPH_DEFAULT);
102     REGEX_ITERNAL_automaton_destroy (a);
103     error += filecheck (filename);
104
105     a = REGEX_ITERNAL_construct_nfa (regex[i], strlen (regex[i]));
106     REGEX_ITERNAL_automaton_save_graph (a, filename,
107                                        REGEX_ITERNAL_GRAPH_DEFAULT |
108                                        REGEX_ITERNAL_GRAPH_VERBOSE);
109     REGEX_ITERNAL_automaton_destroy (a);
110     error += filecheck (filename);
111
112     a = REGEX_ITERNAL_construct_nfa (regex[i], strlen (regex[i]));
113     REGEX_ITERNAL_automaton_save_graph (a, filename,
114                                        REGEX_ITERNAL_GRAPH_DEFAULT |
115                                        REGEX_ITERNAL_GRAPH_COLORING);
116     REGEX_ITERNAL_automaton_destroy (a);
117     error += filecheck (filename);
118
119     a = REGEX_ITERNAL_construct_nfa (regex[i], strlen (regex[i]));
120     REGEX_ITERNAL_automaton_save_graph (a, filename,
121                                        REGEX_ITERNAL_GRAPH_DEFAULT |
122                                        REGEX_ITERNAL_GRAPH_VERBOSE |
123                                        REGEX_ITERNAL_GRAPH_COLORING);
124     REGEX_ITERNAL_automaton_destroy (a);
125     error += filecheck (filename);
126
127
128     /* Check DFA graph creation */
129     a = REGEX_ITERNAL_construct_dfa (regex[i], strlen (regex[i]), 0);
130     REGEX_ITERNAL_automaton_save_graph (a, filename, REGEX_ITERNAL_GRAPH_DEFAULT);
131     REGEX_ITERNAL_automaton_destroy (a);
132     error += filecheck (filename);
133
134     a = REGEX_ITERNAL_construct_dfa (regex[i], strlen (regex[i]), 0);
135     REGEX_ITERNAL_automaton_save_graph (a, filename,
136                                        REGEX_ITERNAL_GRAPH_DEFAULT |
137                                        REGEX_ITERNAL_GRAPH_VERBOSE);
138     REGEX_ITERNAL_automaton_destroy (a);
139     error += filecheck (filename);
140
141     a = REGEX_ITERNAL_construct_dfa (regex[i], strlen (regex[i]), 0);
142     REGEX_ITERNAL_automaton_save_graph (a, filename,
143                                        REGEX_ITERNAL_GRAPH_DEFAULT |
144                                        REGEX_ITERNAL_GRAPH_COLORING);
145     REGEX_ITERNAL_automaton_destroy (a);
146     error += filecheck (filename);
147
148
149     a = REGEX_ITERNAL_construct_dfa (regex[i], strlen (regex[i]), 4);
150     REGEX_ITERNAL_automaton_save_graph (a, filename, REGEX_ITERNAL_GRAPH_DEFAULT);
151     REGEX_ITERNAL_automaton_destroy (a);
152     error += filecheck (filename);
153
154   }
155
156   return error;
157 }