mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-07-28 07:55:41 +00:00
Only Constants.h remains in no namespace; but it may be addressed in the future as well. Bug: dawn:302 Change-Id: Ib9b9ab4b974ad1de1bb9f2302f4d24d8216f55e4 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/132841 Kokoro: Austin Eng <enga@chromium.org> Auto-Submit: Austin Eng <enga@chromium.org> Reviewed-by: Loko Kung <lokokung@google.com> Commit-Queue: Austin Eng <enga@chromium.org>
113 lines
3.1 KiB
C++
113 lines
3.1 KiB
C++
// Copyright 2020 The Dawn Authors
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
#include <memory>
|
|
|
|
#include "dawn/common/PlacementAllocated.h"
|
|
#include "gmock/gmock.h"
|
|
#include "gtest/gtest.h"
|
|
|
|
namespace dawn {
|
|
namespace {
|
|
|
|
using testing::InSequence;
|
|
using testing::StrictMock;
|
|
|
|
enum class DestructedClass {
|
|
Foo,
|
|
Bar,
|
|
};
|
|
|
|
class MockDestructor {
|
|
public:
|
|
MOCK_METHOD(void, Call, (void*, DestructedClass));
|
|
};
|
|
|
|
std::unique_ptr<StrictMock<MockDestructor>> mockDestructor;
|
|
|
|
class PlacementAllocatedTests : public testing::Test {
|
|
void SetUp() override { mockDestructor = std::make_unique<StrictMock<MockDestructor>>(); }
|
|
|
|
void TearDown() override { mockDestructor = nullptr; }
|
|
};
|
|
|
|
struct Foo : PlacementAllocated {
|
|
virtual ~Foo() { mockDestructor->Call(this, DestructedClass::Foo); }
|
|
};
|
|
|
|
struct Bar : Foo {
|
|
~Bar() override { mockDestructor->Call(this, DestructedClass::Bar); }
|
|
};
|
|
|
|
// Test that deletion calls the destructor and does not free memory.
|
|
TEST_F(PlacementAllocatedTests, DeletionDoesNotFreeMemory) {
|
|
void* ptr = malloc(sizeof(Foo));
|
|
|
|
Foo* foo = new (ptr) Foo();
|
|
|
|
EXPECT_CALL(*mockDestructor, Call(foo, DestructedClass::Foo));
|
|
delete foo;
|
|
|
|
// Touch the memory, this shouldn't crash.
|
|
static_assert(sizeof(Foo) >= sizeof(uint32_t));
|
|
*reinterpret_cast<uint32_t*>(foo) = 42;
|
|
|
|
free(ptr);
|
|
}
|
|
|
|
// Test that destructing an instance of a derived class calls the derived, then base destructor, and
|
|
// does not free memory.
|
|
TEST_F(PlacementAllocatedTests, DeletingDerivedClassCallsBaseDestructor) {
|
|
void* ptr = malloc(sizeof(Bar));
|
|
|
|
Bar* bar = new (ptr) Bar();
|
|
|
|
{
|
|
InSequence s;
|
|
EXPECT_CALL(*mockDestructor, Call(bar, DestructedClass::Bar));
|
|
EXPECT_CALL(*mockDestructor, Call(bar, DestructedClass::Foo));
|
|
delete bar;
|
|
}
|
|
|
|
// Touch the memory, this shouldn't crash.
|
|
static_assert(sizeof(Bar) >= sizeof(uint32_t));
|
|
*reinterpret_cast<uint32_t*>(bar) = 42;
|
|
|
|
free(ptr);
|
|
}
|
|
|
|
// Test that destructing an instance of a base class calls the derived, then base destructor, and
|
|
// does not free memory.
|
|
TEST_F(PlacementAllocatedTests, DeletingBaseClassCallsDerivedDestructor) {
|
|
void* ptr = malloc(sizeof(Bar));
|
|
|
|
Foo* foo = new (ptr) Bar();
|
|
|
|
{
|
|
InSequence s;
|
|
EXPECT_CALL(*mockDestructor, Call(foo, DestructedClass::Bar));
|
|
EXPECT_CALL(*mockDestructor, Call(foo, DestructedClass::Foo));
|
|
delete foo;
|
|
}
|
|
|
|
// Touch the memory, this shouldn't crash.
|
|
static_assert(sizeof(Bar) >= sizeof(uint32_t));
|
|
*reinterpret_cast<uint32_t*>(foo) = 42;
|
|
|
|
free(ptr);
|
|
}
|
|
|
|
} // anonymous namespace
|
|
} // namespace dawn
|