1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/webrtc/trunk/testing/gtest/test/gtest-linked_ptr_test.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,154 @@ 1.4 +// Copyright 2003, Google Inc. 1.5 +// All rights reserved. 1.6 +// 1.7 +// Redistribution and use in source and binary forms, with or without 1.8 +// modification, are permitted provided that the following conditions are 1.9 +// met: 1.10 +// 1.11 +// * Redistributions of source code must retain the above copyright 1.12 +// notice, this list of conditions and the following disclaimer. 1.13 +// * Redistributions in binary form must reproduce the above 1.14 +// copyright notice, this list of conditions and the following disclaimer 1.15 +// in the documentation and/or other materials provided with the 1.16 +// distribution. 1.17 +// * Neither the name of Google Inc. nor the names of its 1.18 +// contributors may be used to endorse or promote products derived from 1.19 +// this software without specific prior written permission. 1.20 +// 1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.32 +// 1.33 +// Authors: Dan Egnor (egnor@google.com) 1.34 +// Ported to Windows: Vadim Berman (vadimb@google.com) 1.35 + 1.36 +#include "gtest/internal/gtest-linked_ptr.h" 1.37 + 1.38 +#include <stdlib.h> 1.39 +#include "gtest/gtest.h" 1.40 + 1.41 +namespace { 1.42 + 1.43 +using testing::Message; 1.44 +using testing::internal::linked_ptr; 1.45 + 1.46 +int num; 1.47 +Message* history = NULL; 1.48 + 1.49 +// Class which tracks allocation/deallocation 1.50 +class A { 1.51 + public: 1.52 + A(): mynum(num++) { *history << "A" << mynum << " ctor\n"; } 1.53 + virtual ~A() { *history << "A" << mynum << " dtor\n"; } 1.54 + virtual void Use() { *history << "A" << mynum << " use\n"; } 1.55 + protected: 1.56 + int mynum; 1.57 +}; 1.58 + 1.59 +// Subclass 1.60 +class B : public A { 1.61 + public: 1.62 + B() { *history << "B" << mynum << " ctor\n"; } 1.63 + ~B() { *history << "B" << mynum << " dtor\n"; } 1.64 + virtual void Use() { *history << "B" << mynum << " use\n"; } 1.65 +}; 1.66 + 1.67 +class LinkedPtrTest : public testing::Test { 1.68 + public: 1.69 + LinkedPtrTest() { 1.70 + num = 0; 1.71 + history = new Message; 1.72 + } 1.73 + 1.74 + virtual ~LinkedPtrTest() { 1.75 + delete history; 1.76 + history = NULL; 1.77 + } 1.78 +}; 1.79 + 1.80 +TEST_F(LinkedPtrTest, GeneralTest) { 1.81 + { 1.82 + linked_ptr<A> a0, a1, a2; 1.83 + // Use explicit function call notation here to suppress self-assign warning. 1.84 + a0.operator=(a0); 1.85 + a1 = a2; 1.86 + ASSERT_EQ(a0.get(), static_cast<A*>(NULL)); 1.87 + ASSERT_EQ(a1.get(), static_cast<A*>(NULL)); 1.88 + ASSERT_EQ(a2.get(), static_cast<A*>(NULL)); 1.89 + ASSERT_TRUE(a0 == NULL); 1.90 + ASSERT_TRUE(a1 == NULL); 1.91 + ASSERT_TRUE(a2 == NULL); 1.92 + 1.93 + { 1.94 + linked_ptr<A> a3(new A); 1.95 + a0 = a3; 1.96 + ASSERT_TRUE(a0 == a3); 1.97 + ASSERT_TRUE(a0 != NULL); 1.98 + ASSERT_TRUE(a0.get() == a3); 1.99 + ASSERT_TRUE(a0 == a3.get()); 1.100 + linked_ptr<A> a4(a0); 1.101 + a1 = a4; 1.102 + linked_ptr<A> a5(new A); 1.103 + ASSERT_TRUE(a5.get() != a3); 1.104 + ASSERT_TRUE(a5 != a3.get()); 1.105 + a2 = a5; 1.106 + linked_ptr<B> b0(new B); 1.107 + linked_ptr<A> a6(b0); 1.108 + ASSERT_TRUE(b0 == a6); 1.109 + ASSERT_TRUE(a6 == b0); 1.110 + ASSERT_TRUE(b0 != NULL); 1.111 + a5 = b0; 1.112 + a5 = b0; 1.113 + a3->Use(); 1.114 + a4->Use(); 1.115 + a5->Use(); 1.116 + a6->Use(); 1.117 + b0->Use(); 1.118 + (*b0).Use(); 1.119 + b0.get()->Use(); 1.120 + } 1.121 + 1.122 + a0->Use(); 1.123 + a1->Use(); 1.124 + a2->Use(); 1.125 + 1.126 + a1 = a2; 1.127 + a2.reset(new A); 1.128 + a0.reset(); 1.129 + 1.130 + linked_ptr<A> a7; 1.131 + } 1.132 + 1.133 + ASSERT_STREQ( 1.134 + "A0 ctor\n" 1.135 + "A1 ctor\n" 1.136 + "A2 ctor\n" 1.137 + "B2 ctor\n" 1.138 + "A0 use\n" 1.139 + "A0 use\n" 1.140 + "B2 use\n" 1.141 + "B2 use\n" 1.142 + "B2 use\n" 1.143 + "B2 use\n" 1.144 + "B2 use\n" 1.145 + "B2 dtor\n" 1.146 + "A2 dtor\n" 1.147 + "A0 use\n" 1.148 + "A0 use\n" 1.149 + "A1 use\n" 1.150 + "A3 ctor\n" 1.151 + "A0 dtor\n" 1.152 + "A3 dtor\n" 1.153 + "A1 dtor\n", 1.154 + history->GetString().c_str()); 1.155 +} 1.156 + 1.157 +} // Unnamed namespace