Imported Upstream version 0.2.12
[platform/upstream/libdatrie.git] / tests / test_byte_alpha.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) 2018  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_byte_alpha.c - Test byte stream (full-range 0..255) alpha map
23  * Created: 2018-04-21
24  * Author:  Theppitak Karoonboonyanan <theppitak@gmail.com>
25  *          Based on test case in issue #6
26  *          https://github.com/tlwg/libdatrie/issues/6
27  */
28
29 #include <datrie/trie.h>
30 #include "utils.h"
31 #include <stdio.h>
32 #include <stdlib.h>
33
34 #define TEST_DATA 255
35 int
36 main (void)
37 {
38     AlphaMap    *alpha_map;
39     Trie        *test_trie;
40     AlphaChar    key[3];
41     TrieData     data;
42
43     msg_step ("Preparing alpha map");
44     alpha_map = alpha_map_new ();
45     if (!alpha_map) {
46         printf ("Fail to allocate alpha map\n");
47         goto err_alpha_map_not_created;
48     }
49     if (alpha_map_add_range (alpha_map, 0x00, 0xff) != 0) {
50         printf ("Fail to add full alpha map range\n");
51         goto err_alpha_map_created;
52     }
53
54     msg_step ("Preparing trie");
55     test_trie = trie_new (alpha_map);
56     alpha_map_free (alpha_map);
57     if (!test_trie) {
58         printf ("Fail to create test trie\n");
59         goto err_alpha_map_created;
60     }
61
62     msg_step ("Storing key to test trie");
63     key[0] = 0xff;
64     key[1] = 0xff;
65     key[2] = 0;
66     if (!trie_store (test_trie, key, TEST_DATA)) {
67         printf ("Fail to store key to test trie\n");
68         goto err_trie_created;
69     }
70
71     msg_step ("Retrieving data from test trie");
72     if (!trie_retrieve (test_trie, key, &data)) {
73         printf ("Fail to retrieve key from test trie\n");
74         goto err_trie_created;
75     }
76     if (TEST_DATA != data) {
77         printf ("Retrieved data = %d, not %d\n", data, TEST_DATA);
78         goto err_trie_created;
79     }
80
81     msg_step ("Freeing test trie");
82     trie_free (test_trie);
83     return 0;
84
85 err_trie_created:
86     trie_free (test_trie);
87 err_alpha_map_created:
88     alpha_map_free (alpha_map);
89 err_alpha_map_not_created:
90     return 1;
91 }
92
93 /*
94 vi:ts=4:ai:expandtab
95 */