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