aboutsummaryrefslogtreecommitdiff
path: root/src/regex/test_regex_eval_api.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_eval_api.c
parent039c0a8e1c193c692c4f492d75cc2b98643203ef (diff)
downloadgnunet-24f2c9d570bd181c622955506f6ecc000d5b2a98.tar.gz
gnunet-24f2c9d570bd181c622955506f6ecc000d5b2a98.zip
new and improved tests
Diffstat (limited to 'src/regex/test_regex_eval_api.c')
-rw-r--r--src/regex/test_regex_eval_api.c102
1 files changed, 21 insertions, 81 deletions
diff --git a/src/regex/test_regex_eval_api.c b/src/regex/test_regex_eval_api.c
index b6cdbe100..6d575a05c 100644
--- a/src/regex/test_regex_eval_api.c
+++ b/src/regex/test_regex_eval_api.c
@@ -26,6 +26,7 @@
26#include <time.h> 26#include <time.h>
27#include "platform.h" 27#include "platform.h"
28#include "gnunet_regex_lib.h" 28#include "gnunet_regex_lib.h"
29#include "regex_internal.h"
29 30
30enum Match_Result 31enum Match_Result
31{ 32{
@@ -41,8 +42,6 @@ struct Regex_String_Pair
41 enum Match_Result expected_results[20]; 42 enum Match_Result expected_results[20];
42}; 43};
43 44
44static const char allowed_literals[] =
45 "0123456789" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz";
46 45
47/** 46/**
48 * Random regex test. Generate a random regex as well as 'str_count' strings to 47 * Random regex test. Generate a random regex as well as 'str_count' strings to
@@ -60,15 +59,8 @@ test_random (unsigned int rx_length, unsigned int max_str_len,
60 unsigned int str_count) 59 unsigned int str_count)
61{ 60{
62 int i; 61 int i;
63 int j; 62 char *rand_rx;
64 int rx_exp; 63 char *matching_str;
65 char rand_rx[rx_length + 1];
66 char matching_str[str_count][max_str_len + 1];
67 char *rand_rxp;
68 char *matching_strp;
69 int char_op_switch;
70 int last_was_op;
71 char current_char;
72 int eval; 64 int eval;
73 int eval_check; 65 int eval_check;
74 int eval_canonical; 66 int eval_canonical;
@@ -77,7 +69,7 @@ test_random (unsigned int rx_length, unsigned int max_str_len,
77 regmatch_t matchptr[1]; 69 regmatch_t matchptr[1];
78 char error[200]; 70 char error[200];
79 int result; 71 int result;
80 unsigned int str_len; 72 size_t str_len;
81 char *canonical_regex; 73 char *canonical_regex;
82 74
83 // At least one string is needed for matching 75 // At least one string is needed for matching
@@ -85,76 +77,20 @@ test_random (unsigned int rx_length, unsigned int max_str_len,
85 // The string should be at least as long as the regex itself 77 // The string should be at least as long as the regex itself
86 GNUNET_assert (max_str_len >= rx_length); 78 GNUNET_assert (max_str_len >= rx_length);
87 79
88 rand_rxp = rand_rx;
89 matching_strp = matching_str[0];
90 current_char = 0;
91 last_was_op = 1;
92
93 // Generate random regex and a string that matches the regex 80 // Generate random regex and a string that matches the regex
94 for (i = 0; i < rx_length; i++) 81 matching_str = GNUNET_malloc (rx_length + 1);
95 { 82 rand_rx = GNUNET_REGEX_generate_random_regex (rx_length, matching_str);
96 char_op_switch = 0 + (int) (1.0 * rand () / (RAND_MAX + 1.0));
97
98 if (0 == char_op_switch && !last_was_op)
99 {
100 last_was_op = 1;
101 rx_exp = rand () % 4;
102
103 switch (rx_exp)
104 {
105 case 0:
106 current_char = '+';
107 break;
108 case 1:
109 current_char = '*';
110 break;
111 case 2:
112 current_char = '?';
113 break;
114 case 3:
115 if (i < rx_length - 1) // '|' cannot be at the end
116 current_char = '|';
117 else
118 current_char =
119 allowed_literals[rand () % (sizeof (allowed_literals) - 1)];
120 break;
121 }
122 }
123 else
124 {
125 current_char =
126 allowed_literals[rand () % (sizeof (allowed_literals) - 1)];
127 last_was_op = 0;
128 }
129
130 if (current_char != '+' && current_char != '*' && current_char != '?' &&
131 current_char != '|')
132 {
133 *matching_strp = current_char;
134 matching_strp++;
135 }
136
137 *rand_rxp = current_char;
138 rand_rxp++;
139 }
140 *rand_rxp = '\0';
141 *matching_strp = '\0';
142
143 // Generate some random strings for matching...
144 // Start at 1, because the first string is generated above during regex generation
145 for (i = 1; i < str_count; i++)
146 {
147 str_len = rand () % max_str_len;
148 for (j = 0; j < str_len; j++)
149 matching_str[i][j] =
150 allowed_literals[rand () % (sizeof (allowed_literals) - 1)];
151 matching_str[i][str_len] = '\0';
152 }
153 83
154 // Now match 84 // Now match
155 result = 0; 85 result = 0;
156 for (i = 0; i < str_count; i++) 86 for (i = 0; i < str_count; i++)
157 { 87 {
88 if (0 < i)
89 {
90 matching_str = GNUNET_REGEX_generate_random_string (max_str_len);
91 str_len = strlen (matching_str);
92 }
93
158 // Match string using DFA 94 // Match string using DFA
159 dfa = GNUNET_REGEX_construct_dfa (rand_rx, strlen (rand_rx)); 95 dfa = GNUNET_REGEX_construct_dfa (rand_rx, strlen (rand_rx));
160 if (NULL == dfa) 96 if (NULL == dfa)
@@ -163,7 +99,7 @@ test_random (unsigned int rx_length, unsigned int max_str_len,
163 return -1; 99 return -1;
164 } 100 }
165 101
166 eval = GNUNET_REGEX_eval (dfa, matching_str[i]); 102 eval = GNUNET_REGEX_eval (dfa, matching_str);
167 canonical_regex = GNUNET_strdup (GNUNET_REGEX_get_canonical_regex (dfa)); 103 canonical_regex = GNUNET_strdup (GNUNET_REGEX_get_canonical_regex (dfa));
168 GNUNET_REGEX_automaton_destroy (dfa); 104 GNUNET_REGEX_automaton_destroy (dfa);
169 105
@@ -175,7 +111,7 @@ test_random (unsigned int rx_length, unsigned int max_str_len,
175 return -1; 111 return -1;
176 } 112 }
177 113
178 eval_check = regexec (&rx, matching_str[i], 1, matchptr, 0); 114 eval_check = regexec (&rx, matching_str, 1, matchptr, 0);
179 regfree (&rx); 115 regfree (&rx);
180 116
181 // Match canonical regex 117 // Match canonical regex
@@ -187,14 +123,13 @@ test_random (unsigned int rx_length, unsigned int max_str_len,
187 return -1; 123 return -1;
188 } 124 }
189 125
190 eval_canonical = regexec (&rx, matching_str[i], 1, matchptr, 0); 126 eval_canonical = regexec (&rx, matching_str, 1, matchptr, 0);
191 regfree (&rx); 127 regfree (&rx);
192 GNUNET_free (canonical_regex); 128 GNUNET_free (canonical_regex);
193 129
194 // We only want to match the whole string, because that's what our DFA does, too. 130 // We only want to match the whole string, because that's what our DFA does, too.
195 if (eval_check == 0 && 131 if (eval_check == 0 &&
196 (matchptr[0].rm_so != 0 || 132 (matchptr[0].rm_so != 0 || matchptr[0].rm_eo != strlen (matching_str)))
197 matchptr[0].rm_eo != strlen (matching_str[i])))
198 eval_check = 1; 133 eval_check = 1;
199 134
200 // compare result 135 // compare result
@@ -206,7 +141,12 @@ test_random (unsigned int rx_length, unsigned int max_str_len,
206 rand_rx, matching_str, eval, eval_check, error); 141 rand_rx, matching_str, eval, eval_check, error);
207 result += 1; 142 result += 1;
208 } 143 }
144
145 GNUNET_free (matching_str);
209 } 146 }
147
148 GNUNET_free (rand_rx);
149
210 return result; 150 return result;
211} 151}
212 152