10225501448 李度 10225101546 陈胤遒 10215501422 高宇菲
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.2 KiB

  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 <cstdint>
  5. #include <cstdlib>
  6. #include <utility>
  7. #include "util/no_destructor.h"
  8. #include "util/testharness.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. class NoDestructorTest { };
  23. TEST(NoDestructorTest, StackInstance) {
  24. NoDestructor<DoNotDestruct> instance(kGoldenA, kGoldenB);
  25. ASSERT_EQ(kGoldenA, instance.get()->a);
  26. ASSERT_EQ(kGoldenB, instance.get()->b);
  27. }
  28. TEST(NoDestructorTest, StaticInstance) {
  29. static NoDestructor<DoNotDestruct> instance(kGoldenA, kGoldenB);
  30. ASSERT_EQ(kGoldenA, instance.get()->a);
  31. ASSERT_EQ(kGoldenB, instance.get()->b);
  32. }
  33. } // namespace leveldb
  34. int main(int argc, char** argv) {
  35. return leveldb::test::RunAllTests();
  36. }