/** Testuje zapisywanie słownika. @param state Środowisko testowe. */ static void dictionary_save_test(void** state) { struct dictionary *dict = dictionary_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); } dictionary_insert(dict, L"ciupaga"); assert_true(dictionary_save(dict, stream) == 0); fflush(stream); assert_true(wcscmp(L"ciupaga*^^^^^^^\n0\n", buf) == 0); fseek(stream, 0, SEEK_SET); fclose(stream); # undef free free(buf); # define free(ptr) _test_free(ptr, __FILE__, __LINE__) dictionary_done(dict); }
int dictionary_save(const struct dictionary *dict, FILE* stream) { int valid = 0; wchar_t key[2]; key[0] = dict->key; key[1] = L'\0'; if (fprintf(stream, "%ls%d", key, dict->children_size) < 0) return -1; for (int i = 0; i < dict->children_size; i++) valid += dictionary_save(*(dict->children + i), stream); return valid; }