1 /******************************************************************************
2  *
3  *  Copyright 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 "osi/include/hash_map_utils.h"
19 
20 #include <gtest/gtest.h>
21 
22 #include <cstring>
23 
24 #include "osi/include/allocator.h"
25 
26 class HashMapUtilsTest : public ::testing::Test {
27 protected:
TearDown()28   void TearDown() override { map.clear(); }
29 
30   std::unordered_map<std::string, std::string> map;
31 };
32 
TEST_F(HashMapUtilsTest,test_empty_string_params)33 TEST_F(HashMapUtilsTest, test_empty_string_params) {
34   char params[] = "";
35   map = hash_map_utils_new_from_string_params(params);
36   EXPECT_TRUE(map.empty());
37 }
38 
TEST_F(HashMapUtilsTest,test_semicolons)39 TEST_F(HashMapUtilsTest, test_semicolons) {
40   char params[] = ";;;";
41   map = hash_map_utils_new_from_string_params(params);
42   EXPECT_TRUE(map.empty());
43 }
44 
TEST_F(HashMapUtilsTest,test_equal_sign_in_value)45 TEST_F(HashMapUtilsTest, test_equal_sign_in_value) {
46   char params[] = "keyOfSomething=value=OfSomething";
47   char key[] = "keyOfSomething";
48   char value[] = "value=OfSomething";
49   map = hash_map_utils_new_from_string_params(params);
50   EXPECT_EQ(1u, map.size());
51   EXPECT_EQ(value, map[key]);
52   map.clear();
53 }
54 
TEST_F(HashMapUtilsTest,test_two_pairs_with_same_key)55 TEST_F(HashMapUtilsTest, test_two_pairs_with_same_key) {
56   char params[] = "key=valu0;key=value1";
57   char key[] = "key";
58   char value1[] = "value1";
59   map = hash_map_utils_new_from_string_params(params);
60   EXPECT_EQ(1u, map.size());
61   EXPECT_EQ(value1, map[key]);
62 }
63 
TEST_F(HashMapUtilsTest,test_one_key_value_pair_without_semicolon)64 TEST_F(HashMapUtilsTest, test_one_key_value_pair_without_semicolon) {
65   char params[] = "keyOfSomething=valueOfSomething";
66   char key[] = "keyOfSomething";
67   char value[] = "valueOfSomething";
68   map = hash_map_utils_new_from_string_params(params);
69   EXPECT_EQ(1u, map.size());
70   EXPECT_EQ(value, map[key]);
71 }
72 
TEST_F(HashMapUtilsTest,test_one_key_value_pair_with_semicolon)73 TEST_F(HashMapUtilsTest, test_one_key_value_pair_with_semicolon) {
74   char params[] = "keyOfSomething=valueOfSomething;";
75   char key[] = "keyOfSomething";
76   char value[] = "valueOfSomething";
77   map = hash_map_utils_new_from_string_params(params);
78   EXPECT_EQ(1u, map.size());
79   EXPECT_EQ(value, map[key]);
80 }
81 
TEST_F(HashMapUtilsTest,test_one_pair_with_empty_value)82 TEST_F(HashMapUtilsTest, test_one_pair_with_empty_value) {
83   char params[] = "keyOfSomething=;";
84   char key[] = "keyOfSomething";
85   char value[] = "";
86   map = hash_map_utils_new_from_string_params(params);
87   EXPECT_EQ(1u, map.size());
88   EXPECT_EQ(value, map[key]);
89 }
90 
TEST_F(HashMapUtilsTest,test_one_pair_with_empty_key)91 TEST_F(HashMapUtilsTest, test_one_pair_with_empty_key) {
92   char params[] = "=valueOfSomething;";
93   map = hash_map_utils_new_from_string_params(params);
94   EXPECT_TRUE(map.empty());
95 }
96 
TEST_F(HashMapUtilsTest,test_two_key_value_pairs)97 TEST_F(HashMapUtilsTest, test_two_key_value_pairs) {
98   char params[] = "key0=value0;key1=value1;";
99   char key0[] = "key0";
100   char value0[] = "value0";
101   char key1[] = "key1";
102   char value1[] = "value1";
103   map = hash_map_utils_new_from_string_params(params);
104   EXPECT_EQ(2u, map.size());
105   EXPECT_EQ(value0, map[key0]);
106   EXPECT_EQ(value1, map[key1]);
107 }
108