Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / base / memory / ref_counted_unittest.cc
blob7a033f3a2a79d0589887b00e21f9eabdad6d57f5
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/memory/ref_counted.h"
6 #include "testing/gtest/include/gtest/gtest.h"
8 namespace {
10 class SelfAssign : public base::RefCounted<SelfAssign> {
11 friend class base::RefCounted<SelfAssign>;
13 ~SelfAssign() {}
16 class CheckDerivedMemberAccess : public scoped_refptr<SelfAssign> {
17 public:
18 CheckDerivedMemberAccess() {
19 // This shouldn't compile if we don't have access to the member variable.
20 SelfAssign** pptr = &ptr_;
21 EXPECT_EQ(*pptr, ptr_);
25 class ScopedRefPtrToSelf : public base::RefCounted<ScopedRefPtrToSelf> {
26 public:
27 ScopedRefPtrToSelf() : self_ptr_(this) {}
29 static bool was_destroyed() { return was_destroyed_; }
31 void SelfDestruct() { self_ptr_ = NULL; }
33 private:
34 friend class base::RefCounted<ScopedRefPtrToSelf>;
35 ~ScopedRefPtrToSelf() { was_destroyed_ = true; }
37 static bool was_destroyed_;
39 scoped_refptr<ScopedRefPtrToSelf> self_ptr_;
42 bool ScopedRefPtrToSelf::was_destroyed_ = false;
44 } // end namespace
46 TEST(RefCountedUnitTest, TestSelfAssignment) {
47 SelfAssign* p = new SelfAssign;
48 scoped_refptr<SelfAssign> var(p);
49 var = var;
50 EXPECT_EQ(var.get(), p);
53 TEST(RefCountedUnitTest, ScopedRefPtrMemberAccess) {
54 CheckDerivedMemberAccess check;
57 TEST(RefCountedUnitTest, ScopedRefPtrToSelf) {
58 ScopedRefPtrToSelf* check = new ScopedRefPtrToSelf();
59 EXPECT_FALSE(ScopedRefPtrToSelf::was_destroyed());
60 check->SelfDestruct();
61 EXPECT_TRUE(ScopedRefPtrToSelf::was_destroyed());
64 TEST(RefCountedUnitTest, ScopedRefPtrBooleanOperations) {
65 scoped_refptr<SelfAssign> p1 = new SelfAssign;
66 scoped_refptr<SelfAssign> p2;
68 EXPECT_TRUE(p1);
69 EXPECT_FALSE(!p1);
71 EXPECT_TRUE(!p2);
72 EXPECT_FALSE(p2);
74 EXPECT_NE(p1, p2);
76 SelfAssign* raw_p = new SelfAssign;
77 p2 = raw_p;
78 EXPECT_NE(p1, p2);
79 EXPECT_EQ(raw_p, p2);
81 p2 = p1;
82 EXPECT_NE(raw_p, p2);
83 EXPECT_EQ(p1, p2);