Imported Upstream version 0.2.12
[platform/upstream/libdatrie.git] / tests / utils.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 <theppitak@gmail.com>
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  * utils.c - Utility functions for datrie test cases
23  * Created: 2013-10-16
24  * Author:  Theppitak Karoonboonyanan <theppitak@gmail.com>
25  */
26
27 #include <datrie/trie.h>
28 #include "utils.h"
29
30 /*---------------------*
31  *  Debugging helpers  *
32  *---------------------*/
33 void
34 msg_step (const char *msg)
35 {
36     printf ("=> %s...\n", msg);
37 }
38
39 /*-------------------------*
40  *  Trie creation helpers  *
41  *-------------------------*/
42 static AlphaMap *
43 en_alpha_map_new (void)
44 {
45     AlphaMap *en_map;
46
47     en_map = alpha_map_new ();
48     if (!en_map)
49         goto err_map_not_created;
50
51     if (alpha_map_add_range (en_map, 0x0061, 0x007a) != 0)
52         goto err_map_created;
53
54     return en_map;
55
56 err_map_created:
57     alpha_map_free (en_map);
58 err_map_not_created:
59     return NULL;
60 }
61
62 Trie *
63 en_trie_new (void)
64 {
65     AlphaMap *en_map;
66     Trie     *en_trie;
67
68     en_map = en_alpha_map_new ();
69     if (!en_map)
70         goto err_map_not_created;
71
72     en_trie = trie_new (en_map);
73     if (!en_trie)
74         goto err_map_created;
75
76     alpha_map_free (en_map);
77     return en_trie;
78
79 err_map_created:
80     alpha_map_free (en_map);
81 err_map_not_created:
82     return NULL;
83 }
84
85 /*---------------------------*
86  *  Dict source for testing  *
87  *---------------------------*/
88 DictRec dict_src[] = {
89     {(AlphaChar *)L"a",          TRIE_DATA_UNREAD},
90     {(AlphaChar *)L"abacus",     TRIE_DATA_UNREAD},
91     {(AlphaChar *)L"abandon",    TRIE_DATA_UNREAD},
92     {(AlphaChar *)L"accident",   TRIE_DATA_UNREAD},
93     {(AlphaChar *)L"accredit",   TRIE_DATA_UNREAD},
94     {(AlphaChar *)L"algorithm",  TRIE_DATA_UNREAD},
95     {(AlphaChar *)L"ammonia",    TRIE_DATA_UNREAD},
96     {(AlphaChar *)L"angel",      TRIE_DATA_UNREAD},
97     {(AlphaChar *)L"angle",      TRIE_DATA_UNREAD},
98     {(AlphaChar *)L"azure",      TRIE_DATA_UNREAD},
99     {(AlphaChar *)L"bat",        TRIE_DATA_UNREAD},
100     {(AlphaChar *)L"bet",        TRIE_DATA_UNREAD},
101     {(AlphaChar *)L"best",       TRIE_DATA_UNREAD},
102     {(AlphaChar *)L"home",       TRIE_DATA_UNREAD},
103     {(AlphaChar *)L"house",      TRIE_DATA_UNREAD},
104     {(AlphaChar *)L"hut",        TRIE_DATA_UNREAD},
105     {(AlphaChar *)L"king",       TRIE_DATA_UNREAD},
106     {(AlphaChar *)L"kite",       TRIE_DATA_UNREAD},
107     {(AlphaChar *)L"name",       TRIE_DATA_UNREAD},
108     {(AlphaChar *)L"net",        TRIE_DATA_UNREAD},
109     {(AlphaChar *)L"network",    TRIE_DATA_UNREAD},
110     {(AlphaChar *)L"nut",        TRIE_DATA_UNREAD},
111     {(AlphaChar *)L"nutshell",   TRIE_DATA_UNREAD},
112     {(AlphaChar *)L"quality",    TRIE_DATA_UNREAD},
113     {(AlphaChar *)L"quantum",    TRIE_DATA_UNREAD},
114     {(AlphaChar *)L"quantity",   TRIE_DATA_UNREAD},
115     {(AlphaChar *)L"quartz",     TRIE_DATA_UNREAD},
116     {(AlphaChar *)L"quick",      TRIE_DATA_UNREAD},
117     {(AlphaChar *)L"quiz",       TRIE_DATA_UNREAD},
118     {(AlphaChar *)L"run",        TRIE_DATA_UNREAD},
119     {(AlphaChar *)L"tape",       TRIE_DATA_UNREAD},
120     {(AlphaChar *)L"test",       TRIE_DATA_UNREAD},
121     {(AlphaChar *)L"what",       TRIE_DATA_UNREAD},
122     {(AlphaChar *)L"when",       TRIE_DATA_UNREAD},
123     {(AlphaChar *)L"where",      TRIE_DATA_UNREAD},
124     {(AlphaChar *)L"which",      TRIE_DATA_UNREAD},
125     {(AlphaChar *)L"who",        TRIE_DATA_UNREAD},
126     {(AlphaChar *)L"why",        TRIE_DATA_UNREAD},
127     {(AlphaChar *)L"zebra",      TRIE_DATA_UNREAD},
128     {(AlphaChar *)NULL,          TRIE_DATA_ERROR},
129 };
130
131 int
132 dict_src_n_entries (void)
133 {
134     return sizeof (dict_src) / sizeof (dict_src[0]) - 1;
135 }
136
137 TrieData
138 dict_src_get_data (const AlphaChar *key)
139 {
140     const DictRec *dict_p;
141
142     for (dict_p = dict_src; dict_p->key; dict_p++) {
143         if (alpha_char_strcmp (dict_p->key, key) == 0) {
144             return dict_p->data;
145         }
146     }
147
148     return TRIE_DATA_ERROR;
149 }
150
151 int
152 dict_src_set_data (const AlphaChar *key, TrieData data)
153 {
154     DictRec *dict_p;
155
156     for (dict_p = dict_src; dict_p->key; dict_p++) {
157         if (alpha_char_strcmp (dict_p->key, key) == 0) {
158             dict_p->data = data;
159             return 0;
160         }
161     }
162
163     return -1;
164 }
165
166 /*
167 vi:ts=4:ai:expandtab
168 */