/** Testuje wczytywanie drzewa. @param state Środowisko testowe. */ static void trie_load_test(void** state) { Trie *trie = NULL; IO *io = io_new(stdin, stdout, stderr); push_word_to_io_mock(L""); trie = trie_load(io); assert_non_null(trie); trie_done(trie); // Poprawny zapis push_word_to_io_mock(L"ciupagą*^^^^^^^\n"); trie = trie_load(io); pop_remaining_chars(io); assert_true(trie_has_word(trie, L"ciupagą")); assert_false(trie_has_word(trie, L"ciupaga")); assert_false(trie_has_word(trie, L"ciupag")); assert_false(trie_has_word(trie, L"ciupagąą")); trie_done(trie); // Próba dojścia wyżej niż korzeń push_word_to_io_mock(L"a*^^\n"); trie = trie_load(io); pop_remaining_chars(io); assert_null(trie); // Znaki spoza alfabetu push_word_to_io_mock(L"&*^\n"); trie = trie_load(io); pop_remaining_chars(); assert_null(trie); io_done(io); }
/** Niszczy środowisko testwowe. @param state Środowisko testowe. @return 0 jeśli się udało, -1 w p.p */ static int trie_teardown(void **state) { Trie *trie = *state; trie_done(trie); return 0; }
/** Testuje wstawianie do drzewa. @param state Środowisko testowe. */ static void trie_insert_word_test(void** state) { Trie *trie = trie_new(); size_t n_words = 4; wchar_t *words[] = {L"wątły", L"wątły", L"wątlejszy", L"łódka"}; for (size_t i = 0; i < n_words; i++) { if (i == 1) assert_false(trie_insert_word(trie, words[i])); else assert_true(trie_insert_word(trie, words[i])); } for (size_t i = 0; i < n_words; i++) assert_true(trie_has_word(trie, words[i])); trie_done(trie); }
/** Testuje zapisywanie drzewa. @param state Środowisko testowe. */ static void trie_save_test(void** state) { Trie *trie = trie_new(); FILE *stream; wchar_t *buf = NULL; size_t len; stream = open_wmemstream(&buf, &len); if (stream == NULL) { fprintf(stderr, "Failed to open memory stream\n"); exit(EXIT_FAILURE); } IO *io = io_new(stdin, stream, stderr); assert_true(trie_save(trie, io) == 0); fflush(stream); assert_true(wcscmp(L"\n", buf) == 0); fseek(stream, 0, SEEK_SET); trie_insert_word(trie, L"ciupaga"); assert_true(trie_save(trie, io) == 0); fflush(stream); assert_true(wcscmp(L"ciupaga*^^^^^^^\n", buf) == 0); fseek(stream, 0, SEEK_SET); trie_delete_word(trie, L"ciupaga"); assert_true(trie_save(trie, io) == 0); fflush(stream); assert_true(wcscmp(L"\n", buf) == 0); fclose(stream); io_done(io); # undef free free(buf); # define free(ptr) _test_free(ptr, __FILE__, __LINE__) trie_done(trie); }
/** Testuje inicjalizację drzewa. @param state Środowisko testowe. */ static void trie_init_test(void** state) { Trie *trie = trie_new(); trie_done(trie); }
void dictionary_done(struct dictionary *dict) { trie_done(dict->tree); free(dict); }