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 | |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame^] | 16 | #include "shill/error.h" |
| 17 | |
Chris Masone | 27bf103 | 2011-06-28 17:02:01 -0700 | [diff] [blame] | 18 | using std::map; |
| 19 | using std::string; |
| 20 | using std::vector; |
| 21 | using ::testing::Return; |
| 22 | using ::testing::Test; |
| 23 | |
| 24 | namespace shill { |
| 25 | |
| 26 | TEST(PropertyAccessorTest, SignedIntCorrectness) { |
| 27 | int32 int_store = 0; |
| 28 | { |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame^] | 29 | Error error; |
Chris Masone | 27bf103 | 2011-06-28 17:02:01 -0700 | [diff] [blame] | 30 | Int32Accessor accessor(new PropertyAccessor<int32>(&int_store)); |
| 31 | EXPECT_EQ(int_store, accessor->Get()); |
| 32 | |
| 33 | int32 expected_int32 = 127; |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame^] | 34 | accessor->Set(expected_int32, &error); |
| 35 | ASSERT_TRUE(error.IsSuccess()); |
Chris Masone | 27bf103 | 2011-06-28 17:02:01 -0700 | [diff] [blame] | 36 | EXPECT_EQ(expected_int32, accessor->Get()); |
| 37 | |
| 38 | int_store = std::numeric_limits<int32>::max(); |
| 39 | EXPECT_EQ(std::numeric_limits<int32>::max(), accessor->Get()); |
| 40 | } |
| 41 | { |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame^] | 42 | Error error; |
Chris Masone | 27bf103 | 2011-06-28 17:02:01 -0700 | [diff] [blame] | 43 | Int32Accessor accessor(new ConstPropertyAccessor<int32>(&int_store)); |
| 44 | EXPECT_EQ(int_store, accessor->Get()); |
| 45 | |
| 46 | int32 expected_int32 = 127; |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame^] | 47 | accessor->Set(expected_int32, &error); |
| 48 | ASSERT_FALSE(error.IsSuccess()); |
| 49 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
Chris Masone | 27bf103 | 2011-06-28 17:02:01 -0700 | [diff] [blame] | 50 | EXPECT_EQ(int_store, accessor->Get()); |
| 51 | |
| 52 | int_store = std::numeric_limits<int32>::max(); |
| 53 | EXPECT_EQ(std::numeric_limits<int32>::max(), accessor->Get()); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | TEST(PropertyAccessorTest, UnsignedIntCorrectness) { |
| 58 | uint32 int_store = 0; |
| 59 | { |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame^] | 60 | Error error; |
Chris Masone | 27bf103 | 2011-06-28 17:02:01 -0700 | [diff] [blame] | 61 | Uint32Accessor accessor(new PropertyAccessor<uint32>(&int_store)); |
| 62 | EXPECT_EQ(int_store, accessor->Get()); |
| 63 | |
| 64 | uint32 expected_uint32 = 127; |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame^] | 65 | accessor->Set(expected_uint32, &error); |
| 66 | ASSERT_TRUE(error.IsSuccess()); |
Chris Masone | 27bf103 | 2011-06-28 17:02:01 -0700 | [diff] [blame] | 67 | EXPECT_EQ(expected_uint32, accessor->Get()); |
| 68 | |
| 69 | int_store = std::numeric_limits<uint32>::max(); |
| 70 | EXPECT_EQ(std::numeric_limits<uint32>::max(), accessor->Get()); |
| 71 | } |
| 72 | { |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame^] | 73 | Error error; |
Chris Masone | 27bf103 | 2011-06-28 17:02:01 -0700 | [diff] [blame] | 74 | Uint32Accessor accessor(new ConstPropertyAccessor<uint32>(&int_store)); |
| 75 | EXPECT_EQ(int_store, accessor->Get()); |
| 76 | |
| 77 | uint32 expected_uint32 = 127; |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame^] | 78 | accessor->Set(expected_uint32, &error); |
| 79 | ASSERT_FALSE(error.IsSuccess()); |
| 80 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
Chris Masone | 27bf103 | 2011-06-28 17:02:01 -0700 | [diff] [blame] | 81 | EXPECT_EQ(int_store, accessor->Get()); |
| 82 | |
| 83 | int_store = std::numeric_limits<uint32>::max(); |
| 84 | EXPECT_EQ(std::numeric_limits<uint32>::max(), accessor->Get()); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | TEST(PropertyAccessorTest, StringCorrectness) { |
| 89 | string storage; |
| 90 | { |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame^] | 91 | Error error; |
Chris Masone | 27bf103 | 2011-06-28 17:02:01 -0700 | [diff] [blame] | 92 | StringAccessor accessor(new PropertyAccessor<string>(&storage)); |
| 93 | EXPECT_EQ(storage, accessor->Get()); |
| 94 | |
| 95 | string expected_string("what"); |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame^] | 96 | accessor->Set(expected_string, &error); |
| 97 | ASSERT_TRUE(error.IsSuccess()); |
Chris Masone | 27bf103 | 2011-06-28 17:02:01 -0700 | [diff] [blame] | 98 | EXPECT_EQ(expected_string, accessor->Get()); |
| 99 | |
| 100 | storage = "nooooo"; |
| 101 | EXPECT_EQ(storage, accessor->Get()); |
| 102 | } |
| 103 | { |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame^] | 104 | Error error; |
Chris Masone | 27bf103 | 2011-06-28 17:02:01 -0700 | [diff] [blame] | 105 | StringAccessor accessor(new ConstPropertyAccessor<string>(&storage)); |
| 106 | EXPECT_EQ(storage, accessor->Get()); |
| 107 | |
| 108 | string expected_string("what"); |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame^] | 109 | accessor->Set(expected_string, &error); |
| 110 | ASSERT_FALSE(error.IsSuccess()); |
| 111 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
Chris Masone | 27bf103 | 2011-06-28 17:02:01 -0700 | [diff] [blame] | 112 | EXPECT_EQ(storage, accessor->Get()); |
| 113 | |
| 114 | storage = "nooooo"; |
| 115 | EXPECT_EQ(storage, accessor->Get()); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | } // namespace shill |