blob: 6497fda525c14b785fa67744b75f2b7bb5718ce5 [file] [log] [blame]
zsteinf42cc9d2017-03-27 16:17:19 -07001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/ptr_util.h"
zsteinf42cc9d2017-03-27 16:17:19 -070012
13#include <stddef.h>
14#include <string>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/gunit.h"
zsteinf42cc9d2017-03-27 16:17:19 -070017
18namespace rtc {
19
20namespace {
21
22class 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
33size_t DeleteCounter::count_ = 0;
34
35} // namespace
36
37TEST(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
47TEST(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
55TEST(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
61TEST(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