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