#include "test.h" #include "trie.h" #include "fuzzy.h" #define TEST_SIZE(ct, size) \ TEST_CHECK(trie_size(ct) == size); \ TEST_MSG("Size: %zu", trie_size(ct)) \ void test_init() { Trie* ct = trie_init(); TEST_CHECK(ct != NULL); TEST_SIZE(ct, 0); trie_free(ct); } void test_add_one() { Trie* ct = trie_init(); TEST_CHECK(ct != NULL); Entry *entry = entry_new(Redirect, ""); const char* string = "this is a test"; TEST_CHECK(trie_add(ct, string, entry)); TEST_CHECK(trie_search(ct, string) == entry); TEST_SIZE(ct, 1); trie_free(ct); } void test_search_not_present() { Trie* ct = trie_init(); TEST_CHECK(ct != NULL); TEST_CHECK(trie_add(ct, "this string exists", NULL)); TEST_CHECK(trie_search(ct, "this string does not exist") == NULL); trie_free(ct); } void test_add_more() { Trie* ct = trie_init(); TEST_CHECK(ct != NULL); const char* one = "one"; const char* two = "two"; const char* twenty = "twenty"; const char* twentytwo = "twentytwo"; Entry *entry = entry_new(Redirect, ""); TEST_CHECK(trie_add(ct, one, entry)); TEST_CHECK(trie_add(ct, two, entry)); TEST_CHECK(trie_add(ct, twenty, entry)); TEST_CHECK(trie_add(ct, twentytwo, entry)); TEST_SIZE(ct, 4); TEST_CHECK(trie_search(ct, one) == entry); TEST_CHECK(trie_search(ct, two) == entry); TEST_CHECK(trie_search(ct, twenty) == entry); TEST_CHECK(trie_search(ct, twentytwo) == entry); TEST_CHECK(!trie_add(ct, one, NULL)); TEST_CHECK(!trie_add(ct, two, NULL)); TEST_CHECK(!trie_add(ct, twenty, NULL)); TEST_CHECK(!trie_add(ct, twentytwo, NULL)); trie_free(ct); } /* void test_remove_one() { */ /* Trie* ct = trie_init(); */ /* TEST_CHECK(ct != NULL); */ /* const char* string = "this is a test"; */ /* TEST_CHECK(trie_add(ct, string, NULL)); */ /* TEST_SIZE(ct, 1); */ /* TEST_CHECK(trie_remove(ct, string)); */ /* TEST_SIZE(ct, 0); */ /* trie_free(ct); */ /* } */ /* void test_remove_more() { */ /* Trie* ct = trie_init(); */ /* TEST_CHECK(ct != NULL); */ /* const char* one = "one"; */ /* const char* two = "two"; */ /* const char* twenty = "twenty"; */ /* const char* twentytwo = "twentytwo"; */ /* TEST_CHECK(trie_add(ct, one, NULL)); */ /* TEST_CHECK(trie_add(ct, two, NULL)); */ /* TEST_CHECK(trie_add(ct, twenty, NULL)); */ /* TEST_CHECK(trie_add(ct, twentytwo, NULL)); */ /* TEST_SIZE(ct, 4); */ /* TEST_CHECK(trie_remove(ct, one)); */ /* TEST_CHECK(trie_remove(ct, two)); */ /* TEST_CHECK(trie_remove(ct, twenty)); */ /* TEST_CHECK(trie_remove(ct, twentytwo)); */ /* TEST_SIZE(ct, 0); */ /* trie_free(ct); */ /* } */ /* void test_remove_not_present() { */ /* Trie* ct = trie_init(); */ /* TEST_CHECK(ct != NULL); */ /* TEST_CHECK(trie_add(ct, "this string exists", NULL)); */ /* TEST_CHECK(!trie_remove(ct, "this string does not exist")); */ /* trie_free(ct); */ /* } */ // Test seeds that are known to fail so we don't get regressions void test_fuzzy_set() { FuzzyConfig configs[] = { { 403318210, 5, 500}, { 588218406, 16, 460}, { 297512224, 21, 500}, { 403318210, 5, 500} }; int count = sizeof(configs) / sizeof(FuzzyConfig); int res; for (int i = 0; i < count; i++) { res = fuzzy_test_trie_seed(configs[i], trie_init, trie_free, trie_add_no_lock, NULL, trie_size); TEST_CHECK_(res == 0, "Failed config, seed = %i, len = %i, count = %i, code=%i", configs[i].seed, configs[i].word_length, configs[i].word_count, res); } } TEST_LIST = { {"trie init",test_init }, { "trie add one",test_add_one }, { "trie add more",test_add_more }, { "trie search not present",test_search_not_present}, /* { "trie remove one",test_remove_one }, */ /* { "trie remove more",test_remove_more }, */ /* { "trie remove not present",test_remove_not_present}, */ { "trie fuzzy set", test_fuzzy_set }, { NULL, NULL} };