coverity
[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 "gnunet_regex_lib.h"
29
30 #define KEEP_FILES 0
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 int
40 filecheck (const char *filename)
41 {
42   int error = 0;
43   FILE *fp = NULL;
44
45   // Check if file was created and delete it again
46   fp = fopen (filename, "r");
47
48   if (NULL == fp)
49   {
50     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not find graph %s\n", filename);
51     return ++error;
52   }
53
54   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++;
60   }
61
62   error += fclose (fp);
63
64   if (!KEEP_FILES)
65   {
66     if (0 != unlink (filename))
67     {
68       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not remove temp files (%s)\n",
69                   filename);
70     }
71   }
72
73
74   return error;
75 }
76
77 int
78 main (int argc, char *argv[])
79 {
80   GNUNET_log_setup ("test-regex", "WARNING", NULL);
81
82   int error;
83   struct GNUNET_REGEX_Automaton *a;
84   unsigned int i;
85   const char *filename = "test_graph.dot";
86
87   error = 0;
88
89   const char *regex[12] = {
90     "ab(c|d)+c*(a(b|c)+d)+(bla)+",
91     "(bla)*",
92     "b(lab)*la",
93     "(ab)*",
94     "ab(c|d)+c*(a(b|c)+d)+(bla)(bla)*",
95     "z(abc|def)?xyz",
96     "1*0(0|1)*",
97     "a*b*",
98     "a+X*y+c|p|R|Z*K*y*R+w|Y*6+n+h*k*w+V*F|W*B*e*",
99     "a",
100     "a|b",
101 //    "abc(d+|e)fgh"
102     "PADPADPADPADPADPabcdefghixxxxxxxxxxxxxjklmnop*qstoisdjfguisdfguihsdfgbdsuivggsd"
103   };
104
105   for (i = 0; i < 12; i++)
106   {
107     // Check NFA graph creation
108     a = GNUNET_REGEX_construct_nfa (regex[i], strlen (regex[i]));
109     GNUNET_REGEX_automaton_save_graph (a, filename, GNUNET_REGEX_GRAPH_DEFAULT);
110     GNUNET_REGEX_automaton_destroy (a);
111     error += filecheck (filename);
112
113     a = GNUNET_REGEX_construct_nfa (regex[i], strlen (regex[i]));
114     GNUNET_REGEX_automaton_save_graph (a, filename,
115                                        GNUNET_REGEX_GRAPH_DEFAULT |
116                                        GNUNET_REGEX_GRAPH_VERBOSE);
117     GNUNET_REGEX_automaton_destroy (a);
118     error += filecheck (filename);
119
120     a = GNUNET_REGEX_construct_nfa (regex[i], strlen (regex[i]));
121     GNUNET_REGEX_automaton_save_graph (a, filename,
122                                        GNUNET_REGEX_GRAPH_DEFAULT |
123                                        GNUNET_REGEX_GRAPH_COLORING);
124     GNUNET_REGEX_automaton_destroy (a);
125     error += filecheck (filename);
126
127     a = GNUNET_REGEX_construct_nfa (regex[i], strlen (regex[i]));
128     GNUNET_REGEX_automaton_save_graph (a, filename,
129                                        GNUNET_REGEX_GRAPH_DEFAULT |
130                                        GNUNET_REGEX_GRAPH_VERBOSE |
131                                        GNUNET_REGEX_GRAPH_COLORING);
132     GNUNET_REGEX_automaton_destroy (a);
133     error += filecheck (filename);
134
135
136     // Check DFA graph creation
137     a = GNUNET_REGEX_construct_dfa (regex[i], strlen (regex[i]));
138     GNUNET_REGEX_automaton_save_graph (a, filename, GNUNET_REGEX_GRAPH_DEFAULT);
139     GNUNET_REGEX_automaton_destroy (a);
140     error += filecheck (filename);
141
142     a = GNUNET_REGEX_construct_dfa (regex[i], strlen (regex[i]));
143     GNUNET_REGEX_automaton_save_graph (a, filename,
144                                        GNUNET_REGEX_GRAPH_DEFAULT |
145                                        GNUNET_REGEX_GRAPH_VERBOSE);
146     GNUNET_REGEX_automaton_destroy (a);
147     error += filecheck (filename);
148
149     a = GNUNET_REGEX_construct_dfa (regex[i], strlen (regex[i]));
150     GNUNET_REGEX_automaton_save_graph (a, filename,
151                                        GNUNET_REGEX_GRAPH_DEFAULT |
152                                        GNUNET_REGEX_GRAPH_COLORING);
153     GNUNET_REGEX_automaton_destroy (a);
154     error += filecheck (filename);
155
156
157     a = GNUNET_REGEX_construct_dfa (regex[i], strlen (regex[i]));
158     GNUNET_REGEX_automaton_save_graph (a, filename, GNUNET_REGEX_GRAPH_DEFAULT);        //|
159     //                               GNUNET_REGEX_GRAPH_VERBOSE |
160     //GNUNET_REGEX_GRAPH_COLORING);
161     GNUNET_REGEX_automaton_destroy (a);
162     error += filecheck (filename);
163
164   }
165
166   return error;
167 }