ThreadLocalTest.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //===- llvm/unittest/Support/ThreadLocalTest.cpp - ThreadLocal tests ------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/Support/ThreadLocal.h"
  9. #include "gtest/gtest.h"
  10. #include <type_traits>
  11. using namespace llvm;
  12. using namespace sys;
  13. namespace {
  14. class ThreadLocalTest : public ::testing::Test {
  15. };
  16. struct S {
  17. int i;
  18. };
  19. TEST_F(ThreadLocalTest, Basics) {
  20. ThreadLocal<const S> x;
  21. static_assert(
  22. std::is_const<std::remove_pointer<decltype(x.get())>::type>::value,
  23. "ThreadLocal::get didn't return a pointer to const object");
  24. EXPECT_EQ(nullptr, x.get());
  25. S s;
  26. x.set(&s);
  27. EXPECT_EQ(&s, x.get());
  28. x.erase();
  29. EXPECT_EQ(nullptr, x.get());
  30. ThreadLocal<S> y;
  31. static_assert(
  32. !std::is_const<std::remove_pointer<decltype(y.get())>::type>::value,
  33. "ThreadLocal::get returned a pointer to const object");
  34. EXPECT_EQ(nullptr, y.get());
  35. y.set(&s);
  36. EXPECT_EQ(&s, y.get());
  37. y.erase();
  38. EXPECT_EQ(nullptr, y.get());
  39. }
  40. }