platform-unittest.cc 2.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/base/platform/platform.h"

#include "testing/gtest/include/gtest/gtest.h"

namespace v8 {
namespace base {

TEST(OS, GetCurrentProcessId) {
#if V8_OS_POSIX
  EXPECT_EQ(static_cast<int>(getpid()), OS::GetCurrentProcessId());
#endif

#if V8_OS_WIN
  EXPECT_EQ(static_cast<int>(::GetCurrentProcessId()),
            OS::GetCurrentProcessId());
#endif
}


namespace {

class ThreadLocalStorageTest : public Thread, public ::testing::Test {
 public:
28
  ThreadLocalStorageTest() : Thread(Options("ThreadLocalStorageTest")) {
29
    for (size_t i = 0; i < arraysize(keys_); ++i) {
30 31 32
      keys_[i] = Thread::CreateThreadLocalKey();
    }
  }
33
  ~ThreadLocalStorageTest() override {
34
    for (size_t i = 0; i < arraysize(keys_); ++i) {
35 36 37 38
      Thread::DeleteThreadLocalKey(keys_[i]);
    }
  }

39
  void Run() final {
40
    for (size_t i = 0; i < arraysize(keys_); i++) {
41 42
      CHECK(!Thread::HasThreadLocal(keys_[i]));
    }
43
    for (size_t i = 0; i < arraysize(keys_); i++) {
44 45
      Thread::SetThreadLocal(keys_[i], GetValue(i));
    }
46
    for (size_t i = 0; i < arraysize(keys_); i++) {
47 48
      CHECK(Thread::HasThreadLocal(keys_[i]));
    }
49
    for (size_t i = 0; i < arraysize(keys_); i++) {
50 51 52
      CHECK_EQ(GetValue(i), Thread::GetThreadLocal(keys_[i]));
      CHECK_EQ(GetValue(i), Thread::GetExistingThreadLocal(keys_[i]));
    }
53 54
    for (size_t i = 0; i < arraysize(keys_); i++) {
      Thread::SetThreadLocal(keys_[i], GetValue(arraysize(keys_) - i - 1));
55
    }
56
    for (size_t i = 0; i < arraysize(keys_); i++) {
57 58
      CHECK(Thread::HasThreadLocal(keys_[i]));
    }
59 60
    for (size_t i = 0; i < arraysize(keys_); i++) {
      CHECK_EQ(GetValue(arraysize(keys_) - i - 1),
61
               Thread::GetThreadLocal(keys_[i]));
62
      CHECK_EQ(GetValue(arraysize(keys_) - i - 1),
63 64 65 66 67 68
               Thread::GetExistingThreadLocal(keys_[i]));
    }
  }

 private:
  static void* GetValue(size_t x) {
69
    return bit_cast<void*>(static_cast<uintptr_t>(x + 1));
70 71
  }

72 73 74
  // Older versions of Android have fewer TLS slots (nominally 64, but the
  // system uses "about 5 of them" itself).
  Thread::LocalStorageKey keys_[32];
75 76
};

77
}  // namespace
78 79 80 81 82 83 84 85 86 87


TEST_F(ThreadLocalStorageTest, DoTest) {
  Run();
  Start();
  Join();
}

}  // namespace base
}  // namespace v8