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