OLD | NEW |
(Empty) | |
| 1 /* |
| 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 |
| 11 #include <memory> |
| 12 |
| 13 #include "webrtc/base/constructormagic.h" |
| 14 #include "webrtc/base/scopedptrcollection.h" |
| 15 #include "webrtc/base/gunit.h" |
| 16 |
| 17 namespace rtc { |
| 18 |
| 19 namespace { |
| 20 |
| 21 class 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 |
| 34 RTC_DISALLOW_COPY_AND_ASSIGN(InstanceCounter); |
| 35 }; |
| 36 |
| 37 } // namespace |
| 38 |
| 39 class ScopedPtrCollectionTest : public testing::Test { |
| 40 protected: |
| 41 ScopedPtrCollectionTest() |
| 42 : num_instances_(0), |
| 43 collection_(new ScopedPtrCollection<InstanceCounter>()) { |
| 44 } |
| 45 |
| 46 int num_instances_; |
| 47 std::unique_ptr<ScopedPtrCollection<InstanceCounter> > collection_; |
| 48 }; |
| 49 |
| 50 TEST_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 |
| 63 TEST_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 |
OLD | NEW |