gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r20883 - in gnunet/src: include regex


From: gnunet
Subject: [GNUnet-SVN] r20883 - in gnunet/src: include regex
Date: Tue, 3 Apr 2012 15:46:36 +0200

Author: szengel
Date: 2012-04-03 15:46:35 +0200 (Tue, 03 Apr 2012)
New Revision: 20883

Removed:
   gnunet/src/regex/regex.h
Modified:
   gnunet/src/include/gnunet_regex_lib.h
   gnunet/src/regex/Makefile.am
   gnunet/src/regex/regex.c
   gnunet/src/regex/test_regex.c
Log:
fix


Modified: gnunet/src/include/gnunet_regex_lib.h
===================================================================
--- gnunet/src/include/gnunet_regex_lib.h       2012-04-03 13:46:00 UTC (rev 
20882)
+++ gnunet/src/include/gnunet_regex_lib.h       2012-04-03 13:46:35 UTC (rev 
20883)
@@ -89,7 +89,7 @@
  * @param a automaton
  * @param string string to check
  *
- * @return GNUNET_YES if 'a' matches 'string', GNUNET_NO otherwise
+ * @return 0 if string matches, non 0 otherwise
  */
 int
 GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, 

Modified: gnunet/src/regex/Makefile.am
===================================================================
--- gnunet/src/regex/Makefile.am        2012-04-03 13:46:00 UTC (rev 20882)
+++ gnunet/src/regex/Makefile.am        2012-04-03 13:46:35 UTC (rev 20883)
@@ -11,7 +11,7 @@
 lib_LTLIBRARIES = libgnunetregex.la
 
 libgnunetregex_la_SOURCES = \
-  regex.c regex.h
+  regex.c
 libgnunetregex_la_LIBADD = -lm \
  $(top_builddir)/src/util/libgnunetutil.la
 libgnunetregex_la_LDFLAGS = \

Modified: gnunet/src/regex/regex.c
===================================================================
--- gnunet/src/regex/regex.c    2012-04-03 13:46:00 UTC (rev 20882)
+++ gnunet/src/regex/regex.c    2012-04-03 13:46:35 UTC (rev 20883)
@@ -721,7 +721,10 @@
       for (k = 0; k < cls->len; k++)
       {
         if (sset->states[j]->id == cls->states[k]->id)
+        {
           contains = 1;
+          break;
+        }
       }
       if (!contains)
         GNUNET_array_append (cls->states, cls->len, sset->states[j]);
@@ -1249,7 +1252,7 @@
  * @param a automaton, type must be DFA
  * @param string string that should be evaluated
  *
- * @return GNUNET_YES if string matches, GNUNET_NO if not, GNUNET_SYSERR 
otherwise
+ * @return 0 if string matches, non 0 otherwise
  */
 static int
 evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
@@ -1261,7 +1264,7 @@
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Tried to evaluate DFA, but NFA automaton given");
-    return GNUNET_SYSERR;
+    return -1;
   }
 
   s = a->start;
@@ -1274,9 +1277,9 @@
   }
 
   if (NULL != s && s->accepting)
-    return GNUNET_YES;
+    return 0;
 
-  return GNUNET_NO;
+  return 1;
 }
 
 /**
@@ -1285,7 +1288,7 @@
  * @param a automaton, type must be NFA
  * @param string string that should be evaluated
  *
- * @return GNUNET_YES if string matches, GNUNET_NO if not, GNUNET_SYSERR 
otherwise
+ * @return 0 if string matches, non 0 otherwise
  */
 static int
 evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
@@ -1301,13 +1304,12 @@
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Tried to evaluate NFA, but DFA automaton given");
-    return GNUNET_SYSERR;
+    return -1;
   }
 
-  result = GNUNET_NO;
+  result = 1;
   strp = string;
-  sset = GNUNET_malloc (sizeof (struct StateSet));
-  GNUNET_array_append (sset->states, sset->len, a->start);
+  sset = nfa_closure_create (a->start, 0);
 
   for (strp = string; NULL != strp && *strp; strp++)
   {
@@ -1322,7 +1324,7 @@
     s = sset->states[i];
     if (NULL != s && s->accepting)
     {
-      result = GNUNET_YES;
+      result = 0;
       break;
     }
   }
@@ -1338,7 +1340,7 @@
  * @param a automaton
  * @param string string to check
  *
- * @return GNUNET_YES if 'a' matches 'string', GNUNET_NO otherwise
+ * @return 0 if string matches, non 0 otherwise
  */
 int
 GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)

Deleted: gnunet/src/regex/regex.h
===================================================================
--- gnunet/src/regex/regex.h    2012-04-03 13:46:00 UTC (rev 20882)
+++ gnunet/src/regex/regex.h    2012-04-03 13:46:35 UTC (rev 20883)
@@ -1,29 +0,0 @@
-/*
-     This file is part of GNUnet
-     (C) 2012 Christian Grothoff (and other contributing authors)
-
-     GNUnet is free software; you can redistribute it and/or modify
-     it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 3, or (at your
-     option) any later version.
-
-     GNUnet is distributed in the hope that it will be useful, but
-     WITHOUT ANY WARRANTY; without even the implied warranty of
-     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     General Public License for more details.
-
-     You should have received a copy of the GNU General Public License
-     along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
-*/
-/**
- * @file src/regex/regex.h
- * @brief library to create automatons from regular expressions
- * @author Maximilian Szengel
- */
-#ifndef REGEX_H
-#define REGEX_H
-#include "gnunet_regex_lib.h"
-
-#endif

Modified: gnunet/src/regex/test_regex.c
===================================================================
--- gnunet/src/regex/test_regex.c       2012-04-03 13:46:00 UTC (rev 20882)
+++ gnunet/src/regex/test_regex.c       2012-04-03 13:46:35 UTC (rev 20883)
@@ -22,12 +22,62 @@
  * @brief test for regex.c
  * @author Maximilian Szengel
  */
+#include <regex.h>
+
 #include "platform.h"
 #include "gnunet_regex_lib.h"
 
-static int err = 0;
+enum Match_Result
+{
+  match = 0,
+  nomatch = 1
+};
 
+struct Regex_String_Pair
+{
+  char *regex;
+  char *string;
+  enum Match_Result expected_result;
+};
+
+
 int
+test_automaton (struct GNUNET_REGEX_Automaton *a, struct Regex_String_Pair 
*rxstr)
+{
+  regex_t rx;
+  int result;
+  int eval;
+  int eval_check;
+
+  if (NULL == a)
+    return 1;
+
+  result = 0;
+
+  eval = GNUNET_REGEX_eval (a, rxstr->string);
+  regcomp (&rx, rxstr->regex, REG_EXTENDED);
+  eval_check = regexec (&rx, rxstr->string, 0, NULL, 0);
+
+  if ((rxstr->expected_result == match
+       && (0 != eval || 0 != eval_check))
+      ||
+      (rxstr->expected_result == nomatch
+       && (0 == eval || 0 == eval_check)))
+  {
+      result = 1;
+      char error[200];
+      regerror (eval_check, &rx, error, sizeof error);
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
+                  "Unexpected result:\nregex: %s\nstring: %s\nexpected result: 
%i\ngnunet regex: %i\nglibc regex: %i\nglibc error: %s\n\n", 
+                  rxstr->regex, rxstr->string, rxstr->expected_result, eval, 
eval_check, error);
+  }
+
+  regfree (&rx);
+
+  return result;
+}
+
+int
 main (int argc, char *argv[])
 {
   GNUNET_log_setup ("test-regex",
@@ -38,47 +88,36 @@
 #endif
                     NULL);
 
-  struct GNUNET_REGEX_Automaton *nfa;
-  struct GNUNET_REGEX_Automaton *dfa;
-  char *regex;
-  char *string;
-  int eval;
+  int check_nfa;
+  int check_dfa;
+  struct Regex_String_Pair rxstr[3];
+  struct GNUNET_REGEX_Automaton *a;
+  int i;
 
-  nfa = NULL;
-  dfa = NULL;
+  rxstr[0].regex = "ab(c|d)+c*(a(b|c)d)+";
+  rxstr[0].string = "abcdcdcdcdddddabd";
+  rxstr[0].expected_result = match;
 
-  regex = "a\\*b(c|d)+c*(a(b|c)d)+";
-  string = "a*bcdcdcdcdddddabd";
-  /*regex = "VPN TCP (IPv4|IPv6) Port53"; */
-  /*string = "VPN TCP IPv4 Port53"; */
-  /*regex = "\\*a(a|b)b"; */
-  /*regex = "a(a|b)c"; */
-  /*regex = "(a|aa)+"; */
-  nfa = GNUNET_REGEX_construct_nfa (regex, strlen (regex));
+  rxstr[1].regex = "a*";
+  rxstr[1].string = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
+  rxstr[1].expected_result = match;
 
-  if (nfa)
+  rxstr[2].regex = "a*b*c*d+";
+  rxstr[2].string = "a";
+  rxstr[2].expected_result = nomatch;
+
+  for (i=0; i<3; i++)
   {
-    GNUNET_REGEX_automaton_save_graph (nfa, "nfa_graph.dot");
-    eval = GNUNET_REGEX_eval (nfa, string);
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Evaluating %s result: %i\n", string,
-                eval);
-    if (GNUNET_YES != eval)
-      err = 1;
-    GNUNET_REGEX_automaton_destroy (nfa);
+    // NFA test
+    a = GNUNET_REGEX_construct_nfa (rxstr[i].regex, strlen (rxstr[i].regex));
+    check_nfa += test_automaton (a, &rxstr[i]);
+    GNUNET_REGEX_automaton_destroy (a);
+
+    // DFA test
+    a = GNUNET_REGEX_construct_dfa (rxstr[i].regex, strlen (rxstr[i].regex));
+    check_dfa += test_automaton (a, &rxstr[i]);
+    GNUNET_REGEX_automaton_destroy (a);
   }
-  else
-    err = 1;
 
-  dfa = GNUNET_REGEX_construct_dfa (regex, strlen (regex));
-  if (dfa)
-  {
-    GNUNET_REGEX_automaton_save_graph (dfa, "dfa_graph.dot");
-    eval = GNUNET_REGEX_eval (dfa, string);
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Evaluating %s result: %i\n", string,
-                eval);
-    if (GNUNET_YES != eval)
-      err = 1;
-    GNUNET_REGEX_automaton_destroy (dfa);
-  }
-  return err;
+  return check_nfa + check_dfa;
 }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]