Imported Upstream version 0.2.7
[platform/upstream/libdatrie.git] / tests / test_iterator.c
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 <thep@linux.thai.net>
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_iterator.c - Test for datrie iterator operations
23  * Created: 2013-10-16
24  * Author:  Theppitak Karoonboonyanan <thep@linux.thai.net>
25  */
26
27 #include <datrie/trie.h>
28 #include "utils.h"
29 #include <stdio.h>
30
31 int
32 main ()
33 {
34     Trie         *test_trie;
35     DictRec      *dict_p;
36     TrieState    *trie_root_state;
37     TrieIterator *trie_it;
38     Bool          is_failed;
39
40     msg_step ("Preparing trie");
41     test_trie = en_trie_new ();
42     if (!test_trie) {
43         fprintf (stderr, "Fail to create test trie\n");
44         goto err_trie_not_created;
45     }
46
47     /* store */
48     msg_step ("Adding data to trie");
49     for (dict_p = dict_src; dict_p->key; dict_p++) {
50         if (!trie_store (test_trie, dict_p->key, dict_p->data)) {
51             printf ("Failed to add key '%ls', data %d.\n",
52                     dict_p->key, dict_p->data);
53             goto err_trie_created;
54         }
55     }
56
57     /* iterate & check */
58     msg_step ("Iterating and checking trie contents");
59     trie_root_state = trie_root (test_trie);
60     if (!trie_root_state) {
61         printf ("Failed to get trie root state\n");
62         goto err_trie_created;
63     }
64     trie_it = trie_iterator_new (trie_root_state);
65     if (!trie_it) {
66         printf ("Failed to get trie iterator\n");
67         goto err_trie_root_created;
68     }
69
70     is_failed = FALSE;
71     while (trie_iterator_next (trie_it)) {
72         AlphaChar *key;
73         TrieData   key_data, src_data;
74
75         key = trie_iterator_get_key (trie_it);
76         if (!key) {
77             printf ("Failed to get key from trie iterator\n");
78             is_failed = TRUE;
79             continue;
80         }
81         key_data = trie_iterator_get_data (trie_it);
82         if (TRIE_DATA_ERROR == key_data) {
83             printf ("Failed to get data from trie iterator for key '%ls'\n",
84                     key);
85             is_failed = TRUE;
86         }
87         /* mark entries found in trie */
88         src_data = dict_src_get_data (key);
89         if (TRIE_DATA_ERROR == src_data) {
90             printf ("Extra entry in trie: key '%ls', data %d.\n",
91                     key, key_data);
92             is_failed = TRUE;
93         } else if (src_data != key_data) {
94             printf ("Data mismatch for: key '%ls', expected %d, got %d.\n",
95                     key, src_data, key_data);
96             is_failed = TRUE;
97         } else {
98             dict_src_set_data (key, TRIE_DATA_READ);
99         }
100
101         free (key);
102     }
103
104     /* check for unmarked entries, (i.e. missed in trie) */
105     for (dict_p = dict_src; dict_p->key; dict_p++) {
106         if (dict_p->data != TRIE_DATA_READ) {
107             printf ("Entry missed in trie: key '%ls', data %d.\n",
108                     dict_p->key, dict_p->data);
109             is_failed = TRUE;
110         }
111     }
112
113     if (is_failed) {
114         printf ("Errors found in trie iteration.\n");
115         goto err_trie_it_created;
116     }
117
118     trie_iterator_free (trie_it);
119     trie_state_free (trie_root_state);
120     trie_free (test_trie);
121     return 0;
122
123 err_trie_it_created:
124     trie_iterator_free (trie_it);
125 err_trie_root_created:
126     trie_state_free (trie_root_state);
127 err_trie_created:
128     trie_free (test_trie);
129 err_trie_not_created:
130     return 1;
131 }
132
133 /*
134 vi:ts=4:ai:expandtab
135 */