OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/sync/base/proto_value_ptr.h" | 5 #include "components/sync/base/proto_value_ptr.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 | 13 |
13 namespace syncer { | 14 namespace syncer { |
14 | 15 |
15 namespace { | 16 namespace { |
16 | 17 |
17 // Simple test struct that wraps an integer | 18 // Simple test struct that wraps an integer |
18 struct IntValue { | 19 struct IntValue { |
19 public: | 20 public: |
20 explicit IntValue(int value) { value_ = value; } | 21 explicit IntValue(int value) { value_ = value; } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 // The following 4 functions are expected by the traits struct to exist | 53 // The following 4 functions are expected by the traits struct to exist |
53 // in this class. | 54 // in this class. |
54 void CopyFrom(const TestValue& from) { | 55 void CopyFrom(const TestValue& from) { |
55 // Expected to always copy from an initialized instance | 56 // Expected to always copy from an initialized instance |
56 // to an uninitialized one. | 57 // to an uninitialized one. |
57 // Not expected either value to be default. | 58 // Not expected either value to be default. |
58 ASSERT_FALSE(is_initialized()); | 59 ASSERT_FALSE(is_initialized()); |
59 ASSERT_FALSE(is_default()); | 60 ASSERT_FALSE(is_default()); |
60 ASSERT_TRUE(from.is_initialized()); | 61 ASSERT_TRUE(from.is_initialized()); |
61 ASSERT_FALSE(from.is_default()); | 62 ASSERT_FALSE(from.is_default()); |
62 value_.reset(new IntValue(from.value())); | 63 value_ = base::MakeUnique<IntValue>(from.value()); |
63 g_copy_count++; | 64 g_copy_count++; |
64 } | 65 } |
65 | 66 |
66 void Swap(TestValue* src) { | 67 void Swap(TestValue* src) { |
67 // Expected to always swap with an initialized instance. | 68 // Expected to always swap with an initialized instance. |
68 // The current instance must always be an uninitialized one. | 69 // The current instance must always be an uninitialized one. |
69 // Not expected either value to be default. | 70 // Not expected either value to be default. |
70 ASSERT_FALSE(is_initialized()); | 71 ASSERT_FALSE(is_initialized()); |
71 ASSERT_FALSE(is_default()); | 72 ASSERT_FALSE(is_default()); |
72 ASSERT_TRUE(src->is_initialized()); | 73 ASSERT_TRUE(src->is_initialized()); |
73 ASSERT_FALSE(src->is_default()); | 74 ASSERT_FALSE(src->is_default()); |
74 // Not exactly swap, but good enough for the test. | 75 // Not exactly swap, but good enough for the test. |
75 value_ = std::move(src->value_); | 76 value_ = std::move(src->value_); |
76 } | 77 } |
77 | 78 |
78 void ParseFromArray(const void* blob, int length) { | 79 void ParseFromArray(const void* blob, int length) { |
79 // Similarly to CopyFrom this is expected to be called on | 80 // Similarly to CopyFrom this is expected to be called on |
80 // an uninitialized instance. | 81 // an uninitialized instance. |
81 ASSERT_FALSE(is_initialized()); | 82 ASSERT_FALSE(is_initialized()); |
82 ASSERT_FALSE(is_default()); | 83 ASSERT_FALSE(is_default()); |
83 // The blob is an address of an integer | 84 // The blob is an address of an integer |
84 ASSERT_EQ(static_cast<int>(sizeof(int)), length); | 85 ASSERT_EQ(static_cast<int>(sizeof(int)), length); |
85 value_.reset(new IntValue(*static_cast<const int*>(blob))); | 86 value_ = base::MakeUnique<IntValue>(*static_cast<const int*>(blob)); |
86 g_parse_count++; | 87 g_parse_count++; |
87 } | 88 } |
88 | 89 |
89 int ByteSize() const { return is_initialized() ? sizeof(int) : 0; } | 90 int ByteSize() const { return is_initialized() ? sizeof(int) : 0; } |
90 | 91 |
91 static const TestValue& default_instance() { | 92 static const TestValue& default_instance() { |
92 static TestValue default_instance; | 93 static TestValue default_instance; |
93 default_instance.is_default_ = true; | 94 default_instance.is_default_ = true; |
94 return default_instance; | 95 return default_instance; |
95 } | 96 } |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 EXPECT_EQ(1, TestValue::parse_count()); | 240 EXPECT_EQ(1, TestValue::parse_count()); |
240 EXPECT_EQ(0, TestValue::copy_count()); | 241 EXPECT_EQ(0, TestValue::copy_count()); |
241 | 242 |
242 EXPECT_EQ(v1, ptr1->value()); | 243 EXPECT_EQ(v1, ptr1->value()); |
243 } | 244 } |
244 | 245 |
245 EXPECT_EQ(1, TestValue::delete_count()); | 246 EXPECT_EQ(1, TestValue::delete_count()); |
246 } | 247 } |
247 | 248 |
248 } // namespace syncer | 249 } // namespace syncer |
OLD | NEW |