Home | History | Annotate | Download | only in base
      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 "webrtc/base/scopedptrcollection.h"
     12 #include "webrtc/base/gunit.h"
     13 
     14 namespace rtc {
     15 
     16 namespace {
     17 
     18 class InstanceCounter {
     19  public:
     20   explicit InstanceCounter(int* num_instances)
     21       : num_instances_(num_instances) {
     22     ++(*num_instances_);
     23   }
     24   ~InstanceCounter() {
     25     --(*num_instances_);
     26   }
     27 
     28  private:
     29   int* num_instances_;
     30 
     31   RTC_DISALLOW_COPY_AND_ASSIGN(InstanceCounter);
     32 };
     33 
     34 }  // namespace
     35 
     36 class ScopedPtrCollectionTest : public testing::Test {
     37  protected:
     38   ScopedPtrCollectionTest()
     39       : num_instances_(0),
     40       collection_(new ScopedPtrCollection<InstanceCounter>()) {
     41   }
     42 
     43   int num_instances_;
     44   scoped_ptr<ScopedPtrCollection<InstanceCounter> > collection_;
     45 };
     46 
     47 TEST_F(ScopedPtrCollectionTest, PushBack) {
     48   EXPECT_EQ(0u, collection_->collection().size());
     49   EXPECT_EQ(0, num_instances_);
     50   const int kNum = 100;
     51   for (int i = 0; i < kNum; ++i) {
     52     collection_->PushBack(new InstanceCounter(&num_instances_));
     53   }
     54   EXPECT_EQ(static_cast<size_t>(kNum), collection_->collection().size());
     55   EXPECT_EQ(kNum, num_instances_);
     56   collection_.reset();
     57   EXPECT_EQ(0, num_instances_);
     58 }
     59 
     60 TEST_F(ScopedPtrCollectionTest, Remove) {
     61   InstanceCounter* ic = new InstanceCounter(&num_instances_);
     62   collection_->PushBack(ic);
     63   EXPECT_EQ(1u, collection_->collection().size());
     64   collection_->Remove(ic);
     65   EXPECT_EQ(1, num_instances_);
     66   collection_.reset();
     67   EXPECT_EQ(1, num_instances_);
     68   delete ic;
     69   EXPECT_EQ(0, num_instances_);
     70 }
     71 
     72 
     73 }  // namespace rtc
     74