2 * Copyright (C) 2014 Intel Corporation. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
25 #include <shared/hash.h>
27 #include "testsuite.h"
31 static void countfreecalls(void *v
)
36 static int test_hash_new(const struct test
*t
)
38 struct hash
*h
= hash_new(8, NULL
);
39 assert_return(h
!= NULL
, EXIT_FAILURE
);
43 DEFINE_TEST(test_hash_new
,
44 .description
= "test hash_new");
47 static int test_hash_get_count(const struct test
*t
)
49 struct hash
*h
= hash_new(8, NULL
);
50 const char *k1
= "k1", *k2
= "k2", *k3
= "k3";
51 const char *v1
= "v1", *v2
= "v2", *v3
= "v3";
57 assert_return(hash_get_count(h
) == 3, EXIT_FAILURE
);
62 DEFINE_TEST(test_hash_get_count
,
63 .description
= "test hash_add / hash_get_count");
66 static int test_hash_replace(const struct test
*t
)
68 struct hash
*h
= hash_new(8, countfreecalls
);
69 const char *k1
= "k1", *k2
= "k2", *k3
= "k3";
70 const char *v1
= "v1", *v2
= "v2", *v3
= "v3", *v4
= "v4";
74 r
|= hash_add(h
, k1
, v1
);
75 r
|= hash_add(h
, k2
, v2
);
76 r
|= hash_add(h
, k3
, v3
);
79 r
|= hash_add(h
, k1
, v4
);
81 assert_return(r
== 0, EXIT_FAILURE
);
82 assert_return(hash_get_count(h
) == 3, EXIT_FAILURE
);
84 v
= hash_find(h
, "k1");
85 assert_return(strcmp(v
, v4
) == 0, EXIT_FAILURE
);
87 assert_return(freecount
== 1, EXIT_FAILURE
);
92 DEFINE_TEST(test_hash_replace
,
93 .description
= "test hash_add replacing existing value");
96 static int test_hash_replace_failing(const struct test
*t
)
98 struct hash
*h
= hash_new(8, countfreecalls
);
99 const char *k1
= "k1", *k2
= "k2", *k3
= "k3";
100 const char *v1
= "v1", *v2
= "v2", *v3
= "v3", *v4
= "v4";
104 r
|= hash_add(h
, k1
, v1
);
105 r
|= hash_add(h
, k2
, v2
);
106 r
|= hash_add(h
, k3
, v3
);
108 assert_return(r
== 0, EXIT_FAILURE
);
111 r
= hash_add_unique(h
, k1
, v4
);
112 assert_return(r
!= 0, EXIT_FAILURE
);
113 assert_return(hash_get_count(h
) == 3, EXIT_FAILURE
);
115 v
= hash_find(h
, "k1");
116 assert_return(strcmp(v
, v1
) == 0, EXIT_FAILURE
);
118 assert_return(freecount
== 0, EXIT_FAILURE
);
123 DEFINE_TEST(test_hash_replace_failing
,
124 .description
= "test hash_add_unique failing to replace existing value");
127 static int test_hash_iter(const struct test
*t
)
129 struct hash
*h
= hash_new(8, NULL
);
130 struct hash
*h2
= hash_new(8, NULL
);
131 const char *k1
= "k1", *k2
= "k2", *k3
= "k3";
132 const char *v1
= "v1", *v2
= "v2", *v3
= "v3";
133 struct hash_iter iter
;
137 hash_add(h2
, k1
, v1
);
139 hash_add(h2
, k2
, v2
);
141 hash_add(h2
, k3
, v3
);
143 for (hash_iter_init(h
, &iter
);
144 hash_iter_next(&iter
, &k
, (const void **) &v
);) {
145 v2
= hash_find(h2
, k
);
146 assert_return(v2
!= NULL
, EXIT_FAILURE
);
150 assert_return(hash_get_count(h
) == 3, EXIT_FAILURE
);
151 assert_return(hash_get_count(h2
) == 0, EXIT_FAILURE
);
157 DEFINE_TEST(test_hash_iter
,
158 .description
= "test hash_iter");
161 static int test_hash_iter_after_del(const struct test
*t
)
163 struct hash
*h
= hash_new(8, NULL
);
164 struct hash
*h2
= hash_new(8, NULL
);
165 const char *k1
= "k1", *k2
= "k2", *k3
= "k3";
166 const char *v1
= "v1", *v2
= "v2", *v3
= "v3";
167 struct hash_iter iter
;
171 hash_add(h2
, k1
, v1
);
173 hash_add(h2
, k2
, v2
);
175 hash_add(h2
, k3
, v3
);
179 for (hash_iter_init(h
, &iter
);
180 hash_iter_next(&iter
, &k
, (const void **) &v
);) {
181 v2
= hash_find(h2
, k
);
182 assert_return(v2
!= NULL
, EXIT_FAILURE
);
186 assert_return(hash_get_count(h
) == 2, EXIT_FAILURE
);
187 assert_return(hash_get_count(h2
) == 1, EXIT_FAILURE
);
193 DEFINE_TEST(test_hash_iter_after_del
,
194 .description
= "test hash_iter, after deleting element");
197 static int test_hash_free(const struct test
*t
)
199 struct hash
*h
= hash_new(8, countfreecalls
);
200 const char *k1
= "k1", *k2
= "k2", *k3
= "k3";
201 const char *v1
= "v1", *v2
= "v2", *v3
= "v3";
209 assert_return(freecount
== 1, EXIT_FAILURE
);
211 assert_return(hash_get_count(h
) == 2, EXIT_FAILURE
);
215 assert_return(freecount
== 3, EXIT_FAILURE
);
219 DEFINE_TEST(test_hash_free
,
220 .description
= "test hash_free calling free function for all values");