summaryrefslogtreecommitdiff
path: root/system/osi/test/hash_map_utils_test.cc
blob: d5df2d774d6b5beb3f479eeb4f84f4c8634e8260 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/******************************************************************************
 *
 *  Copyright 2015 Google, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/
#include <cstring>

#include <gtest/gtest.h>

#include "AllocationTestHarness.h"

#include "osi/include/hash_map_utils.h"

#include "osi/include/allocator.h"

class HashMapUtilsTest : public AllocationTestHarness {
 protected:
  void SetUp() override { AllocationTestHarness::SetUp(); }
  void TearDown() override {
    map.clear();
    AllocationTestHarness::TearDown();
  }

  std::unordered_map<std::string, std::string> map;
};

TEST_F(HashMapUtilsTest, test_empty_string_params) {
  char params[] = "";
  map = hash_map_utils_new_from_string_params(params);
  EXPECT_TRUE(map.empty());
}

TEST_F(HashMapUtilsTest, test_semicolons) {
  char params[] = ";;;";
  map = hash_map_utils_new_from_string_params(params);
  EXPECT_TRUE(map.empty());
}

TEST_F(HashMapUtilsTest, test_equal_sign_in_value) {
  char params[] = "keyOfSomething=value=OfSomething";
  char key[] = "keyOfSomething";
  char value[] = "value=OfSomething";
  map = hash_map_utils_new_from_string_params(params);
  EXPECT_EQ(1u, map.size());
  EXPECT_EQ(value, map[key]);
  map.clear();
}

TEST_F(HashMapUtilsTest, test_two_pairs_with_same_key) {
  char params[] = "key=valu0;key=value1";
  char key[] = "key";
  char value1[] = "value1";
  map = hash_map_utils_new_from_string_params(params);
  EXPECT_EQ(1u, map.size());
  EXPECT_EQ(value1, map[key]);
}

TEST_F(HashMapUtilsTest, test_one_key_value_pair_without_semicolon) {
  char params[] = "keyOfSomething=valueOfSomething";
  char key[] = "keyOfSomething";
  char value[] = "valueOfSomething";
  map = hash_map_utils_new_from_string_params(params);
  EXPECT_EQ(1u, map.size());
  EXPECT_EQ(value, map[key]);
}

TEST_F(HashMapUtilsTest, test_one_key_value_pair_with_semicolon) {
  char params[] = "keyOfSomething=valueOfSomething;";
  char key[] = "keyOfSomething";
  char value[] = "valueOfSomething";
  map = hash_map_utils_new_from_string_params(params);
  EXPECT_EQ(1u, map.size());
  EXPECT_EQ(value, map[key]);
}

TEST_F(HashMapUtilsTest, test_one_pair_with_empty_value) {
  char params[] = "keyOfSomething=;";
  char key[] = "keyOfSomething";
  char value[] = "";
  map = hash_map_utils_new_from_string_params(params);
  EXPECT_EQ(1u, map.size());
  EXPECT_EQ(value, map[key]);
}

TEST_F(HashMapUtilsTest, test_one_pair_with_empty_key) {
  char params[] = "=valueOfSomething;";
  map = hash_map_utils_new_from_string_params(params);
  EXPECT_TRUE(map.empty());
}

TEST_F(HashMapUtilsTest, test_two_key_value_pairs) {
  char params[] = "key0=value0;key1=value1;";
  char key0[] = "key0";
  char value0[] = "value0";
  char key1[] = "key1";
  char value1[] = "value1";
  map = hash_map_utils_new_from_string_params(params);
  EXPECT_EQ(2u, map.size());
  EXPECT_EQ(value0, map[key0]);
  EXPECT_EQ(value1, map[key1]);
}