-consistently use struct GNUNET_HashCode
[oweals/gnunet.git] / src / regex / test_regex_eval_api.c
index c63e97c11c800605cb2a237d42c18bed81411cb3..89a7578060e815edee4d5e4496868157f95d7db9 100644 (file)
@@ -60,12 +60,14 @@ test_random (unsigned int rx_length, unsigned int max_str_len,
   char current_char;
   int eval;
   int eval_check;
+  int eval_computed;
   struct GNUNET_REGEX_Automaton *dfa;
   regex_t rx;
   regmatch_t matchptr[1];
   char error[200];
   int result;
   unsigned int str_len;
+  char *computed_regex;
 
   // At least one string is needed for matching
   GNUNET_assert (str_count > 0);
@@ -151,6 +153,7 @@ test_random (unsigned int rx_length, unsigned int max_str_len,
     }
 
     eval = GNUNET_REGEX_eval (dfa, matching_str[i]);
+    computed_regex = GNUNET_strdup (GNUNET_REGEX_get_computed_regex (dfa));
     GNUNET_REGEX_automaton_destroy (dfa);
 
     // Match string using glibc regex
@@ -164,6 +167,19 @@ test_random (unsigned int rx_length, unsigned int max_str_len,
     eval_check = regexec (&rx, matching_str[i], 1, matchptr, 0);
     regfree (&rx);
 
+    // Match computed regex
+    if (0 != regcomp (&rx, computed_regex, REG_EXTENDED))
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                  "Could not compile regex using regcomp: %s\n",
+                  computed_regex);
+      return -1;
+    }
+
+    eval_computed = regexec (&rx, matching_str[i], 1, matchptr, 0);
+    regfree (&rx);
+    GNUNET_free (computed_regex);
+
     // We only want to match the whole string, because that's what our DFA does, too.
     if (eval_check == 0 &&
         (matchptr[0].rm_so != 0 ||
@@ -247,8 +263,9 @@ main (int argc, char *argv[])
   int check_nfa;
   int check_dfa;
   int check_rand;
+  char *check_proof;
 
-  struct Regex_String_Pair rxstr[8] = {
+  struct Regex_String_Pair rxstr[12] = {
     {"ab?(abcd)?", 5,
      {"ababcd", "abab", "aabcd", "a", "abb"},
      {match, nomatch, match, match, nomatch}},
@@ -272,6 +289,19 @@ main (int argc, char *argv[])
     {"V|M*o?x*p*d+h+b|E*m?h?Y*E*O?W*W*P+o?Z+H*M|I*q+C*a+5?5*9|b?z|G*y*k?R|p+u|8*h?B+l*H|e|L*O|1|F?v*0?5|C+", 1,
      {"VMoxpdhbEmhYEOWWPoZHMIqCa559bzGykRpu8hBlHeLO1Fv05C"},
      {nomatch}},
+    {"(bla)*", 8,
+     {"", "bla", "blabla", "bl", "la", "b", "l", "a"},
+     {match, match, match, nomatch, nomatch, nomatch, nomatch, nomatch}},
+    {"ab(c|d)+c*(a(b|c)+d)+(bla)(bla)*", 8,
+     {"ab", "abcabdbla", "abdcccccccccccabcbccdblablabla", "bl", "la", "b", "l",
+      "a"},
+     {nomatch, match, match, nomatch, nomatch, nomatch, nomatch, nomatch}},
+    {"a|aa*a", 6,
+     {"", "a", "aa", "aaa", "aaaa", "aaaaa"},
+     {nomatch, match, match, match, match, match}},
+    {"ab(c|d)+c*(a(b|c)+d)+(bla)+", 1,
+     {"abcabdblaacdbla"},
+     {nomatch}},
     {"ab(c|d)+c*(a(b|c)d)+", 1,
      {"abacd"},
      {nomatch}}
@@ -281,7 +311,7 @@ main (int argc, char *argv[])
   check_dfa = 0;
   check_rand = 0;
 
-  for (i = 0; i < 8; i++)
+  for (i = 0; i < 12; i++)
   {
     if (0 != regcomp (&rx, rxstr[i].regex, REG_EXTENDED))
     {
@@ -298,7 +328,14 @@ main (int argc, char *argv[])
     // DFA test
     a = GNUNET_REGEX_construct_dfa (rxstr[i].regex, strlen (rxstr[i].regex));
     check_dfa += test_automaton (a, &rx, &rxstr[i]);
+    check_proof = GNUNET_strdup (GNUNET_REGEX_get_computed_regex (a));
+    GNUNET_REGEX_automaton_destroy (a);
+    a = GNUNET_REGEX_construct_dfa (check_proof, strlen (check_proof));
+    check_dfa += test_automaton (a, &rx, &rxstr[i]);
     GNUNET_REGEX_automaton_destroy (a);
+    if (0 != check_dfa)
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "check_proof: %s\n", check_proof);
+    GNUNET_free_non_null (check_proof);
 
     regfree (&rx);
   }