no_destructor_test.cc
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 <cstdint> 6 #include <cstdlib> 7 #include <utility> 8 9 #include "util/no_destructor.h" 10 #include "util/testharness.h" 11 12 namespace leveldb { 13 14 namespace { 15 16 struct DoNotDestruct { 17 public: 18 DoNotDestruct(uint32_t a, uint64_t b) : a(a), b(b) {} 19 ~DoNotDestruct() { std::abort(); } 20 21 // Used to check constructor argument forwarding. 22 uint32_t a; 23 uint64_t b; 24 }; 25 26 constexpr const uint32_t kGoldenA = 0xdeadbeef; 27 constexpr const uint64_t kGoldenB = 0xaabbccddeeffaabb; 28 29 } // namespace 30 31 class NoDestructorTest {}; 32 33 TEST(NoDestructorTest, StackInstance) { 34 NoDestructor<DoNotDestruct> instance(kGoldenA, kGoldenB); 35 ASSERT_EQ(kGoldenA, instance.get()->a); 36 ASSERT_EQ(kGoldenB, instance.get()->b); 37 } 38 39 TEST(NoDestructorTest, StaticInstance) { 40 static NoDestructor<DoNotDestruct> instance(kGoldenA, kGoldenB); 41 ASSERT_EQ(kGoldenA, instance.get()->a); 42 ASSERT_EQ(kGoldenB, instance.get()->b); 43 } 44 45 } // namespace leveldb 46 47 int main(int argc, char** argv) { return leveldb::test::RunAllTests(); }