Debian Codegraph @

tests/test_store-retrieve.c

raw
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * libdatrie - Double-Array Trie Library
4 * Copyright (C) 2013 Theppitak Karoonboonyanan <[email protected]>
5 *
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.
10 *
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.
15 *
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
19 */
20
21 /*
22 * test_store-retrieve.c - Test for datrie store/retrieve operations
23 * Created: 2013-10-16
24 * Author: Theppitak Karoonboonyanan <[email protected]>
25 */
26
27 #include <datrie/trie.h>
28 #include "utils.h"
29 #include <stdio.h>
30 #include <wchar.h>
31 #include <stdlib.h>
32 #include <time.h>
33
34 int
35 main (void)
36 {
37 Trie *test_trie;
38 DictRec *dict_p;
39 TrieData trie_data;
40 Bool is_failed;
41 int n_entries, n_dels, i;
42 TrieState *trie_root_state;
43 TrieIterator *trie_it;
44
45 msg_step ("Preparing trie");
46 test_trie = en_trie_new ();
47 if (!test_trie) {
48 fprintf (stderr, "Fail to create test trie\n");
49 goto err_trie_not_created;
50 }
51
52 /* store */
53 msg_step ("Adding data to trie");
54 for (dict_p = dict_src; dict_p->key; dict_p++) {
55 if (!trie_store (test_trie, dict_p->key, dict_p->data)) {
56 printf ("Failed to add key '%ls', data %d.\n",
57 (wchar_t *)dict_p->key, dict_p->data);
58 goto err_trie_created;
59 }
60 }
61
62 /* retrieve */
63 msg_step ("Retrieving data from trie");
64 is_failed = FALSE;
65 for (dict_p = dict_src; dict_p->key; dict_p++) {
66 if (!trie_retrieve (test_trie, dict_p->key, &trie_data)) {
67 printf ("Failed to retrieve key '%ls'.\n", (wchar_t *)dict_p->key);
68 is_failed = TRUE;
69 }
70 if (trie_data != dict_p->data) {
71 printf ("Wrong data for key '%ls'; expected %d, got %d.\n",
72 (wchar_t *)dict_p->key, dict_p->data, trie_data);
73 is_failed = TRUE;
74 }
75 }
76 if (is_failed) {
77 printf ("Trie store/retrieval test failed.\n");
78 goto err_trie_created;
79 }
80
81 /* delete */
82 msg_step ("Deleting some entries from trie");
83 n_entries = dict_src_n_entries ();
84 srand (time (NULL));
85 for (n_dels = n_entries/3 + 1; n_dels > 0; n_dels--) {
86 /* pick an undeleted entry */
87 do {
88 i = rand () % n_entries;
89 } while (TRIE_DATA_READ == dict_src[i].data);
90
91 printf ("Deleting '%ls'\n", (wchar_t *)dict_src[i].key);
92 if (!trie_delete (test_trie, dict_src[i].key)) {
93 printf ("Failed to delete '%ls'\n", (wchar_t *)dict_src[i].key);
94 is_failed = TRUE;
95 }
96 dict_src[i].data = TRIE_DATA_READ;
97 }
98 if (is_failed) {
99 printf ("Trie deletion test failed.\n");
100 goto err_trie_created;
101 }
102
103 /* retrieve */
104 msg_step ("Retrieving data from trie again after deletions");
105 for (dict_p = dict_src; dict_p->key; dict_p++) {
106 /* skip deleted entries */
107 if (TRIE_DATA_READ == dict_p->data)
108 continue;
109
110 if (!trie_retrieve (test_trie, dict_p->key, &trie_data)) {
111 printf ("Failed to retrieve key '%ls'.\n", (wchar_t *)dict_p->key);
112 is_failed = TRUE;
113 }
114 if (trie_data != dict_p->data) {
115 printf ("Wrong data for key '%ls'; expected %d, got %d.\n",
116 (wchar_t *)dict_p->key, dict_p->data, trie_data);
117 is_failed = TRUE;
118 }
119 }
120 if (is_failed) {
121 printf ("Trie retrival-after-deletion test failed.\n");
122 goto err_trie_created;
123 }
124
125 /* enumerate & check */
126 msg_step ("Iterating trie contents after deletions");
127 trie_root_state = trie_root (test_trie);
128 if (!trie_root_state) {
129 printf ("Failed to get trie root state\n");
130 goto err_trie_created;
131 }
132 trie_it = trie_iterator_new (trie_root_state);
133 if (!trie_it) {
134 printf ("Failed to get trie iterator\n");
135 goto err_trie_root_created;
136 }
137
138 while (trie_iterator_next (trie_it)) {
139 AlphaChar *key;
140 TrieData key_data, src_data;
141
142 key = trie_iterator_get_key (trie_it);
143 if (!key) {
144 printf ("Failed to get key from trie iterator\n");
145 is_failed = TRUE;
146 continue;
147 }
148 key_data = trie_iterator_get_data (trie_it);
149 if (TRIE_DATA_ERROR == key_data) {
150 printf ("Failed to get data from trie iterator for key '%ls'\n",
151 (wchar_t *)key);
152 is_failed = TRUE;
153 }
154 /* mark entries found in trie */
155 src_data = dict_src_get_data (key);
156 if (TRIE_DATA_ERROR == src_data) {
157 printf ("Extra entry in trie: key '%ls', data %d.\n",
158 (wchar_t *)key, key_data);
159 is_failed = TRUE;
160 } else if (src_data != key_data) {
161 printf ("Data mismatch for: key '%ls', expected %d, got %d.\n",
162 (wchar_t *)key, src_data, key_data);
163 is_failed = TRUE;
164 } else {
165 dict_src_set_data (key, TRIE_DATA_READ);
166 }
167
168 free (key);
169 }
170
171 /* check for unmarked entries, (i.e. missed in trie) */
172 for (dict_p = dict_src; dict_p->key; dict_p++) {
173 if (dict_p->data != TRIE_DATA_READ) {
174 printf ("Entry missed in trie: key '%ls', data %d.\n",
175 (wchar_t *)dict_p->key, dict_p->data);
176 is_failed = TRUE;
177 }
178 }
179
180 if (is_failed) {
181 printf ("Errors found in trie iteration after deletions.\n");
182 goto err_trie_it_created;
183 }
184
185 trie_iterator_free (trie_it);
186 trie_state_free (trie_root_state);
187 trie_free (test_trie);
188 return 0;
189
190 err_trie_it_created:
191 trie_iterator_free (trie_it);
192 err_trie_root_created:
193 trie_state_free (trie_root_state);
194 err_trie_created:
195 trie_free (test_trie);
196 err_trie_not_created:
197 return 1;
198 }
199
200 /*
201 vi:ts=4:ai:expandtab
202 */