[NFC][Coroutines] Use structured binding with llvm::enumerate in CoroSplit (#116879)
[llvm-project.git] / lldb / unittests / Utility / StatusTest.cpp
blobe37c94ac17f2d09a9cfb0a8a05fe744da959205d
1 //===-- StatusTest.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 "lldb/Utility/Status.h"
10 #include "gtest/gtest.h"
12 #ifdef _WIN32
13 #include <windows.h>
14 #endif
16 using namespace lldb_private;
17 using namespace lldb;
19 TEST(StatusTest, Formatv) {
20 EXPECT_EQ("", llvm::formatv("{0}", Status()).str());
21 EXPECT_EQ(
22 "Hello Status",
23 llvm::formatv("{0}", Status::FromErrorString("Hello Status")).str());
24 EXPECT_EQ(
25 "Hello",
26 llvm::formatv("{0:5}", Status::FromErrorString("Hello Error")).str());
29 TEST(StatusTest, ErrorConstructor) {
30 EXPECT_TRUE(Status::FromError(llvm::Error::success()).Success());
32 Status eagain = Status::FromError(
33 llvm::errorCodeToError(std::error_code(EAGAIN, std::generic_category())));
34 EXPECT_TRUE(eagain.Fail());
35 EXPECT_EQ(eErrorTypePOSIX, eagain.GetType());
36 EXPECT_EQ(Status::ValueType(EAGAIN), eagain.GetError());
38 Status foo = Status::FromError(llvm::createStringError("foo"));
39 EXPECT_TRUE(foo.Fail());
40 EXPECT_EQ(eErrorTypeGeneric, foo.GetType());
41 EXPECT_STREQ("foo", foo.AsCString());
43 foo = Status::FromError(llvm::Error::success());
44 EXPECT_TRUE(foo.Success());
47 TEST(StatusTest, ErrorCodeConstructor) {
48 EXPECT_TRUE(Status(std::error_code()).Success());
50 Status eagain = std::error_code(EAGAIN, std::generic_category());
51 EXPECT_TRUE(eagain.Fail());
52 EXPECT_EQ(eErrorTypePOSIX, eagain.GetType());
53 EXPECT_EQ(Status::ValueType(EAGAIN), eagain.GetError());
55 llvm::Error list = llvm::joinErrors(llvm::createStringError("foo"),
56 llvm::createStringError("bar"));
57 Status foobar = Status::FromError(std::move(list));
58 EXPECT_EQ(std::string("foo\nbar"), std::string(foobar.AsCString()));
61 TEST(StatusTest, ErrorConversion) {
62 EXPECT_FALSE(bool(Status().ToError()));
64 llvm::Error eagain = Status(EAGAIN, ErrorType::eErrorTypePOSIX).ToError();
65 EXPECT_TRUE(bool(eagain));
66 std::error_code ec = llvm::errorToErrorCode(std::move(eagain));
67 EXPECT_EQ(EAGAIN, ec.value());
68 EXPECT_EQ(std::generic_category(), ec.category());
70 llvm::Error foo = Status::FromErrorString("foo").ToError();
71 EXPECT_TRUE(bool(foo));
72 EXPECT_EQ("foo", llvm::toString(std::move(foo)));
74 llvm::Error eperm = llvm::errorCodeToError({EPERM, std::generic_category()});
75 llvm::Error eintr = llvm::errorCodeToError({EINTR, std::generic_category()});
76 llvm::Error elist = llvm::joinErrors(std::move(eperm), std::move(eintr));
77 elist = llvm::joinErrors(std::move(elist), llvm::createStringError("foo"));
78 Status list = Status::FromError(std::move(elist));
79 EXPECT_EQ((int)list.GetError(), EPERM);
80 EXPECT_EQ(list.GetType(), eErrorTypePOSIX);
83 #ifdef _WIN32
84 TEST(StatusTest, ErrorWin32) {
85 auto success = Status(NO_ERROR, ErrorType::eErrorTypeWin32);
86 EXPECT_STREQ(NULL, success.AsCString());
87 EXPECT_FALSE(success.ToError());
88 EXPECT_TRUE(success.Success());
90 WCHAR name[128]{};
91 ULONG nameLen = std::size(name);
92 ULONG langs = 0;
93 GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &langs,
94 reinterpret_cast<PZZWSTR>(&name), &nameLen);
95 // Skip the following tests on non-English, non-US, locales because the
96 // formatted messages will be different.
97 bool skip = wcscmp(L"en-US", name) != 0;
99 Status s = Status(ERROR_ACCESS_DENIED, ErrorType::eErrorTypeWin32);
100 EXPECT_TRUE(s.Fail());
101 if (!skip)
102 EXPECT_STREQ("Access is denied. ", s.AsCString());
104 s = Status(ERROR_IPSEC_IKE_TIMED_OUT, ErrorType::eErrorTypeWin32);
105 if (!skip)
106 EXPECT_STREQ("Negotiation timed out ", s.AsCString());
108 s = Status(16000, ErrorType::eErrorTypeWin32);
109 if (!skip)
110 EXPECT_STREQ("unknown error", s.AsCString());
112 #endif