3 * Copyright 2014 Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "talk/base/scopedptrcollection.h"
29 #include "talk/base/gunit.h"
35 class InstanceCounter {
37 explicit InstanceCounter(int* num_instances)
38 : num_instances_(num_instances) {
48 DISALLOW_COPY_AND_ASSIGN(InstanceCounter);
53 class ScopedPtrCollectionTest : public testing::Test {
55 ScopedPtrCollectionTest()
57 collection_(new ScopedPtrCollection<InstanceCounter>()) {
61 scoped_ptr<ScopedPtrCollection<InstanceCounter> > collection_;
64 TEST_F(ScopedPtrCollectionTest, PushBack) {
65 EXPECT_EQ(0u, collection_->collection().size());
66 EXPECT_EQ(0, num_instances_);
68 for (int i = 0; i < kNum; ++i) {
69 collection_->PushBack(new InstanceCounter(&num_instances_));
71 EXPECT_EQ(static_cast<size_t>(kNum), collection_->collection().size());
72 EXPECT_EQ(kNum, num_instances_);
74 EXPECT_EQ(0, num_instances_);
77 TEST_F(ScopedPtrCollectionTest, Remove) {
78 InstanceCounter* ic = new InstanceCounter(&num_instances_);
79 collection_->PushBack(ic);
80 EXPECT_EQ(1u, collection_->collection().size());
81 collection_->Remove(ic);
82 EXPECT_EQ(1, num_instances_);
84 EXPECT_EQ(1, num_instances_);
86 EXPECT_EQ(0, num_instances_);
90 } // namespace talk_base