blob: 822d13da613e95da26f74b5f56e7c058640cd774 [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;
Darin Petkov7f060332012-03-14 11:46:47 +010010using testing::Test;
11
12namespace shill {
13
14class KeyValueStoreTest : public Test {
15 public:
16 KeyValueStoreTest() {}
17
18 protected:
19 KeyValueStore store_;
20};
21
Paul Stewart1062d9d2012-04-27 10:42:27 -070022TEST_F(KeyValueStoreTest, Bool) {
23 const string kKey("foo");
24 const bool kDefaultValue = true;
25 const bool kValue = false;
26 EXPECT_FALSE(store_.ContainsBool(kKey));
27 EXPECT_EQ(kDefaultValue, store_.LookupBool(kKey, kDefaultValue));
28 store_.SetBool(kKey, kValue);
29 EXPECT_TRUE(store_.ContainsBool(kKey));
Han Shenfc349252012-08-30 11:36:04 -070030 // TODO: investigate if a newer version of gtest handles EXPECT_EQ for bools
31 // in a manner that gcc 4.7 is happy with. (Inproper conversion from "false"
32 // to "NULL".)
33 EXPECT_EQ(static_cast<int>(kValue),
34 static_cast<int>(store_.LookupBool(kKey, kDefaultValue)));
35 EXPECT_EQ(static_cast<int>(kValue),
36 static_cast<int>(store_.GetBool(kKey)));
Darin Petkovb536a742012-04-26 11:31:28 +020037}
38
Paul Stewart1062d9d2012-04-27 10:42:27 -070039TEST_F(KeyValueStoreTest, Int) {
40 const string kKey("foo");
41 const int kValue = 456;
42 EXPECT_FALSE(store_.ContainsInt(kKey));
43 store_.SetInt(kKey, kValue);
44 EXPECT_TRUE(store_.ContainsInt(kKey));
45 EXPECT_EQ(kValue, store_.GetInt(kKey));
46 store_.RemoveInt(kKey);
47 EXPECT_FALSE(store_.ContainsInt(kKey));
Darin Petkov7f060332012-03-14 11:46:47 +010048}
49
Paul Stewart1062d9d2012-04-27 10:42:27 -070050TEST_F(KeyValueStoreTest, String) {
51 const string kKey("foo");
52 const string kDefaultValue("bar");
53 const string kValue("baz");
54 EXPECT_FALSE(store_.ContainsString(kKey));
55 EXPECT_EQ(kDefaultValue, store_.LookupString(kKey, kDefaultValue));
56 store_.SetString(kKey, kValue);
57 EXPECT_TRUE(store_.ContainsString(kKey));
58 EXPECT_EQ(kValue, store_.LookupString(kKey, kDefaultValue));
59 EXPECT_EQ(kValue, store_.GetString(kKey));
Paul Stewart88125fb2012-03-26 07:13:51 -070060 store_.RemoveString(kKey);
Paul Stewart1062d9d2012-04-27 10:42:27 -070061 EXPECT_FALSE(store_.ContainsString(kKey));
62 EXPECT_EQ(kDefaultValue, store_.LookupString(kKey, kDefaultValue));
63}
64
65TEST_F(KeyValueStoreTest, Uint) {
66 const string kKey("foo");
Paul Stewartdef189e2012-08-02 20:12:09 -070067 const uint32 kValue = 456;
Paul Stewart1062d9d2012-04-27 10:42:27 -070068 EXPECT_FALSE(store_.ContainsUint(kKey));
69 store_.SetUint(kKey, kValue);
70 EXPECT_TRUE(store_.ContainsUint(kKey));
71 EXPECT_EQ(kValue, store_.GetUint(kKey));
72}
73
74TEST_F(KeyValueStoreTest, DoubleRemove) {
75 const string kKey("foo");
Paul Stewart88125fb2012-03-26 07:13:51 -070076 // Make sure we don't get an exception/infinite loop if we do a
Paul Stewart1062d9d2012-04-27 10:42:27 -070077 // "Remove()" when the key does not exist.
78 store_.RemoveInt(kKey);
79 store_.RemoveInt(kKey);
80 store_.RemoveString(kKey);
Paul Stewart88125fb2012-03-26 07:13:51 -070081 store_.RemoveString(kKey);
82}
83
Paul Stewartdef189e2012-08-02 20:12:09 -070084TEST_F(KeyValueStoreTest, Clear) {
85 const string kBoolKey("foo");
86 const bool kBoolValue = true;
87 store_.SetBool(kBoolKey, kBoolValue);
88 const string kIntKey("bar");
89 const int kIntValue = 123;
90 store_.SetInt(kIntKey, kIntValue);
91 const string kStringKey("baz");
92 const string kStringValue("string");
93 store_.SetString(kStringKey, kStringValue);
94 const string kUintKey("bun");
95 const uint32 kUintValue = 456;
96 store_.SetUint(kUintKey, kUintValue);
97
98 EXPECT_TRUE(store_.ContainsBool(kBoolKey));
99 EXPECT_TRUE(store_.ContainsInt(kIntKey));
100 EXPECT_TRUE(store_.ContainsString(kStringKey));
101 EXPECT_TRUE(store_.ContainsUint(kUintKey));
102 store_.Clear();
103 EXPECT_FALSE(store_.ContainsBool(kBoolKey));
104 EXPECT_FALSE(store_.ContainsInt(kIntKey));
105 EXPECT_FALSE(store_.ContainsString(kStringKey));
106 EXPECT_FALSE(store_.ContainsUint(kUintKey));
107}
108
Darin Petkov7f060332012-03-14 11:46:47 +0100109} // namespace shill