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