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_iterator.c - Test for datrie iterator operations
24 * Author: Theppitak Karoonboonyanan <theppitak@gmail.com>
27 #include <datrie/trie.h>
37 TrieState *trie_root_state;
38 TrieIterator *trie_it;
41 msg_step ("Preparing trie");
42 test_trie = en_trie_new ();
44 fprintf (stderr, "Fail to create test trie\n");
45 goto err_trie_not_created;
49 msg_step ("Adding data to trie");
50 for (dict_p = dict_src; dict_p->key; dict_p++) {
51 if (!trie_store (test_trie, dict_p->key, dict_p->data)) {
52 printf ("Failed to add key '%ls', data %d.\n",
53 dict_p->key, dict_p->data);
54 goto err_trie_created;
59 msg_step ("Iterating and checking trie contents");
60 trie_root_state = trie_root (test_trie);
61 if (!trie_root_state) {
62 printf ("Failed to get trie root state\n");
63 goto err_trie_created;
65 trie_it = trie_iterator_new (trie_root_state);
67 printf ("Failed to get trie iterator\n");
68 goto err_trie_root_created;
72 while (trie_iterator_next (trie_it)) {
74 TrieData key_data, src_data;
76 key = trie_iterator_get_key (trie_it);
78 printf ("Failed to get key from trie iterator\n");
82 key_data = trie_iterator_get_data (trie_it);
83 if (TRIE_DATA_ERROR == key_data) {
84 printf ("Failed to get data from trie iterator for key '%ls'\n",
88 /* mark entries found in trie */
89 src_data = dict_src_get_data (key);
90 if (TRIE_DATA_ERROR == src_data) {
91 printf ("Extra entry in trie: key '%ls', data %d.\n",
94 } else if (src_data != key_data) {
95 printf ("Data mismatch for: key '%ls', expected %d, got %d.\n",
96 key, src_data, key_data);
99 dict_src_set_data (key, TRIE_DATA_READ);
105 /* check for unmarked entries, (i.e. missed in trie) */
106 for (dict_p = dict_src; dict_p->key; dict_p++) {
107 if (dict_p->data != TRIE_DATA_READ) {
108 printf ("Entry missed in trie: key '%ls', data %d.\n",
109 dict_p->key, dict_p->data);
115 printf ("Errors found in trie iteration.\n");
116 goto err_trie_it_created;
119 trie_iterator_free (trie_it);
120 trie_state_free (trie_root_state);
121 trie_free (test_trie);
125 trie_iterator_free (trie_it);
126 err_trie_root_created:
127 trie_state_free (trie_root_state);
129 trie_free (test_trie);
130 err_trie_not_created: