[gnome-builder] tests: add fuzzy search test helper



commit 3baa72738c7b3666adbc373a260dd008e806b542
Author: Christian Hergert <christian hergert me>
Date:   Fri Jun 12 15:50:17 2015 -0700

    tests: add fuzzy search test helper
    
    ./test-fuzzy some_file_to_index query

 tests/Makefile.am  |   19 ++++++++++++++++
 tests/test-fuzzy.c |   59 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+), 0 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 8393ac5..e5b4441 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -24,6 +24,19 @@ rg_libs = \
        $(RG_LIBS) \
        $(NULL)
 
+search_cflags = \
+       $(DEBUG_CFLAGS) \
+       $(SEARCH_CFLAGS) \
+       -I$(top_srcdir)/contrib/search \
+       -I$(top_srcdir)/libide/util \
+       $(NULL)
+
+search_libs = \
+       $(SEARCH_LIBS) \
+       $(top_builddir)/contrib/search/libsearch.la \
+       $(top_builddir)/libide/libide-1.0.la \
+       $(NULL)
+
 tests_cflags = \
        $(egg_cflags) \
        -I$(top_srcdir)/libide \
@@ -130,6 +143,12 @@ test_cpu_graph_CFLAGS = $(rg_cflags)
 test_cpu_graph_LDADD = $(rg_libs)
 
 
+misc_programs += test-fuzzy
+test_fuzzy_SOURCES = test-fuzzy.c
+test_fuzzy_CFLAGS = $(search_cflags)
+test_fuzzy_LDADD = $(search_libs)
+
+
 TESTS += test-ide-ctags
 test_ide_ctags_SOURCES = test-ide-ctags.c
 test_ide_ctags_CFLAGS = $(tests_cflags)
diff --git a/tests/test-fuzzy.c b/tests/test-fuzzy.c
new file mode 100644
index 0000000..b8f078e
--- /dev/null
+++ b/tests/test-fuzzy.c
@@ -0,0 +1,59 @@
+#include <fuzzy.h>
+#include <ide-line-reader.h>
+
+int
+main (int argc,
+      char *argv[])
+{
+  IdeLineReader reader;
+  Fuzzy *fuzzy;
+  GArray *ar;
+  gchar *contents;
+  gchar *line;
+  gsize len;
+  gsize line_len;
+
+  if (argc < 3)
+    {
+      g_printerr ("usage: %s FILENAME QUERY\n", argv[0]);
+      return 1;
+    }
+
+  fuzzy = fuzzy_new (FALSE);
+
+  g_print ("Loading contents\n");
+  g_file_get_contents (argv [1], &contents, &len, NULL);
+  g_print ("Loaded\n");
+
+  ide_line_reader_init (&reader, contents, len);
+
+  fuzzy_begin_bulk_insert (fuzzy);
+
+  g_print ("Building index.\n");
+  while ((line = ide_line_reader_next (&reader, &line_len)))
+    {
+      line [line_len] = '\0';
+      fuzzy_insert (fuzzy, line, NULL);
+    }
+  fuzzy_end_bulk_insert (fuzzy);
+  g_print ("Built.\n");
+
+  g_free (contents);
+
+  ar = fuzzy_match (fuzzy, argv[2], 0);
+
+  for (guint i = 0; i < ar->len; i++)
+    {
+      FuzzyMatch *m = &g_array_index (ar, FuzzyMatch, i);
+
+      g_print ("%0.3lf: %s\n", m->score, m->key);
+    }
+
+  g_print ("%d matches\n", ar->len);
+
+  g_array_unref (ar);
+
+  fuzzy_unref (fuzzy);
+
+  return 0;
+}


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