2389ef78d52b9a523ada49c02d48eb68323fe0b0
[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 <stdio.h>
31 #include <stdlib.h>
32
33 #define TEST_DATA 255
34 int
35 main ()
36 {
37     AlphaMap    *alpha_map;
38     Trie        *test_trie;
39     AlphaChar    key[3];
40     TrieData     data;
41
42     msg_step ("Preparing alpha map");
43     alpha_map = alpha_map_new ();
44     if (!alpha_map) {
45         printf ("Fail to allocate alpha map\n");
46         goto err_alpha_map_not_created;
47     }
48     if (alpha_map_add_range (alpha_map, 0x00, 0xff) != 0) {
49         printf ("Fail to add full alpha map range\n");
50         goto err_alpha_map_created;
51     }
52
53     msg_step ("Preparing trie");
54     test_trie = trie_new (alpha_map);
55     alpha_map_free (alpha_map);
56     if (!test_trie) {
57         printf ("Fail to create test trie\n");
58         goto err_alpha_map_created;
59     }
60
61     msg_step ("Storing key to test trie");
62     key[0] = 0xff;
63     key[1] = 0xff;
64     key[2] = 0;
65     if (!trie_store (test_trie, key, TEST_DATA)) {
66         printf ("Fail to store key to test trie\n");
67         goto err_trie_created;
68     }
69
70     msg_step ("Retrieving data from test trie");
71     if (!trie_retrieve (test_trie, key, &data)) {
72         printf ("Fail to retrieve key from test trie\n");
73         goto err_trie_created;
74     }
75     if (TEST_DATA != data) {
76         printf ("Retrieved data = %d, not %d\n", data, TEST_DATA);
77         goto err_trie_created;
78     }
79
80     msg_step ("Freeing test trie");
81     trie_free (test_trie);
82     return 0;
83
84 err_trie_created:
85     trie_free (test_trie);
86 err_alpha_map_created:
87     alpha_map_free (alpha_map);
88 err_alpha_map_not_created:
89     return 1;
90 }
91
92 /*
93 vi:ts=4:ai:expandtab
94 */