1 /******************************************************************************
3 * Copyright (C) 2014 Google, Inc.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 ******************************************************************************/
19 #include <gtest/gtest.h>
21 #include "AllocationTestHarness.h"
24 #include "osi/include/hash_map.h"
25 #include "osi/include/osi.h"
28 class HashMapTest : public AllocationTestHarness {};
30 hash_index_t hash_map_fn00(const void *key) {
31 hash_index_t hash_key = (hash_index_t)key;
35 static size_t g_key_free;
36 void key_free_fn00(UNUSED_ATTR void *data) {
40 static size_t g_data_free;
41 void data_free_fn00(UNUSED_ATTR void *data) {
45 TEST_F(HashMapTest, test_new_free_simple) {
46 hash_map_t *hash_map = hash_map_new(5, hash_map_fn00, NULL, NULL, NULL);
47 ASSERT_TRUE(hash_map != NULL);
48 hash_map_free(hash_map);
51 TEST_F(HashMapTest, test_insert_simple) {
52 hash_map_t *hash_map = hash_map_new(5, hash_map_fn00, NULL, NULL, NULL);
53 ASSERT_TRUE(hash_map != NULL);
65 size_t data_sz = sizeof(data)/sizeof(data[0]);
67 for (size_t i = 0; i < data_sz; i++) {
68 EXPECT_EQ(i, hash_map_size(hash_map));
69 hash_map_set(hash_map, data[i].key, (void*)data[i].data);
70 EXPECT_EQ(i + 1, hash_map_size(hash_map));
73 EXPECT_EQ(data_sz, hash_map_size(hash_map));
75 for (size_t i = 0; i < data_sz; i++) {
76 char *val = (char *)hash_map_get(hash_map, data[i].key);
77 EXPECT_STREQ(data[i].data, val);
79 EXPECT_EQ(data_sz, hash_map_size(hash_map));
81 hash_map_free(hash_map);
84 TEST_F(HashMapTest, test_insert_same) {
85 hash_map_t *hash_map = hash_map_new(5, hash_map_fn00, NULL, NULL, NULL);
86 ASSERT_TRUE(hash_map != NULL);
98 size_t data_sz = sizeof(data)/sizeof(data[0]);
100 for (size_t i = 0; i < data_sz; i++) {
101 hash_map_set(hash_map, data[i].key, (void*)data[i].data);
102 EXPECT_EQ(1U, hash_map_size(hash_map));
105 EXPECT_EQ(1U, hash_map_size(hash_map));
107 for (size_t i = 0; i < data_sz; i++) {
108 char *val = (char *)hash_map_get(hash_map, data[i].key);
109 EXPECT_STREQ(data[data_sz - 1].data, val);
112 hash_map_free(hash_map);
115 TEST_F(HashMapTest, test_functions) {
116 hash_map_t *hash_map = hash_map_new(5, hash_map_fn00, key_free_fn00, data_free_fn00, NULL);
117 ASSERT_TRUE(hash_map != NULL);
132 size_t data_sz = sizeof(data)/sizeof(data[0]);
134 for (size_t i = 0; i < data_sz; i++) {
135 EXPECT_EQ(hash_map_size(hash_map), i);
136 hash_map_set(hash_map, data[i].key, (void*)data[i].data);
139 EXPECT_EQ(data_sz, hash_map_size(hash_map));
140 EXPECT_EQ((size_t)0, g_data_free);
141 EXPECT_EQ((size_t)0, g_key_free);
143 for (size_t i = 0; i < data_sz; i++) {
144 char *val = (char *)hash_map_get(hash_map, data[i].key);
145 EXPECT_TRUE(val != NULL);
146 EXPECT_STREQ(data[i].data, val);
147 hash_map_erase(hash_map, (void*)data[i].key);
148 EXPECT_EQ(i + 1, g_data_free);
149 EXPECT_EQ(i + 1, g_key_free);
152 hash_map_free(hash_map);
155 struct hash_test_iter_data_s {
158 } hash_test_iter_data[] = {
163 { "elephant", "big" },
165 { "gerbil", "small" },
168 bool hash_test_iter_ro_cb(hash_map_entry_t *hash_map_entry, void *context) {
169 const char *key = (const char *)hash_map_entry->key;
170 char *data = (char *)hash_map_entry->data;
171 EXPECT_TRUE(data != NULL);
173 size_t hash_test_iter_data_sz = sizeof(hash_test_iter_data)/sizeof(hash_test_iter_data[0]);
175 for (i = 0; i < hash_test_iter_data_sz; i++) {
176 if (!strcmp(hash_test_iter_data[i].key, key))
179 EXPECT_NE(hash_test_iter_data_sz, i);
180 EXPECT_EQ(NULL, context);
181 EXPECT_STREQ(hash_test_iter_data[i].data, data);
185 TEST_F(HashMapTest, test_iter) {
186 hash_map_t *hash_map = hash_map_new(5, hash_map_fn00, key_free_fn00, data_free_fn00, NULL);
187 ASSERT_TRUE(hash_map != NULL);
191 size_t hash_test_iter_data_sz = sizeof(hash_test_iter_data)/sizeof(hash_test_iter_data[0]);
193 for (size_t i = 0; i < hash_test_iter_data_sz; i++) {
194 EXPECT_EQ(hash_map_size(hash_map), i);
195 hash_map_set(hash_map, hash_test_iter_data[i].key, (void*)hash_test_iter_data[i].data);
198 void *context = NULL;
199 hash_map_foreach(hash_map, hash_test_iter_ro_cb, context);
201 hash_map_free(hash_map);