| 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 "base/memory/ptr_util.h" | 5 #include "base/memory/ptr_util.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| 11 namespace base { | 11 namespace base { |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 class DeleteCounter { | 15 class DeleteCounter { |
| 16 public: | 16 public: |
| 17 DeleteCounter() { ++count_; } | 17 DeleteCounter() { ++count_; } |
| 18 ~DeleteCounter() { --count_; } | 18 ~DeleteCounter() { --count_; } |
| 19 | 19 |
| 20 static size_t count() { return count_; } | 20 static size_t count() { return count_; } |
| 21 | 21 |
| 22 private: | 22 private: |
| 23 static size_t count_; | 23 static size_t count_; |
| 24 }; | 24 }; |
| 25 | 25 |
| 26 struct SumUpDeleter { |
| 27 explicit SumUpDeleter(int* value) : value_(value) { |
| 28 } |
| 29 |
| 30 void operator()(int* x) { |
| 31 if (x) |
| 32 *value_ += *x; |
| 33 delete x; |
| 34 } |
| 35 |
| 36 int* value_; |
| 37 }; |
| 38 |
| 26 size_t DeleteCounter::count_ = 0; | 39 size_t DeleteCounter::count_ = 0; |
| 27 | 40 |
| 28 } // namespace | 41 } // namespace |
| 29 | 42 |
| 30 TEST(PtrUtilTest, WrapUnique) { | 43 TEST(PtrUtilTest, WrapUnique) { |
| 31 EXPECT_EQ(0u, DeleteCounter::count()); | 44 EXPECT_EQ(0u, DeleteCounter::count()); |
| 32 DeleteCounter* counter = new DeleteCounter; | 45 DeleteCounter* counter = new DeleteCounter; |
| 33 EXPECT_EQ(1u, DeleteCounter::count()); | 46 EXPECT_EQ(1u, DeleteCounter::count()); |
| 34 std::unique_ptr<DeleteCounter> owned_counter = WrapUnique(counter); | 47 std::unique_ptr<DeleteCounter> owned_counter = WrapUnique(counter); |
| 35 EXPECT_EQ(1u, DeleteCounter::count()); | 48 EXPECT_EQ(1u, DeleteCounter::count()); |
| 36 owned_counter.reset(); | 49 owned_counter.reset(); |
| 37 EXPECT_EQ(0u, DeleteCounter::count()); | 50 EXPECT_EQ(0u, DeleteCounter::count()); |
| 38 } | 51 } |
| 39 | 52 |
| 53 TEST(PtrUtilTest, WrapUniqueWithDeleter) { |
| 54 int sum = 0; |
| 55 auto p = WrapUnique(new int(42), SumUpDeleter(&sum)); |
| 56 EXPECT_EQ(0, sum); |
| 57 p = nullptr; |
| 58 EXPECT_EQ(42, sum); |
| 59 } |
| 60 |
| 40 TEST(PtrUtilTest, MakeUniqueScalar) { | 61 TEST(PtrUtilTest, MakeUniqueScalar) { |
| 41 auto s = MakeUnique<std::string>(); | 62 auto s = MakeUnique<std::string>(); |
| 42 EXPECT_EQ("", *s); | 63 EXPECT_EQ("", *s); |
| 43 | 64 |
| 44 auto s2 = MakeUnique<std::string>("test"); | 65 auto s2 = MakeUnique<std::string>("test"); |
| 45 EXPECT_EQ("test", *s2); | 66 EXPECT_EQ("test", *s2); |
| 46 } | 67 } |
| 47 | 68 |
| 48 TEST(PtrUtilTest, MakeUniqueScalarWithMoveOnlyType) { | 69 TEST(PtrUtilTest, MakeUniqueScalarWithMoveOnlyType) { |
| 49 using MoveOnly = std::unique_ptr<std::string>; | 70 using MoveOnly = std::unique_ptr<std::string>; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 61 | 82 |
| 62 #if 0 | 83 #if 0 |
| 63 // TODO(dcheng): Move this into a nocompile test. | 84 // TODO(dcheng): Move this into a nocompile test. |
| 64 TEST(PtrUtilTest, MakeUniqueArrayWithKnownBounds) { | 85 TEST(PtrUtilTest, MakeUniqueArrayWithKnownBounds) { |
| 65 auto a = MakeUnique<DeleteCounter[1]>(); | 86 auto a = MakeUnique<DeleteCounter[1]>(); |
| 66 auto b = MakeUnique<DeleteCounter[1]>(1); | 87 auto b = MakeUnique<DeleteCounter[1]>(1); |
| 67 } | 88 } |
| 68 #endif | 89 #endif |
| 69 | 90 |
| 70 } // namespace base | 91 } // namespace base |
| OLD | NEW |