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