1 /******************************************************************************
2 *
3 * Copyright (C) 2015 Google, Inc.
4 *
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:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 *
17 ******************************************************************************/
18 #include <cstring>
19
20 #include <gtest/gtest.h>
21
22 #include "AllocationTestHarness.h"
23
24 extern "C" {
25 #include "osi/include/hash_map.h"
26 #include "osi/include/hash_map_utils.h"
27 #include "osi/include/allocator.h"
28 }
29
30 class HashMapUtilsTest : public AllocationTestHarness {
31 protected:
SetUp()32 virtual void SetUp() {
33 AllocationTestHarness::SetUp();
34 map = NULL;
35 }
TearDown()36 virtual void TearDown() {
37 hash_map_free(map);
38 AllocationTestHarness::TearDown();
39 }
40
41 hash_map_t *map;
42 };
43
TEST_F(HashMapUtilsTest,test_empty_string_params)44 TEST_F(HashMapUtilsTest, test_empty_string_params) {
45 char params[] = "";
46 map = hash_map_utils_new_from_string_params(params);
47 EXPECT_TRUE(hash_map_is_empty(map));
48 }
49
TEST_F(HashMapUtilsTest,test_semicolons)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));
54 }
55
TEST_F(HashMapUtilsTest,test_equal_sign_in_value)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)));
63 }
64
TEST_F(HashMapUtilsTest,test_two_pairs_with_same_key)65 TEST_F(HashMapUtilsTest, test_two_pairs_with_same_key) {
66 char params[] = "key=valu0;key=value1";
67 char key[] = "key";
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)));
72 }
73
TEST_F(HashMapUtilsTest,test_one_key_value_pair_without_semicolon)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)));
81 }
82
TEST_F(HashMapUtilsTest,test_one_key_value_pair_with_semicolon)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)));
90 }
91
TEST_F(HashMapUtilsTest,test_one_pair_with_empty_value)92 TEST_F(HashMapUtilsTest, test_one_pair_with_empty_value) {
93 char params[] = "keyOfSomething=;";
94 char key[] = "keyOfSomething";
95 char value[] = "";
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)));
99 }
100
TEST_F(HashMapUtilsTest,test_one_pair_with_empty_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));
105 }
106
TEST_F(HashMapUtilsTest,test_two_key_value_pairs)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)));
117 }
118
TEST_F(HashMapUtilsTest,test_dump_null_map)119 TEST_F(HashMapUtilsTest, test_dump_null_map) {
120 hash_map_t *map = NULL;
121 hash_map_utils_dump_string_keys_string_values(map);
122 }
123