Darin Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 1 | // 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 Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 9 | using std::string; |
Darin Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 10 | using testing::Test; |
| 11 | |
| 12 | namespace shill { |
| 13 | |
| 14 | class KeyValueStoreTest : public Test { |
| 15 | public: |
| 16 | KeyValueStoreTest() {} |
| 17 | |
| 18 | protected: |
| 19 | KeyValueStore store_; |
| 20 | }; |
| 21 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 22 | TEST_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 Shen | fc34925 | 2012-08-30 11:36:04 -0700 | [diff] [blame] | 30 | // 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 Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 37 | } |
| 38 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 39 | TEST_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 Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 48 | } |
| 49 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 50 | TEST_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 Stewart | 88125fb | 2012-03-26 07:13:51 -0700 | [diff] [blame] | 60 | store_.RemoveString(kKey); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 61 | EXPECT_FALSE(store_.ContainsString(kKey)); |
| 62 | EXPECT_EQ(kDefaultValue, store_.LookupString(kKey, kDefaultValue)); |
| 63 | } |
| 64 | |
| 65 | TEST_F(KeyValueStoreTest, Uint) { |
| 66 | const string kKey("foo"); |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 67 | const uint32 kValue = 456; |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 68 | 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 | |
| 74 | TEST_F(KeyValueStoreTest, DoubleRemove) { |
| 75 | const string kKey("foo"); |
Paul Stewart | 88125fb | 2012-03-26 07:13:51 -0700 | [diff] [blame] | 76 | // Make sure we don't get an exception/infinite loop if we do a |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 77 | // "Remove()" when the key does not exist. |
| 78 | store_.RemoveInt(kKey); |
| 79 | store_.RemoveInt(kKey); |
| 80 | store_.RemoveString(kKey); |
Paul Stewart | 88125fb | 2012-03-26 07:13:51 -0700 | [diff] [blame] | 81 | store_.RemoveString(kKey); |
| 82 | } |
| 83 | |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 84 | TEST_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 | |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 109 | TEST_F(KeyValueStoreTest, CopyFrom) { |
| 110 | KeyValueStore donor; |
| 111 | const string kBoolKey("foo"); |
| 112 | const bool kBoolValue = true; |
| 113 | donor.SetBool(kBoolKey, kBoolValue); |
| 114 | const string kIntKey("bar"); |
| 115 | const int kIntValue = 123; |
| 116 | donor.SetInt(kIntKey, kIntValue); |
| 117 | const string kStringKey("baz"); |
| 118 | const string kStringValue("string"); |
| 119 | donor.SetString(kStringKey, kStringValue); |
| 120 | const string kUintKey("bun"); |
| 121 | const uint32 kUintValue = 456; |
| 122 | donor.SetUint(kUintKey, kUintValue); |
| 123 | |
| 124 | EXPECT_FALSE(store_.ContainsBool(kBoolKey)); |
| 125 | EXPECT_FALSE(store_.ContainsInt(kIntKey)); |
| 126 | EXPECT_FALSE(store_.ContainsString(kStringKey)); |
| 127 | EXPECT_FALSE(store_.ContainsUint(kUintKey)); |
| 128 | store_.CopyFrom(donor); |
| 129 | EXPECT_TRUE(store_.ContainsBool(kBoolKey)); |
| 130 | EXPECT_EQ(kBoolValue, store_.GetBool(kBoolKey)); |
| 131 | EXPECT_TRUE(store_.ContainsInt(kIntKey)); |
| 132 | EXPECT_EQ(kIntValue, store_.GetInt(kIntKey)); |
| 133 | EXPECT_TRUE(store_.ContainsString(kStringKey)); |
| 134 | EXPECT_EQ(kStringValue, store_.GetString(kStringKey)); |
| 135 | EXPECT_TRUE(store_.ContainsUint(kUintKey)); |
| 136 | EXPECT_EQ(kUintValue, store_.GetUint(kUintKey)); |
| 137 | } |
Darin Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 138 | } // namespace shill |