zstein | 5dd6166 | 2017-03-27 23:17:19 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
kjellander | 1979696 | 2017-06-30 17:45:21 | [diff] [blame] | 11 | #include "webrtc/rtc_base/ptr_util.h" |
zstein | 5dd6166 | 2017-03-27 23:17:19 | [diff] [blame] | 12 | |
| 13 | #include <stddef.h> |
| 14 | #include <string> |
| 15 | |
kjellander | 1979696 | 2017-06-30 17:45:21 | [diff] [blame] | 16 | #include "webrtc/rtc_base/gunit.h" |
zstein | 5dd6166 | 2017-03-27 23:17:19 | [diff] [blame] | 17 | |
| 18 | namespace rtc { |
| 19 | |
| 20 | namespace { |
| 21 | |
| 22 | class DeleteCounter { |
| 23 | public: |
| 24 | DeleteCounter() { ++count_; } |
| 25 | ~DeleteCounter() { --count_; } |
| 26 | |
| 27 | static size_t count() { return count_; } |
| 28 | |
| 29 | private: |
| 30 | static size_t count_; |
| 31 | }; |
| 32 | |
| 33 | size_t DeleteCounter::count_ = 0; |
| 34 | |
| 35 | } // namespace |
| 36 | |
| 37 | TEST(PtrUtilTest, WrapUnique) { |
| 38 | EXPECT_EQ(0u, DeleteCounter::count()); |
| 39 | DeleteCounter* counter = new DeleteCounter; |
| 40 | EXPECT_EQ(1u, DeleteCounter::count()); |
| 41 | std::unique_ptr<DeleteCounter> owned_counter = WrapUnique(counter); |
| 42 | EXPECT_EQ(1u, DeleteCounter::count()); |
| 43 | owned_counter.reset(); |
| 44 | EXPECT_EQ(0u, DeleteCounter::count()); |
| 45 | } |
| 46 | |
| 47 | TEST(PtrUtilTest, MakeUniqueScalar) { |
| 48 | auto s = MakeUnique<std::string>(); |
| 49 | EXPECT_EQ("", *s); |
| 50 | |
| 51 | auto s2 = MakeUnique<std::string>("test"); |
| 52 | EXPECT_EQ("test", *s2); |
| 53 | } |
| 54 | |
| 55 | TEST(PtrUtilTest, MakeUniqueScalarWithMoveOnlyType) { |
| 56 | using MoveOnly = std::unique_ptr<std::string>; |
| 57 | auto p = MakeUnique<MoveOnly>(MakeUnique<std::string>("test")); |
| 58 | EXPECT_EQ("test", **p); |
| 59 | } |
| 60 | |
| 61 | TEST(PtrUtilTest, MakeUniqueArray) { |
| 62 | EXPECT_EQ(0u, DeleteCounter::count()); |
| 63 | auto a = MakeUnique<DeleteCounter[]>(5); |
| 64 | EXPECT_EQ(5u, DeleteCounter::count()); |
| 65 | a.reset(); |
| 66 | EXPECT_EQ(0u, DeleteCounter::count()); |
| 67 | } |
| 68 | |
| 69 | } // namespace rtc |