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