V8 Project
platform-unittest.cc
Go to the documentation of this file.
1 // Copyright 2014 the V8 project 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.
4 
6 
7 #if V8_OS_POSIX
8 #include <unistd.h> // NOLINT
9 #endif
10 
11 #if V8_OS_WIN
12 #include "src/base/win32-headers.h"
13 #endif
14 #include "testing/gtest/include/gtest/gtest.h"
15 
16 namespace v8 {
17 namespace base {
18 
19 TEST(OS, GetCurrentProcessId) {
20 #if V8_OS_POSIX
21  EXPECT_EQ(static_cast<int>(getpid()), OS::GetCurrentProcessId());
22 #endif
23 
24 #if V8_OS_WIN
25  EXPECT_EQ(static_cast<int>(::GetCurrentProcessId()),
27 #endif
28 }
29 
30 
31 namespace {
32 
33 class SelfJoinThread FINAL : public Thread {
34  public:
35  SelfJoinThread() : Thread(Options("SelfJoinThread")) {}
36  virtual void Run() OVERRIDE { Join(); }
37 };
38 
39 } // namespace
40 
41 
42 TEST(Thread, SelfJoin) {
43  SelfJoinThread thread;
44  thread.Start();
45  thread.Join();
46 }
47 
48 
49 namespace {
50 
51 class ThreadLocalStorageTest : public Thread, public ::testing::Test {
52  public:
53  ThreadLocalStorageTest() : Thread(Options("ThreadLocalStorageTest")) {
54  for (size_t i = 0; i < arraysize(keys_); ++i) {
56  }
57  }
59  for (size_t i = 0; i < arraysize(keys_); ++i) {
61  }
62  }
63 
64  virtual void Run() FINAL OVERRIDE {
65  for (size_t i = 0; i < arraysize(keys_); i++) {
66  CHECK(!Thread::HasThreadLocal(keys_[i]));
67  }
68  for (size_t i = 0; i < arraysize(keys_); i++) {
69  Thread::SetThreadLocal(keys_[i], GetValue(i));
70  }
71  for (size_t i = 0; i < arraysize(keys_); i++) {
73  }
74  for (size_t i = 0; i < arraysize(keys_); i++) {
75  CHECK_EQ(GetValue(i), Thread::GetThreadLocal(keys_[i]));
76  CHECK_EQ(GetValue(i), Thread::GetExistingThreadLocal(keys_[i]));
77  }
78  for (size_t i = 0; i < arraysize(keys_); i++) {
79  Thread::SetThreadLocal(keys_[i], GetValue(arraysize(keys_) - i - 1));
80  }
81  for (size_t i = 0; i < arraysize(keys_); i++) {
83  }
84  for (size_t i = 0; i < arraysize(keys_); i++) {
85  CHECK_EQ(GetValue(arraysize(keys_) - i - 1),
86  Thread::GetThreadLocal(keys_[i]));
87  CHECK_EQ(GetValue(arraysize(keys_) - i - 1),
89  }
90  }
91 
92  private:
93  static void* GetValue(size_t x) {
94  return reinterpret_cast<void*>(static_cast<uintptr_t>(x + 1));
95  }
96 
98 };
99 
100 } // namespace
101 
102 
103 TEST_F(ThreadLocalStorageTest, DoTest) {
104  Run();
105  Start();
106  Join();
107 }
108 
109 } // namespace base
110 } // namespace v8
static int GetCurrentProcessId()
static void DeleteThreadLocalKey(LocalStorageKey key)
int32_t LocalStorageKey
Definition: platform.h:416
static LocalStorageKey CreateThreadLocalKey()
static void * GetThreadLocal(LocalStorageKey key)
static void * GetExistingThreadLocal(LocalStorageKey key)
Definition: platform.h:481
static bool HasThreadLocal(LocalStorageKey key)
Definition: platform.h:469
static void SetThreadLocal(LocalStorageKey key, void *value)
#define OVERRIDE
#define CHECK_EQ(expected, value)
Definition: logging.h:169
#define CHECK(condition)
Definition: logging.h:36
#define arraysize(array)
Definition: macros.h:86
TEST_F(ThreadLocalStorageTest, DoTest)
TEST(CPUTest, FeatureImplications)
Definition: cpu-unittest.cc:11
Debugger support for the V8 JavaScript engine.
Definition: accessors.cc:20