2014-03-15 20:44:07 -04:00
|
|
|
/*
|
|
|
|
* Copyright 2014 (c) Anna Schumaker.
|
|
|
|
*/
|
2014-06-05 10:19:22 -04:00
|
|
|
#include <core/filter.h>
|
2015-11-10 10:19:45 -05:00
|
|
|
#include <tests/test.h>
|
2014-03-15 20:44:07 -04:00
|
|
|
|
2015-11-10 09:05:53 -05:00
|
|
|
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",
|
|
|
|
};
|
2014-03-15 20:44:07 -04:00
|
|
|
|
2015-11-10 09:05:53 -05:00
|
|
|
#define NUM_STRINGS (sizeof(test_strings) / sizeof(gchar *))
|
2015-01-04 17:56:54 -05:00
|
|
|
|
2015-11-10 09:05:53 -05:00
|
|
|
static void test_filter()
|
2014-03-15 20:44:07 -04:00
|
|
|
{
|
2016-04-29 11:10:48 -04:00
|
|
|
GHashTable *res;
|
2015-11-10 09:05:53 -05:00
|
|
|
unsigned int i;
|
2014-03-15 20:44:07 -04:00
|
|
|
|
2015-11-10 09:05:53 -05:00
|
|
|
filter_init();
|
|
|
|
for (i = 0; i < NUM_STRINGS; i++) {
|
2016-04-29 09:03:20 -04:00
|
|
|
filter_add(test_strings[i], GUINT_TO_POINTER(i));
|
2015-10-21 15:57:21 -04:00
|
|
|
} test_loop_passed();
|
2014-03-15 20:44:07 -04:00
|
|
|
|
2015-11-10 09:05:53 -05:00
|
|
|
/* Search for a word! */
|
2016-04-29 11:10:48 -04:00
|
|
|
res = filter_search("hyrule");
|
2016-04-29 10:35:32 -04:00
|
|
|
test_equal(g_hash_table_size(res), 3);
|
|
|
|
test_equal(g_hash_table_contains(res, GUINT_TO_POINTER(1)), true); /* hyrule symphony */
|
|
|
|
test_equal(g_hash_table_contains(res, GUINT_TO_POINTER(3)), true); /* hyrule field */
|
|
|
|
test_equal(g_hash_table_contains(res, GUINT_TO_POINTER(4)), true); /* hyrule castle */
|
2016-04-29 11:10:48 -04:00
|
|
|
g_hash_table_destroy(res);
|
2015-11-10 09:05:53 -05:00
|
|
|
|
|
|
|
/* A second search should clear the set. */
|
2016-04-29 11:10:48 -04:00
|
|
|
res = filter_search("zelda");
|
2016-04-29 10:35:32 -04:00
|
|
|
test_equal(g_hash_table_size(res), 2);
|
|
|
|
test_equal(g_hash_table_contains(res, GUINT_TO_POINTER(11)), true); /* princess zelda */
|
|
|
|
test_equal(g_hash_table_contains(res, GUINT_TO_POINTER(13)), true); /* the legend of zelda medley */
|
2016-04-29 11:10:48 -04:00
|
|
|
g_hash_table_destroy(res);
|
2014-03-15 20:44:07 -04:00
|
|
|
|
2015-11-10 09:05:53 -05:00
|
|
|
/* Partial word search. */
|
2016-04-29 11:10:48 -04:00
|
|
|
res = filter_search("ko");
|
2016-04-29 10:35:32 -04:00
|
|
|
test_equal(g_hash_table_size(res), 2);
|
|
|
|
test_equal(g_hash_table_contains(res, GUINT_TO_POINTER(0)), true); /* koji kondo */
|
|
|
|
test_equal(g_hash_table_contains(res, GUINT_TO_POINTER(2)), true); /* kokiri forest */
|
2016-04-29 11:10:48 -04:00
|
|
|
g_hash_table_destroy(res);
|
2014-03-15 20:44:07 -04:00
|
|
|
|
2015-11-10 09:05:53 -05:00
|
|
|
/* Multiple word search. */
|
2016-04-29 11:10:48 -04:00
|
|
|
res = filter_search("hyrule field");
|
2016-04-29 10:35:32 -04:00
|
|
|
test_equal(g_hash_table_size(res), 1);
|
|
|
|
test_equal(g_hash_table_contains(res, GUINT_TO_POINTER(3)), true); /* hyrule field */
|
2016-04-29 11:10:48 -04:00
|
|
|
g_hash_table_destroy(res);
|
2014-03-15 20:44:07 -04:00
|
|
|
|
2015-11-10 09:05:53 -05:00
|
|
|
/* Search for unknown word. */
|
2016-04-29 11:10:48 -04:00
|
|
|
res = filter_search("field termina");
|
|
|
|
test_equal((void *)res, NULL);
|
2014-03-15 20:44:07 -04:00
|
|
|
|
2015-11-10 09:05:53 -05:00
|
|
|
/* Search for empty string. */
|
2016-04-29 11:10:48 -04:00
|
|
|
res = filter_search("");
|
|
|
|
test_equal((void *)res, NULL);
|
2015-01-30 10:52:15 -05:00
|
|
|
|
2016-04-29 08:51:58 -04:00
|
|
|
/* Remove a string and search again. */
|
2016-04-29 09:03:20 -04:00
|
|
|
filter_remove("hyrule symphony", GUINT_TO_POINTER(1));
|
2016-04-29 11:10:48 -04:00
|
|
|
res = filter_search("hyrule");
|
2016-04-29 10:35:32 -04:00
|
|
|
test_equal(g_hash_table_size(res), 2);
|
|
|
|
test_equal(g_hash_table_contains(res, GUINT_TO_POINTER(3)), true); /* hyrule field */
|
|
|
|
test_equal(g_hash_table_contains(res, GUINT_TO_POINTER(4)), true); /* hyrule castle */
|
2016-04-29 11:10:48 -04:00
|
|
|
g_hash_table_destroy(res);
|
2016-04-29 08:51:58 -04:00
|
|
|
|
2015-09-29 16:25:16 -04:00
|
|
|
filter_deinit();
|
2014-05-03 19:31:29 -04:00
|
|
|
}
|
|
|
|
|
2015-08-27 08:11:59 -04:00
|
|
|
DECLARE_UNIT_TESTS(
|
2015-11-10 09:05:53 -05:00
|
|
|
UNIT_TEST("Filter", test_filter),
|
2015-08-27 08:11:59 -04:00
|
|
|
);
|