190 lines
4.7 KiB
C
190 lines
4.7 KiB
C
#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();
|
|
|
|
Entry *entry = entry_new(Redirect, "");
|
|
const char* string = "this is a test";
|
|
|
|
TEST_CHECK(trie_add(ct, string, entry) == Ok);
|
|
Entry *entry2;
|
|
TEST_CHECK(trie_search(ct, &entry2, string) == Ok);
|
|
TEST_CHECK(entry == entry2);
|
|
TEST_SIZE(ct, 1);
|
|
trie_free(ct);
|
|
}
|
|
|
|
void test_add_prefix() {
|
|
TRIE_INIT();
|
|
|
|
const char *s1 = "halloween-2022";
|
|
const char *s2 = "halloween-202";
|
|
|
|
Entry *entry1 = entry_new(Redirect, "");
|
|
Entry *entry2 = entry_new(Redirect, "");
|
|
|
|
TEST_CHECK(trie_add(ct, s1, entry1) == Ok);
|
|
TEST_CHECK(trie_add(ct, s2, entry2) == Ok);
|
|
|
|
Entry *entry3;
|
|
|
|
TEST_CHECK(trie_search(ct, &entry3, s1) == Ok);
|
|
TEST_CHECK(entry3 == entry1);
|
|
entry2 = NULL;
|
|
|
|
TEST_CHECK(trie_search(ct, &entry3, s2) == Ok);
|
|
TEST_CHECK(entry3 == entry2);
|
|
|
|
trie_free(ct);
|
|
}
|
|
|
|
void test_search_not_present() {
|
|
TRIE_INIT();
|
|
|
|
TEST_CHECK(trie_add(ct, "this string exists", NULL) == Ok);
|
|
Entry *entry;
|
|
TEST_CHECK(trie_search(ct, &entry, "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";
|
|
|
|
Entry *entry = entry_new(Redirect, "");
|
|
|
|
TEST_CHECK(trie_add(ct, one, entry) == Ok);
|
|
TEST_CHECK(trie_add(ct, two, entry) == Ok);
|
|
TEST_CHECK(trie_add(ct, twenty, entry) == Ok);
|
|
TEST_CHECK(trie_add(ct, twentytwo, entry) == Ok);
|
|
|
|
TEST_SIZE(ct, 4);
|
|
|
|
Entry *entry2;
|
|
TEST_CHECK(trie_search(ct, &entry2, one) == Ok);
|
|
TEST_CHECK(entry2 == entry);
|
|
entry2 = NULL;
|
|
|
|
TEST_CHECK(trie_search(ct, &entry2, two) == Ok);
|
|
TEST_CHECK(entry2 == entry);
|
|
entry2 = NULL;
|
|
|
|
TEST_CHECK(trie_search(ct, &entry2, twenty) == Ok);
|
|
TEST_CHECK(entry2 == entry);
|
|
entry2 = NULL;
|
|
|
|
TEST_CHECK(trie_search(ct, &entry2, twentytwo) == Ok);
|
|
TEST_CHECK(entry2 == entry);
|
|
entry2 = NULL;
|
|
|
|
TEST_CHECK(trie_add(ct, one, NULL) == AlreadyPresent);
|
|
TEST_CHECK(trie_add(ct, two, NULL) == AlreadyPresent);
|
|
TEST_CHECK(trie_add(ct, twenty, NULL) == AlreadyPresent);
|
|
TEST_CHECK(trie_add(ct, twentytwo, NULL) == 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(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]);
|
|
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}
|
|
};
|