blob: e11cff81f069eadbd9b58a17978ca8fd4b6fa997 [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));
30 EXPECT_EQ(kValue, store_.LookupBool(kKey, kDefaultValue));
31 EXPECT_EQ(kValue, store_.GetBool(kKey));
Darin Petkovb536a742012-04-26 11:31:28 +020032}
33
Paul Stewart1062d9d2012-04-27 10:42:27 -070034TEST_F(KeyValueStoreTest, Int) {
35 const string kKey("foo");
36 const int kValue = 456;
37 EXPECT_FALSE(store_.ContainsInt(kKey));
38 store_.SetInt(kKey, kValue);
39 EXPECT_TRUE(store_.ContainsInt(kKey));
40 EXPECT_EQ(kValue, store_.GetInt(kKey));
41 store_.RemoveInt(kKey);
42 EXPECT_FALSE(store_.ContainsInt(kKey));
Darin Petkov7f060332012-03-14 11:46:47 +010043}
44
Paul Stewart1062d9d2012-04-27 10:42:27 -070045TEST_F(KeyValueStoreTest, String) {
46 const string kKey("foo");
47 const string kDefaultValue("bar");
48 const string kValue("baz");
49 EXPECT_FALSE(store_.ContainsString(kKey));
50 EXPECT_EQ(kDefaultValue, store_.LookupString(kKey, kDefaultValue));
51 store_.SetString(kKey, kValue);
52 EXPECT_TRUE(store_.ContainsString(kKey));
53 EXPECT_EQ(kValue, store_.LookupString(kKey, kDefaultValue));
54 EXPECT_EQ(kValue, store_.GetString(kKey));
Paul Stewart88125fb2012-03-26 07:13:51 -070055 store_.RemoveString(kKey);
Paul Stewart1062d9d2012-04-27 10:42:27 -070056 EXPECT_FALSE(store_.ContainsString(kKey));
57 EXPECT_EQ(kDefaultValue, store_.LookupString(kKey, kDefaultValue));
58}
59
60TEST_F(KeyValueStoreTest, Uint) {
61 const string kKey("foo");
62 const int32 kValue = 456;
63 EXPECT_FALSE(store_.ContainsUint(kKey));
64 store_.SetUint(kKey, kValue);
65 EXPECT_TRUE(store_.ContainsUint(kKey));
66 EXPECT_EQ(kValue, store_.GetUint(kKey));
67}
68
69TEST_F(KeyValueStoreTest, DoubleRemove) {
70 const string kKey("foo");
Paul Stewart88125fb2012-03-26 07:13:51 -070071 // Make sure we don't get an exception/infinite loop if we do a
Paul Stewart1062d9d2012-04-27 10:42:27 -070072 // "Remove()" when the key does not exist.
73 store_.RemoveInt(kKey);
74 store_.RemoveInt(kKey);
75 store_.RemoveString(kKey);
Paul Stewart88125fb2012-03-26 07:13:51 -070076 store_.RemoveString(kKey);
77}
78
Darin Petkov7f060332012-03-14 11:46:47 +010079} // namespace shill