| 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_file.c - Test for datrie file 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 <wchar.h> |
| 31 |
|
| 32 |
#define TRIE_FILENAME "test_file.tri" |
| 33 |
|
| 34 |
static Bool |
| 35 |
trie_enum_mark_rec (const AlphaChar *key, TrieData key_data, void *user_data) |
| 36 |
{ |
| 37 |
Bool *is_failed = (Bool *)user_data; |
| 38 |
TrieData src_data; |
| 39 |
|
| 40 |
src_data = dict_src_get_data (key); |
| 41 |
if (TRIE_DATA_ERROR == src_data) { |
| 42 |
printf ("Extra entry in file: key '%ls', data %d.\n", |
| 43 |
(wchar_t *)key, key_data); |
| 44 |
*is_failed = TRUE; |
| 45 |
} else if (src_data != key_data) { |
| 46 |
printf ("Data mismatch for: key '%ls', expected %d, got %d.\n", |
| 47 |
(wchar_t *)key, src_data, key_data); |
| 48 |
*is_failed = TRUE; |
| 49 |
} else { |
| 50 |
dict_src_set_data (key, TRIE_DATA_READ); |
| 51 |
} |
| 52 |
|
| 53 |
return TRUE; |
| 54 |
} |
| 55 |
|
| 56 |
int |
| 57 |
main (void) |
| 58 |
{ |
| 59 |
Trie *test_trie; |
| 60 |
DictRec *dict_p; |
| 61 |
Bool is_failed; |
| 62 |
|
| 63 |
msg_step ("Preparing trie"); |
| 64 |
test_trie = en_trie_new (); |
| 65 |
if (!test_trie) { |
| 66 |
printf ("Failed to allocate test trie.\n"); |
| 67 |
goto err_trie_not_created; |
| 68 |
} |
| 69 |
|
| 70 |
/* add/remove some words */ |
| 71 |
for (dict_p = dict_src; dict_p->key; dict_p++) { |
| 72 |
if (!trie_store (test_trie, dict_p->key, dict_p->data)) { |
| 73 |
printf ("Failed to add key '%ls', data %d.\n", |
| 74 |
(wchar_t *)dict_p->key, dict_p->data); |
| 75 |
goto err_trie_created; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/* save & close */ |
| 80 |
msg_step ("Saving trie to file"); |
| 81 |
remove (TRIE_FILENAME); /* error ignored */ |
| 82 |
if (trie_save (test_trie, TRIE_FILENAME) != 0) { |
| 83 |
printf ("Failed to save trie to file '%s'.\n", TRIE_FILENAME); |
| 84 |
goto err_trie_created; |
| 85 |
} |
| 86 |
trie_free (test_trie); |
| 87 |
|
| 88 |
/* reload from file */ |
| 89 |
msg_step ("Reloading trie from the saved file"); |
| 90 |
test_trie = trie_new_from_file (TRIE_FILENAME); |
| 91 |
if (!test_trie) { |
| 92 |
printf ("Failed to reload saved trie from '%s'.\n", |
| 93 |
TRIE_FILENAME); |
| 94 |
goto err_trie_saved; |
| 95 |
} |
| 96 |
|
| 97 |
/* enumerate & check */ |
| 98 |
msg_step ("Checking trie contents"); |
| 99 |
is_failed = FALSE; |
| 100 |
/* mark entries found in file */ |
| 101 |
if (!trie_enumerate (test_trie, trie_enum_mark_rec, (void *)&is_failed)) { |
| 102 |
printf ("Failed to enumerate trie file contents.\n"); |
| 103 |
goto err_trie_saved; |
| 104 |
} |
| 105 |
/* check for unmarked entries, (i.e. missed in file) */ |
| 106 |
for (dict_p = dict_src; dict_p->key; dict_p++) { |
| 107 |
if (dict_p->data != TRIE_DATA_READ) { |
| 108 |
printf ("Entry missed in file: key '%ls', data %d.\n", |
| 109 |
(wchar_t *)dict_p->key, dict_p->data); |
| 110 |
is_failed = TRUE; |
| 111 |
} |
| 112 |
} |
| 113 |
if (is_failed) { |
| 114 |
printf ("Errors found in trie saved contents.\n"); |
| 115 |
goto err_trie_saved; |
| 116 |
} |
| 117 |
|
| 118 |
remove (TRIE_FILENAME); |
| 119 |
trie_free (test_trie); |
| 120 |
return 0; |
| 121 |
|
| 122 |
err_trie_saved: |
| 123 |
remove (TRIE_FILENAME); |
| 124 |
err_trie_created: |
| 125 |
trie_free (test_trie); |
| 126 |
err_trie_not_created: |
| 127 |
return 1; |
| 128 |
} |
| 129 |
|
| 130 |
/* |
| 131 |
vi:ts=4:ai:expandtab |
| 132 |
*/ |