Debian Codegraph @

glib/girepository/tests/cmph-bdz.c

raw
1 /* GObject introspection: Test cmph hashing
2 *
3 * Copyright (C) 2010 Red Hat, Inc.
4 *
5 * SPDX-License-Identifier: LGPL-2.1-or-later
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
23 #include <glib-object.h>
24 #include "cmph.h"
25
26 static cmph_t *
27 build (void)
28 {
29 cmph_config_t *config;
30 cmph_io_adapter_t *io;
31 char **strings;
32 cmph_t *c;
33 uint32_t size;
34
35 strings = g_strsplit ("foo,bar,baz", ",", -1);
36
37 io = cmph_io_vector_adapter (strings, g_strv_length (strings));
38 config = cmph_config_new (io);
39 cmph_config_set_algo (config, CMPH_BDZ);
40
41 c = cmph_new (config);
42 size = cmph_size (c);
43 g_assert_cmpuint (size, ==, g_strv_length (strings));
44
45 cmph_config_destroy (config);
46 cmph_io_vector_adapter_destroy (io);
47 g_strfreev (strings);
48
49 return c;
50 }
51
52 static void
53 assert_hashes_unique (size_t n_hashes,
54 uint32_t* hashes)
55 {
56 size_t i;
57
58 for (i = 0; i < n_hashes; i++)
59 {
60 for (size_t j = 0; j < n_hashes; j++)
61 {
62 if (j != i)
63 g_assert_cmpuint (hashes[i], !=, hashes[j]);
64 }
65 }
66 }
67
68 static void
69 test_search (void)
70 {
71 cmph_t *c = build();
72 size_t i;
73 uint32_t hash;
74 uint32_t hashes[3];
75 uint32_t size;
76
77 size = cmph_size (c);
78
79 i = 0;
80 hash = cmph_search (c, "foo", 3);
81 g_assert_cmpuint (hash, >=, 0);
82 g_assert_cmpuint (hash, <, size);
83 hashes[i++] = hash;
84
85 hash = cmph_search (c, "bar", 3);
86 g_assert_cmpuint (hash, >=, 0);
87 g_assert_cmpuint (hash, <, size);
88 hashes[i++] = hash;
89
90 hash = cmph_search (c, "baz", 3);
91 g_assert_cmpuint (hash, >=, 0);
92 g_assert_cmpuint (hash, <, size);
93 hashes[i++] = hash;
94
95 assert_hashes_unique (G_N_ELEMENTS (hashes), &hashes[0]);
96
97 cmph_destroy (c);
98 }
99
100 static void
101 test_search_packed (void)
102 {
103 cmph_t *c = build();
104 size_t i;
105 uint32_t bufsize;
106 uint32_t hash;
107 uint32_t hashes[3];
108 uint32_t size;
109 uint8_t *buf;
110
111 bufsize = cmph_packed_size (c);
112 buf = g_malloc (bufsize);
113 cmph_pack (c, buf);
114
115 size = cmph_size (c);
116
117 cmph_destroy (c);
118 c = NULL;
119
120 i = 0;
121 hash = cmph_search_packed (buf, "foo", 3);
122 g_assert_cmpuint (hash, >=, 0);
123 g_assert_cmpuint (hash, <, size);
124 hashes[i++] = hash;
125
126 hash = cmph_search_packed (buf, "bar", 3);
127 g_assert_cmpuint (hash, >=, 0);
128 g_assert_cmpuint (hash, <, size);
129 hashes[i++] = hash;
130
131 hash = cmph_search_packed (buf, "baz", 3);
132 g_assert_cmpuint (hash, >=, 0);
133 g_assert_cmpuint (hash, <, size);
134 hashes[i++] = hash;
135
136 assert_hashes_unique (G_N_ELEMENTS (hashes), &hashes[0]);
137
138 g_free (buf);
139 }
140
141 int
142 main(int argc, char **argv)
143 {
144 g_test_init (&argc, &argv, NULL);
145
146 g_test_add_func ("/cmph-bdz/search", test_search);
147 g_test_add_func ("/cmph-bdz/search-packed", test_search_packed);
148
149 return g_test_run ();
150 }
151