summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--Makefile12
-rw-r--r--fzytest.c42
-rw-r--r--test.rb54
-rw-r--r--testscore.c22
5 files changed, 50 insertions, 82 deletions
diff --git a/.gitignore b/.gitignore
index f54609d..8a74f1f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,3 @@
fzy
-testscore
+fzytest
*.o
diff --git a/Makefile b/Makefile
index ba1de16..aaca829 100644
--- a/Makefile
+++ b/Makefile
@@ -1,12 +1,12 @@
CFLAGS+=-Wall -Wextra -g -std=c99
-all: fzy testscore
+all: fzy fzytest
-testscore: testscore.o match.o
+fzytest: fzytest.o match.o
$(CC) $(CCFLAGS) -o $@ $^
-test: testscore
- ruby test.rb
+test: fzytest
+ -./fzytest
fzy: fzy.o match.o
$(CC) $(CCFLAGS) -o $@ $^
@@ -15,4 +15,6 @@ fzy: fzy.o match.o
$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<
clean:
- $(RM) fzy testscore *.o
+ $(RM) fzy fzytest *.o
+
+.PHONY: test all clean
diff --git a/fzytest.c b/fzytest.c
new file mode 100644
index 0000000..240d16a
--- /dev/null
+++ b/fzytest.c
@@ -0,0 +1,42 @@
+#include <stdio.h>
+#include "fzy.h"
+
+const char *testname;
+int testsrun = 0, testspassed = 0;
+
+#define TEST(name) int test_##name(){ testname = #name; testsrun++; do
+#define ENDTEST while(0); testspassed++; return 0;}
+#define assert(x) if(!(x)){fprintf(stderr, "test \"%s\" failed\n assert(%s) was false\n at %s:%i\n\n", testname, #x, __FILE__ ,__LINE__);return -1;}
+
+TEST(match){
+ assert(has_match("a", "a"));
+ assert(has_match("a", "ab"));
+ assert(has_match("a", "ba"));
+ assert(has_match("abc", "a|b|c"));
+
+ /* non-match */
+ assert(!has_match("a", ""));
+ assert(!has_match("a", "b"));
+
+ /* match when query is empty */
+ assert(has_match("", ""));
+ assert(has_match("", "a"));
+}ENDTEST
+
+TEST(scoring){
+ assert(match("amo", "app/models/foo") < match("amo", "app/models/order"));
+}ENDTEST
+
+void summary(){
+ printf("%i tests run: %i passed %i failed\n", testsrun, testspassed, testsrun - testspassed);
+}
+
+int main(int argc, char *argv[]){
+ test_match();
+ test_scoring();
+
+ summary();
+
+ /* exit 0 if all tests pass */
+ return testsrun != testspassed;
+}
diff --git a/test.rb b/test.rb
deleted file mode 100644
index cf02c53..0000000
--- a/test.rb
+++ /dev/null
@@ -1,54 +0,0 @@
-require "minitest/autorun"
-
-# Largely borrowed from selecta
-describe "score" do
- def score(candidate, query)
- # FIXME: should escape this properly
- ret = `./testscore '#{query}' '#{candidate}'`
- ret.to_f unless ret.empty?
- end
-
- def assert_unmatched(candidate, query)
- assert_equal nil, score(candidate, query)
- end
-
- def assert_matched(candidate, query)
- assert_operator 0, :<=, score(candidate, query)
- end
-
- it "scores 1 when the query is empty" do
- assert_equal 1, score("a", "")
- end
-
- it "scores 0 when the choice is empty" do
- assert_unmatched "", "a"
- end
-
- it "scores 1 when exact match" do
- assert_equal 1, score("a", "a")
- end
-
- it "scores 0 when the query is longer than the choice" do
- assert_unmatched "short", "longer"
- end
-
- it "scores 0 when the query doesn't match at all" do
- assert_unmatched "a", "b"
- end
-
- it "scores 0 when only a prefix of the query matches" do
- assert_unmatched "ab", "ac"
- end
-
- it "scores greater than 0 when it matches" do
- assert_matched "a", "a"
- assert_matched "ab", "a"
- assert_matched "ba", "a"
- assert_matched "bab", "a"
- assert_matched "bababababab", "aaaaa"
- end
-
- it "prefers start of words" do
- assert_operator score("app/models/foo", "amo"), :<, score("app/models/order", "amo")
- end
-end
diff --git a/testscore.c b/testscore.c
deleted file mode 100644
index 00d227f..0000000
--- a/testscore.c
+++ /dev/null
@@ -1,22 +0,0 @@
-#include <stdlib.h>
-#include <stdio.h>
-
-#include "fzy.h"
-double match(const char *needle, const char *haystack);
-
-void usage(const char *argv0){
- fprintf(stderr, "USAGE: %s QUERY CANDIDATE\n", argv0);
-}
-
-int main(int argc, char *argv[]){
- if(argc != 3){
- usage(argv[0]);
- }
-
- if(has_match(argv[1], argv[2])){
- double result = match(argv[1], argv[2]);
- printf("%f\n", result);
- }
-
- return 0;
-}