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)); |
| 30 | EXPECT_EQ(kValue, store_.LookupBool(kKey, kDefaultValue)); |
| 31 | EXPECT_EQ(kValue, store_.GetBool(kKey)); |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 32 | } |
| 33 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 34 | TEST_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 Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 43 | } |
| 44 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 45 | TEST_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 Stewart | 88125fb | 2012-03-26 07:13:51 -0700 | [diff] [blame] | 55 | store_.RemoveString(kKey); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 56 | EXPECT_FALSE(store_.ContainsString(kKey)); |
| 57 | EXPECT_EQ(kDefaultValue, store_.LookupString(kKey, kDefaultValue)); |
| 58 | } |
| 59 | |
| 60 | TEST_F(KeyValueStoreTest, Uint) { |
| 61 | const string kKey("foo"); |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 62 | const uint32 kValue = 456; |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 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 | |
| 69 | TEST_F(KeyValueStoreTest, DoubleRemove) { |
| 70 | const string kKey("foo"); |
Paul Stewart | 88125fb | 2012-03-26 07:13:51 -0700 | [diff] [blame] | 71 | // 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] | 72 | // "Remove()" when the key does not exist. |
| 73 | store_.RemoveInt(kKey); |
| 74 | store_.RemoveInt(kKey); |
| 75 | store_.RemoveString(kKey); |
Paul Stewart | 88125fb | 2012-03-26 07:13:51 -0700 | [diff] [blame] | 76 | store_.RemoveString(kKey); |
| 77 | } |
| 78 | |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 79 | TEST_F(KeyValueStoreTest, Clear) { |
| 80 | const string kBoolKey("foo"); |
| 81 | const bool kBoolValue = true; |
| 82 | store_.SetBool(kBoolKey, kBoolValue); |
| 83 | const string kIntKey("bar"); |
| 84 | const int kIntValue = 123; |
| 85 | store_.SetInt(kIntKey, kIntValue); |
| 86 | const string kStringKey("baz"); |
| 87 | const string kStringValue("string"); |
| 88 | store_.SetString(kStringKey, kStringValue); |
| 89 | const string kUintKey("bun"); |
| 90 | const uint32 kUintValue = 456; |
| 91 | store_.SetUint(kUintKey, kUintValue); |
| 92 | |
| 93 | EXPECT_TRUE(store_.ContainsBool(kBoolKey)); |
| 94 | EXPECT_TRUE(store_.ContainsInt(kIntKey)); |
| 95 | EXPECT_TRUE(store_.ContainsString(kStringKey)); |
| 96 | EXPECT_TRUE(store_.ContainsUint(kUintKey)); |
| 97 | store_.Clear(); |
| 98 | EXPECT_FALSE(store_.ContainsBool(kBoolKey)); |
| 99 | EXPECT_FALSE(store_.ContainsInt(kIntKey)); |
| 100 | EXPECT_FALSE(store_.ContainsString(kStringKey)); |
| 101 | EXPECT_FALSE(store_.ContainsUint(kUintKey)); |
| 102 | } |
| 103 | |
Darin Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 104 | } // namespace shill |