1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * libdatrie - Double-Array Trie Library
4 * Copyright (C) 2018 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_term_state.c - Test data retrieval from terminal state
24 * Author: Theppitak Karoonboonyanan <theppitak@gmail.com>
27 #include <datrie/trie.h>
35 * (1) -a-> (2) -b-> (3) -#-> [4] {data=1}
37 * +---c-> (5) -#-> [6] {data=2}
44 TrieState *trie_state;
48 msg_step ("Preparing trie");
49 test_trie = en_trie_new ();
51 printf ("Fail to create test trie\n");
52 goto err_trie_not_created;
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;
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;
68 /* try retrieving data */
69 msg_step ("Preparing root state");
70 trie_state = trie_root (test_trie);
72 printf ("Failed to get trie root state\n");
73 goto err_trie_created;
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");
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);
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");
95 data = trie_state_get_data (trie_state);
97 printf ("Retrieved data for key 'ab' is %d, not 1.\n", data);
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");
107 data = trie_state_get_data (trie_state);
109 printf ("Retrieved data for key 'abc' is %d, not 2.\n", data);
113 trie_state_free (trie_state);
116 printf ("Errors found in terminal state data retrieval.\n");
117 goto err_trie_created;
120 trie_free (test_trie);
124 trie_free (test_trie);
125 err_trie_not_created: