2 This file is part of GNUnet
3 Copyright (C) 2012 GNUnet e.V.
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.
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.
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/>.
19 * @file regex/test_regex_proofs.c
20 * @brief test for regex.c
21 * @author Maximilian Szengel
24 #include "regex_internal_lib.h"
25 #include "regex_test_lib.h"
26 #include "regex_internal.h"
30 * Test if the given regex's canonical regex is the same as this canonical
31 * regex's canonical regex. Confused? Ok, then: 1. construct a dfa A from the
32 * given 'regex' 2. get the canonical regex of dfa A 3. construct a dfa B from
33 * this canonical regex 3. compare the canonical regex of dfa A with the
34 * canonical regex of dfa B.
36 * @param regex regular expression used for this test (see above).
38 * @return 0 on success, 1 on failure
41 test_proof (const char *regex)
44 struct REGEX_INTERNAL_Automaton *dfa;
48 dfa = REGEX_INTERNAL_construct_dfa (regex, strlen (regex), 1);
49 GNUNET_assert (NULL != dfa);
50 c_rx1 = GNUNET_strdup (REGEX_INTERNAL_get_canonical_regex (dfa));
51 REGEX_INTERNAL_automaton_destroy (dfa);
52 dfa = REGEX_INTERNAL_construct_dfa (c_rx1, strlen (c_rx1), 1);
53 GNUNET_assert (NULL != dfa);
54 c_rx2 = REGEX_INTERNAL_get_canonical_regex (dfa);
56 error = (0 == strcmp (c_rx1, c_rx2)) ? 0 : 1;
60 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
61 "Comparing canonical regex of\n%s\nfailed:\n%s\nvs.\n%s\n",
66 REGEX_INTERNAL_automaton_destroy (dfa);
73 * Use 'test_proof' function to randomly test the canonical regexes of 'count'
74 * random expressions of length 'rx_length'.
76 * @param count number of random regular expressions to test.
77 * @param rx_length length of the random regular expressions.
79 * @return 0 on succes, number of failures otherwise.
82 test_proofs_random (unsigned int count, size_t rx_length)
86 unsigned int failures;
90 for (i = 0; i < count; i++)
92 rand_rx = REGEX_TEST_generate_random_regex (rx_length, NULL);
93 failures += test_proof (rand_rx);
94 GNUNET_free (rand_rx);
102 * Test a number of known examples of regexes for proper canonicalization.
104 * @return 0 on success, number of failures otherwise.
107 test_proofs_static ()
112 const char *regex[8] = {
119 "((a|b)c|(a|b)(d|(a|b)e))",
120 "((a|b)(c|d)|(a|b)(a|b)e)"
123 const char *canon_rx1;
124 const char *canon_rx2;
125 struct REGEX_INTERNAL_Automaton *dfa1;
126 struct REGEX_INTERNAL_Automaton *dfa2;
130 for (i = 0; i < 8; i += 2)
132 dfa1 = REGEX_INTERNAL_construct_dfa (regex[i], strlen (regex[i]), 1);
133 dfa2 = REGEX_INTERNAL_construct_dfa (regex[i + 1], strlen (regex[i + 1]), 1);
134 GNUNET_assert (NULL != dfa1);
135 GNUNET_assert (NULL != dfa2);
137 canon_rx1 = REGEX_INTERNAL_get_canonical_regex (dfa1);
138 canon_rx2 = REGEX_INTERNAL_get_canonical_regex (dfa2);
140 error += (0 == strcmp (canon_rx1, canon_rx2)) ? 0 : 1;
144 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
145 "Comparing canonical regex failed:\nrx1:\t%s\ncrx1:\t%s\nrx2:\t%s\ncrx2:\t%s\n",
146 regex[i], canon_rx1, regex[i + 1], canon_rx2);
149 REGEX_INTERNAL_automaton_destroy (dfa1);
150 REGEX_INTERNAL_automaton_destroy (dfa2);
158 main (int argc, char *argv[])
160 GNUNET_log_setup ("test-regex", "WARNING", NULL);
166 error += test_proofs_static ();
167 error += test_proofs_random (100, 30);