/* * 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 = g_hash_table_new(g_direct_hash, g_direct_equal); 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! */ filter_search("hyrule", res); 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 */ /* A second search should clear the set. */ filter_search("zelda", res); 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 */ /* Partial word search. */ filter_search("ko", res); 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 */ /* Multiple word search. */ filter_search("hyrule field", res); test_equal(g_hash_table_size(res), 1); test_equal(g_hash_table_contains(res, GUINT_TO_POINTER(3)), true); /* hyrule field */ /* Search for unknown word. */ filter_search("field termina", res); test_equal(g_hash_table_size(res), 0); /* Search for empty string. */ filter_search("", res); test_equal(g_hash_table_size(res), 0); /* Remove a string and search again. */ filter_remove("hyrule symphony", GUINT_TO_POINTER(1)); filter_search("hyrule", res); 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 */ filter_deinit(); g_hash_table_destroy(res); } DECLARE_UNIT_TESTS( UNIT_TEST("Filter", test_filter), );