ThreadLocalTest.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //===- llvm/unittest/Support/ThreadLocalTest.cpp - ThreadLocal tests ------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/Support/ThreadLocal.h"
  10. #include "gtest/gtest.h"
  11. #include <type_traits>
  12. using namespace llvm;
  13. using namespace sys;
  14. namespace {
  15. class ThreadLocalTest : public ::testing::Test {
  16. };
  17. struct S {
  18. int i;
  19. };
  20. TEST_F(ThreadLocalTest, Basics) {
  21. ThreadLocal<const S> x;
  22. static_assert(
  23. std::is_const<std::remove_pointer<decltype(x.get())>::type>::value,
  24. "ThreadLocal::get didn't return a pointer to const object");
  25. EXPECT_EQ(nullptr, x.get());
  26. S s;
  27. x.set(&s);
  28. EXPECT_EQ(&s, x.get());
  29. x.erase();
  30. EXPECT_EQ(nullptr, x.get());
  31. ThreadLocal<S> y;
  32. static_assert(
  33. !std::is_const<std::remove_pointer<decltype(y.get())>::type>::value,
  34. "ThreadLocal::get returned a pointer to const object");
  35. EXPECT_EQ(nullptr, y.get());
  36. y.set(&s);
  37. EXPECT_EQ(&s, y.get());
  38. y.erase();
  39. EXPECT_EQ(nullptr, y.get());
  40. }
  41. }