1 | // Copyright (c) 2018 The LevelDB Authors. All rights reserved. |
2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. |
4 | |
5 | #include "util/no_destructor.h" |
6 | |
7 | #include <cstdint> |
8 | #include <cstdlib> |
9 | #include <utility> |
10 | |
11 | #include "gtest/gtest.h" |
12 | |
13 | namespace leveldb { |
14 | |
15 | namespace { |
16 | |
17 | struct DoNotDestruct { |
18 | public: |
19 | DoNotDestruct(uint32_t a, uint64_t b) : a(a), b(b) {} |
20 | ~DoNotDestruct() { std::abort(); } |
21 | |
22 | // Used to check constructor argument forwarding. |
23 | uint32_t a; |
24 | uint64_t b; |
25 | }; |
26 | |
27 | constexpr const uint32_t kGoldenA = 0xdeadbeef; |
28 | constexpr const uint64_t kGoldenB = 0xaabbccddeeffaabb; |
29 | |
30 | } // namespace |
31 | |
32 | TEST(NoDestructorTest, StackInstance) { |
33 | NoDestructor<DoNotDestruct> instance(kGoldenA, kGoldenB); |
34 | ASSERT_EQ(kGoldenA, instance.get()->a); |
35 | ASSERT_EQ(kGoldenB, instance.get()->b); |
36 | } |
37 | |
38 | TEST(NoDestructorTest, StaticInstance) { |
39 | static NoDestructor<DoNotDestruct> instance(kGoldenA, kGoldenB); |
40 | ASSERT_EQ(kGoldenA, instance.get()->a); |
41 | ASSERT_EQ(kGoldenB, instance.get()->b); |
42 | } |
43 | |
44 | } // namespace leveldb |
45 | |