/* * Copyright 2014 (c) Anna Schumaker. */ #include #include 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", }; #define NUM_STRINGS (sizeof(test_strings) / sizeof(gchar *)) static void test_filter() { GHashTable *res; unsigned int i; filter_init(); for (i = 0; i < NUM_STRINGS; i++) { filter_add(test_strings[i], GUINT_TO_POINTER(i)); } test_loop_passed(); /* Search for a word! */ res = filter_search("hyrule"); 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 */ g_hash_table_destroy(res); /* A second search should clear the set. */ res = filter_search("zelda"); 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 */ g_hash_table_destroy(res); /* Partial word search. */ res = filter_search("ko"); 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 */ g_hash_table_destroy(res); /* Multiple word search. */ res = filter_search("hyrule field"); test_equal(g_hash_table_size(res), 1); test_equal(g_hash_table_contains(res, GUINT_TO_POINTER(3)), true); /* hyrule field */ g_hash_table_destroy(res); /* Search for unknown word. */ res = filter_search("field termina"); test_equal((void *)res, NULL); /* Search for empty string. */ res = filter_search(""); test_equal((void *)res, NULL); /* Remove a string and search again. */ filter_remove("hyrule symphony", GUINT_TO_POINTER(1)); res = filter_search("hyrule"); 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 */ g_hash_table_destroy(res); filter_deinit(); } DECLARE_UNIT_TESTS( UNIT_TEST("Filter", test_filter), );