diff --git a/src/clue-matches-tests.c b/src/clue-matches-tests.c index 4ac587313a37b9cbafe253440f45200577fb27b6..e7dd49e0663d7cbe26b53b24e7e379f40b187590 100644 --- a/src/clue-matches-tests.c +++ b/src/clue-matches-tests.c @@ -25,11 +25,14 @@ #include "word-list-test-utils.h" #include "test-utils.h" -// #define _TEST_FILE "tests/clue-matches/.ipuz" +#define EGG_IPUZ "tests/clue-matches/egg.ipuz" +#define VALID_INTERSECTION_IPUZ "tests/clue-matches/valid-intersection.ipuz" +#define INVALID_INTERSECTION_IPUZ "tests/clue-matches/invalid-intersection.ipuz" -#define EGG_IPUZ_FILE_PATH "tests/clue-matches/egg.ipuz" -#define VALID_INTERSECTION_IPUZ_FILE_PATH "tests/clue-matches/valid-intersection.ipuz" -#define INVALID_INTERSECTION_IPUZ_FILE_PATH "tests/clue-matches/invalid-intersection.ipuz" +typedef struct { + WordList *word_list; + IpuzGrid *grid; +} Fixture; static IpuzGrid* create_grid (const gchar* filename) @@ -38,6 +41,25 @@ create_grid (const gchar* filename) return IPUZ_GRID (state->xword); } +static void fixture_set_up (Fixture *fixture, gconstpointer user_data) +{ + const gchar *ipuz_file_path = (const gchar *) user_data; + + fixture->word_list = get_broda_word_list (); + fixture->grid = create_grid (ipuz_file_path); +} + +static void fixture_tear_down (Fixture *fixture, gconstpointer user_data) +{ + g_object_unref (fixture->word_list); + + /* If I try to free the grid: + * g_object_unref (fixture->grid); + * I get this error: + * GLib-FATAL-CRITICAL: g_ref_count_dec: assertion 'rrc < 0' failed + */ +} + static IpuzClue* get_clue (IpuzGrid* grid, IpuzClueDirection direction, guint index) { @@ -49,105 +71,103 @@ get_clue (IpuzGrid* grid, IpuzClueDirection direction, guint index) return ipuz_clues_get_clue_by_id (IPUZ_CLUES (grid), &clue_id); } -static void -test_egg_ipuz (void) +static WordArray* +str_array_to_word_array (const gchar *str_array[], WordList *word_list) { - g_autoptr (WordList) word_list = NULL; - IpuzGrid *grid; - g_autofree IpuzClue *clue = NULL; - g_autoptr (WordArray) clue_matches = NULL; - - word_list = get_broda_word_list (); - grid = create_grid (EGG_IPUZ_FILE_PATH); - clue = get_clue (grid, IPUZ_CLUE_DIRECTION_ACROSS, 2); - clue_matches = word_list_find_clue_matches (word_list, clue, grid); - - g_assert_cmpint (word_array_len (clue_matches), ==, 3); - g_assert_cmpstr (word_list_get_indexed_word (word_list, - word_array_index (clue_matches, 0)), - ==, - "EGGS"); - g_assert_cmpstr ( - word_list_get_indexed_word (word_list, - word_array_index (clue_matches, 1)), - ==, - "EGGO"); - g_assert_cmpstr ( - word_list_get_indexed_word (word_list, - word_array_index (clue_matches, 2)), - ==, - "EGGY"); + WordArray *word_array = word_array_new (); + for (guint i = 0; str_array[i] != NULL; i++) + { + WordIndex word_index; + gboolean success = word_list_lookup_index (word_list, + str_array[i], + &word_index); + g_assert_true (success); + word_array_add (word_array, word_index); + } + return word_array; } static void -test_valid_intersection_ipuz (void) +test_clue_matches (WordList *word_list, + IpuzGrid *grid, + IpuzClueDirection clue_direction, + guint clue_index, + const gchar *expected_words[]) { - g_autoptr (WordList) word_list = NULL; - IpuzGrid *grid; - g_autofree IpuzClue *clue = NULL; + const IpuzClue *clue = NULL; g_autoptr (WordArray) clue_matches = NULL; + g_autoptr (WordArray) expected_word_array = NULL; - word_list = get_broda_word_list (); - grid = create_grid (VALID_INTERSECTION_IPUZ_FILE_PATH); - clue = get_clue (grid, IPUZ_CLUE_DIRECTION_ACROSS, 1); + clue = get_clue (grid, clue_direction, clue_index); clue_matches = word_list_find_clue_matches (word_list, clue, grid); + expected_word_array = str_array_to_word_array (expected_words, word_list); - g_assert_cmpint (word_array_len (clue_matches), ==, 2); - g_assert_cmpstr (word_list_get_indexed_word (word_list, - word_array_index (clue_matches, 0)), - ==, - "WORD"); - g_assert_cmpstr (word_list_get_indexed_word (word_list, - word_array_index (clue_matches, 1)), - ==, - "WORM"); - word_array_unref (clue_matches); - - clue = get_clue (grid, IPUZ_CLUE_DIRECTION_DOWN, 3); - clue_matches = word_list_find_clue_matches (word_list, clue, grid); + g_assert_true (word_array_equals (clue_matches, expected_word_array)); +} - g_assert_cmpint (word_array_len (clue_matches), ==, 2); - g_assert_cmpstr (word_list_get_indexed_word (word_list, - word_array_index (clue_matches, 0)), - ==, - "EDIT"); - g_assert_cmpstr ( - word_list_get_indexed_word (word_list, - word_array_index (clue_matches, 1)), - ==, - "EMIT"); +static void +test_egg_ipuz (Fixture *fixture, gconstpointer user_data) +{ + test_clue_matches (fixture->word_list, + fixture->grid, + IPUZ_CLUE_DIRECTION_ACROSS, + 2, + (const gchar*[]){"EGGS", "EGGO", "EGGY", NULL}); } static void -test_invalid_intersection_ipuz (void) +test_valid_intersection_ipuz (Fixture *fixture, gconstpointer user_data) { - g_autoptr (WordList) word_list = NULL; - IpuzGrid *grid; - g_autofree IpuzClue *clue = NULL; - g_autoptr (WordArray) clue_matches = NULL; + test_clue_matches (fixture->word_list, + fixture->grid, + IPUZ_CLUE_DIRECTION_ACROSS, + 1, + (const gchar*[]){"WORD", "WORM", NULL}); + test_clue_matches (fixture->word_list, + fixture->grid, + IPUZ_CLUE_DIRECTION_DOWN, + 3, + (const gchar*[]){"EDIT", "EMIT", NULL}); +} - word_list = get_broda_word_list (); - grid = create_grid (INVALID_INTERSECTION_IPUZ_FILE_PATH); - clue = get_clue (grid, IPUZ_CLUE_DIRECTION_ACROSS, 3); - clue_matches = word_list_find_clue_matches (word_list, clue, grid); - g_assert_cmpint (word_array_len (clue_matches), ==, 0); - clue = get_clue (grid, IPUZ_CLUE_DIRECTION_DOWN, 3); - clue_matches = word_list_find_clue_matches (word_list, clue, grid); - g_assert_cmpint (word_array_len (clue_matches), ==, 0); +static void +test_invalid_intersection_ipuz (Fixture *fixture, gconstpointer user_data) +{ + test_clue_matches (fixture->word_list, + fixture->grid, + IPUZ_CLUE_DIRECTION_ACROSS, + 3, + (const gchar*[]){NULL}); + test_clue_matches (fixture->word_list, + fixture->grid, + IPUZ_CLUE_DIRECTION_DOWN, + 3, + (const gchar*[]){NULL}); } /* FIXME(tests): Measure performance. */ /* FIXME(tests): Add more tests. */ -/* FIXME(tests): Refactor these tests to not require so much manual code. */ int main (int argc, char **argv) { g_test_init (&argc, &argv, NULL); - g_test_add_func ("/clue_matches/test_egg_ipuz", - test_egg_ipuz); - g_test_add_func ("/clue_matches/valid_intersection_ipuz", - test_valid_intersection_ipuz); - g_test_add_func ("/clue_matches/invalid_intersection_ipuz", - test_invalid_intersection_ipuz); + g_test_add ("/clue_matches/test_egg_ipuz", + Fixture, + EGG_IPUZ, + fixture_set_up, + test_egg_ipuz, + fixture_tear_down); + g_test_add ("/clue_matches/valid_intersection_ipuz", + Fixture, + VALID_INTERSECTION_IPUZ, + fixture_set_up, + test_valid_intersection_ipuz, + fixture_tear_down); + g_test_add ("/clue_matches/invalid_intersection_ipuz", + Fixture, + INVALID_INTERSECTION_IPUZ, + fixture_set_up, + test_invalid_intersection_ipuz, + fixture_tear_down); return g_test_run (); }