1 //===- unittest/Support/ProcessTest.cpp -----------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/Process.h"
11 #include "gtest/gtest.h"
22 TEST(ProcessTest, SelfProcess) {
23 EXPECT_TRUE(process::get_self());
24 EXPECT_EQ(process::get_self(), process::get_self());
26 #if defined(LLVM_ON_UNIX)
27 EXPECT_EQ(getpid(), process::get_self()->get_id());
28 #elif defined(LLVM_ON_WIN32)
29 EXPECT_EQ(GetCurrentProcessId(), process::get_self()->get_id());
32 EXPECT_LT(1u, process::get_self()->page_size());
34 EXPECT_LT(TimeValue::MinTime, process::get_self()->get_user_time());
35 EXPECT_GT(TimeValue::MaxTime, process::get_self()->get_user_time());
36 EXPECT_LT(TimeValue::MinTime, process::get_self()->get_system_time());
37 EXPECT_GT(TimeValue::MaxTime, process::get_self()->get_system_time());
38 EXPECT_LT(TimeValue::MinTime, process::get_self()->get_wall_time());
39 EXPECT_GT(TimeValue::MaxTime, process::get_self()->get_wall_time());
42 TEST(ProcessTest, GetRandomNumberTest) {
43 const unsigned r1 = Process::GetRandomNumber();
44 const unsigned r2 = Process::GetRandomNumber();
45 // It should be extremely unlikely that both r1 and r2 are 0.
46 EXPECT_NE((r1 | r2), 0u);
50 #define setenv(name, var, ignore) _putenv_s(name, var)
53 #if HAVE_SETENV || _MSC_VER
54 TEST(ProcessTest, Basic) {
55 setenv("__LLVM_TEST_ENVIRON_VAR__", "abc", true);
56 Optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));
57 EXPECT_TRUE(val.hasValue());
58 EXPECT_STREQ("abc", val->c_str());
61 TEST(ProcessTest, None) {
62 Optional<std::string> val(
63 Process::GetEnv("__LLVM_TEST_ENVIRON_NO_SUCH_VAR__"));
64 EXPECT_FALSE(val.hasValue());
69 TEST(ProcessTest, Wchar) {
70 SetEnvironmentVariableW(L"__LLVM_TEST_ENVIRON_VAR__", L"abcdefghijklmnopqrs");
71 Optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));
72 EXPECT_TRUE(val.hasValue());
73 EXPECT_STREQ("abcdefghijklmnopqrs", val->c_str());
77 } // end anonymous namespace