Austin Eng | e5534c4 | 2020-03-04 17:48:24 +0000 | [diff] [blame] | 1 | // Copyright 2020 The Dawn Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include <gmock/gmock.h> |
| 16 | #include <gtest/gtest.h> |
| 17 | |
| 18 | #include "common/PlacementAllocated.h" |
| 19 | |
| 20 | using namespace testing; |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | enum class DestructedClass { |
| 25 | Foo, |
| 26 | Bar, |
| 27 | }; |
| 28 | |
| 29 | class MockDestructor { |
| 30 | public: |
Corentin Wallez | f941205 | 2020-04-16 16:39:06 +0000 | [diff] [blame] | 31 | MOCK_METHOD(void, Call, (void*, DestructedClass)); |
Austin Eng | e5534c4 | 2020-03-04 17:48:24 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | std::unique_ptr<StrictMock<MockDestructor>> mockDestructor; |
| 35 | |
| 36 | class PlacementAllocatedTests : public Test { |
| 37 | void SetUp() override { |
| 38 | mockDestructor = std::make_unique<StrictMock<MockDestructor>>(); |
| 39 | } |
| 40 | |
| 41 | void TearDown() override { |
| 42 | mockDestructor = nullptr; |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | struct Foo : PlacementAllocated { |
| 47 | virtual ~Foo() { |
| 48 | mockDestructor->Call(this, DestructedClass::Foo); |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | struct Bar : Foo { |
| 53 | ~Bar() override { |
| 54 | mockDestructor->Call(this, DestructedClass::Bar); |
| 55 | } |
| 56 | }; |
| 57 | } // namespace |
| 58 | |
| 59 | // Test that deletion calls the destructor and does not free memory. |
| 60 | TEST_F(PlacementAllocatedTests, DeletionDoesNotFreeMemory) { |
| 61 | void* ptr = malloc(sizeof(Foo)); |
| 62 | |
| 63 | Foo* foo = new (ptr) Foo(); |
| 64 | |
| 65 | EXPECT_CALL(*mockDestructor, Call(foo, DestructedClass::Foo)); |
| 66 | delete foo; |
| 67 | |
| 68 | // Touch the memory, this shouldn't crash. |
| 69 | static_assert(sizeof(Foo) >= sizeof(uint32_t), ""); |
| 70 | *reinterpret_cast<uint32_t*>(foo) = 42; |
| 71 | |
| 72 | free(ptr); |
| 73 | } |
| 74 | |
| 75 | // Test that destructing an instance of a derived class calls the derived, then base destructor, and |
| 76 | // does not free memory. |
| 77 | TEST_F(PlacementAllocatedTests, DeletingDerivedClassCallsBaseDestructor) { |
| 78 | void* ptr = malloc(sizeof(Bar)); |
| 79 | |
| 80 | Bar* bar = new (ptr) Bar(); |
| 81 | |
| 82 | { |
| 83 | InSequence s; |
| 84 | EXPECT_CALL(*mockDestructor, Call(bar, DestructedClass::Bar)); |
| 85 | EXPECT_CALL(*mockDestructor, Call(bar, DestructedClass::Foo)); |
| 86 | delete bar; |
| 87 | } |
| 88 | |
| 89 | // Touch the memory, this shouldn't crash. |
| 90 | static_assert(sizeof(Bar) >= sizeof(uint32_t), ""); |
| 91 | *reinterpret_cast<uint32_t*>(bar) = 42; |
| 92 | |
| 93 | free(ptr); |
| 94 | } |
| 95 | |
| 96 | // Test that destructing an instance of a base class calls the derived, then base destructor, and |
| 97 | // does not free memory. |
| 98 | TEST_F(PlacementAllocatedTests, DeletingBaseClassCallsDerivedDestructor) { |
| 99 | void* ptr = malloc(sizeof(Bar)); |
| 100 | |
| 101 | Foo* foo = new (ptr) Bar(); |
| 102 | |
| 103 | { |
| 104 | InSequence s; |
| 105 | EXPECT_CALL(*mockDestructor, Call(foo, DestructedClass::Bar)); |
| 106 | EXPECT_CALL(*mockDestructor, Call(foo, DestructedClass::Foo)); |
| 107 | delete foo; |
| 108 | } |
| 109 | |
| 110 | // Touch the memory, this shouldn't crash. |
| 111 | static_assert(sizeof(Bar) >= sizeof(uint32_t), ""); |
| 112 | *reinterpret_cast<uint32_t*>(foo) = 42; |
| 113 | |
| 114 | free(ptr); |
| 115 | } |