1 /******************************************************************************
3 * Copyright (C) 2015 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 ******************************************************************************/
20 #include <gtest/gtest.h>
22 #include "AllocationTestHarness.h"
25 #include "osi/include/hash_map.h"
26 #include "osi/include/hash_map_utils.h"
27 #include "osi/include/allocator.h"
30 class HashMapUtilsTest : public AllocationTestHarness {
32 virtual void SetUp() {
33 AllocationTestHarness::SetUp();
36 virtual void TearDown() {
38 AllocationTestHarness::TearDown();
44 TEST_F(HashMapUtilsTest, test_empty_string_params) {
46 map = hash_map_utils_new_from_string_params(params);
47 EXPECT_TRUE(hash_map_is_empty(map));
50 TEST_F(HashMapUtilsTest, test_semicolons) {
51 char params[] = ";;;";
52 map = hash_map_utils_new_from_string_params(params);
53 EXPECT_TRUE(hash_map_is_empty(map));
56 TEST_F(HashMapUtilsTest, test_equal_sign_in_value) {
57 char params[] = "keyOfSomething=value=OfSomething";
58 char key[] = "keyOfSomething";
59 char value[] = "value=OfSomething";
60 map = hash_map_utils_new_from_string_params(params);
61 EXPECT_EQ(1u, hash_map_size(map));
62 EXPECT_EQ(0, strcmp(value, (char *)hash_map_get(map, key)));
65 TEST_F(HashMapUtilsTest, test_two_pairs_with_same_key) {
66 char params[] = "key=valu0;key=value1";
68 char value1[] = "value1";
69 map = hash_map_utils_new_from_string_params(params);
70 EXPECT_EQ(1u, hash_map_size(map));
71 EXPECT_EQ(0, strcmp(value1, (char *)hash_map_get(map, key)));
74 TEST_F(HashMapUtilsTest, test_one_key_value_pair_without_semicolon) {
75 char params[] = "keyOfSomething=valueOfSomething";
76 char key[] = "keyOfSomething";
77 char value[] = "valueOfSomething";
78 map = hash_map_utils_new_from_string_params(params);
79 EXPECT_EQ(1u, hash_map_size(map));
80 EXPECT_EQ(0, strcmp(value, (char *)hash_map_get(map, key)));
83 TEST_F(HashMapUtilsTest, test_one_key_value_pair_with_semicolon) {
84 char params[] = "keyOfSomething=valueOfSomething;";
85 char key[] = "keyOfSomething";
86 char value[] = "valueOfSomething";
87 map = hash_map_utils_new_from_string_params(params);
88 EXPECT_EQ(1u, hash_map_size(map));
89 EXPECT_EQ(0, strcmp(value, (char *)hash_map_get(map, key)));
92 TEST_F(HashMapUtilsTest, test_one_pair_with_empty_value) {
93 char params[] = "keyOfSomething=;";
94 char key[] = "keyOfSomething";
96 map = hash_map_utils_new_from_string_params(params);
97 EXPECT_EQ(1u, hash_map_size(map));
98 EXPECT_EQ(0, strcmp(value, (char *)hash_map_get(map, key)));
101 TEST_F(HashMapUtilsTest, test_one_pair_with_empty_key) {
102 char params[] = "=valueOfSomething;";
103 map = hash_map_utils_new_from_string_params(params);
104 EXPECT_TRUE(hash_map_is_empty(map));
107 TEST_F(HashMapUtilsTest, test_two_key_value_pairs) {
108 char params[] = "key0=value0;key1=value1;";
109 char key0[] = "key0";
110 char value0[] = "value0";
111 char key1[] = "key1";
112 char value1[] = "value1";
113 map = hash_map_utils_new_from_string_params(params);
114 EXPECT_EQ(2u, hash_map_size(map));
115 EXPECT_EQ(0, strcmp(value0, (char *)hash_map_get(map, key0)));
116 EXPECT_EQ(0, strcmp(value1, (char *)hash_map_get(map, key1)));
119 TEST_F(HashMapUtilsTest, test_dump_null_map) {
120 hash_map_t *map = NULL;
121 hash_map_utils_dump_string_keys_string_values(map);