core/filter: Rewrite unit test
Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
746c55ac56
commit
b98b1a5855
|
@ -1,91 +1,74 @@
|
|||
/*
|
||||
* Copyright 2014 (c) Anna Schumaker.
|
||||
* Test the filtering code
|
||||
*/
|
||||
|
||||
#include <core/filter.h>
|
||||
#include "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);
|
||||
}
|
||||
const gchar *test_strings[] = {
|
||||
/* 0 */ "koji kondo",
|
||||
/* 1 */ "hyrule symphony",
|
||||
/* 2 */ "kokiri forest",
|
||||
/* 3 */ "hyrule field",
|
||||
/* 4 */ "hyrule castle",
|
||||
/* 5 */ "lon lon ranch",
|
||||
/* 6 */ "kakariko village",
|
||||
/* 7 */ "death mountain",
|
||||
/* 8 */ "zoras domain",
|
||||
/* 9 */ "gerudo valley",
|
||||
/* 10 */ "ganondorf",
|
||||
/* 11 */ "princess zelda",
|
||||
/* 12 */ "ocarina medley",
|
||||
/* 13 */ "the legend of zelda medley",
|
||||
};
|
||||
|
||||
static void test_addition()
|
||||
{
|
||||
filter_init();
|
||||
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 kooloolimpah");
|
||||
do_test_add("Well excuse me, Princess!",
|
||||
"well excuse me princess");
|
||||
}
|
||||
#define NUM_STRINGS (sizeof(test_strings) / sizeof(gchar *))
|
||||
|
||||
|
||||
static void do_test_search(const std::string &text, unsigned int len,
|
||||
unsigned int *ids)
|
||||
static void test_filter()
|
||||
{
|
||||
struct set res = SET_INIT();
|
||||
unsigned int i = 0;
|
||||
unsigned int i;
|
||||
|
||||
for (unsigned int i = 1; i <= 6; i++)
|
||||
set_insert(&res, i);
|
||||
|
||||
filter :: search(text, &res);
|
||||
test_equal(set_size(&res), (size_t)len);
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
test_loop_equal(set_has(&res, ids[i]), true, i);
|
||||
filter_init();
|
||||
for (i = 0; i < NUM_STRINGS; i++) {
|
||||
filter :: add(test_strings[i], i);
|
||||
} test_loop_passed();
|
||||
}
|
||||
|
||||
static void test_search()
|
||||
{
|
||||
unsigned int res1[] = {2, 3};
|
||||
do_test_search("error", 2, res1);
|
||||
/* Search for a word! */
|
||||
filter :: search("hyrule", &res);
|
||||
test_equal(set_size(&res), 3);
|
||||
test_equal(set_has(&res, 1), (bool)true); /* hyrule symphony */
|
||||
test_equal(set_has(&res, 3), (bool)true); /* hyrule field */
|
||||
test_equal(set_has(&res, 4), (bool)true); /* hyrule castle */
|
||||
|
||||
unsigned int res2[] = {4, 5, 6, 8};
|
||||
do_test_search("the", 4, res2);
|
||||
/* A second search should clear the set. */
|
||||
filter :: search("zelda", &res);
|
||||
test_equal(set_size(&res), 2);
|
||||
test_equal(set_has(&res, 11), (bool)true); /* princess zelda */
|
||||
test_equal(set_has(&res, 13), (bool)true); /* the legend of zelda medley */
|
||||
|
||||
unsigned int res3[] = {4, 6};
|
||||
do_test_search("the ch", 2, res3);
|
||||
do_test_search("the CH", 2, res3);
|
||||
/* Partial word search. */
|
||||
filter :: search("ko", &res);
|
||||
test_equal(set_size(&res), 2);
|
||||
test_equal(set_has(&res, 0), (bool)true); /* koji kondo */
|
||||
test_equal(set_has(&res, 2), (bool)true); /* kokiri forest */
|
||||
|
||||
unsigned int res4[] = {4};
|
||||
do_test_search("the ch y", 1, res4);
|
||||
/* Multiple word search. */
|
||||
filter :: search("hyrule field", &res);
|
||||
test_equal(set_size(&res), 1);
|
||||
test_equal(set_has(&res, 3), (bool)true); /* hyrule field */
|
||||
|
||||
unsigned int res5[] = {};
|
||||
do_test_search("unknown terms", 0, res5);
|
||||
do_test_search("the chestZ", 0, res5);
|
||||
/* Search for unknown word. */
|
||||
filter :: search("field termina", &res);
|
||||
test_equal(set_size(&res), 0);
|
||||
|
||||
/* Search for empty string. */
|
||||
filter :: search("", &res);
|
||||
test_equal(set_size(&res), 0);
|
||||
|
||||
unsigned int res6[] = {0, 1, 8};
|
||||
do_test_search("d", 3, res6);
|
||||
filter_deinit();
|
||||
set_deinit(&res);
|
||||
}
|
||||
|
||||
DECLARE_UNIT_TESTS(
|
||||
UNIT_TEST("Filter Addition", test_addition),
|
||||
UNIT_TEST("Filter Search", test_search),
|
||||
UNIT_TEST("Filter", test_filter),
|
||||
);
|
||||
|
|
Loading…
Reference in New Issue