1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * libdatrie - Double-Array Trie Library
4 * Copyright (C) 2013 Theppitak Karoonboonyanan <theppitak@gmail.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * test_file.c - Test for datrie file operations
24 * Author: Theppitak Karoonboonyanan <theppitak@gmail.com>
27 #include <datrie/trie.h>
33 #define TRIE_FILENAME "test.tri"
36 trie_enum_mark_rec (const AlphaChar *key, TrieData key_data, void *user_data)
38 Bool *is_failed = (Bool *)user_data;
41 src_data = dict_src_get_data (key);
42 if (TRIE_DATA_ERROR == src_data) {
43 printf ("Extra entry in file: key '%ls', data %d.\n",
44 (wchar_t *)key, key_data);
46 } else if (src_data != key_data) {
47 printf ("Data mismatch for: key '%ls', expected %d, got %d.\n",
48 (wchar_t *)key, src_data, key_data);
51 dict_src_set_data (key, TRIE_DATA_READ);
64 msg_step ("Preparing trie");
65 test_trie = en_trie_new ();
67 printf ("Failed to allocate test trie.\n");
68 goto err_trie_not_created;
71 /* add/remove some words */
72 for (dict_p = dict_src; dict_p->key; dict_p++) {
73 if (!trie_store (test_trie, dict_p->key, dict_p->data)) {
74 printf ("Failed to add key '%ls', data %d.\n",
75 (wchar_t *)dict_p->key, dict_p->data);
76 goto err_trie_created;
81 msg_step ("Saving trie to file");
82 unlink (TRIE_FILENAME); /* error ignored */
83 if (trie_save (test_trie, TRIE_FILENAME) != 0) {
84 printf ("Failed to save trie to file '%s'.\n", TRIE_FILENAME);
85 goto err_trie_created;
87 trie_free (test_trie);
89 /* reload from file */
90 msg_step ("Reloading trie from the saved file");
91 test_trie = trie_new_from_file (TRIE_FILENAME);
93 printf ("Failed to reload saved trie from '%s'.\n",
98 /* enumerate & check */
99 msg_step ("Checking trie contents");
101 /* mark entries found in file */
102 if (!trie_enumerate (test_trie, trie_enum_mark_rec, (void *)&is_failed)) {
103 printf ("Failed to enumerate trie file contents.\n");
106 /* check for unmarked entries, (i.e. missed in file) */
107 for (dict_p = dict_src; dict_p->key; dict_p++) {
108 if (dict_p->data != TRIE_DATA_READ) {
109 printf ("Entry missed in file: key '%ls', data %d.\n",
110 (wchar_t *)dict_p->key, dict_p->data);
115 printf ("Errors found in trie saved contents.\n");
119 unlink (TRIE_FILENAME);
120 trie_free (test_trie);
124 unlink (TRIE_FILENAME);
126 trie_free (test_trie);
127 err_trie_not_created: