aboutsummaryrefslogtreecommitdiff
path: root/src/regex/test_regex_proofs.c
diff options
context:
space:
mode:
authorMaximilian Szengel <gnunet@maxsz.de>2012-06-27 16:13:48 +0000
committerMaximilian Szengel <gnunet@maxsz.de>2012-06-27 16:13:48 +0000
commit24f2c9d570bd181c622955506f6ecc000d5b2a98 (patch)
tree9bfb6f07aae4ce7e6df353becbb371fb63c24651 /src/regex/test_regex_proofs.c
parent039c0a8e1c193c692c4f492d75cc2b98643203ef (diff)
downloadgnunet-24f2c9d570bd181c622955506f6ecc000d5b2a98.tar.gz
gnunet-24f2c9d570bd181c622955506f6ecc000d5b2a98.zip
new and improved tests
Diffstat (limited to 'src/regex/test_regex_proofs.c')
-rw-r--r--src/regex/test_regex_proofs.c156
1 files changed, 117 insertions, 39 deletions
diff --git a/src/regex/test_regex_proofs.c b/src/regex/test_regex_proofs.c
index 5d0aabd00..85fc3079d 100644
--- a/src/regex/test_regex_proofs.c
+++ b/src/regex/test_regex_proofs.c
@@ -22,68 +22,146 @@
22 * @brief test for regex.c 22 * @brief test for regex.c
23 * @author Maximilian Szengel 23 * @author Maximilian Szengel
24 */ 24 */
25#include <regex.h>
26#include <time.h>
27#include "platform.h" 25#include "platform.h"
28#include "gnunet_regex_lib.h" 26#include "gnunet_regex_lib.h"
27#include "regex_internal.h"
29 28
30int 29
31main (int argc, char *argv[]) 30/**
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.
36 *
37 * @param regex regular expression used for this test (see above).
38 *
39 * @return 0 on success, 1 on failure
40 */
41unsigned int
42test_proof (const char *regex)
32{ 43{
33 GNUNET_log_setup ("test-regex", 44 unsigned int error;
34#if VERBOSE 45 struct GNUNET_REGEX_Automaton *dfa;
35 "DEBUG", 46 char *c_rx1;
36#else 47 const char *c_rx2;
37 "WARNING",
38#endif
39 NULL);
40 48
41 int error; 49 dfa = GNUNET_REGEX_construct_dfa (regex, strlen (regex));
42 int i; 50 c_rx1 = GNUNET_strdup (GNUNET_REGEX_get_canonical_regex (dfa));
43 51 GNUNET_REGEX_automaton_destroy (dfa);
44 const char *regex[21] = { 52 dfa = GNUNET_REGEX_construct_dfa (c_rx1, strlen (c_rx1));
45 "ab(c|d)+c*(a(b|c)+d)+(bla)+", 53 c_rx2 = GNUNET_REGEX_get_canonical_regex (dfa);
46 "(bla)*", 54
47 "b(lab)*la", 55 error = (0 == strcmp (c_rx1, c_rx2)) ? 0 : 1;
48 "(ab)*", 56
49 "ab(c|d)+c*(a(b|c)+d)+(bla)(bla)*", 57 if (error > 0)
50 "z(abc|def)?xyz", 58 {
51 "1*0(0|1)*", 59 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
52 "a+X*y+c|p|R|Z*K*y*R+w|Y*6+n+h*k*w+V*F|W*B*e*", 60 "Comparing canonical regex of\n%s\nfailed:\n%s\nvs.\n%s\n",
53 "(cd|ab)*", 61 regex, c_rx1, c_rx2);
54 "abcd:(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1):(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)", 62 }
55 "abc(1|0)*def", 63
56 "ab|ac", 64 GNUNET_free (c_rx1);
57 "(ab)(ab)*", 65 GNUNET_REGEX_automaton_destroy (dfa);
58 "ab|cd|ef|gh", 66
59 "a|b|c|d|e|f|g", 67 return error;
60 "(ab)|(ac)", 68}
61 "a(b|c)", 69
62 "a*a", 70/**
63 "ab?(abcd)?", 71 * Use 'test_proof' function to randomly test the canonical regexes of 'count'
64 "(ab|cs|df|sdf)*", 72 * random expressions of length 'rx_length'.
65 "a|aa*a" 73 *
74 * @param count number of random regular expressions to test.
75 * @param rx_length length of the random regular expressions.
76 *
77 * @return 0 on succes, number of failures otherwise.
78 */
79unsigned int
80test_proofs_random (unsigned int count, size_t rx_length)
81{
82 unsigned int i;
83 char *rand_rx;
84 unsigned int failures;
85
86 failures = 0;
87
88 for (i = 0; i < count; i++)
89 {
90 rand_rx = GNUNET_REGEX_generate_random_regex (rx_length, NULL);
91 failures += test_proof (rand_rx);
92 GNUNET_free (rand_rx);
93 }
94
95 return failures;
96}
97
98/**
99 * Test a number of known examples of regexes for proper canonicalization.
100 *
101 * @return 0 on success, number of failures otherwise.
102 */
103unsigned int
104test_proofs_static (void)
105{
106 unsigned int i;
107 unsigned int error;
108
109 const char *regex[4] = {
110 "a|aa*a",
111 "a+",
112 "a*",
113 "a*a*"
66 }; 114 };
115
67 char *canonical_regex; 116 char *canonical_regex;
68 struct GNUNET_REGEX_Automaton *dfa; 117 struct GNUNET_REGEX_Automaton *dfa;
69 118
70 error = 0; 119 error = 0;
71 120
72 for (i = 0; i < 21; i++) 121 for (i = 0; i < 4; i += 2)
73 { 122 {
74 dfa = GNUNET_REGEX_construct_dfa (regex[i], strlen (regex[i])); 123 dfa = GNUNET_REGEX_construct_dfa (regex[i], strlen (regex[i]));
75 canonical_regex = GNUNET_strdup (GNUNET_REGEX_get_canonical_regex (dfa)); 124 canonical_regex = GNUNET_strdup (GNUNET_REGEX_get_canonical_regex (dfa));
76 GNUNET_REGEX_automaton_destroy (dfa); 125 GNUNET_REGEX_automaton_destroy (dfa);
77 126
78 dfa = 127 dfa = GNUNET_REGEX_construct_dfa (regex[i + 1], strlen (regex[i + 1]));
79 GNUNET_REGEX_construct_dfa (canonical_regex, strlen (canonical_regex));
80 error += 128 error +=
81 (0 == 129 (0 ==
82 strcmp (canonical_regex, 130 strcmp (canonical_regex,
83 GNUNET_REGEX_get_canonical_regex (dfa))) ? 0 : 1; 131 GNUNET_REGEX_get_canonical_regex (dfa))) ? 0 : 1;
132
133 if (error > 0)
134 {
135 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
136 "Comparing canonical regex of %s with %s failed.\n", regex[i],
137 regex[i + 1]);
138 }
139
84 GNUNET_free (canonical_regex); 140 GNUNET_free (canonical_regex);
85 GNUNET_REGEX_automaton_destroy (dfa); 141 GNUNET_REGEX_automaton_destroy (dfa);
86 } 142 }
87 143
88 return error; 144 return error;
89} 145}
146
147
148int
149main (int argc, char *argv[])
150{
151 GNUNET_log_setup ("test-regex",
152#if VERBOSE
153 "DEBUG",
154#else
155 "WARNING",
156#endif
157 NULL);
158
159 int error;
160
161 error = 0;
162
163 error += test_proofs_static ();
164// error += test_proofs_random (100, 10);
165
166 return error;
167}