tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

scoped_refptr_unittest.cc (3017B)


      1 /*
      2 *  Copyright 2019 The WebRTC Project Authors. All rights reserved.
      3 *
      4 *  Use of this source code is governed by a BSD-style license
      5 *  that can be found in the LICENSE file in the root of the source
      6 *  tree. An additional intellectual property rights grant can be found
      7 *  in the file PATENTS.  All contributing project authors may
      8 *  be found in the AUTHORS file in the root of the source tree.
      9 */
     10 
     11 #include "api/scoped_refptr.h"
     12 
     13 #include <type_traits>
     14 #include <utility>
     15 #include <vector>
     16 
     17 #include "test/gtest.h"
     18 
     19 namespace webrtc {
     20 namespace {
     21 
     22 struct FunctionsCalled {
     23  int addref = 0;
     24  int release = 0;
     25 };
     26 
     27 class ScopedRefCounted {
     28 public:
     29  explicit ScopedRefCounted(FunctionsCalled* called) : called_(*called) {}
     30  ScopedRefCounted(const ScopedRefCounted&) = delete;
     31  ScopedRefCounted& operator=(const ScopedRefCounted&) = delete;
     32 
     33  void AddRef() {
     34    ++called_.addref;
     35    ++ref_count_;
     36  }
     37  void Release() {
     38    ++called_.release;
     39    if (0 == --ref_count_)
     40      delete this;
     41  }
     42 
     43 private:
     44  ~ScopedRefCounted() = default;
     45 
     46  FunctionsCalled& called_;
     47  int ref_count_ = 0;
     48 };
     49 
     50 TEST(ScopedRefptrTest, IsCopyConstructable) {
     51  FunctionsCalled called;
     52  scoped_refptr<ScopedRefCounted> ptr(new ScopedRefCounted(&called));
     53  scoped_refptr<ScopedRefCounted> another_ptr = ptr;
     54 
     55  EXPECT_TRUE(ptr);
     56  EXPECT_TRUE(another_ptr);
     57  EXPECT_EQ(called.addref, 2);
     58 }
     59 
     60 TEST(ScopedRefptrTest, IsCopyAssignable) {
     61  FunctionsCalled called;
     62  scoped_refptr<ScopedRefCounted> another_ptr;
     63  scoped_refptr<ScopedRefCounted> ptr(new ScopedRefCounted(&called));
     64  another_ptr = ptr;
     65 
     66  EXPECT_TRUE(ptr);
     67  EXPECT_TRUE(another_ptr);
     68  EXPECT_EQ(called.addref, 2);
     69 }
     70 
     71 TEST(ScopedRefptrTest, IsMoveConstructableWithoutExtraAddRefRelease) {
     72  FunctionsCalled called;
     73  scoped_refptr<ScopedRefCounted> ptr(new ScopedRefCounted(&called));
     74  scoped_refptr<ScopedRefCounted> another_ptr = std::move(ptr);
     75 
     76  EXPECT_FALSE(ptr);
     77  EXPECT_TRUE(another_ptr);
     78  EXPECT_EQ(called.addref, 1);
     79  EXPECT_EQ(called.release, 0);
     80 }
     81 
     82 TEST(ScopedRefptrTest, IsMoveAssignableWithoutExtraAddRefRelease) {
     83  FunctionsCalled called;
     84  scoped_refptr<ScopedRefCounted> another_ptr;
     85  scoped_refptr<ScopedRefCounted> ptr(new ScopedRefCounted(&called));
     86  another_ptr = std::move(ptr);
     87 
     88  EXPECT_FALSE(ptr);
     89  EXPECT_TRUE(another_ptr);
     90  EXPECT_EQ(called.addref, 1);
     91  EXPECT_EQ(called.release, 0);
     92 }
     93 
     94 TEST(ScopedRefptrTest, MovableDuringVectorReallocation) {
     95  static_assert(
     96      std::is_nothrow_move_constructible<scoped_refptr<ScopedRefCounted>>(),
     97      "");
     98  // Test below describes a scenario where it is helpful for move constructor
     99  // to be noexcept.
    100  FunctionsCalled called;
    101  std::vector<scoped_refptr<ScopedRefCounted>> ptrs;
    102  ptrs.reserve(1);
    103  // Insert more elements than reserved to provoke reallocation.
    104  ptrs.emplace_back(new ScopedRefCounted(&called));
    105  ptrs.emplace_back(new ScopedRefCounted(&called));
    106 
    107  EXPECT_EQ(called.addref, 2);
    108  EXPECT_EQ(called.release, 0);
    109 }
    110 
    111 }  // namespace
    112 }  // namespace webrtc