blob: dd9002e42f115ab69fddec356b7b2da7bde5188d [file] [log] [blame]
sergeyu@chromium.org70022fa2014-02-07 19:03:26 +00001/*
2 * libjingle
3 * Copyright 2014 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
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.
15 *
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.
26 */
27
28#include "talk/base/scopedptrcollection.h"
29#include "talk/base/gunit.h"
30
31namespace talk_base {
32
33namespace {
34
35class InstanceCounter {
36 public:
37 explicit InstanceCounter(int* num_instances)
38 : num_instances_(num_instances) {
39 ++(*num_instances_);
40 }
41 ~InstanceCounter() {
42 --(*num_instances_);
43 }
44
45 private:
46 int* num_instances_;
47
48 DISALLOW_COPY_AND_ASSIGN(InstanceCounter);
49};
50
51} // namespace
52
53class ScopedPtrCollectionTest : public testing::Test {
54 protected:
55 ScopedPtrCollectionTest()
56 : num_instances_(0),
57 collection_(new ScopedPtrCollection<InstanceCounter>()) {
58 }
59
60 int num_instances_;
61 scoped_ptr<ScopedPtrCollection<InstanceCounter> > collection_;
62};
63
64TEST_F(ScopedPtrCollectionTest, PushBack) {
65 EXPECT_EQ(0u, collection_->collection().size());
66 EXPECT_EQ(0, num_instances_);
67 const int kNum = 100;
68 for (int i = 0; i < kNum; ++i) {
69 collection_->PushBack(new InstanceCounter(&num_instances_));
70 }
71 EXPECT_EQ(static_cast<size_t>(kNum), collection_->collection().size());
72 EXPECT_EQ(kNum, num_instances_);
73 collection_.reset();
74 EXPECT_EQ(0, num_instances_);
75}
76
77TEST_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_);
83 collection_.reset();
84 EXPECT_EQ(1, num_instances_);
85 delete ic;
86 EXPECT_EQ(0, num_instances_);
87}
88
89
90} // namespace talk_base