new test for regex
authorMaximilian Szengel <gnunet@maxsz.de>
Mon, 25 Jun 2012 11:15:41 +0000 (11:15 +0000)
committerMaximilian Szengel <gnunet@maxsz.de>
Mon, 25 Jun 2012 11:15:41 +0000 (11:15 +0000)
src/regex/Makefile.am
src/regex/regex.c
src/regex/test_regex_proofs.c [new file with mode: 0644]

index 8c73c607c047a47d1968deb09311961bcfbd0efe..cb9bc093a0529e5e89c98efcbb14292d93d0b1dd 100644 (file)
@@ -20,7 +20,8 @@ libgnunetregex_la_LDFLAGS = \
 
 check_PROGRAMS = \
  test_regex_eval_api \
- test_regex_iterate_api
+ test_regex_iterate_api \
+ test_regex_proofs     
 
 if ENABLE_TEST_RUN
 TESTS = $(check_PROGRAMS)
@@ -38,5 +39,11 @@ test_regex_iterate_api_LDADD = \
  $(top_builddir)/src/regex/libgnunetregex.la \
  $(top_builddir)/src/util/libgnunetutil.la
 
+test_regex_proofs_SOURCES = \
+ test_regex_proofs.c
+test_regex_proofs_LDADD = \
+ $(top_builddir)/src/regex/libgnunetregex.la \
+ $(top_builddir)/src/util/libgnunetutil.la
+
 EXTRA_DIST = 
 # test_regex_data.conf
index bf65703b4866d0b10381c74a0f6dd4bd7e56b934..b0fb4c175252271f210edca9f27ea12de09ba997 100644 (file)
@@ -919,7 +919,15 @@ remove_epsilon (const char *str)
   return GNUNET_strdup (str);
 }
 
-
+/** 
+ * Compare 'str1', starting from position 'k',  with whole 'str2'
+ * 
+ * @param str1 first string to compare, starting from position 'k'
+ * @param str2 second string for comparison
+ * @param k starting position in 'str1'
+ * 
+ * @return -1 if any of the strings is NULL, 0 if equal, non 0 otherwise
+ */
 static int
 strkcmp (const char *str1, const char *str2, size_t k)
 {
diff --git a/src/regex/test_regex_proofs.c b/src/regex/test_regex_proofs.c
new file mode 100644 (file)
index 0000000..47cc4ee
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+     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 regex/test_regex_proofs.c
+ * @brief test for regex.c
+ * @author Maximilian Szengel
+ */
+#include <regex.h>
+#include <time.h>
+#include "platform.h"
+#include "gnunet_regex_lib.h"
+
+int
+main (int argc, char *argv[])
+{
+  GNUNET_log_setup ("test-regex",
+#if VERBOSE
+                    "DEBUG",
+#else
+                    "WARNING",
+#endif
+                    NULL);
+
+  int error;
+  int i;
+  const char *regex[21] = {
+    "ab(c|d)+c*(a(b|c)+d)+(bla)+",
+    "(bla)*",
+    "b(lab)*la",
+    "(ab)*",
+    "ab(c|d)+c*(a(b|c)+d)+(bla)(bla)*",
+    "z(abc|def)?xyz",
+    "1*0(0|1)*",
+    "a+X*y+c|p|R|Z*K*y*R+w|Y*6+n+h*k*w+V*F|W*B*e*",
+    "(cd|ab)*",
+    "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)",
+    "abc(1|0)*def",
+    "ab|ac",
+    "(ab)(ab)*",
+    "ab|cd|ef|gh",
+    "a|b|c|d|e|f|g",
+    "(ab)|(ac)",
+    "a(b|c)",
+    "a*a",
+    "ab?(abcd)?",
+    "(ab|cs|df|sdf)*",
+    "a|aa*a"
+  };
+  char *computed_regex;
+  struct GNUNET_REGEX_Automaton *dfa;
+
+  error = 0;
+
+  for (i = 0; i < 21; i++)
+  {
+    dfa = GNUNET_REGEX_construct_dfa (regex[i], strlen (regex[i]));
+    computed_regex = GNUNET_strdup (GNUNET_REGEX_get_computed_regex (dfa));
+    GNUNET_REGEX_automaton_destroy (dfa);
+
+    dfa = GNUNET_REGEX_construct_dfa (computed_regex, strlen (computed_regex));
+    error += (0 == strcmp (computed_regex, GNUNET_REGEX_get_computed_regex (dfa))) ? 0 : 1;
+    GNUNET_free (computed_regex);
+    GNUNET_REGEX_automaton_destroy (dfa);
+  }
+  
+  return error;
+}