v8/test/unittests/heap/cppgc/object-size-trait-unittest.cc
Michael Lippautz 6d11bcda29 cppgc: Introduce ObjectSizeTrait
Users of padded objects must know the actual object size for
implementing custom finalizers.

Bug: chromium:1056170
Change-Id: I0ddf9066cfece0a8d18a9e6fd985d09449eea92a
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2644941
Commit-Queue: Michael Lippautz <mlippautz@chromium.org>
Reviewed-by: Omer Katz <omerkatz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#72269}
2021-01-22 20:53:55 +00:00

52 lines
1.5 KiB
C++

// 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.
#include "include/cppgc/object-size-trait.h"
#include "include/cppgc/allocation.h"
#include "include/cppgc/garbage-collected.h"
#include "src/heap/cppgc/heap.h"
#include "test/unittests/heap/cppgc/tests.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace cppgc {
namespace internal {
namespace {
class ObjectSizeTraitTest : public testing::TestWithHeap {};
class GCed : public GarbageCollected<GCed> {
public:
void Trace(Visitor*) const {}
};
class NotGCed {};
class Mixin : public GarbageCollectedMixin {};
class UnmanagedMixinWithDouble {
protected:
virtual void ForceVTable() {}
};
class GCedWithMixin : public GarbageCollected<GCedWithMixin>,
public UnmanagedMixinWithDouble,
public Mixin {};
} // namespace
TEST_F(ObjectSizeTraitTest, GarbageCollected) {
auto* obj = cppgc::MakeGarbageCollected<GCed>(GetAllocationHandle());
EXPECT_GE(subtle::ObjectSizeTrait<GCed>::GetSize(*obj), sizeof(GCed));
}
TEST_F(ObjectSizeTraitTest, GarbageCollectedMixin) {
auto* obj = cppgc::MakeGarbageCollected<GCedWithMixin>(GetAllocationHandle());
Mixin& mixin = static_cast<Mixin&>(*obj);
EXPECT_NE(static_cast<void*>(&mixin), obj);
EXPECT_GE(subtle::ObjectSizeTrait<Mixin>::GetSize(mixin),
sizeof(GCedWithMixin));
}
} // namespace internal
} // namespace cppgc