add nick name for cached records
[oweals/gnunet.git] / src / regex / test_regex_proofs.c
index 831f4dc48e92d6470c4cc6d754d141a5409d11d7..74f0c1615e38cbe174e4e23d4ccf9f5bd0029be3 100644 (file)
@@ -23,7 +23,8 @@
  * @author Maximilian Szengel
  */
 #include "platform.h"
-#include "gnunet_regex_lib.h"
+#include "regex_internal_lib.h"
+#include "regex_test_lib.h"
 #include "regex_internal.h"
 
 
  *
  * @return 0 on success, 1 on failure
  */
-unsigned int
+static unsigned int
 test_proof (const char *regex)
 {
   unsigned int error;
-  struct GNUNET_REGEX_Automaton *dfa;
+  struct REGEX_INTERNAL_Automaton *dfa;
   char *c_rx1;
   const char *c_rx2;
 
-  dfa = GNUNET_REGEX_construct_dfa (regex, strlen (regex));
-  c_rx1 = GNUNET_strdup (GNUNET_REGEX_get_canonical_regex (dfa));
-  GNUNET_REGEX_automaton_destroy (dfa);
-  dfa = GNUNET_REGEX_construct_dfa (c_rx1, strlen (c_rx1));
-  c_rx2 = GNUNET_REGEX_get_canonical_regex (dfa);
+  dfa = REGEX_INTERNAL_construct_dfa (regex, strlen (regex), 1);
+  GNUNET_assert (NULL != dfa);
+  c_rx1 = GNUNET_strdup (REGEX_INTERNAL_get_canonical_regex (dfa));
+  REGEX_INTERNAL_automaton_destroy (dfa);
+  dfa = REGEX_INTERNAL_construct_dfa (c_rx1, strlen (c_rx1), 1);
+  GNUNET_assert (NULL != dfa);
+  c_rx2 = REGEX_INTERNAL_get_canonical_regex (dfa);
 
   error = (0 == strcmp (c_rx1, c_rx2)) ? 0 : 1;
 
@@ -62,11 +65,12 @@ test_proof (const char *regex)
   }
 
   GNUNET_free (c_rx1);
-  GNUNET_REGEX_automaton_destroy (dfa);
+  REGEX_INTERNAL_automaton_destroy (dfa);
 
   return error;
 }
 
+
 /**
  * Use 'test_proof' function to randomly test the canonical regexes of 'count'
  * random expressions of length 'rx_length'.
@@ -76,7 +80,7 @@ test_proof (const char *regex)
  *
  * @return 0 on succes, number of failures otherwise.
  */
-unsigned int
+static unsigned int
 test_proofs_random (unsigned int count, size_t rx_length)
 {
   unsigned int i;
@@ -87,7 +91,7 @@ test_proofs_random (unsigned int count, size_t rx_length)
 
   for (i = 0; i < count; i++)
   {
-    rand_rx = GNUNET_REGEX_generate_random_regex (rx_length, NULL);
+    rand_rx = REGEX_TEST_generate_random_regex (rx_length, NULL);
     failures += test_proof (rand_rx);
     GNUNET_free (rand_rx);
   }
@@ -95,59 +99,57 @@ test_proofs_random (unsigned int count, size_t rx_length)
   return failures;
 }
 
+
 /**
  * Test a number of known examples of regexes for proper canonicalization.
  *
  * @return 0 on success, number of failures otherwise.
  */
-unsigned int
-test_proofs_static (void)
+static unsigned int
+test_proofs_static ()
 {
   unsigned int i;
   unsigned int error;
 
-  const char *regex[6] = {
+  const char *regex[8] = {
     "a|aa*a",
     "a+",
     "a*",
     "a*a*",
     "(F*C|WfPf|y+F*C)",
     "y*F*C|WfPf",
-    /* "2?jA?e?D*K", */
-    /* "((j|2j)K|(j|2j)AK|(j|2j)(D|e|(j|2j)A(D|e))D*K)", */
-    /* "((j|2j)K|(j|2j)(D|e|((j|2j)j|(j|2j)2j)A(D|e))D*K|(j|2j)AK)", */
+    "((a|b)c|(a|b)(d|(a|b)e))",
+    "((a|b)(c|d)|(a|b)(a|b)e)"
   };
 
   const char *canon_rx1;
   const char *canon_rx2;
-  struct GNUNET_REGEX_Automaton *dfa1;
-  struct GNUNET_REGEX_Automaton *dfa2;
+  struct REGEX_INTERNAL_Automaton *dfa1;
+  struct REGEX_INTERNAL_Automaton *dfa2;
 
   error = 0;
 
-  for (i = 0; i < 6; i += 2)
+  for (i = 0; i < 8; i += 2)
   {
-    dfa1 = GNUNET_REGEX_construct_dfa (regex[i], strlen (regex[i]));
-    dfa2 = GNUNET_REGEX_construct_dfa (regex[i + 1], strlen (regex[i + 1]));
+    dfa1 = REGEX_INTERNAL_construct_dfa (regex[i], strlen (regex[i]), 1);
+    dfa2 = REGEX_INTERNAL_construct_dfa (regex[i + 1], strlen (regex[i + 1]), 1);
+    GNUNET_assert (NULL != dfa1);
+    GNUNET_assert (NULL != dfa2);
 
-    canon_rx1 = GNUNET_REGEX_get_canonical_regex (dfa1);
-    canon_rx2 = GNUNET_REGEX_get_canonical_regex (dfa2);
+    canon_rx1 = REGEX_INTERNAL_get_canonical_regex (dfa1);
+    canon_rx2 = REGEX_INTERNAL_get_canonical_regex (dfa2);
 
     error += (0 == strcmp (canon_rx1, canon_rx2)) ? 0 : 1;
 
     if (error > 0)
     {
       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                  "Comparing canonical regex failed:\nrx1: %s\ncrx1: %s\nrx2: %s\ncrx2: %s\n",
+                  "Comparing canonical regex failed:\nrx1:\t%s\ncrx1:\t%s\nrx2:\t%s\ncrx2:\t%s\n",
                   regex[i], canon_rx1, regex[i + 1], canon_rx2);
-
-      /* Save the graphs of the two conflicting DFAs */
-      /* GNUNET_REGEX_automaton_save_graph (dfa1, "proofs_fail_dfa1.dot"); */
-      /* GNUNET_REGEX_automaton_save_graph (dfa2, "proofs_fail_dfa2.dot"); */
     }
 
-    GNUNET_REGEX_automaton_destroy (dfa1);
-    GNUNET_REGEX_automaton_destroy (dfa2);
+    REGEX_INTERNAL_automaton_destroy (dfa1);
+    REGEX_INTERNAL_automaton_destroy (dfa2);
   }
 
   return error;
@@ -157,20 +159,14 @@ test_proofs_static (void)
 int
 main (int argc, char *argv[])
 {
-  GNUNET_log_setup ("test-regex",
-#if VERBOSE
-                    "DEBUG",
-#else
-                    "WARNING",
-#endif
-                    NULL);
+  GNUNET_log_setup ("test-regex", "WARNING", NULL);
 
   int error;
 
   error = 0;
 
   error += test_proofs_static ();
-  /* error += test_proofs_random (10000, 10); */
+  error += test_proofs_random (100, 30);
 
   return error;
 }