chromium/third_party/pdfium/core/fxcrt/unowned_ptr_unittest.cpp

// Copyright 2017 The PDFium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "core/fxcrt/unowned_ptr.h"

#include <atomic>
#include <functional>
#include <memory>
#include <set>
#include <utility>

#include "core/fxcrt/containers/contains.h"
#include "testing/gtest/include/gtest/gtest.h"

#if defined(PDF_USE_PARTITION_ALLOC)
#include "partition_alloc/shim/allocator_shim_default_dispatch_to_partition_alloc.h"
#endif

namespace fxcrt {
namespace {

template <typename T, typename C = std::less<T>>
class NoLinearSearchSet : public std::set<T, C> {
 public:
  typename std::set<T, C>::iterator begin() noexcept = delete;
  typename std::set<T, C>::const_iterator cbegin() const noexcept = delete;
};

class Clink {};

void DeleteDangling() {}

void AssignDangling() {}

void ReleaseDangling() {}

}  // namespace

TEST(UnownedPtr, DefaultCtor) {}

TEST(UnownedPtr, NullptrCtor) {}

TEST(UnownedPtr, RawCtor) {}

TEST(UnownedPtr, CopyCtor) {}

TEST(UnownedPtr, MoveCtor) {}

TEST(UnownedPtr, CopyConversionCtor) {}

TEST(UnownedPtr, MoveConversionCtor) {}

TEST(UnownedPtr, NullptrAssign) {}

TEST(UnownedPtr, RawAssign) {}

TEST(UnownedPtr, CopyAssign) {}

TEST(UnownedPtr, MoveAssign) {}

TEST(UnownedPtr, CopyConversionAssign) {}

TEST(UnownedPtr, MoveConversionAssign) {}

TEST(UnownedPtr, PtrOk) {}

TEST(UnownedPtr, PtrNotOk) {}

TEST(UnownedPtr, AssignOk) {}

TEST(UnownedPtr, AssignNotOk) {}

TEST(UnownedPtr, ReleaseOk) {}

TEST(UnownedPtr, ReleaseNotOk) {}

TEST(UnownedPtr, OperatorEQ) {}

TEST(UnownedPtr, OperatorNE) {}

TEST(UnownedPtr, OperatorLT) {}

TEST(UnownedPtr, TransparentCompare) {}

#if defined(PDF_USE_PARTITION_ALLOC)
#if PA_BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) &&   \
    PA_BUILDFLAG(ENABLE_BACKUP_REF_PTR_SUPPORT) &&   \
    !PA_BUILDFLAG(ENABLE_DANGLING_RAW_PTR_CHECKS) && \
    PA_BUILDFLAG(HAS_64_BIT_POINTERS)

TEST(UnownedPtr, DanglingGetsQuarantined) {
  partition_alloc::PartitionRoot* root =
      allocator_shim::internal::PartitionAllocMalloc::Allocator();
  size_t original_byte_count =
      root->total_size_of_brp_quarantined_bytes.load(std::memory_order_relaxed);

  auto ptr = std::make_unique<double>(4.0);
  UnownedPtr<double> dangler = ptr.get();
  EXPECT_EQ(
      root->total_size_of_brp_quarantined_bytes.load(std::memory_order_relaxed),
      original_byte_count);

  ptr.reset();
  EXPECT_GE(
      root->total_size_of_brp_quarantined_bytes.load(std::memory_order_relaxed),
      original_byte_count + sizeof(double));

  dangler = nullptr;
  EXPECT_EQ(
      root->total_size_of_brp_quarantined_bytes.load(std::memory_order_relaxed),
      original_byte_count);
}

#endif  // PA_BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) ...
#endif  // PDF_USE_PARTITION_ALLOC

}  // namespace fxcrt