• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
10 #include "llvm/Support/ThreadLocal.h"
11 #include "gtest/gtest.h"
12 #include <type_traits>
13 
14 using namespace llvm;
15 using namespace sys;
16 
17 namespace {
18 
19 class ThreadLocalTest : public ::testing::Test {
20 };
21 
22 struct S {
23   int i;
24 };
25 
TEST_F(ThreadLocalTest,Basics)26 TEST_F(ThreadLocalTest, Basics) {
27   ThreadLocal<const S> x;
28 
29   static_assert(
30       std::is_const<std::remove_pointer<decltype(x.get())>::type>::value,
31       "ThreadLocal::get didn't return a pointer to const object");
32 
33   EXPECT_EQ(nullptr, x.get());
34 
35   S s;
36   x.set(&s);
37   EXPECT_EQ(&s, x.get());
38 
39   x.erase();
40   EXPECT_EQ(nullptr, x.get());
41 
42   ThreadLocal<S> y;
43 
44   static_assert(
45       !std::is_const<std::remove_pointer<decltype(y.get())>::type>::value,
46       "ThreadLocal::get returned a pointer to const object");
47 
48   EXPECT_EQ(nullptr, y.get());
49 
50   y.set(&s);
51   EXPECT_EQ(&s, y.get());
52 
53   y.erase();
54   EXPECT_EQ(nullptr, y.get());
55 }
56 
57 }
58