| 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 <[email protected]> |
| 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_byte_list.c - Test byte trie enumeration |
| 23 |
* Created: 2018-11-20 |
| 24 |
* Author: Theppitak Karoonboonyanan <[email protected]> |
| 25 |
* Based on test case in issue #9 |
| 26 |
* https://github.com/tlwg/libdatrie/issues/9 |
| 27 |
*/ |
| 28 |
|
| 29 |
#include <datrie/trie.h> |
| 30 |
#include "utils.h" |
| 31 |
#include <stdio.h> |
| 32 |
#include <stdlib.h> |
| 33 |
|
| 34 |
typedef struct _DictEntry DictEntry; |
| 35 |
struct _DictEntry { |
| 36 |
AlphaChar key[4]; |
| 37 |
TrieData data; |
| 38 |
int is_checked; |
| 39 |
}; |
| 40 |
|
| 41 |
#define N_ELM(v) (sizeof(v)/sizeof(v[0])) |
| 42 |
|
| 43 |
/* Dictionary source */ |
| 44 |
static DictEntry Source[] = { |
| 45 |
{ { '1', '2', 0 }, 1, 0 }, |
| 46 |
{ { '1', '2', '3', 0 }, 2, 0 }, |
| 47 |
}; |
| 48 |
|
| 49 |
static void |
| 50 |
dump_key_data (const AlphaChar *key, TrieData data) |
| 51 |
{ |
| 52 |
const AlphaChar *p; |
| 53 |
|
| 54 |
printf ("["); |
| 55 |
for (p = key; *p; ++p) { |
| 56 |
if (p != key) { |
| 57 |
printf (", "); |
| 58 |
} |
| 59 |
printf ("%04x", *p); |
| 60 |
} |
| 61 |
printf ("] : %d\n", data); |
| 62 |
} |
| 63 |
|
| 64 |
static void |
| 65 |
dump_entry (const TrieIterator *iter) |
| 66 |
{ |
| 67 |
AlphaChar *key = trie_iterator_get_key (iter); |
| 68 |
dump_key_data (key, trie_iterator_get_data (iter)); |
| 69 |
free (key); |
| 70 |
} |
| 71 |
|
| 72 |
/* |
| 73 |
* Check if the trie entry referenced by iter match any Source[] element |
| 74 |
* and mark the matched element as checked. |
| 75 |
* Return: 1 if matched, 0 otherwise |
| 76 |
*/ |
| 77 |
static int |
| 78 |
validate_entry (const TrieIterator *iter) |
| 79 |
{ |
| 80 |
AlphaChar *key = trie_iterator_get_key (iter); |
| 81 |
TrieData data = trie_iterator_get_data (iter); |
| 82 |
int i; |
| 83 |
|
| 84 |
for (i = 0; i < N_ELM (Source); i++) { |
| 85 |
if (alpha_char_strcmp (Source[i].key, key) == 0 |
| 86 |
&& Source[i].data == data) |
| 87 |
{ |
| 88 |
Source[i].is_checked = 1; |
| 89 |
free (key); |
| 90 |
return 1; |
| 91 |
} |
| 92 |
} |
| 93 |
free (key); |
| 94 |
return 0; |
| 95 |
} |
| 96 |
|
| 97 |
/* |
| 98 |
* Check if all Source[] elements are checked and reported unchecked one. |
| 99 |
* Return: 1 if all are checked, 0 otherwise. |
| 100 |
*/ |
| 101 |
static int |
| 102 |
is_all_checked() |
| 103 |
{ |
| 104 |
int i; |
| 105 |
int ret = 1; |
| 106 |
|
| 107 |
for (i = 0; i < N_ELM (Source); i++) { |
| 108 |
if (!Source[i].is_checked) { |
| 109 |
printf ("Not visited Source entry: "); |
| 110 |
dump_key_data (Source[i].key, Source[i].data); |
| 111 |
ret = 0; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
return ret; |
| 116 |
} |
| 117 |
|
| 118 |
int |
| 119 |
main (void) |
| 120 |
{ |
| 121 |
AlphaMap *alpha_map; |
| 122 |
Trie *test_trie; |
| 123 |
int i; |
| 124 |
TrieState *root; |
| 125 |
TrieIterator *iter; |
| 126 |
int ret = 0; |
| 127 |
|
| 128 |
msg_step ("Preparing alpha map"); |
| 129 |
alpha_map = alpha_map_new (); |
| 130 |
if (!alpha_map) { |
| 131 |
printf ("Fail to allocate alpha map\n"); |
| 132 |
goto err_alpha_map_not_created; |
| 133 |
} |
| 134 |
if (alpha_map_add_range (alpha_map, 0x00, 0xff) != 0) { |
| 135 |
printf ("Fail to add full alpha map range\n"); |
| 136 |
goto err_alpha_map_created; |
| 137 |
} |
| 138 |
|
| 139 |
msg_step ("Preparing trie"); |
| 140 |
test_trie = trie_new (alpha_map); |
| 141 |
alpha_map_free (alpha_map); |
| 142 |
if (!test_trie) { |
| 143 |
printf ("Fail to create test trie\n"); |
| 144 |
goto err_alpha_map_created; |
| 145 |
} |
| 146 |
|
| 147 |
msg_step ("Storing entries to test trie"); |
| 148 |
for (i = 0; i < N_ELM (Source); i++) { |
| 149 |
if (!trie_store (test_trie, Source[i].key, Source[i].data)) { |
| 150 |
printf ("Fail to store entry %d to test trie:\n", i); |
| 151 |
dump_key_data (Source[i].key, Source[i].data); |
| 152 |
goto err_trie_created; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
msg_step ("Iterating trie"); |
| 157 |
root = trie_root (test_trie); |
| 158 |
iter = trie_iterator_new (root); |
| 159 |
while (trie_iterator_next (iter)) { |
| 160 |
if (!validate_entry (iter)) { |
| 161 |
printf ("Fail to validate trie entry:\n"); |
| 162 |
dump_entry (iter); |
| 163 |
ret = 1; |
| 164 |
} |
| 165 |
} |
| 166 |
if (!is_all_checked()) { |
| 167 |
ret = 1; |
| 168 |
} |
| 169 |
trie_iterator_free (iter); |
| 170 |
trie_state_free (root); |
| 171 |
|
| 172 |
msg_step ("Freeing test trie"); |
| 173 |
trie_free (test_trie); |
| 174 |
return ret; |
| 175 |
|
| 176 |
err_trie_created: |
| 177 |
trie_free (test_trie); |
| 178 |
err_alpha_map_created: |
| 179 |
alpha_map_free (alpha_map); |
| 180 |
err_alpha_map_not_created: |
| 181 |
return 1; |
| 182 |
} |
| 183 |
|
| 184 |
/* |
| 185 |
vi:ts=4:ai:expandtab |
| 186 |
*/ |