Chris Masone | 27bf103 | 2011-06-28 17:02:01 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 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/property_accessor.h" |
| 6 | |
| 7 | #include <limits> |
| 8 | #include <map> |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include <base/basictypes.h> |
| 13 | #include <gtest/gtest.h> |
| 14 | #include <gmock/gmock.h> |
| 15 | |
| 16 | using std::map; |
| 17 | using std::string; |
| 18 | using std::vector; |
| 19 | using ::testing::Return; |
| 20 | using ::testing::Test; |
| 21 | |
| 22 | namespace shill { |
| 23 | |
| 24 | TEST(PropertyAccessorTest, SignedIntCorrectness) { |
| 25 | int32 int_store = 0; |
| 26 | { |
| 27 | Int32Accessor accessor(new PropertyAccessor<int32>(&int_store)); |
| 28 | EXPECT_EQ(int_store, accessor->Get()); |
| 29 | |
| 30 | int32 expected_int32 = 127; |
| 31 | ASSERT_TRUE(accessor->Set(expected_int32)); |
| 32 | EXPECT_EQ(expected_int32, accessor->Get()); |
| 33 | |
| 34 | int_store = std::numeric_limits<int32>::max(); |
| 35 | EXPECT_EQ(std::numeric_limits<int32>::max(), accessor->Get()); |
| 36 | } |
| 37 | { |
| 38 | Int32Accessor accessor(new ConstPropertyAccessor<int32>(&int_store)); |
| 39 | EXPECT_EQ(int_store, accessor->Get()); |
| 40 | |
| 41 | int32 expected_int32 = 127; |
| 42 | ASSERT_FALSE(accessor->Set(expected_int32)); |
| 43 | EXPECT_EQ(int_store, accessor->Get()); |
| 44 | |
| 45 | int_store = std::numeric_limits<int32>::max(); |
| 46 | EXPECT_EQ(std::numeric_limits<int32>::max(), accessor->Get()); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | TEST(PropertyAccessorTest, UnsignedIntCorrectness) { |
| 51 | uint32 int_store = 0; |
| 52 | { |
| 53 | Uint32Accessor accessor(new PropertyAccessor<uint32>(&int_store)); |
| 54 | EXPECT_EQ(int_store, accessor->Get()); |
| 55 | |
| 56 | uint32 expected_uint32 = 127; |
| 57 | ASSERT_TRUE(accessor->Set(expected_uint32)); |
| 58 | EXPECT_EQ(expected_uint32, accessor->Get()); |
| 59 | |
| 60 | int_store = std::numeric_limits<uint32>::max(); |
| 61 | EXPECT_EQ(std::numeric_limits<uint32>::max(), accessor->Get()); |
| 62 | } |
| 63 | { |
| 64 | Uint32Accessor accessor(new ConstPropertyAccessor<uint32>(&int_store)); |
| 65 | EXPECT_EQ(int_store, accessor->Get()); |
| 66 | |
| 67 | uint32 expected_uint32 = 127; |
| 68 | ASSERT_FALSE(accessor->Set(expected_uint32)); |
| 69 | EXPECT_EQ(int_store, accessor->Get()); |
| 70 | |
| 71 | int_store = std::numeric_limits<uint32>::max(); |
| 72 | EXPECT_EQ(std::numeric_limits<uint32>::max(), accessor->Get()); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | TEST(PropertyAccessorTest, StringCorrectness) { |
| 77 | string storage; |
| 78 | { |
| 79 | StringAccessor accessor(new PropertyAccessor<string>(&storage)); |
| 80 | EXPECT_EQ(storage, accessor->Get()); |
| 81 | |
| 82 | string expected_string("what"); |
| 83 | ASSERT_TRUE(accessor->Set(expected_string)); |
| 84 | EXPECT_EQ(expected_string, accessor->Get()); |
| 85 | |
| 86 | storage = "nooooo"; |
| 87 | EXPECT_EQ(storage, accessor->Get()); |
| 88 | } |
| 89 | { |
| 90 | StringAccessor accessor(new ConstPropertyAccessor<string>(&storage)); |
| 91 | EXPECT_EQ(storage, accessor->Get()); |
| 92 | |
| 93 | string expected_string("what"); |
| 94 | ASSERT_FALSE(accessor->Set(expected_string)); |
| 95 | EXPECT_EQ(storage, accessor->Get()); |
| 96 | |
| 97 | storage = "nooooo"; |
| 98 | EXPECT_EQ(storage, accessor->Get()); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | } // namespace shill |