11be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Copyright 2003, Google Inc. 21be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// All rights reserved. 31be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 41be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Redistribution and use in source and binary forms, with or without 51be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// modification, are permitted provided that the following conditions are 61be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// met: 71be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 81be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * Redistributions of source code must retain the above copyright 91be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// notice, this list of conditions and the following disclaimer. 101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * Redistributions in binary form must reproduce the above 111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// copyright notice, this list of conditions and the following disclaimer 121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// in the documentation and/or other materials provided with the 131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// distribution. 141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * Neither the name of Google Inc. nor the names of its 151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// contributors may be used to endorse or promote products derived from 161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// this software without specific prior written permission. 171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Authors: Dan Egnor (egnor@google.com) 311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Ported to Windows: Vadim Berman (vadimb@google.com) 321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <gtest/internal/gtest-linked_ptr.h> 341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <stdlib.h> 361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <gtest/gtest.h> 371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace { 391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniausing testing::Message; 411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniausing testing::internal::linked_ptr; 421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint num; 441be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaMessage* history = NULL; 451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Class which tracks allocation/deallocation 471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass A { 481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania A(): mynum(num++) { *history << "A" << mynum << " ctor\n"; } 501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual ~A() { *history << "A" << mynum << " dtor\n"; } 511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual void Use() { *history << "A" << mynum << " use\n"; } 521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania protected: 531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int mynum; 541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Subclass 571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass B : public A { 581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania B() { *history << "B" << mynum << " ctor\n"; } 601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ~B() { *history << "B" << mynum << " dtor\n"; } 611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual void Use() { *history << "B" << mynum << " use\n"; } 621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass LinkedPtrTest : public testing::Test { 651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania LinkedPtrTest() { 671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania num = 0; 681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania history = new Message; 691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual ~LinkedPtrTest() { 721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania delete history; 731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania history = NULL; 741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 771be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(LinkedPtrTest, GeneralTest) { 781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania { 791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania linked_ptr<A> a0, a1, a2; 801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania a0 = a0; 811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania a1 = a2; 821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_EQ(a0.get(), static_cast<A*>(NULL)); 831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_EQ(a1.get(), static_cast<A*>(NULL)); 841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_EQ(a2.get(), static_cast<A*>(NULL)); 851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_TRUE(a0 == NULL); 861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_TRUE(a1 == NULL); 871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_TRUE(a2 == NULL); 881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania { 901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania linked_ptr<A> a3(new A); 911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania a0 = a3; 921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_TRUE(a0 == a3); 931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_TRUE(a0 != NULL); 941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_TRUE(a0.get() == a3); 951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_TRUE(a0 == a3.get()); 961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania linked_ptr<A> a4(a0); 971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania a1 = a4; 981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania linked_ptr<A> a5(new A); 991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_TRUE(a5.get() != a3); 1001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_TRUE(a5 != a3.get()); 1011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania a2 = a5; 1021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania linked_ptr<B> b0(new B); 1031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania linked_ptr<A> a6(b0); 1041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_TRUE(b0 == a6); 1051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_TRUE(a6 == b0); 1061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_TRUE(b0 != NULL); 1071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania a5 = b0; 1081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania a5 = b0; 1091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania a3->Use(); 1101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania a4->Use(); 1111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania a5->Use(); 1121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania a6->Use(); 1131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania b0->Use(); 1141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania (*b0).Use(); 1151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania b0.get()->Use(); 1161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 1171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania a0->Use(); 1191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania a1->Use(); 1201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania a2->Use(); 1211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania a1 = a2; 1231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania a2.reset(new A); 1241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania a0.reset(); 1251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania linked_ptr<A> a7; 1271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 1281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_STREQ( 1301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "A0 ctor\n" 1311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "A1 ctor\n" 1321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "A2 ctor\n" 1331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "B2 ctor\n" 1341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "A0 use\n" 1351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "A0 use\n" 1361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "B2 use\n" 1371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "B2 use\n" 1381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "B2 use\n" 1391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "B2 use\n" 1401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "B2 use\n" 1411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "B2 dtor\n" 1421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "A2 dtor\n" 1431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "A0 use\n" 1441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "A0 use\n" 1451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "A1 use\n" 1461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "A3 ctor\n" 1471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "A0 dtor\n" 1481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "A3 dtor\n" 1491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "A1 dtor\n", 1501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania history->GetString().c_str() 1511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ); 1521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 1531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // Unnamed namespace 155