diff options
author | John Hawthorn <john.hawthorn@gmail.com> | 2017-04-02 22:32:33 -0700 |
---|---|---|
committer | John Hawthorn <john.hawthorn@gmail.com> | 2017-04-03 00:32:06 -0700 |
commit | 512b3a39ebc2325e4c13dd63ad9897ddd0737683 (patch) | |
tree | 5f24a24284386a585d4b25183932639eb5a47475 /test/fzytest.c | |
parent | 0c6af5bca314ca641e1e071c77a7b65770e8076b (diff) |
Switch test framework to greatest
Diffstat (limited to 'test/fzytest.c')
-rw-r--r-- | test/fzytest.c | 325 |
1 files changed, 161 insertions, 164 deletions
diff --git a/test/fzytest.c b/test/fzytest.c index 7fe6fb2..109fbe3 100644 --- a/test/fzytest.c +++ b/test/fzytest.c @@ -9,200 +9,206 @@ #include "choices.h" #include "options.h" -int testsrun = 0, testsfailed = 0, assertionsrun = 0; - -#define assert(x) \ - if (++assertionsrun && !(x)) { \ - fprintf(stderr, "test \"%s\" failed\n assert(%s) was false\n at %s:%i\n\n", \ - __func__, #x, __FILE__, __LINE__); \ - raise(SIGTRAP); \ - testsfailed++; \ - return; \ - } - -#define assert_streq(a, b) assert(!strcmp(a, b)) +#include "greatest/greatest.h" static options_t default_options; -void runtest(void (*test)()) { - testsrun++; - test(); -} - -void test_match() { - assert(has_match("a", "a")); - assert(has_match("a", "ab")); - assert(has_match("a", "ba")); - assert(has_match("abc", "a|b|c")); +TEST 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")); - assert(!has_match("ass", "tags")); + ASSERT(!has_match("a", "")); + ASSERT(!has_match("a", "b")); + ASSERT(!has_match("ass", "tags")); /* match when query is empty */ - assert(has_match("", "")); - assert(has_match("", "a")); + ASSERT(has_match("", "")); + ASSERT(has_match("", "a")); + + PASS(); } -void test_relative_scores() { +TEST test_relative_scores() { /* App/Models/Order is better than App/MOdels/zRder */ - assert(match("amor", "app/models/order") > match("amor", "app/models/zrder")); + ASSERT(match("amor", "app/models/order") > match("amor", "app/models/zrder")); /* App/MOdels/foo is better than App/M/fOo */ - assert(match("amo", "app/m/foo") < match("amo", "app/models/foo")); + ASSERT(match("amo", "app/m/foo") < match("amo", "app/models/foo")); /* GEMFIle.Lock < GEMFILe */ - assert(match("gemfil", "Gemfile.lock") < match("gemfil", "Gemfile")); + ASSERT(match("gemfil", "Gemfile.lock") < match("gemfil", "Gemfile")); /* GEMFIle.Lock < GEMFILe */ - assert(match("gemfil", "Gemfile.lock") < match("gemfil", "Gemfile")); + ASSERT(match("gemfil", "Gemfile.lock") < match("gemfil", "Gemfile")); /* Prefer shorter matches */ - assert(match("abce", "abcdef") > match("abce", "abc de")); + ASSERT(match("abce", "abcdef") > match("abce", "abc de")); /* Prefer shorter candidates */ - assert(match("test", "tests") > match("test", "testing")); + ASSERT(match("test", "tests") > match("test", "testing")); /* Scores first letter highly */ - assert(match("test", "testing") > match("test", "/testing")); + ASSERT(match("test", "testing") > match("test", "/testing")); /* Prefer shorter matches */ - assert(match("abc", " a b c ") > match("abc", " a b c ")); - assert(match("abc", " a b c ") > match("abc", " a b c ")); + ASSERT(match("abc", " a b c ") > match("abc", " a b c ")); + ASSERT(match("abc", " a b c ") > match("abc", " a b c ")); + + PASS(); } -void test_exact_scores() { +TEST test_exact_scores() { /* Exact match is SCORE_MAX */ - assert(match("abc", "abc") == SCORE_MAX); - assert(match("aBc", "abC") == SCORE_MAX); + ASSERT(match("abc", "abc") == SCORE_MAX); + ASSERT(match("aBc", "abC") == SCORE_MAX); /* Empty query always results in SCORE_MIN */ - assert(match("", "") == SCORE_MIN); - assert(match("", "a") == SCORE_MIN); - assert(match("", "bb") == SCORE_MIN); + ASSERT(match("", "") == SCORE_MIN); + ASSERT(match("", "a") == SCORE_MIN); + ASSERT(match("", "bb") == SCORE_MIN); /* Gaps */ - assert(match("a", "*a") == SCORE_GAP_LEADING); - assert(match("a", "*ba") == SCORE_GAP_LEADING*2); - assert(match("a", "**a*") == SCORE_GAP_LEADING*2 + SCORE_GAP_TRAILING); - assert(match("a", "**a**") == SCORE_GAP_LEADING*2 + SCORE_GAP_TRAILING*2); - assert(match("aa", "**aa**") == SCORE_GAP_LEADING*2 + SCORE_MATCH_CONSECUTIVE + SCORE_GAP_TRAILING*2); - assert(match("aa", "**a*a**") == SCORE_GAP_LEADING + SCORE_GAP_LEADING + SCORE_GAP_INNER + SCORE_GAP_TRAILING + SCORE_GAP_TRAILING); + ASSERT(match("a", "*a") == SCORE_GAP_LEADING); + ASSERT(match("a", "*ba") == SCORE_GAP_LEADING*2); + ASSERT(match("a", "**a*") == SCORE_GAP_LEADING*2 + SCORE_GAP_TRAILING); + ASSERT(match("a", "**a**") == SCORE_GAP_LEADING*2 + SCORE_GAP_TRAILING*2); + ASSERT(match("aa", "**aa**") == SCORE_GAP_LEADING*2 + SCORE_MATCH_CONSECUTIVE + SCORE_GAP_TRAILING*2); + ASSERT(match("aa", "**a*a**") == SCORE_GAP_LEADING + SCORE_GAP_LEADING + SCORE_GAP_INNER + SCORE_GAP_TRAILING + SCORE_GAP_TRAILING); /* Consecutive */ - assert(match("aa", "*aa") == SCORE_GAP_LEADING + SCORE_MATCH_CONSECUTIVE); - assert(match("aaa", "*aaa") == SCORE_GAP_LEADING + SCORE_MATCH_CONSECUTIVE*2); - assert(match("aaa", "*a*aa") == SCORE_GAP_LEADING + SCORE_GAP_INNER + SCORE_MATCH_CONSECUTIVE); + ASSERT(match("aa", "*aa") == SCORE_GAP_LEADING + SCORE_MATCH_CONSECUTIVE); + ASSERT(match("aaa", "*aaa") == SCORE_GAP_LEADING + SCORE_MATCH_CONSECUTIVE*2); + ASSERT(match("aaa", "*a*aa") == SCORE_GAP_LEADING + SCORE_GAP_INNER + SCORE_MATCH_CONSECUTIVE); /* Slash */ - assert(match("a", "/a") == SCORE_GAP_LEADING + SCORE_MATCH_SLASH); - assert(match("a", "*/a") == SCORE_GAP_LEADING*2 + SCORE_MATCH_SLASH); - assert(match("aa", "a/aa") == SCORE_GAP_LEADING*2 + SCORE_MATCH_SLASH + SCORE_MATCH_CONSECUTIVE); + ASSERT(match("a", "/a") == SCORE_GAP_LEADING + SCORE_MATCH_SLASH); + ASSERT(match("a", "*/a") == SCORE_GAP_LEADING*2 + SCORE_MATCH_SLASH); + ASSERT(match("aa", "a/aa") == SCORE_GAP_LEADING*2 + SCORE_MATCH_SLASH + SCORE_MATCH_CONSECUTIVE); /* Capital */ - assert(match("a", "bA") == SCORE_GAP_LEADING + SCORE_MATCH_CAPITAL); - assert(match("a", "baA") == SCORE_GAP_LEADING*2 + SCORE_MATCH_CAPITAL); - assert(match("aa", "baAa") == SCORE_GAP_LEADING*2 + SCORE_MATCH_CAPITAL + SCORE_MATCH_CONSECUTIVE); + ASSERT(match("a", "bA") == SCORE_GAP_LEADING + SCORE_MATCH_CAPITAL); + ASSERT(match("a", "baA") == SCORE_GAP_LEADING*2 + SCORE_MATCH_CAPITAL); + ASSERT(match("aa", "baAa") == SCORE_GAP_LEADING*2 + SCORE_MATCH_CAPITAL + SCORE_MATCH_CONSECUTIVE); /* Dot */ - assert(match("a", ".a") == SCORE_GAP_LEADING + SCORE_MATCH_DOT); - assert(match("a", "*a.a") == SCORE_GAP_LEADING*3 + SCORE_MATCH_DOT); - assert(match("a", "*a.a") == SCORE_GAP_LEADING + SCORE_GAP_INNER + SCORE_MATCH_DOT); + ASSERT(match("a", ".a") == SCORE_GAP_LEADING + SCORE_MATCH_DOT); + ASSERT(match("a", "*a.a") == SCORE_GAP_LEADING*3 + SCORE_MATCH_DOT); + ASSERT(match("a", "*a.a") == SCORE_GAP_LEADING + SCORE_GAP_INNER + SCORE_MATCH_DOT); + + PASS(); } -void test_positions_1() { +TEST test_positions_1() { size_t positions[3]; match_positions("amo", "app/models/foo", positions); - assert(positions[0] == 0); - assert(positions[1] == 4); - assert(positions[2] == 5); + ASSERT(positions[0] == 0); + ASSERT(positions[1] == 4); + ASSERT(positions[2] == 5); + + PASS(); } -void test_positions_2() { +TEST test_positions_2() { /* * We should prefer matching the 'o' in order, since it's the beginning * of a word. */ size_t positions[4]; match_positions("amor", "app/models/order", positions); - assert(positions[0] == 0); - assert(positions[1] == 4); - assert(positions[2] == 11); + ASSERT(positions[0] == 0); + ASSERT(positions[1] == 4); + ASSERT(positions[2] == 11); + + PASS(); } -void test_positions_3() { +TEST test_positions_3() { size_t positions[2]; match_positions("as", "tags", positions); - assert(positions[0] == 1); - assert(positions[1] == 3); + ASSERT(positions[0] == 1); + ASSERT(positions[1] == 3); + + PASS(); } -void test_positions_4() { +TEST test_positions_4() { size_t positions[2]; match_positions("as", "examples.txt", positions); - assert(positions[0] == 2); - assert(positions[1] == 7); + ASSERT(positions[0] == 2); + ASSERT(positions[1] == 7); + + PASS(); } -void test_positions_5() { +TEST test_positions_5() { size_t positions[3]; match_positions("abc", "a/a/b/c/c", positions); - assert(positions[0] == 2); - assert(positions[1] == 4); - assert(positions[2] == 6); + ASSERT(positions[0] == 2); + ASSERT(positions[1] == 4); + ASSERT(positions[2] == 6); + + PASS(); } -void test_positions_exact() { +TEST test_positions_exact() { size_t positions[3]; match_positions("foo", "foo", positions); - assert(positions[0] == 0); - assert(positions[1] == 1); - assert(positions[2] == 2); + ASSERT(positions[0] == 0); + ASSERT(positions[1] == 1); + ASSERT(positions[2] == 2); + + PASS(); } -void test_choices_empty() { +TEST test_choices_empty() { choices_t choices; choices_init(&choices, &default_options); - assert(choices.size == 0); - assert(choices.available == 0); - assert(choices.selection == 0); + ASSERT(choices.size == 0); + ASSERT(choices.available == 0); + ASSERT(choices.selection == 0); choices_prev(&choices); - assert(choices.selection == 0); + ASSERT(choices.selection == 0); choices_next(&choices); - assert(choices.selection == 0); + ASSERT(choices.selection == 0); choices_destroy(&choices); + + PASS(); } -void test_choices_1() { +TEST test_choices_1() { choices_t choices; choices_init(&choices, &default_options); choices_add(&choices, "tags"); choices_search(&choices, ""); - assert(choices.available == 1); - assert(choices.selection == 0); + ASSERT(choices.available == 1); + ASSERT(choices.selection == 0); choices_search(&choices, "t"); - assert(choices.available == 1); - assert(choices.selection == 0); + ASSERT(choices.available == 1); + ASSERT(choices.selection == 0); choices_prev(&choices); - assert(choices.selection == 0); + ASSERT(choices.selection == 0); choices_next(&choices); - assert(choices.selection == 0); + ASSERT(choices.selection == 0); - assert(!strcmp(choices_get(&choices, 0), "tags")); - assert(choices_get(&choices, 1) == NULL); + ASSERT(!strcmp(choices_get(&choices, 0), "tags")); + ASSERT(choices_get(&choices, 1) == NULL); choices_destroy(&choices); + + PASS(); } -void test_choices_2() { +TEST test_choices_2() { choices_t choices; choices_init(&choices, &default_options); choices_add(&choices, "tags"); @@ -210,71 +216,73 @@ void test_choices_2() { /* Empty search */ choices_search(&choices, ""); - assert(choices.selection == 0); - assert(choices.available == 2); - assert_streq(choices_get(&choices, 0), "tags"); - assert_streq(choices_get(&choices, 1), "test"); + ASSERT(choices.selection == 0); + ASSERT(choices.available == 2); choices_next(&choices); - assert(choices.selection == 1); + ASSERT(choices.selection == 1); choices_next(&choices); - assert(choices.selection == 0); + ASSERT(choices.selection == 0); choices_prev(&choices); - assert(choices.selection == 1); + ASSERT(choices.selection == 1); choices_prev(&choices); - assert(choices.selection == 0); + ASSERT(choices.selection == 0); /* Filtered search */ choices_search(&choices, "te"); - assert(choices.available == 1); - assert(choices.selection == 0); - assert_streq(choices_get(&choices, 0), "test"); + ASSERT(choices.available == 1); + ASSERT(choices.selection == 0); + ASSERT_STR_EQ(choices_get(&choices, 0), "test"); choices_next(&choices); - assert(choices.selection == 0); + ASSERT(choices.selection == 0); choices_prev(&choices); - assert(choices.selection == 0); + ASSERT(choices.selection == 0); /* No results */ choices_search(&choices, "foobar"); - assert(choices.available == 0); - assert(choices.selection == 0); + ASSERT(choices.available == 0); + ASSERT(choices.selection == 0); /* Different order due to scoring */ choices_search(&choices, "ts"); - assert(choices.available == 2); - assert(choices.selection == 0); - assert_streq(choices_get(&choices, 0), "test"); - assert_streq(choices_get(&choices, 1), "tags"); + ASSERT(choices.available == 2); + ASSERT(choices.selection == 0); + ASSERT_STR_EQ(choices_get(&choices, 0), "test"); + ASSERT_STR_EQ(choices_get(&choices, 1), "tags"); choices_destroy(&choices); + + PASS(); } -void test_choices_without_search() { +TEST test_choices_without_search() { /* Before a search is run, it should return no results */ choices_t choices; choices_init(&choices, &default_options); - assert(choices.available == 0); - assert(choices.selection == 0); - assert(choices.size == 0); - assert(choices_get(&choices, 0) == NULL); + ASSERT(choices.available == 0); + ASSERT(choices.selection == 0); + ASSERT(choices.size == 0); + ASSERT(choices_get(&choices, 0) == NULL); choices_add(&choices, "test"); - assert(choices.available == 0); - assert(choices.selection == 0); - assert(choices.size == 1); - assert(choices_get(&choices, 0) == NULL); + ASSERT(choices.available == 0); + ASSERT(choices.selection == 0); + ASSERT(choices.size == 1); + ASSERT(choices_get(&choices, 0) == NULL); choices_destroy(&choices); + + PASS(); } /* Regression test for segfault */ -void test_choices_unicode() { +TEST test_choices_unicode() { choices_t choices; choices_init(&choices, &default_options); @@ -282,9 +290,10 @@ void test_choices_unicode() { choices_search(&choices, "e"); choices_destroy(&choices); + PASS(); } -void test_choices_large_input() { +TEST test_choices_large_input() { choices_t choices; choices_init(&choices, &default_options); @@ -299,54 +308,42 @@ void test_choices_large_input() { choices_search(&choices, "12"); /* Must match `seq 0 99999 | grep '.*1.*2.*' | wc -l` */ - assert(choices.available == 8146); + ASSERT(choices.available == 8146); - assert_streq(choices_get(&choices, 0), "12") + ASSERT_STR_EQ(choices_get(&choices, 0), "12"); for(int i = 0; i < N; i++) { free(strings[i]); } choices_destroy(&choices); -} -void summary() { - printf("%i tests, %i assertions, %i failures\n", testsrun, assertionsrun, testsfailed); + PASS(); } -static void ignore_signal(int signum) { - (void)signum; -} +GREATEST_MAIN_DEFS(); int main(int argc, char *argv[]) { - (void)argc; - (void)argv; - - /* We raise sigtrap on all assertion failures. - * If we have no debugger running, we should ignore it */ - signal(SIGTRAP, ignore_signal); + GREATEST_MAIN_BEGIN(); options_init(&default_options); - runtest(test_match); - runtest(test_relative_scores); - runtest(test_exact_scores); - runtest(test_positions_1); - runtest(test_positions_2); - runtest(test_positions_3); - runtest(test_positions_4); - runtest(test_positions_5); - runtest(test_positions_exact); - - runtest(test_choices_empty); - runtest(test_choices_1); - runtest(test_choices_2); - runtest(test_choices_without_search); - runtest(test_choices_unicode); - runtest(test_choices_large_input); - - summary(); - - /* exit 0 if all tests pass */ - return !!testsfailed; + RUN_TEST(test_match); + RUN_TEST(test_relative_scores); + RUN_TEST(test_exact_scores); + RUN_TEST(test_positions_1); + RUN_TEST(test_positions_2); + RUN_TEST(test_positions_3); + RUN_TEST(test_positions_4); + RUN_TEST(test_positions_5); + RUN_TEST(test_positions_exact); + + RUN_TEST(test_choices_empty); + RUN_TEST(test_choices_1); + RUN_TEST(test_choices_2); + RUN_TEST(test_choices_without_search); + RUN_TEST(test_choices_unicode); + RUN_TEST(test_choices_large_input); + + GREATEST_MAIN_END(); } |