Blame nss/external_tests/google_test/gtest/test/gtest-linked_ptr_test.cc

Packit 40b132
// Copyright 2003, Google Inc.
Packit 40b132
// All rights reserved.
Packit 40b132
//
Packit 40b132
// Redistribution and use in source and binary forms, with or without
Packit 40b132
// modification, are permitted provided that the following conditions are
Packit 40b132
// met:
Packit 40b132
//
Packit 40b132
//     * Redistributions of source code must retain the above copyright
Packit 40b132
// notice, this list of conditions and the following disclaimer.
Packit 40b132
//     * Redistributions in binary form must reproduce the above
Packit 40b132
// copyright notice, this list of conditions and the following disclaimer
Packit 40b132
// in the documentation and/or other materials provided with the
Packit 40b132
// distribution.
Packit 40b132
//     * Neither the name of Google Inc. nor the names of its
Packit 40b132
// contributors may be used to endorse or promote products derived from
Packit 40b132
// this software without specific prior written permission.
Packit 40b132
//
Packit 40b132
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 40b132
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 40b132
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit 40b132
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit 40b132
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit 40b132
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit 40b132
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 40b132
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 40b132
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 40b132
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 40b132
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 40b132
//
Packit 40b132
// Authors: Dan Egnor (egnor@google.com)
Packit 40b132
// Ported to Windows: Vadim Berman (vadimb@google.com)
Packit 40b132
Packit 40b132
#include "gtest/internal/gtest-linked_ptr.h"
Packit 40b132
Packit 40b132
#include <stdlib.h>
Packit 40b132
#include "gtest/gtest.h"
Packit 40b132
Packit 40b132
namespace {
Packit 40b132
Packit 40b132
using testing::Message;
Packit 40b132
using testing::internal::linked_ptr;
Packit 40b132
Packit 40b132
int num;
Packit 40b132
Message* history = NULL;
Packit 40b132
Packit 40b132
// Class which tracks allocation/deallocation
Packit 40b132
class A {
Packit 40b132
 public:
Packit 40b132
  A(): mynum(num++) { *history << "A" << mynum << " ctor\n"; }
Packit 40b132
  virtual ~A() { *history << "A" << mynum << " dtor\n"; }
Packit 40b132
  virtual void Use() { *history << "A" << mynum << " use\n"; }
Packit 40b132
 protected:
Packit 40b132
  int mynum;
Packit 40b132
};
Packit 40b132
Packit 40b132
// Subclass
Packit 40b132
class B : public A {
Packit 40b132
 public:
Packit 40b132
  B() { *history << "B" << mynum << " ctor\n"; }
Packit 40b132
  ~B() { *history << "B" << mynum << " dtor\n"; }
Packit 40b132
  virtual void Use() { *history << "B" << mynum << " use\n"; }
Packit 40b132
};
Packit 40b132
Packit 40b132
class LinkedPtrTest : public testing::Test {
Packit 40b132
 public:
Packit 40b132
  LinkedPtrTest() {
Packit 40b132
    num = 0;
Packit 40b132
    history = new Message;
Packit 40b132
  }
Packit 40b132
Packit 40b132
  virtual ~LinkedPtrTest() {
Packit 40b132
    delete history;
Packit 40b132
    history = NULL;
Packit 40b132
  }
Packit 40b132
};
Packit 40b132
Packit 40b132
TEST_F(LinkedPtrTest, GeneralTest) {
Packit 40b132
  {
Packit 40b132
    linked_ptr a0, a1, a2;
Packit 40b132
    // Use explicit function call notation here to suppress self-assign warning.
Packit 40b132
    a0.operator=(a0);
Packit 40b132
    a1 = a2;
Packit 40b132
    ASSERT_EQ(a0.get(), static_cast<A*>(NULL));
Packit 40b132
    ASSERT_EQ(a1.get(), static_cast<A*>(NULL));
Packit 40b132
    ASSERT_EQ(a2.get(), static_cast<A*>(NULL));
Packit 40b132
    ASSERT_TRUE(a0 == NULL);
Packit 40b132
    ASSERT_TRUE(a1 == NULL);
Packit 40b132
    ASSERT_TRUE(a2 == NULL);
Packit 40b132
Packit 40b132
    {
Packit 40b132
      linked_ptr a3(new A);
Packit 40b132
      a0 = a3;
Packit 40b132
      ASSERT_TRUE(a0 == a3);
Packit 40b132
      ASSERT_TRUE(a0 != NULL);
Packit 40b132
      ASSERT_TRUE(a0.get() == a3);
Packit 40b132
      ASSERT_TRUE(a0 == a3.get());
Packit 40b132
      linked_ptr a4(a0);
Packit 40b132
      a1 = a4;
Packit 40b132
      linked_ptr a5(new A);
Packit 40b132
      ASSERT_TRUE(a5.get() != a3);
Packit 40b132
      ASSERT_TRUE(a5 != a3.get());
Packit 40b132
      a2 = a5;
Packit 40b132
      linked_ptr b0(new B);
Packit 40b132
      linked_ptr a6(b0);
Packit 40b132
      ASSERT_TRUE(b0 == a6);
Packit 40b132
      ASSERT_TRUE(a6 == b0);
Packit 40b132
      ASSERT_TRUE(b0 != NULL);
Packit 40b132
      a5 = b0;
Packit 40b132
      a5 = b0;
Packit 40b132
      a3->Use();
Packit 40b132
      a4->Use();
Packit 40b132
      a5->Use();
Packit 40b132
      a6->Use();
Packit 40b132
      b0->Use();
Packit 40b132
      (*b0).Use();
Packit 40b132
      b0.get()->Use();
Packit 40b132
    }
Packit 40b132
Packit 40b132
    a0->Use();
Packit 40b132
    a1->Use();
Packit 40b132
    a2->Use();
Packit 40b132
Packit 40b132
    a1 = a2;
Packit 40b132
    a2.reset(new A);
Packit 40b132
    a0.reset();
Packit 40b132
Packit 40b132
    linked_ptr a7;
Packit 40b132
  }
Packit 40b132
Packit 40b132
  ASSERT_STREQ(
Packit 40b132
    "A0 ctor\n"
Packit 40b132
    "A1 ctor\n"
Packit 40b132
    "A2 ctor\n"
Packit 40b132
    "B2 ctor\n"
Packit 40b132
    "A0 use\n"
Packit 40b132
    "A0 use\n"
Packit 40b132
    "B2 use\n"
Packit 40b132
    "B2 use\n"
Packit 40b132
    "B2 use\n"
Packit 40b132
    "B2 use\n"
Packit 40b132
    "B2 use\n"
Packit 40b132
    "B2 dtor\n"
Packit 40b132
    "A2 dtor\n"
Packit 40b132
    "A0 use\n"
Packit 40b132
    "A0 use\n"
Packit 40b132
    "A1 use\n"
Packit 40b132
    "A3 ctor\n"
Packit 40b132
    "A0 dtor\n"
Packit 40b132
    "A3 dtor\n"
Packit 40b132
    "A1 dtor\n",
Packit 40b132
    history->GetString().c_str());
Packit 40b132
}
Packit 40b132
Packit 40b132
}  // Unnamed namespace