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