blob: 267b110aae404a4c56791b709ebcb0d36b75ef22 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2014 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
jbauch555604a2016-04-26 03:13:22 -070011#include <memory>
12
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013#include "webrtc/base/scopedptrcollection.h"
14#include "webrtc/base/gunit.h"
15
16namespace rtc {
17
18namespace {
19
20class InstanceCounter {
21 public:
22 explicit InstanceCounter(int* num_instances)
23 : num_instances_(num_instances) {
24 ++(*num_instances_);
25 }
26 ~InstanceCounter() {
27 --(*num_instances_);
28 }
29
30 private:
31 int* num_instances_;
32
henrikg3c089d72015-09-16 05:37:44 -070033 RTC_DISALLOW_COPY_AND_ASSIGN(InstanceCounter);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000034};
35
36} // namespace
37
38class ScopedPtrCollectionTest : public testing::Test {
39 protected:
40 ScopedPtrCollectionTest()
41 : num_instances_(0),
42 collection_(new ScopedPtrCollection<InstanceCounter>()) {
43 }
44
45 int num_instances_;
jbauch555604a2016-04-26 03:13:22 -070046 std::unique_ptr<ScopedPtrCollection<InstanceCounter> > collection_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000047};
48
49TEST_F(ScopedPtrCollectionTest, PushBack) {
50 EXPECT_EQ(0u, collection_->collection().size());
51 EXPECT_EQ(0, num_instances_);
52 const int kNum = 100;
53 for (int i = 0; i < kNum; ++i) {
54 collection_->PushBack(new InstanceCounter(&num_instances_));
55 }
56 EXPECT_EQ(static_cast<size_t>(kNum), collection_->collection().size());
57 EXPECT_EQ(kNum, num_instances_);
58 collection_.reset();
59 EXPECT_EQ(0, num_instances_);
60}
61
62TEST_F(ScopedPtrCollectionTest, Remove) {
63 InstanceCounter* ic = new InstanceCounter(&num_instances_);
64 collection_->PushBack(ic);
65 EXPECT_EQ(1u, collection_->collection().size());
66 collection_->Remove(ic);
67 EXPECT_EQ(1, num_instances_);
68 collection_.reset();
69 EXPECT_EQ(1, num_instances_);
70 delete ic;
71 EXPECT_EQ(0, num_instances_);
72}
73
74
75} // namespace rtc