Imported Upstream version 0.2.12
[platform/upstream/libdatrie.git] / tests / test_nonalpha.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) 2014  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_nonalpha.c - Test for datrie behaviors on non-alphabet inputs
23  * Created: 2014-01-06
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
32 const AlphaChar *nonalpha_src[] = {
33     (AlphaChar *)L"a6acus",
34     (AlphaChar *)L"a5acus",
35     NULL
36 };
37
38 int
39 main (void)
40 {
41     Trie             *test_trie;
42     DictRec          *dict_p;
43     const AlphaChar **nonalpha_key;
44     TrieData          trie_data;
45     Bool              is_fail;
46
47     msg_step ("Preparing trie");
48     test_trie = en_trie_new ();
49     if (!test_trie) {
50         fprintf (stderr, "Fail to create test trie\n");
51         goto err_trie_not_created;
52     }
53
54     /* store */
55     msg_step ("Adding data to trie");
56     for (dict_p = dict_src; dict_p->key; dict_p++) {
57         if (!trie_store (test_trie, dict_p->key, dict_p->data)) {
58             printf ("Failed to add key '%ls', data %d.\n",
59                     (wchar_t *)dict_p->key, dict_p->data);
60             goto err_trie_created;
61         }
62     }
63
64     /* test storing keys with non-alphabet chars */
65     is_fail = FALSE;
66     for (nonalpha_key = nonalpha_src; *nonalpha_key; nonalpha_key++) {
67         if (trie_retrieve (test_trie, *nonalpha_key, &trie_data)) {
68             printf ("False duplication on key '%ls', with existing data %d.\n",
69                     (wchar_t *)*nonalpha_key, trie_data);
70             is_fail = TRUE;
71         }
72         if (trie_store (test_trie, *nonalpha_key, TRIE_DATA_UNREAD)) {
73             printf ("Wrongly added key '%ls' containing non-alphanet char\n",
74                     (wchar_t *)*nonalpha_key);
75             is_fail = TRUE;
76         }
77     }
78
79     if (is_fail)
80         goto err_trie_created;
81
82     trie_free (test_trie);
83     return 0;
84
85 err_trie_created:
86     trie_free (test_trie);
87 err_trie_not_created:
88     return 1;
89 }
90
91 /*
92 vi:ts=4:ai:expandtab
93 */