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>
32 #define TRIE_FILENAME "test.tri"
35 trie_enum_mark_rec (const AlphaChar *key, TrieData key_data, void *user_data)
37 Bool *is_failed = (Bool *)user_data;
40 src_data = dict_src_get_data (key);
41 if (TRIE_DATA_ERROR == src_data) {
42 printf ("Extra entry in file: key '%ls', data %d.\n", key, key_data);
44 } else if (src_data != key_data) {
45 printf ("Data mismatch for: key '%ls', expected %d, got %d.\n",
46 key, src_data, key_data);
49 dict_src_set_data (key, TRIE_DATA_READ);
62 msg_step ("Preparing trie");
63 test_trie = en_trie_new ();
65 printf ("Failed to allocate test trie.\n");
66 goto err_trie_not_created;
69 /* add/remove some words */
70 for (dict_p = dict_src; dict_p->key; dict_p++) {
71 if (!trie_store (test_trie, dict_p->key, dict_p->data)) {
72 printf ("Failed to add key '%ls', data %d.\n",
73 dict_p->key, dict_p->data);
74 goto err_trie_created;
79 msg_step ("Saving trie to file");
80 unlink (TRIE_FILENAME); /* error ignored */
81 if (trie_save (test_trie, TRIE_FILENAME) != 0) {
82 printf ("Failed to save trie to file '%s'.\n", TRIE_FILENAME);
83 goto err_trie_created;
85 trie_free (test_trie);
87 /* reload from file */
88 msg_step ("Reloading trie from the saved file");
89 test_trie = trie_new_from_file (TRIE_FILENAME);
91 printf ("Failed to reload saved trie from '%s'.\n",
96 /* enumerate & check */
97 msg_step ("Checking trie contents");
99 /* mark entries found in file */
100 if (!trie_enumerate (test_trie, trie_enum_mark_rec, (void *)&is_failed)) {
101 printf ("Failed to enumerate trie file contents.\n");
104 /* check for unmarked entries, (i.e. missed in file) */
105 for (dict_p = dict_src; dict_p->key; dict_p++) {
106 if (dict_p->data != TRIE_DATA_READ) {
107 printf ("Entry missed in file: key '%ls', data %d.\n",
108 dict_p->key, dict_p->data);
113 printf ("Errors found in trie saved contents.\n");
117 unlink (TRIE_FILENAME);
118 trie_free (test_trie);
122 unlink (TRIE_FILENAME);
124 trie_free (test_trie);
125 err_trie_not_created: