2 This file is part of GNUnet
3 (C) 2012 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file regex/test_regex_proofs.c
22 * @brief test for regex.c
23 * @author Maximilian Szengel
26 #include "gnunet_regex_lib.h"
27 #include "regex_internal.h"
31 * Test if the given regex's canonical regex is the same as this canonical
32 * regex's canonical regex. Confused? Ok, then: 1. construct a dfa A from the
33 * given 'regex' 2. get the canonical regex of dfa A 3. construct a dfa B from
34 * this canonical regex 3. compare the canonical regex of dfa A with the
35 * canonical regex of dfa B.
37 * @param regex regular expression used for this test (see above).
39 * @return 0 on success, 1 on failure
42 test_proof (const char *regex)
45 struct GNUNET_REGEX_Automaton *dfa;
49 dfa = GNUNET_REGEX_construct_dfa (regex, strlen (regex), 1);
50 GNUNET_assert (NULL != dfa);
51 c_rx1 = GNUNET_strdup (GNUNET_REGEX_get_canonical_regex (dfa));
52 GNUNET_REGEX_automaton_destroy (dfa);
53 dfa = GNUNET_REGEX_construct_dfa (c_rx1, strlen (c_rx1), 1);
54 GNUNET_assert (NULL != dfa);
55 c_rx2 = GNUNET_REGEX_get_canonical_regex (dfa);
57 error = (0 == strcmp (c_rx1, c_rx2)) ? 0 : 1;
61 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
62 "Comparing canonical regex of\n%s\nfailed:\n%s\nvs.\n%s\n",
67 GNUNET_REGEX_automaton_destroy (dfa);
74 * Use 'test_proof' function to randomly test the canonical regexes of 'count'
75 * random expressions of length 'rx_length'.
77 * @param count number of random regular expressions to test.
78 * @param rx_length length of the random regular expressions.
80 * @return 0 on succes, number of failures otherwise.
83 test_proofs_random (unsigned int count, size_t rx_length)
87 unsigned int failures;
91 for (i = 0; i < count; i++)
93 rand_rx = GNUNET_REGEX_generate_random_regex (rx_length, NULL);
94 failures += test_proof (rand_rx);
95 GNUNET_free (rand_rx);
103 * Test a number of known examples of regexes for proper canonicalization.
105 * @return 0 on success, number of failures otherwise.
108 test_proofs_static ()
113 const char *regex[8] = {
120 "((a|b)c|(a|b)(d|(a|b)e))",
121 "((a|b)(c|d)|(a|b)(a|b)e)"
124 const char *canon_rx1;
125 const char *canon_rx2;
126 struct GNUNET_REGEX_Automaton *dfa1;
127 struct GNUNET_REGEX_Automaton *dfa2;
131 for (i = 0; i < 8; i += 2)
133 dfa1 = GNUNET_REGEX_construct_dfa (regex[i], strlen (regex[i]), 1);
134 dfa2 = GNUNET_REGEX_construct_dfa (regex[i + 1], strlen (regex[i + 1]), 1);
135 GNUNET_assert (NULL != dfa1);
136 GNUNET_assert (NULL != dfa2);
138 canon_rx1 = GNUNET_REGEX_get_canonical_regex (dfa1);
139 canon_rx2 = GNUNET_REGEX_get_canonical_regex (dfa2);
141 error += (0 == strcmp (canon_rx1, canon_rx2)) ? 0 : 1;
145 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
146 "Comparing canonical regex failed:\nrx1:\t%s\ncrx1:\t%s\nrx2:\t%s\ncrx2:\t%s\n",
147 regex[i], canon_rx1, regex[i + 1], canon_rx2);
150 GNUNET_REGEX_automaton_destroy (dfa1);
151 GNUNET_REGEX_automaton_destroy (dfa2);
159 main (int argc, char *argv[])
161 GNUNET_log_setup ("test-regex", "WARNING", NULL);
167 error += test_proofs_static ();
168 error += test_proofs_random (100, 30);