no_destructor_test.cc 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. #include "util/no_destructor.h"
  5. #include <cstdint>
  6. #include <cstdlib>
  7. #include <utility>
  8. #include "gtest/gtest.h"
  9. namespace leveldb {
  10. namespace {
  11. struct DoNotDestruct {
  12. public:
  13. DoNotDestruct(uint32_t a, uint64_t b) : a(a), b(b) {}
  14. ~DoNotDestruct() { std::abort(); }
  15. // Used to check constructor argument forwarding.
  16. uint32_t a;
  17. uint64_t b;
  18. };
  19. constexpr const uint32_t kGoldenA = 0xdeadbeef;
  20. constexpr const uint64_t kGoldenB = 0xaabbccddeeffaabb;
  21. } // namespace
  22. TEST(NoDestructorTest, StackInstance) {
  23. NoDestructor<DoNotDestruct> instance(kGoldenA, kGoldenB);
  24. ASSERT_EQ(kGoldenA, instance.get()->a);
  25. ASSERT_EQ(kGoldenB, instance.get()->b);
  26. }
  27. TEST(NoDestructorTest, StaticInstance) {
  28. static NoDestructor<DoNotDestruct> instance(kGoldenA, kGoldenB);
  29. ASSERT_EQ(kGoldenA, instance.get()->a);
  30. ASSERT_EQ(kGoldenB, instance.get()->b);
  31. }
  32. } // namespace leveldb