ae21ffc3d514de2f4b8f09568c1477a57ab10792
[platform/upstream/libdatrie.git] / tests / test_null_trie.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) 2015  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_null_trie.c - Test for datrie iteration on empty trie
23  * Created: 2015-04-21
24  * Author:  Theppitak Karoonboonyanan <theppitak@gmail.com>
25  */
26
27 #include <datrie/trie.h>
28 #include "utils.h"
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 int
33 main ()
34 {
35     Trie         *test_trie;
36     TrieState    *trie_root_state;
37     TrieIterator *trie_it;
38     Bool          is_failed;
39
40     msg_step ("Preparing empty trie");
41     test_trie = en_trie_new ();
42     if (!test_trie) {
43         fprintf (stderr, "Fail to create test trie\n");
44         goto err_trie_not_created;
45     }
46
47     /* iterate & check */
48     msg_step ("Iterating");
49     trie_root_state = trie_root (test_trie);
50     if (!trie_root_state) {
51         printf ("Failed to get trie root state\n");
52         goto err_trie_created;
53     }
54     trie_it = trie_iterator_new (trie_root_state);
55     if (!trie_it) {
56         printf ("Failed to get trie iterator\n");
57         goto err_trie_root_created;
58     }
59
60     is_failed = FALSE;
61     while (trie_iterator_next (trie_it)) {
62         AlphaChar *key;
63
64         printf ("Got entry from empty trie, which is weird!\n");
65
66         key = trie_iterator_get_key (trie_it);
67         if (key) {
68             printf ("Got key from empty trie, which is weird! (key='%ls')\n",
69                     key);
70             is_failed = TRUE;
71             free (key);
72         }
73     }
74
75     if (is_failed) {
76         printf ("Errors found in empty trie iteration.\n");
77         goto err_trie_it_created;
78     }
79
80     trie_iterator_free (trie_it);
81     trie_state_free (trie_root_state);
82     trie_free (test_trie);
83     return 0;
84
85 err_trie_it_created:
86     trie_iterator_free (trie_it);
87 err_trie_root_created:
88     trie_state_free (trie_root_state);
89 err_trie_created:
90     trie_free (test_trie);
91 err_trie_not_created:
92     return 1;
93 }
94
95 /*
96 vi:ts=4:ai:expandtab
97 */