xref: /minix3/external/bsd/llvm/dist/llvm/unittests/Support/ProcessTest.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===- unittest/Support/ProcessTest.cpp -----------------------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc 
10f4a2713aSLionel Sambuc #include "llvm/Support/Process.h"
11f4a2713aSLionel Sambuc #include "gtest/gtest.h"
12f4a2713aSLionel Sambuc 
13f4a2713aSLionel Sambuc #ifdef LLVM_ON_WIN32
14*0a6a1f1dSLionel Sambuc #include <windows.h>
15f4a2713aSLionel Sambuc #endif
16f4a2713aSLionel Sambuc 
17f4a2713aSLionel Sambuc namespace {
18f4a2713aSLionel Sambuc 
19f4a2713aSLionel Sambuc using namespace llvm;
20f4a2713aSLionel Sambuc using namespace sys;
21f4a2713aSLionel Sambuc 
TEST(ProcessTest,GetRandomNumberTest)22*0a6a1f1dSLionel Sambuc TEST(ProcessTest, GetRandomNumberTest) {
23*0a6a1f1dSLionel Sambuc   const unsigned r1 = Process::GetRandomNumber();
24*0a6a1f1dSLionel Sambuc   const unsigned r2 = Process::GetRandomNumber();
25*0a6a1f1dSLionel Sambuc   // It should be extremely unlikely that both r1 and r2 are 0.
26*0a6a1f1dSLionel Sambuc   EXPECT_NE((r1 | r2), 0u);
27f4a2713aSLionel Sambuc }
28f4a2713aSLionel Sambuc 
29f4a2713aSLionel Sambuc #ifdef _MSC_VER
30f4a2713aSLionel Sambuc #define setenv(name, var, ignore) _putenv_s(name, var)
31f4a2713aSLionel Sambuc #endif
32f4a2713aSLionel Sambuc 
33f4a2713aSLionel Sambuc #if HAVE_SETENV || _MSC_VER
TEST(ProcessTest,Basic)34f4a2713aSLionel Sambuc TEST(ProcessTest, Basic) {
35f4a2713aSLionel Sambuc   setenv("__LLVM_TEST_ENVIRON_VAR__", "abc", true);
36f4a2713aSLionel Sambuc   Optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));
37f4a2713aSLionel Sambuc   EXPECT_TRUE(val.hasValue());
38f4a2713aSLionel Sambuc   EXPECT_STREQ("abc", val->c_str());
39f4a2713aSLionel Sambuc }
40f4a2713aSLionel Sambuc 
TEST(ProcessTest,None)41f4a2713aSLionel Sambuc TEST(ProcessTest, None) {
42f4a2713aSLionel Sambuc   Optional<std::string> val(
43f4a2713aSLionel Sambuc       Process::GetEnv("__LLVM_TEST_ENVIRON_NO_SUCH_VAR__"));
44f4a2713aSLionel Sambuc   EXPECT_FALSE(val.hasValue());
45f4a2713aSLionel Sambuc }
46f4a2713aSLionel Sambuc #endif
47f4a2713aSLionel Sambuc 
48f4a2713aSLionel Sambuc #ifdef LLVM_ON_WIN32
TEST(ProcessTest,Wchar)49f4a2713aSLionel Sambuc TEST(ProcessTest, Wchar) {
50f4a2713aSLionel Sambuc   SetEnvironmentVariableW(L"__LLVM_TEST_ENVIRON_VAR__", L"abcdefghijklmnopqrs");
51f4a2713aSLionel Sambuc   Optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));
52f4a2713aSLionel Sambuc   EXPECT_TRUE(val.hasValue());
53f4a2713aSLionel Sambuc   EXPECT_STREQ("abcdefghijklmnopqrs", val->c_str());
54f4a2713aSLionel Sambuc }
55f4a2713aSLionel Sambuc #endif
56f4a2713aSLionel Sambuc 
57f4a2713aSLionel Sambuc } // end anonymous namespace
58