2020-03-31 17:19:02 +00:00
|
|
|
// Copyright 2020 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2020-04-08 07:48:37 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <vector>
|
2020-03-31 17:19:02 +00:00
|
|
|
|
2020-04-08 07:48:37 +00:00
|
|
|
#include "include/cppgc/allocation.h"
|
2020-04-01 15:15:38 +00:00
|
|
|
#include "include/cppgc/garbage-collected.h"
|
2020-03-31 17:19:02 +00:00
|
|
|
#include "include/cppgc/member.h"
|
2020-04-07 12:35:52 +00:00
|
|
|
#include "include/cppgc/type-traits.h"
|
2020-03-31 17:19:02 +00:00
|
|
|
|
2020-04-08 07:48:37 +00:00
|
|
|
#include "test/unittests/heap/cppgc/tests.h"
|
2020-03-31 17:19:02 +00:00
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
|
|
|
|
|
|
|
namespace cppgc {
|
|
|
|
namespace internal {
|
|
|
|
|
2020-04-08 07:48:37 +00:00
|
|
|
namespace {
|
|
|
|
|
2020-03-31 17:19:02 +00:00
|
|
|
struct GCed : GarbageCollected<GCed> {};
|
|
|
|
struct DerivedGCed : GCed {};
|
|
|
|
|
|
|
|
// Compile tests.
|
|
|
|
static_assert(!IsWeakV<Member<GCed>>, "Member is always strong.");
|
|
|
|
static_assert(IsWeakV<WeakMember<GCed>>, "WeakMember is always weak.");
|
|
|
|
|
|
|
|
struct CustomWriteBarrierPolicy {
|
|
|
|
static size_t InitializingWriteBarriersTriggered;
|
|
|
|
static size_t AssigningWriteBarriersTriggered;
|
|
|
|
static void InitializingBarrier(const void* slot, const void* value) {
|
|
|
|
++InitializingWriteBarriersTriggered;
|
|
|
|
}
|
|
|
|
static void AssigningBarrier(const void* slot, const void* value) {
|
|
|
|
++AssigningWriteBarriersTriggered;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
size_t CustomWriteBarrierPolicy::InitializingWriteBarriersTriggered = 0;
|
|
|
|
size_t CustomWriteBarrierPolicy::AssigningWriteBarriersTriggered = 0;
|
|
|
|
|
|
|
|
using MemberWithCustomBarrier =
|
2020-04-08 08:40:24 +00:00
|
|
|
BasicMember<GCed, StrongMemberTag, CustomWriteBarrierPolicy>;
|
2020-03-31 17:19:02 +00:00
|
|
|
|
|
|
|
struct CustomCheckingPolicy {
|
2020-04-08 07:48:37 +00:00
|
|
|
static std::vector<GCed*> Cached;
|
2020-03-31 17:19:02 +00:00
|
|
|
static size_t ChecksTriggered;
|
|
|
|
void CheckPointer(const void* ptr) {
|
2020-04-08 07:48:37 +00:00
|
|
|
EXPECT_NE(Cached.cend(), std::find(Cached.cbegin(), Cached.cend(), ptr));
|
2020-03-31 17:19:02 +00:00
|
|
|
++ChecksTriggered;
|
|
|
|
}
|
|
|
|
};
|
2020-04-08 07:48:37 +00:00
|
|
|
std::vector<GCed*> CustomCheckingPolicy::Cached;
|
2020-03-31 17:19:02 +00:00
|
|
|
size_t CustomCheckingPolicy::ChecksTriggered = 0;
|
|
|
|
|
|
|
|
using MemberWithCustomChecking =
|
2020-04-08 08:40:24 +00:00
|
|
|
BasicMember<GCed, StrongMemberTag, DijkstraWriteBarrierPolicy,
|
|
|
|
CustomCheckingPolicy>;
|
2020-03-31 17:19:02 +00:00
|
|
|
|
2020-04-08 07:48:37 +00:00
|
|
|
class MemberTest : public testing::TestSupportingAllocationOnly {};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-03-31 17:19:02 +00:00
|
|
|
template <template <typename> class Member>
|
|
|
|
void EmptyTest() {
|
|
|
|
{
|
|
|
|
Member<GCed> empty;
|
|
|
|
EXPECT_EQ(nullptr, empty.Get());
|
|
|
|
EXPECT_EQ(nullptr, empty.Release());
|
|
|
|
}
|
|
|
|
{
|
|
|
|
Member<GCed> empty = nullptr;
|
|
|
|
EXPECT_EQ(nullptr, empty.Get());
|
|
|
|
EXPECT_EQ(nullptr, empty.Release());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-08 07:48:37 +00:00
|
|
|
TEST_F(MemberTest, Empty) {
|
2020-03-31 17:19:02 +00:00
|
|
|
EmptyTest<Member>();
|
|
|
|
EmptyTest<WeakMember>();
|
|
|
|
EmptyTest<UntracedMember>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <template <typename> class Member>
|
2020-04-08 07:48:37 +00:00
|
|
|
void ClearTest(cppgc::Heap* heap) {
|
|
|
|
Member<GCed> member = MakeGarbageCollected<GCed>(heap);
|
2020-03-31 17:19:02 +00:00
|
|
|
EXPECT_NE(nullptr, member.Get());
|
|
|
|
member.Clear();
|
|
|
|
EXPECT_EQ(nullptr, member.Get());
|
|
|
|
}
|
|
|
|
|
2020-04-08 07:48:37 +00:00
|
|
|
TEST_F(MemberTest, Clear) {
|
|
|
|
cppgc::Heap* heap = GetHeap();
|
|
|
|
ClearTest<Member>(heap);
|
|
|
|
ClearTest<WeakMember>(heap);
|
|
|
|
ClearTest<UntracedMember>(heap);
|
2020-03-31 17:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <template <typename> class Member>
|
2020-04-08 07:48:37 +00:00
|
|
|
void ReleaseTest(cppgc::Heap* heap) {
|
|
|
|
GCed* gced = MakeGarbageCollected<GCed>(heap);
|
|
|
|
Member<GCed> member = gced;
|
2020-03-31 17:19:02 +00:00
|
|
|
EXPECT_NE(nullptr, member.Get());
|
|
|
|
GCed* raw = member.Release();
|
2020-04-08 07:48:37 +00:00
|
|
|
EXPECT_EQ(gced, raw);
|
2020-03-31 17:19:02 +00:00
|
|
|
EXPECT_EQ(nullptr, member.Get());
|
|
|
|
}
|
|
|
|
|
2020-04-08 07:48:37 +00:00
|
|
|
TEST_F(MemberTest, Release) {
|
|
|
|
cppgc::Heap* heap = GetHeap();
|
|
|
|
ReleaseTest<Member>(heap);
|
|
|
|
ReleaseTest<WeakMember>(heap);
|
|
|
|
ReleaseTest<UntracedMember>(heap);
|
2020-03-31 17:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <template <typename> class Member1, template <typename> class Member2>
|
2020-04-08 07:48:37 +00:00
|
|
|
void SwapTest(cppgc::Heap* heap) {
|
|
|
|
GCed* gced1 = MakeGarbageCollected<GCed>(heap);
|
|
|
|
GCed* gced2 = MakeGarbageCollected<GCed>(heap);
|
|
|
|
Member1<GCed> member1 = gced1;
|
|
|
|
Member2<GCed> member2 = gced2;
|
|
|
|
EXPECT_EQ(gced1, member1.Get());
|
|
|
|
EXPECT_EQ(gced2, member2.Get());
|
2020-03-31 17:19:02 +00:00
|
|
|
member1.Swap(member2);
|
2020-04-08 07:48:37 +00:00
|
|
|
EXPECT_EQ(gced2, member1.Get());
|
|
|
|
EXPECT_EQ(gced1, member2.Get());
|
2020-03-31 17:19:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 07:48:37 +00:00
|
|
|
TEST_F(MemberTest, Swap) {
|
|
|
|
cppgc::Heap* heap = GetHeap();
|
|
|
|
SwapTest<Member, Member>(heap);
|
|
|
|
SwapTest<Member, WeakMember>(heap);
|
|
|
|
SwapTest<Member, UntracedMember>(heap);
|
|
|
|
SwapTest<WeakMember, Member>(heap);
|
|
|
|
SwapTest<WeakMember, WeakMember>(heap);
|
|
|
|
SwapTest<WeakMember, UntracedMember>(heap);
|
|
|
|
SwapTest<UntracedMember, Member>(heap);
|
|
|
|
SwapTest<UntracedMember, WeakMember>(heap);
|
|
|
|
SwapTest<UntracedMember, UntracedMember>(heap);
|
2020-03-31 17:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <template <typename> class Member1, template <typename> class Member2>
|
2020-04-08 07:48:37 +00:00
|
|
|
void HeterogeneousConversionTest(cppgc::Heap* heap) {
|
2020-03-31 17:19:02 +00:00
|
|
|
{
|
2020-04-08 07:48:37 +00:00
|
|
|
Member1<GCed> member1 = MakeGarbageCollected<GCed>(heap);
|
2020-03-31 17:19:02 +00:00
|
|
|
Member2<GCed> member2 = member1;
|
|
|
|
EXPECT_EQ(member1.Get(), member2.Get());
|
|
|
|
}
|
|
|
|
{
|
2020-04-08 07:48:37 +00:00
|
|
|
Member1<DerivedGCed> member1 = MakeGarbageCollected<DerivedGCed>(heap);
|
2020-03-31 17:19:02 +00:00
|
|
|
Member2<GCed> member2 = member1;
|
|
|
|
EXPECT_EQ(member1.Get(), member2.Get());
|
|
|
|
}
|
|
|
|
{
|
2020-04-08 07:48:37 +00:00
|
|
|
Member1<GCed> member1 = MakeGarbageCollected<GCed>(heap);
|
2020-03-31 17:19:02 +00:00
|
|
|
Member2<GCed> member2;
|
|
|
|
member2 = member1;
|
|
|
|
EXPECT_EQ(member1.Get(), member2.Get());
|
|
|
|
}
|
|
|
|
{
|
2020-04-08 07:48:37 +00:00
|
|
|
Member1<DerivedGCed> member1 = MakeGarbageCollected<DerivedGCed>(heap);
|
2020-03-31 17:19:02 +00:00
|
|
|
Member2<GCed> member2;
|
|
|
|
member2 = member1;
|
|
|
|
EXPECT_EQ(member1.Get(), member2.Get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-08 07:48:37 +00:00
|
|
|
TEST_F(MemberTest, HeterogeneousInterface) {
|
|
|
|
cppgc::Heap* heap = GetHeap();
|
|
|
|
HeterogeneousConversionTest<Member, Member>(heap);
|
|
|
|
HeterogeneousConversionTest<Member, WeakMember>(heap);
|
|
|
|
HeterogeneousConversionTest<Member, UntracedMember>(heap);
|
|
|
|
HeterogeneousConversionTest<WeakMember, Member>(heap);
|
|
|
|
HeterogeneousConversionTest<WeakMember, WeakMember>(heap);
|
|
|
|
HeterogeneousConversionTest<WeakMember, UntracedMember>(heap);
|
|
|
|
HeterogeneousConversionTest<UntracedMember, Member>(heap);
|
|
|
|
HeterogeneousConversionTest<UntracedMember, WeakMember>(heap);
|
|
|
|
HeterogeneousConversionTest<UntracedMember, UntracedMember>(heap);
|
2020-03-31 17:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <template <typename> class Member1, template <typename> class Member2>
|
2020-04-08 07:48:37 +00:00
|
|
|
void EqualityTest(cppgc::Heap* heap) {
|
2020-03-31 17:19:02 +00:00
|
|
|
{
|
2020-04-08 07:48:37 +00:00
|
|
|
GCed* gced = MakeGarbageCollected<GCed>(heap);
|
|
|
|
Member1<GCed> member1 = gced;
|
|
|
|
Member2<GCed> member2 = gced;
|
2020-03-31 17:19:02 +00:00
|
|
|
EXPECT_TRUE(member1 == member2);
|
|
|
|
EXPECT_FALSE(member1 != member2);
|
|
|
|
member2 = member1;
|
|
|
|
EXPECT_TRUE(member1 == member2);
|
|
|
|
EXPECT_FALSE(member1 != member2);
|
|
|
|
}
|
|
|
|
{
|
2020-04-08 07:48:37 +00:00
|
|
|
Member1<GCed> member1 = MakeGarbageCollected<GCed>(heap);
|
|
|
|
Member2<GCed> member2 = MakeGarbageCollected<GCed>(heap);
|
2020-03-31 17:19:02 +00:00
|
|
|
EXPECT_TRUE(member1 != member2);
|
|
|
|
EXPECT_FALSE(member1 == member2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-08 07:48:37 +00:00
|
|
|
TEST_F(MemberTest, EqualityTest) {
|
|
|
|
cppgc::Heap* heap = GetHeap();
|
|
|
|
EqualityTest<Member, Member>(heap);
|
|
|
|
EqualityTest<Member, WeakMember>(heap);
|
|
|
|
EqualityTest<Member, UntracedMember>(heap);
|
|
|
|
EqualityTest<WeakMember, Member>(heap);
|
|
|
|
EqualityTest<WeakMember, WeakMember>(heap);
|
|
|
|
EqualityTest<WeakMember, UntracedMember>(heap);
|
|
|
|
EqualityTest<UntracedMember, Member>(heap);
|
|
|
|
EqualityTest<UntracedMember, WeakMember>(heap);
|
|
|
|
EqualityTest<UntracedMember, UntracedMember>(heap);
|
2020-03-31 17:19:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 07:48:37 +00:00
|
|
|
TEST_F(MemberTest, WriteBarrierTriggered) {
|
2020-03-31 17:19:02 +00:00
|
|
|
CustomWriteBarrierPolicy::InitializingWriteBarriersTriggered = 0;
|
|
|
|
CustomWriteBarrierPolicy::AssigningWriteBarriersTriggered = 0;
|
2020-04-08 07:48:37 +00:00
|
|
|
GCed* gced = MakeGarbageCollected<GCed>(GetHeap());
|
|
|
|
MemberWithCustomBarrier member1 = gced;
|
2020-03-31 17:19:02 +00:00
|
|
|
EXPECT_EQ(1u, CustomWriteBarrierPolicy::InitializingWriteBarriersTriggered);
|
|
|
|
EXPECT_EQ(0u, CustomWriteBarrierPolicy::AssigningWriteBarriersTriggered);
|
2020-04-08 07:48:37 +00:00
|
|
|
member1 = gced;
|
2020-03-31 17:19:02 +00:00
|
|
|
EXPECT_EQ(1u, CustomWriteBarrierPolicy::InitializingWriteBarriersTriggered);
|
|
|
|
EXPECT_EQ(1u, CustomWriteBarrierPolicy::AssigningWriteBarriersTriggered);
|
|
|
|
member1 = nullptr;
|
|
|
|
EXPECT_EQ(1u, CustomWriteBarrierPolicy::InitializingWriteBarriersTriggered);
|
|
|
|
EXPECT_EQ(1u, CustomWriteBarrierPolicy::AssigningWriteBarriersTriggered);
|
|
|
|
MemberWithCustomBarrier member2 = nullptr;
|
|
|
|
// No initializing barriers for std::nullptr_t.
|
|
|
|
EXPECT_EQ(1u, CustomWriteBarrierPolicy::InitializingWriteBarriersTriggered);
|
|
|
|
EXPECT_EQ(1u, CustomWriteBarrierPolicy::AssigningWriteBarriersTriggered);
|
2020-04-08 08:40:24 +00:00
|
|
|
member2 = kSentinelPointer;
|
|
|
|
EXPECT_EQ(kSentinelPointer, member2.Get());
|
|
|
|
// No initializing barriers for pointer sentinel.
|
2020-03-31 17:19:02 +00:00
|
|
|
EXPECT_EQ(1u, CustomWriteBarrierPolicy::InitializingWriteBarriersTriggered);
|
|
|
|
EXPECT_EQ(1u, CustomWriteBarrierPolicy::AssigningWriteBarriersTriggered);
|
|
|
|
member2.Swap(member1);
|
|
|
|
EXPECT_EQ(3u, CustomWriteBarrierPolicy::AssigningWriteBarriersTriggered);
|
|
|
|
}
|
|
|
|
|
2020-04-08 07:48:37 +00:00
|
|
|
TEST_F(MemberTest, CheckingPolicy) {
|
|
|
|
static constexpr size_t kElements = 64u;
|
2020-03-31 17:19:02 +00:00
|
|
|
CustomCheckingPolicy::ChecksTriggered = 0u;
|
2020-04-08 07:48:37 +00:00
|
|
|
|
|
|
|
for (std::size_t i = 0; i < kElements; ++i) {
|
|
|
|
CustomCheckingPolicy::Cached.push_back(
|
|
|
|
MakeGarbageCollected<GCed>(GetHeap()));
|
|
|
|
}
|
|
|
|
|
2020-03-31 17:19:02 +00:00
|
|
|
MemberWithCustomChecking member;
|
2020-04-08 07:48:37 +00:00
|
|
|
for (GCed* item : CustomCheckingPolicy::Cached) {
|
|
|
|
member = item;
|
2020-03-31 17:19:02 +00:00
|
|
|
}
|
2020-04-08 07:48:37 +00:00
|
|
|
EXPECT_EQ(CustomCheckingPolicy::Cached.size(),
|
2020-03-31 17:19:02 +00:00
|
|
|
CustomCheckingPolicy::ChecksTriggered);
|
|
|
|
}
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace cppgc
|