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