v8/test/unittests/base/platform/platform-unittest.cc
Bruce Dawson fb9e129964 Remove windows.h from win32-headers.h
Windows.h causes massive namespace pollution with its defining of many
macros, it adds to build times, it disables warnings, and it makes it
easier to write non-portable code.

This change removes windows.h from V8's win32-headers.h. It does this
by replicating the small number of typedefs that are needed and by
defining three "proxy" types that are the same size and layout. The
V8ToWindowsType functions are used to reinterpret_cast between the
types.

Prior to this change there were over 760 v8-related source files that
include windows.h. After this change there are 16.

Bug: chromium:796644
Change-Id: I89efeed47028faae72de2da4f1dae345d8d7746c
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3042215
Commit-Queue: Bruce Dawson <brucedawson@chromium.org>
Reviewed-by: Hannes Payer <hpayer@chromium.org>
Reviewed-by: Leszek Swirski <leszeks@chromium.org>
Cr-Commit-Position: refs/heads/master@{#76064}
2021-08-03 16:07:16 +00:00

110 lines
3.0 KiB
C++

// 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"
#if V8_OS_WIN
#include <windows.h>
#endif
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:
ThreadLocalStorageTest() : Thread(Options("ThreadLocalStorageTest")) {
for (size_t i = 0; i < arraysize(keys_); ++i) {
keys_[i] = Thread::CreateThreadLocalKey();
}
}
~ThreadLocalStorageTest() override {
for (size_t i = 0; i < arraysize(keys_); ++i) {
Thread::DeleteThreadLocalKey(keys_[i]);
}
}
void Run() final {
for (size_t i = 0; i < arraysize(keys_); i++) {
CHECK(!Thread::HasThreadLocal(keys_[i]));
}
for (size_t i = 0; i < arraysize(keys_); i++) {
Thread::SetThreadLocal(keys_[i], GetValue(i));
}
for (size_t i = 0; i < arraysize(keys_); i++) {
CHECK(Thread::HasThreadLocal(keys_[i]));
}
for (size_t i = 0; i < arraysize(keys_); i++) {
CHECK_EQ(GetValue(i), Thread::GetThreadLocal(keys_[i]));
CHECK_EQ(GetValue(i), Thread::GetExistingThreadLocal(keys_[i]));
}
for (size_t i = 0; i < arraysize(keys_); i++) {
Thread::SetThreadLocal(keys_[i], GetValue(arraysize(keys_) - i - 1));
}
for (size_t i = 0; i < arraysize(keys_); i++) {
CHECK(Thread::HasThreadLocal(keys_[i]));
}
for (size_t i = 0; i < arraysize(keys_); i++) {
CHECK_EQ(GetValue(arraysize(keys_) - i - 1),
Thread::GetThreadLocal(keys_[i]));
CHECK_EQ(GetValue(arraysize(keys_) - i - 1),
Thread::GetExistingThreadLocal(keys_[i]));
}
}
private:
static void* GetValue(size_t x) {
return bit_cast<void*>(static_cast<uintptr_t>(x + 1));
}
// Older versions of Android have fewer TLS slots (nominally 64, but the
// system uses "about 5 of them" itself).
Thread::LocalStorageKey keys_[32];
};
} // namespace
TEST_F(ThreadLocalStorageTest, DoTest) {
Run();
CHECK(Start());
Join();
}
TEST(StackTest, GetStackStart) { EXPECT_NE(nullptr, Stack::GetStackStart()); }
TEST(StackTest, GetCurrentStackPosition) {
EXPECT_NE(nullptr, Stack::GetCurrentStackPosition());
}
#if !V8_OS_FUCHSIA
TEST(StackTest, StackVariableInBounds) {
void* dummy;
ASSERT_GT(static_cast<void*>(Stack::GetStackStart()),
Stack::GetCurrentStackPosition());
EXPECT_GT(static_cast<void*>(Stack::GetStackStart()),
Stack::GetRealStackAddressForSlot(&dummy));
EXPECT_LT(static_cast<void*>(Stack::GetCurrentStackPosition()),
Stack::GetRealStackAddressForSlot(&dummy));
}
#endif // !V8_OS_FUCHSIA
} // namespace base
} // namespace v8