]> mj.ucw.cz Git - libucw.git/blobdiff - lib/kmp-test.c
Try to merge recent changes in v3.9 to image branch...
[libucw.git] / lib / kmp-test.c
index fa07f06035ea7d6b780128fa0f809fea2d516023..32d95cea4c9a7eba56d67510e4ab9fa9842d277b 100644 (file)
@@ -1,3 +1,9 @@
+/*
+ *      Test of KMP search
+ *
+ *      (c) 2006, Pavel Charvat <pchar@ucw.cz>
+ */
+
 #include "lib/lib.h"
 #include "lib/mempool.h"
 #include <string.h>
 #define TRACE(x...) do{}while(0)
 #endif
 
+/* TEST1 - multiple searches */
+
 #define KMP_PREFIX(x) GLUE_(kmp1,x)
 #define KMP_WANT_CLEANUP
-#define KMP_WANT_SEARCH
+#include "lib/kmp-new.h"
+#define KMPS_PREFIX(x) GLUE_(kmp1s1,x)
+#define KMPS_KMP_PREFIX(x) GLUE_(kmp1,x)
 #define KMPS_WANT_BEST
 #define KMPS_T uns
 #define KMPS_EXIT(ctx,src,s) do{ return s.best->len; }while(0)
-#include "lib/kmp-new.h"
+#include "lib/kmp-search.h"
+#define KMPS_PREFIX(x) GLUE_(kmp1s2,x)
+#define KMPS_KMP_PREFIX(x) GLUE_(kmp1,x)
+#define KMPS_EXTRA_VAR uns
+#define KMPS_INIT(ctx,src,s) do{ s.v = 0; }while(0)
+#define KMPS_T uns
+#define KMPS_FOUND(ctx,src,s) do{ s.v++; }while(0)
+#define KMPS_EXIT(ctx,src,s) do{ return s.v; }while(0)
+#define KMPS_WANT_BEST
+#include "lib/kmp-search.h"
 
 static void
 test1(void)
 {
-  log(L_INFO, "Running test1");
+  TRACE("Running test1");
   struct kmp1_context ctx;
   kmp1_init(&ctx);
   kmp1_add(&ctx, "ahoj");
   kmp1_add(&ctx, "hoj");
   kmp1_add(&ctx, "aho");
   kmp1_build(&ctx);
-  UNUSED uns best = kmp1_search(&ctx, "asjlahslhalahosjkjhojsas");
+  UNUSED uns best = kmp1s1_search(&ctx, "asjlahslhalahosjkjhojsas");
   TRACE("Best match has %d characters", best);
   ASSERT(best == 3);
+  UNUSED uns count = kmp1s2_search(&ctx, "asjlahslhalahojsjkjhojsas");
+  ASSERT(count == 4);
   kmp1_cleanup(&ctx);
 }
 
+/* TEST2 - various tracing */
+
 #define KMP_PREFIX(x) GLUE_(kmp2,x)
 #define KMP_USE_UTF8
 #define KMP_TOLOWER
@@ -56,7 +79,7 @@ test1(void)
 static void
 test2(void)
 {
-  log(L_INFO, "Running test2");
+  TRACE("Running test2");
   struct kmp2_context ctx;
   kmp2_init(&ctx);
   kmp2_add(&ctx, "ahoj", 1);
@@ -71,6 +94,8 @@ test2(void)
   kmp2_cleanup(&ctx);
 }
 
+/* TEST3 - random tests */
+
 #define KMP_PREFIX(x) GLUE_(kmp3,x)
 #define KMP_NODE uns
 #define KMP_ADD_EXTRA_ARGS uns index
@@ -87,7 +112,7 @@ test2(void)
 static void
 test3(void)
 {
-  log(L_INFO, "Running test3");
+  TRACE("Running test3");
   struct mempool *pool = mp_new(1024);
   for (uns testn = 0; testn < 100; testn++)
   {
@@ -130,11 +155,38 @@ test3(void)
   mp_delete(pool);
 }
 
+/* TEST4 - user-defined character type
+ * FIXME: it would need custom compare and hash functions to be really valid */
+
+#define KMP_PREFIX(x) GLUE_(kmp4,x)
+#define KMP_CHAR byte *
+#define KMP_CONTROL_CHAR NULL
+#define KMP_GET_CHAR(ctx,src,c) ({ c = src++; !!*c; })
+#define KMP_WANT_CLEANUP
+#define KMP_WANT_SEARCH
+#define KMPS_FOUND(ctx,src,s) do{ ASSERT(0); }while(0)
+#define KMPS_ADD_CONTROLS
+#define KMPS_MERGE_CONTROLS
+#include "lib/kmp-new.h"
+
+static void
+test4(void)
+{
+  TRACE("Running test4");
+  struct kmp4_context ctx;
+  kmp4_init(&ctx);
+  kmp4_add(&ctx, "ahoj");
+  kmp4_build(&ctx);
+  kmp4_search(&ctx, "djdhaskjdahoahaahojojshdaksjahdahojskj");
+  kmp4_cleanup(&ctx);
+}
+
 int
 main(void)
 {
   test1();
   test2();
   test3();
+  test4();
   return 0;
 }