Imported Upstream version 0.2.12
[platform/upstream/libdatrie.git] / tests / test_term_state.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) 2018  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  * test_term_state.c - Test data retrieval from terminal state
23  * Created: 2018-03-29
24  * Author:  Theppitak Karoonboonyanan <theppitak@gmail.com>
25  */
26
27 #include <datrie/trie.h>
28 #include "utils.h"
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 /*
33  * Test trie
34  *
35  * (1) -a-> (2) -b-> (3) -#-> [4] {data=1}
36  *                    |
37  *                    +---c-> (5) -#-> [6] {data=2}
38  *
39  */
40 int
41 main (void)
42 {
43     Trie         *test_trie;
44     TrieState    *trie_state;
45     TrieData      data;
46     Bool          is_failed;
47
48     msg_step ("Preparing trie");
49     test_trie = en_trie_new ();
50     if (!test_trie) {
51         printf ("Fail to create test trie\n");
52         goto err_trie_not_created;
53     }
54
55     /* populate trie */
56     msg_step ("Populating trie with test set");
57     if (!trie_store (test_trie, (AlphaChar *)L"ab", 1)) {
58         printf ("Failed to add key 'ab', data 1.\n");
59         goto err_trie_created;
60     }
61     if (!trie_store (test_trie, (AlphaChar *)L"abc", 2)) {
62         printf ("Failed to add key 'abc', data 2.\n");
63         goto err_trie_created;
64     }
65
66     is_failed = FALSE;
67
68     /* try retrieving data */
69     msg_step ("Preparing root state");
70     trie_state = trie_root (test_trie);
71     if (!trie_state) {
72         printf ("Failed to get trie root state\n");
73         goto err_trie_created;
74     }
75
76     msg_step ("Try walking from root with 'a'");
77     if (!trie_state_walk (trie_state, (AlphaChar)L'a')) {
78         printf ("Failed to walk from root with 'a'.\n");
79         is_failed = TRUE;
80     }
81
82     data = trie_state_get_data (trie_state);
83     if (data != TRIE_DATA_ERROR) {
84         printf ("Retrieved data at 'a' is %d, not %d.\n",
85                 data, TRIE_DATA_ERROR);
86         is_failed = TRUE;
87     }
88
89     msg_step ("Try walking further with 'b'");
90     if (!trie_state_walk (trie_state, (AlphaChar)L'b')) {
91         printf ("Failed to continue walking with 'b'.\n");
92         is_failed = TRUE;
93     }
94
95     data = trie_state_get_data (trie_state);
96     if (data != 1) {
97         printf ("Retrieved data for key 'ab' is %d, not 1.\n", data);
98         is_failed = TRUE;
99     }
100
101     msg_step ("Try walking further with 'c'");
102     if (!trie_state_walk (trie_state, (AlphaChar)L'c')) {
103         printf ("Failed to continue walking with 'c'.\n");
104         is_failed = TRUE;
105     }
106
107     data = trie_state_get_data (trie_state);
108     if (data != 2) {
109         printf ("Retrieved data for key 'abc' is %d, not 2.\n", data);
110         is_failed = TRUE;
111     }
112
113     trie_state_free (trie_state);
114
115     if (is_failed) {
116         printf ("Errors found in terminal state data retrieval.\n");
117         goto err_trie_created;
118     }
119
120     trie_free (test_trie);
121     return 0;
122
123 err_trie_created:
124     trie_free (test_trie);
125 err_trie_not_created:
126     return 1;
127 }
128
129 /*
130 vi:ts=4:ai:expandtab
131 */