[llvm-objdump] - Cleanup the error reporting.
[llvm-complete.git] / unittests / Support / ProcessTest.cpp
blobdbfaf8ed0f614cc60a84e7fd1c49c78558106160
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 "gtest/gtest.h"
12 #ifdef _WIN32
13 #include <windows.h>
14 #endif
16 namespace {
18 using namespace llvm;
19 using namespace sys;
21 TEST(ProcessTest, GetRandomNumberTest) {
22 const unsigned r1 = Process::GetRandomNumber();
23 const unsigned r2 = Process::GetRandomNumber();
24 // It should be extremely unlikely that both r1 and r2 are 0.
25 EXPECT_NE((r1 | r2), 0u);
28 #ifdef _MSC_VER
29 #define setenv(name, var, ignore) _putenv_s(name, var)
30 #endif
32 #if HAVE_SETENV || _MSC_VER
33 TEST(ProcessTest, Basic) {
34 setenv("__LLVM_TEST_ENVIRON_VAR__", "abc", true);
35 Optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));
36 EXPECT_TRUE(val.hasValue());
37 EXPECT_STREQ("abc", val->c_str());
40 TEST(ProcessTest, None) {
41 Optional<std::string> val(
42 Process::GetEnv("__LLVM_TEST_ENVIRON_NO_SUCH_VAR__"));
43 EXPECT_FALSE(val.hasValue());
45 #endif
47 #ifdef _WIN32
49 TEST(ProcessTest, EmptyVal) {
50 SetEnvironmentVariableA("__LLVM_TEST_ENVIRON_VAR__", "");
51 Optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));
52 EXPECT_TRUE(val.hasValue());
53 EXPECT_STREQ("", val->c_str());
56 TEST(ProcessTest, Wchar) {
57 SetEnvironmentVariableW(L"__LLVM_TEST_ENVIRON_VAR__", L"abcdefghijklmnopqrs");
58 Optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));
59 EXPECT_TRUE(val.hasValue());
60 EXPECT_STREQ("abcdefghijklmnopqrs", val->c_str());
62 #endif
64 } // end anonymous namespace