#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)) #define TRIE_INIT() \ Trie *ct; \ trie_init(&ct, NULL); \ TEST_CHECK(ct != NULL) void test_init() { TRIE_INIT(); TEST_SIZE(ct, 0); trie_free(ct); } void test_add_one() { TRIE_INIT(); const char* string = "this is a test"; TEST_CHECK(trie_add_copy(ct, string, "", 1) == Ok); void *data; TEST_CHECK(trie_search(ct, &data, string) == Ok); TEST_CHECK(memcmp(data, "", 1) == 0); TEST_SIZE(ct, 1); trie_free(ct); } void test_add_prefix() { TRIE_INIT(); const char *s1 = "halloween-2022"; const char *s2 = "halloween-202"; TEST_CHECK(trie_add_copy(ct, s1, "a", 2) == Ok); TEST_CHECK(trie_add_copy(ct, s2, "b", 2) == Ok); void *data; TEST_CHECK(trie_search(ct, &data, s1) == Ok); TEST_CHECK(memcmp(data, "a", 2) == 0); data = NULL; TEST_CHECK(trie_search(ct, &data, s2) == Ok); TEST_CHECK(memcmp(data, "b", 2) == 0); trie_free(ct); } void test_search_not_present() { TRIE_INIT(); TEST_CHECK(trie_add_copy(ct, "this string exists", "", 1) == Ok); void *data; TEST_CHECK(trie_search(ct, &data, "this string does not exist") == NotFound); trie_free(ct); } void test_add_more() { TRIE_INIT(); const char* one = "one"; const char* two = "two"; const char* twenty = "twenty"; const char* twentytwo = "twentytwo"; TEST_CHECK(trie_add_copy(ct, one, "a", 2) == Ok); TEST_CHECK(trie_add_copy(ct, two, "b", 2) == Ok); TEST_CHECK(trie_add_copy(ct, twenty, "c", 2) == Ok); TEST_CHECK(trie_add_copy(ct, twentytwo, "d", 2) == Ok); TEST_SIZE(ct, 4); void *data; TEST_CHECK(trie_search(ct, &data, one) == Ok); TEST_CHECK(memcmp(data, "a", 2) == 0); data = NULL; TEST_CHECK(trie_search(ct, &data, two) == Ok); TEST_CHECK(memcmp(data, "b", 2) == 0); data = NULL; TEST_CHECK(trie_search(ct, &data, twenty) == Ok); TEST_CHECK(memcmp(data, "c", 2) == 0); data = NULL; TEST_CHECK(trie_search(ct, &data, twentytwo) == Ok); TEST_CHECK(memcmp(data, "d", 2) == 0); TEST_CHECK(trie_add_copy(ct, one, "", 2) == AlreadyPresent); TEST_CHECK(trie_add_copy(ct, two, "", 2) == AlreadyPresent); TEST_CHECK(trie_add_copy(ct, twenty, "", 2) == AlreadyPresent); TEST_CHECK(trie_add_copy(ct, twentytwo, "", 2) == AlreadyPresent); 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_copy(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_copy(ct, one, NULL)); */ /* TEST_CHECK(trie_add_copy(ct, two, NULL)); */ /* TEST_CHECK(trie_add_copy(ct, twenty, NULL)); */ /* TEST_CHECK(trie_add_copy(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_copy(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]); 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} };