c2c1c74e1a497018f98d27f41a49c174dd56f64d
[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 #include "regex_internal.h"
30
31 #define KEEP_FILES 0
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", 
58                 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, 
68                                 "unlink", filename);
69   }
70   return error;
71 }
72
73
74 int
75 main (int argc, char *argv[])
76 {
77   int error;
78   struct GNUNET_REGEX_Automaton *a;
79   unsigned int i;
80   const char *filename = "test_graph.dot";
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 //    "abc(d+|e)fgh"
94     "PADPADPADPADPADPabcdefghixxxxxxxxxxxxxjklmnop*qstoisdjfguisdfguihsdfgbdsuivggsd"
95   };
96
97   GNUNET_log_setup ("test-regex", "WARNING", NULL);
98   error = 0;
99   for (i = 0; i < 12; i++)
100   {
101     // Check NFA graph creation
102     a = GNUNET_REGEX_construct_nfa (regex[i], strlen (regex[i]));
103     GNUNET_REGEX_automaton_save_graph (a, filename, GNUNET_REGEX_GRAPH_DEFAULT);
104     GNUNET_REGEX_automaton_destroy (a);
105     error += filecheck (filename);
106
107     a = GNUNET_REGEX_construct_nfa (regex[i], strlen (regex[i]));
108     GNUNET_REGEX_automaton_save_graph (a, filename,
109                                        GNUNET_REGEX_GRAPH_DEFAULT |
110                                        GNUNET_REGEX_GRAPH_VERBOSE);
111     GNUNET_REGEX_automaton_destroy (a);
112     error += filecheck (filename);
113
114     a = GNUNET_REGEX_construct_nfa (regex[i], strlen (regex[i]));
115     GNUNET_REGEX_automaton_save_graph (a, filename,
116                                        GNUNET_REGEX_GRAPH_DEFAULT |
117                                        GNUNET_REGEX_GRAPH_COLORING);
118     GNUNET_REGEX_automaton_destroy (a);
119     error += filecheck (filename);
120
121     a = GNUNET_REGEX_construct_nfa (regex[i], strlen (regex[i]));
122     GNUNET_REGEX_automaton_save_graph (a, filename,
123                                        GNUNET_REGEX_GRAPH_DEFAULT |
124                                        GNUNET_REGEX_GRAPH_VERBOSE |
125                                        GNUNET_REGEX_GRAPH_COLORING);
126     GNUNET_REGEX_automaton_destroy (a);
127     error += filecheck (filename);
128
129
130     // Check DFA graph creation
131     a = GNUNET_REGEX_construct_dfa (regex[i], strlen (regex[i]), 0);
132     GNUNET_REGEX_automaton_save_graph (a, filename, GNUNET_REGEX_GRAPH_DEFAULT);
133     GNUNET_REGEX_automaton_destroy (a);
134     error += filecheck (filename);
135
136     a = GNUNET_REGEX_construct_dfa (regex[i], strlen (regex[i]), 0);
137     GNUNET_REGEX_automaton_save_graph (a, filename,
138                                        GNUNET_REGEX_GRAPH_DEFAULT |
139                                        GNUNET_REGEX_GRAPH_VERBOSE);
140     GNUNET_REGEX_automaton_destroy (a);
141     error += filecheck (filename);
142
143     a = GNUNET_REGEX_construct_dfa (regex[i], strlen (regex[i]), 0);
144     GNUNET_REGEX_automaton_save_graph (a, filename,
145                                        GNUNET_REGEX_GRAPH_DEFAULT |
146                                        GNUNET_REGEX_GRAPH_COLORING);
147     GNUNET_REGEX_automaton_destroy (a);
148     error += filecheck (filename);
149
150
151     a = GNUNET_REGEX_construct_dfa (regex[i], strlen (regex[i]), 0);
152     GNUNET_REGEX_automaton_save_graph (a, filename, GNUNET_REGEX_GRAPH_DEFAULT);        //|
153     //                               GNUNET_REGEX_GRAPH_VERBOSE |
154     //GNUNET_REGEX_GRAPH_COLORING);
155     GNUNET_REGEX_automaton_destroy (a);
156     error += filecheck (filename);
157
158   }
159
160   return error;
161 }