[PM] Reformat some code with clang-format as I'm going to be editting as
[oota-llvm.git] / unittests / Support / ProcessTest.cpp
1 //===- unittest/Support/ProcessTest.cpp -----------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/Support/Process.h"
11 #include "gtest/gtest.h"
12
13 #ifdef LLVM_ON_WIN32
14 #include "windows.h"
15 #endif
16
17 namespace {
18
19 using namespace llvm;
20 using namespace sys;
21
22 TEST(ProcessTest, SelfProcess) {
23   EXPECT_TRUE(process::get_self());
24   EXPECT_EQ(process::get_self(), process::get_self());
25
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());
30 #endif
31
32   EXPECT_LT(1u, process::get_self()->page_size());
33
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());
40 }
41
42 #ifdef _MSC_VER
43 #define setenv(name, var, ignore) _putenv_s(name, var)
44 #endif
45
46 #if HAVE_SETENV || _MSC_VER
47 TEST(ProcessTest, Basic) {
48   setenv("__LLVM_TEST_ENVIRON_VAR__", "abc", true);
49   Optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));
50   EXPECT_TRUE(val.hasValue());
51   EXPECT_STREQ("abc", val->c_str());
52 }
53
54 TEST(ProcessTest, None) {
55   Optional<std::string> val(
56       Process::GetEnv("__LLVM_TEST_ENVIRON_NO_SUCH_VAR__"));
57   EXPECT_FALSE(val.hasValue());
58 }
59 #endif
60
61 #ifdef LLVM_ON_WIN32
62 TEST(ProcessTest, Wchar) {
63   SetEnvironmentVariableW(L"__LLVM_TEST_ENVIRON_VAR__", L"abcdefghijklmnopqrs");
64   Optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));
65   EXPECT_TRUE(val.hasValue());
66   EXPECT_STREQ("abcdefghijklmnopqrs", val->c_str());
67 }
68 #endif
69
70 } // end anonymous namespace