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