Bump version to 19.1.0-rc3
[llvm-project.git] / llvm / unittests / Support / ProcessTest.cpp
blobd508a67c57cddcc4840bb32989da080ef4caf39b
1 //===- unittest/Support/ProcessTest.cpp -----------------------------------===//
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 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/Process.h"
10 #include "llvm/Support/Error.h"
11 #include "llvm/TargetParser/Host.h"
12 #include "llvm/TargetParser/Triple.h"
13 #include "gtest/gtest.h"
14 #include <optional>
16 #ifdef _WIN32
17 #include <windows.h>
18 #endif
20 namespace {
22 using namespace llvm;
23 using namespace sys;
25 TEST(ProcessTest, GetProcessIdTest) {
26 const Process::Pid pid = Process::getProcessId();
28 #ifdef _WIN32
29 EXPECT_EQ((DWORD)pid, ::GetCurrentProcessId());
30 #else
31 EXPECT_EQ(pid, ::getpid());
32 #endif
35 TEST(ProcessTest, GetRandomNumberTest) {
36 const unsigned r1 = Process::GetRandomNumber();
37 const unsigned r2 = Process::GetRandomNumber();
38 // It should be extremely unlikely that both r1 and r2 are 0.
39 EXPECT_NE((r1 | r2), 0u);
42 #ifdef _MSC_VER
43 #define setenv(name, var, ignore) _putenv_s(name, var)
44 #endif
46 #if HAVE_SETENV || _MSC_VER
47 TEST(ProcessTest, Basic) {
48 setenv("__LLVM_TEST_ENVIRON_VAR__", "abc", true);
49 std::optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));
50 EXPECT_TRUE(val.has_value());
51 EXPECT_STREQ("abc", val->c_str());
54 TEST(ProcessTest, None) {
55 std::optional<std::string> val(
56 Process::GetEnv("__LLVM_TEST_ENVIRON_NO_SUCH_VAR__"));
57 EXPECT_FALSE(val.has_value());
59 #endif
61 #ifdef _WIN32
63 TEST(ProcessTest, EmptyVal) {
64 SetEnvironmentVariableA("__LLVM_TEST_ENVIRON_VAR__", "");
65 std::optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));
66 EXPECT_TRUE(val.has_value());
67 EXPECT_STREQ("", val->c_str());
70 TEST(ProcessTest, Wchar) {
71 SetEnvironmentVariableW(L"__LLVM_TEST_ENVIRON_VAR__", L"abcdefghijklmnopqrs");
72 std::optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));
73 EXPECT_TRUE(val.has_value());
74 EXPECT_STREQ("abcdefghijklmnopqrs", val->c_str());
76 #endif
78 class PageSizeTest : public testing::Test {
79 Triple Host;
81 protected:
82 PageSizeTest() : Host(Triple::normalize(sys::getProcessTriple())) {}
84 bool isSupported() const {
85 // For now just on X86-64 and Aarch64. This can be expanded in the future.
86 return (Host.getArch() == Triple::x86_64 ||
87 Host.getArch() == Triple::aarch64) &&
88 Host.getOS() == Triple::Linux;
91 bool pageSizeAsExpected(unsigned PageSize) const {
92 switch (Host.getArch()) {
93 case Triple::x86_64:
94 return PageSize == 4096;
95 case Triple::aarch64:
96 // supported granule sizes are 4k, 16k and 64k
97 return PageSize == 4096 || PageSize == 16384 || PageSize == 65536;
98 default:
99 llvm_unreachable("unexpected arch!");
104 TEST_F(PageSizeTest, PageSize) {
105 if (!isSupported())
106 GTEST_SKIP();
108 llvm::Expected<unsigned> Result = llvm::sys::Process::getPageSize();
109 ASSERT_FALSE(!Result);
110 ASSERT_TRUE(pageSizeAsExpected(*Result));
113 } // end anonymous namespace