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 
TEST(ProcessTest,GetRandomNumberTest)22 TEST(ProcessTest, GetRandomNumberTest) {
23   const unsigned r1 = Process::GetRandomNumber();
24   const unsigned r2 = Process::GetRandomNumber();
25   // It should be extremely unlikely that both r1 and r2 are 0.
26   EXPECT_NE((r1 | r2), 0u);
27 }
28 
29 #ifdef _MSC_VER
30 #define setenv(name, var, ignore) _putenv_s(name, var)
31 #endif
32 
33 #if HAVE_SETENV || _MSC_VER
TEST(ProcessTest,Basic)34 TEST(ProcessTest, Basic) {
35   setenv("__LLVM_TEST_ENVIRON_VAR__", "abc", true);
36   Optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));
37   EXPECT_TRUE(val.hasValue());
38   EXPECT_STREQ("abc", val->c_str());
39 }
40 
TEST(ProcessTest,None)41 TEST(ProcessTest, None) {
42   Optional<std::string> val(
43       Process::GetEnv("__LLVM_TEST_ENVIRON_NO_SUCH_VAR__"));
44   EXPECT_FALSE(val.hasValue());
45 }
46 #endif
47 
48 #ifdef LLVM_ON_WIN32
TEST(ProcessTest,Wchar)49 TEST(ProcessTest, Wchar) {
50   SetEnvironmentVariableW(L"__LLVM_TEST_ENVIRON_VAR__", L"abcdefghijklmnopqrs");
51   Optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));
52   EXPECT_TRUE(val.hasValue());
53   EXPECT_STREQ("abcdefghijklmnopqrs", val->c_str());
54 }
55 #endif
56 
57 } // end anonymous namespace
58