blob: 7dc846de730bd62db50815c1f459e4ba21be459d [file] [log] [blame]
Darin Petkov7f060332012-03-14 11:46:47 +01001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "shill/key_value_store.h"
6
7#include <gtest/gtest.h>
8
Paul Stewart1062d9d2012-04-27 10:42:27 -07009using std::string;
Paul Stewart3c5e2b32013-06-13 07:49:49 -070010using std::vector;
Darin Petkov7f060332012-03-14 11:46:47 +010011using testing::Test;
12
13namespace shill {
14
15class KeyValueStoreTest : public Test {
16 public:
17 KeyValueStoreTest() {}
18
19 protected:
20 KeyValueStore store_;
21};
22
Paul Stewart1062d9d2012-04-27 10:42:27 -070023TEST_F(KeyValueStoreTest, Bool) {
24 const string kKey("foo");
25 const bool kDefaultValue = true;
26 const bool kValue = false;
27 EXPECT_FALSE(store_.ContainsBool(kKey));
28 EXPECT_EQ(kDefaultValue, store_.LookupBool(kKey, kDefaultValue));
29 store_.SetBool(kKey, kValue);
30 EXPECT_TRUE(store_.ContainsBool(kKey));
Han Shenfc349252012-08-30 11:36:04 -070031 // TODO: investigate if a newer version of gtest handles EXPECT_EQ for bools
32 // in a manner that gcc 4.7 is happy with. (Inproper conversion from "false"
33 // to "NULL".)
34 EXPECT_EQ(static_cast<int>(kValue),
35 static_cast<int>(store_.LookupBool(kKey, kDefaultValue)));
36 EXPECT_EQ(static_cast<int>(kValue),
37 static_cast<int>(store_.GetBool(kKey)));
Darin Petkovb536a742012-04-26 11:31:28 +020038}
39
Paul Stewart1062d9d2012-04-27 10:42:27 -070040TEST_F(KeyValueStoreTest, Int) {
41 const string kKey("foo");
42 const int kValue = 456;
43 EXPECT_FALSE(store_.ContainsInt(kKey));
44 store_.SetInt(kKey, kValue);
45 EXPECT_TRUE(store_.ContainsInt(kKey));
46 EXPECT_EQ(kValue, store_.GetInt(kKey));
47 store_.RemoveInt(kKey);
48 EXPECT_FALSE(store_.ContainsInt(kKey));
Darin Petkov7f060332012-03-14 11:46:47 +010049}
50
Paul Stewart1062d9d2012-04-27 10:42:27 -070051TEST_F(KeyValueStoreTest, String) {
52 const string kKey("foo");
53 const string kDefaultValue("bar");
54 const string kValue("baz");
55 EXPECT_FALSE(store_.ContainsString(kKey));
56 EXPECT_EQ(kDefaultValue, store_.LookupString(kKey, kDefaultValue));
57 store_.SetString(kKey, kValue);
58 EXPECT_TRUE(store_.ContainsString(kKey));
59 EXPECT_EQ(kValue, store_.LookupString(kKey, kDefaultValue));
60 EXPECT_EQ(kValue, store_.GetString(kKey));
Paul Stewart88125fb2012-03-26 07:13:51 -070061 store_.RemoveString(kKey);
Paul Stewart1062d9d2012-04-27 10:42:27 -070062 EXPECT_FALSE(store_.ContainsString(kKey));
63 EXPECT_EQ(kDefaultValue, store_.LookupString(kKey, kDefaultValue));
64}
65
Paul Stewart3c5e2b32013-06-13 07:49:49 -070066TEST_F(KeyValueStoreTest, Strings) {
67 const string kKey("foo");
68 const vector<string> kValue{ "baz0", "baz1", "baz2" };
69 EXPECT_FALSE(store_.ContainsStrings(kKey));
70 store_.SetStrings(kKey, kValue);
71 EXPECT_TRUE(store_.ContainsStrings(kKey));
72 EXPECT_EQ(kValue, store_.GetStrings(kKey));
73 store_.RemoveStrings(kKey);
74 EXPECT_FALSE(store_.ContainsStrings(kKey));
75}
76
Paul Stewart1062d9d2012-04-27 10:42:27 -070077TEST_F(KeyValueStoreTest, Uint) {
78 const string kKey("foo");
Paul Stewartdef189e2012-08-02 20:12:09 -070079 const uint32 kValue = 456;
Paul Stewart1062d9d2012-04-27 10:42:27 -070080 EXPECT_FALSE(store_.ContainsUint(kKey));
81 store_.SetUint(kKey, kValue);
82 EXPECT_TRUE(store_.ContainsUint(kKey));
83 EXPECT_EQ(kValue, store_.GetUint(kKey));
84}
85
86TEST_F(KeyValueStoreTest, DoubleRemove) {
87 const string kKey("foo");
Paul Stewart88125fb2012-03-26 07:13:51 -070088 // Make sure we don't get an exception/infinite loop if we do a
Paul Stewart1062d9d2012-04-27 10:42:27 -070089 // "Remove()" when the key does not exist.
90 store_.RemoveInt(kKey);
91 store_.RemoveInt(kKey);
92 store_.RemoveString(kKey);
Paul Stewart88125fb2012-03-26 07:13:51 -070093 store_.RemoveString(kKey);
94}
95
Paul Stewartdef189e2012-08-02 20:12:09 -070096TEST_F(KeyValueStoreTest, Clear) {
97 const string kBoolKey("foo");
98 const bool kBoolValue = true;
99 store_.SetBool(kBoolKey, kBoolValue);
100 const string kIntKey("bar");
101 const int kIntValue = 123;
102 store_.SetInt(kIntKey, kIntValue);
103 const string kStringKey("baz");
104 const string kStringValue("string");
105 store_.SetString(kStringKey, kStringValue);
106 const string kUintKey("bun");
107 const uint32 kUintValue = 456;
108 store_.SetUint(kUintKey, kUintValue);
109
110 EXPECT_TRUE(store_.ContainsBool(kBoolKey));
111 EXPECT_TRUE(store_.ContainsInt(kIntKey));
112 EXPECT_TRUE(store_.ContainsString(kStringKey));
113 EXPECT_TRUE(store_.ContainsUint(kUintKey));
114 store_.Clear();
115 EXPECT_FALSE(store_.ContainsBool(kBoolKey));
116 EXPECT_FALSE(store_.ContainsInt(kIntKey));
117 EXPECT_FALSE(store_.ContainsString(kStringKey));
118 EXPECT_FALSE(store_.ContainsUint(kUintKey));
119}
120
Paul Stewartd2e1c362013-03-03 19:06:07 -0800121TEST_F(KeyValueStoreTest, CopyFrom) {
122 KeyValueStore donor;
123 const string kBoolKey("foo");
124 const bool kBoolValue = true;
125 donor.SetBool(kBoolKey, kBoolValue);
126 const string kIntKey("bar");
127 const int kIntValue = 123;
128 donor.SetInt(kIntKey, kIntValue);
129 const string kStringKey("baz");
130 const string kStringValue("string");
131 donor.SetString(kStringKey, kStringValue);
132 const string kUintKey("bun");
133 const uint32 kUintValue = 456;
134 donor.SetUint(kUintKey, kUintValue);
135
136 EXPECT_FALSE(store_.ContainsBool(kBoolKey));
137 EXPECT_FALSE(store_.ContainsInt(kIntKey));
138 EXPECT_FALSE(store_.ContainsString(kStringKey));
139 EXPECT_FALSE(store_.ContainsUint(kUintKey));
140 store_.CopyFrom(donor);
141 EXPECT_TRUE(store_.ContainsBool(kBoolKey));
142 EXPECT_EQ(kBoolValue, store_.GetBool(kBoolKey));
143 EXPECT_TRUE(store_.ContainsInt(kIntKey));
144 EXPECT_EQ(kIntValue, store_.GetInt(kIntKey));
145 EXPECT_TRUE(store_.ContainsString(kStringKey));
146 EXPECT_EQ(kStringValue, store_.GetString(kStringKey));
147 EXPECT_TRUE(store_.ContainsUint(kUintKey));
148 EXPECT_EQ(kUintValue, store_.GetUint(kUintKey));
149}
Darin Petkov7f060332012-03-14 11:46:47 +0100150} // namespace shill