1 // Copyright 2018 The Abseil 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 "absl/memory/memory.h"
16 
17 #include "gtest/gtest.h"
18 #include "absl/base/internal/exception_safety_testing.h"
19 
20 namespace absl {
21 namespace {
22 
23 constexpr int kLength = 50;
24 using Thrower = testing::ThrowingValue<testing::TypeSpec::kEverythingThrows>;
25 using ThrowerStorage =
26     absl::aligned_storage_t<sizeof(Thrower), alignof(Thrower)>;
27 using ThrowerList = std::array<ThrowerStorage, kLength>;
28 
TEST(MakeUnique,CheckForLeaks)29 TEST(MakeUnique, CheckForLeaks) {
30   constexpr int kValue = 321;
31   auto tester = testing::MakeExceptionSafetyTester()
32                     .WithInitialValue(Thrower(kValue))
33                     // Ensures make_unique does not modify the input. The real
34                     // test, though, is ConstructorTracker checking for leaks.
35                     .WithInvariants(testing::strong_guarantee);
36 
37   EXPECT_TRUE(tester.Test([](Thrower* thrower) {
38     static_cast<void>(absl::make_unique<Thrower>(*thrower));
39   }));
40 
41   EXPECT_TRUE(tester.Test([](Thrower* thrower) {
42     static_cast<void>(absl::make_unique<Thrower>(std::move(*thrower)));
43   }));
44 
45   // Test T[n] overload
46   EXPECT_TRUE(tester.Test([&](Thrower*) {
47     static_cast<void>(absl::make_unique<Thrower[]>(kLength));
48   }));
49 }
50 
51 }  // namespace
52 }  // namespace absl
53