gtest-linked_ptr_test.cc
Go to the documentation of this file.
00001 // Copyright 2003, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // Authors: Dan Egnor (egnor@google.com)
00031 // Ported to Windows: Vadim Berman (vadimb@google.com)
00032 
00033 #include "gtest/internal/gtest-linked_ptr.h"
00034 
00035 #include <stdlib.h>
00036 #include "gtest/gtest.h"
00037 
00038 namespace {
00039 
00040 using testing::Message;
00041 using testing::internal::linked_ptr;
00042 
00043 int num;
00044 Message* history = NULL;
00045 
00046 // Class which tracks allocation/deallocation
00047 class A {
00048  public:
00049   A(): mynum(num++) { *history << "A" << mynum << " ctor\n"; }
00050   virtual ~A() { *history << "A" << mynum << " dtor\n"; }
00051   virtual void Use() { *history << "A" << mynum << " use\n"; }
00052  protected:
00053   int mynum;
00054 };
00055 
00056 // Subclass
00057 class B : public A {
00058  public:
00059   B() { *history << "B" << mynum << " ctor\n"; }
00060   ~B() { *history << "B" << mynum << " dtor\n"; }
00061   virtual void Use() { *history << "B" << mynum << " use\n"; }
00062 };
00063 
00064 class LinkedPtrTest : public testing::Test {
00065  public:
00066   LinkedPtrTest() {
00067     num = 0;
00068     history = new Message;
00069   }
00070 
00071   virtual ~LinkedPtrTest() {
00072     delete history;
00073     history = NULL;
00074   }
00075 };
00076 
00077 TEST_F(LinkedPtrTest, GeneralTest) {
00078   {
00079     linked_ptr<A> a0, a1, a2;
00080     // Use explicit function call notation here to suppress self-assign warning.
00081     a0.operator=(a0);
00082     a1 = a2;
00083     ASSERT_EQ(a0.get(), static_cast<A*>(NULL));
00084     ASSERT_EQ(a1.get(), static_cast<A*>(NULL));
00085     ASSERT_EQ(a2.get(), static_cast<A*>(NULL));
00086     ASSERT_TRUE(a0 == NULL);
00087     ASSERT_TRUE(a1 == NULL);
00088     ASSERT_TRUE(a2 == NULL);
00089 
00090     {
00091       linked_ptr<A> a3(new A);
00092       a0 = a3;
00093       ASSERT_TRUE(a0 == a3);
00094       ASSERT_TRUE(a0 != NULL);
00095       ASSERT_TRUE(a0.get() == a3);
00096       ASSERT_TRUE(a0 == a3.get());
00097       linked_ptr<A> a4(a0);
00098       a1 = a4;
00099       linked_ptr<A> a5(new A);
00100       ASSERT_TRUE(a5.get() != a3);
00101       ASSERT_TRUE(a5 != a3.get());
00102       a2 = a5;
00103       linked_ptr<B> b0(new B);
00104       linked_ptr<A> a6(b0);
00105       ASSERT_TRUE(b0 == a6);
00106       ASSERT_TRUE(a6 == b0);
00107       ASSERT_TRUE(b0 != NULL);
00108       a5 = b0;
00109       a5 = b0;
00110       a3->Use();
00111       a4->Use();
00112       a5->Use();
00113       a6->Use();
00114       b0->Use();
00115       (*b0).Use();
00116       b0.get()->Use();
00117     }
00118 
00119     a0->Use();
00120     a1->Use();
00121     a2->Use();
00122 
00123     a1 = a2;
00124     a2.reset(new A);
00125     a0.reset();
00126 
00127     linked_ptr<A> a7;
00128   }
00129 
00130   ASSERT_STREQ(
00131     "A0 ctor\n"
00132     "A1 ctor\n"
00133     "A2 ctor\n"
00134     "B2 ctor\n"
00135     "A0 use\n"
00136     "A0 use\n"
00137     "B2 use\n"
00138     "B2 use\n"
00139     "B2 use\n"
00140     "B2 use\n"
00141     "B2 use\n"
00142     "B2 dtor\n"
00143     "A2 dtor\n"
00144     "A0 use\n"
00145     "A0 use\n"
00146     "A1 use\n"
00147     "A3 ctor\n"
00148     "A0 dtor\n"
00149     "A3 dtor\n"
00150     "A1 dtor\n",
00151     history->GetString().c_str());
00152 }
00153 
00154 }  // Unnamed namespace


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:03