ocarina/tests/core/filter.cpp

97 lines
2.7 KiB
C++

/*
* Copyright 2014 (c) Anna Schumaker.
* Test the filtering code
*/
#include <core/filter.h>
#include <tests/test.h>
static void do_test_add(const std::string &text, const std::string &lc)
{
static unsigned int i = 0;
test_equal(filter :: add(text, i++), lc);
}
static void test_add()
{
do_test_add("It's dangerous to go alone! Take this...",
"its dangerous to go alone take this");
do_test_add("DODONGO DISLIKES SMOKE.",
"dodongo dislikes smoke");
do_test_add("I am Error.",
"i am error");
do_test_add("Error knows a secret.",
"error knows a secret");
do_test_add("Hey, you pay, then you can open the chests!",
"hey you pay then you can open the chests");
do_test_add("And the Master Sword sleeps again... FOREVER!",
"and the master sword sleeps again forever");
do_test_add("Link checked the chest. Wow! This is a nice chest!",
"link checked the chest wow this is a nice chest");
do_test_add("Hey! Listen! Hey! Listen! Watch out!",
"hey listen hey listen watch out");
do_test_add("You killed the Deku Tree? How could you?!",
"you killed the deku tree how could you");
do_test_add("You've met with a terrible fate, haven't you?",
"youve met with a terrible fate havent you");
do_test_add("Believe in your strengths... Believe...",
"believe in your strengths believe");
do_test_add("Tingle! Tingle! Kooloo-Limpah!",
"tingle tingle kooloo limpah");
do_test_add("Well excuse me, Princess!",
"well excuse me princess");
}
static std::set<unsigned int>::iterator IT;
static unsigned int *IDS = NULL;
static unsigned int _do_test_search_compare(unsigned int i)
{
return (*IT++ == IDS[i]) ? LOOP_PASSED : LOOP_FAILED;
}
static void do_test_search(const std::string &text, unsigned int len,
unsigned int *ids)
{
int init_values[] = { 1, 2, 3, 4, 5 };
std::set<unsigned int> res(init_values, init_values + 5);
filter :: search(text, res);
test_equal(res.size(), (size_t)len);
IDS = ids;
IT = res.begin();
test_for_each(0, len, 1, _do_test_search_compare);
}
static void test_search()
{
unsigned int res1[] = {2, 3};
do_test_search("error", 2, res1);
unsigned int res2[] = {4, 5, 6, 8};
do_test_search("the", 4, res2);
unsigned int res3[] = {4, 6};
do_test_search("the ch", 2, res3);
do_test_search("the CH", 2, res3);
unsigned int res4[] = {4};
do_test_search("the ch y", 1, res4);
unsigned int res5[] = {};
do_test_search("unknown terms", 0, res5);
do_test_search("the chestZ", 0, res5);
unsigned int res6[] = {0, 1, 8};
do_test_search("d", 3, res6);
}
int main(int argc, char **argv)
{
test :: run("Filter Add Test", test_add);
test :: run("Filter Search Test", test_search);
return 0;
}