brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.9 KiB · e37c94a Raw
113 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "lldb/Utility/Status.h"10#include "gtest/gtest.h"11 12#ifdef _WIN3213#include <windows.h>14#endif15 16using namespace lldb_private;17using namespace lldb;18 19TEST(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());27}28 29TEST(StatusTest, ErrorConstructor) {30  EXPECT_TRUE(Status::FromError(llvm::Error::success()).Success());31 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());37 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());42 43  foo = Status::FromError(llvm::Error::success());44  EXPECT_TRUE(foo.Success());45}46 47TEST(StatusTest, ErrorCodeConstructor) {48  EXPECT_TRUE(Status(std::error_code()).Success());49 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());54 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()));59}60 61TEST(StatusTest, ErrorConversion) {62  EXPECT_FALSE(bool(Status().ToError()));63 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());69 70  llvm::Error foo = Status::FromErrorString("foo").ToError();71  EXPECT_TRUE(bool(foo));72  EXPECT_EQ("foo", llvm::toString(std::move(foo)));73 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);81}82 83#ifdef _WIN3284TEST(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());89 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 the96  // formatted messages will be different.97  bool skip = wcscmp(L"en-US", name) != 0;98 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());103 104  s = Status(ERROR_IPSEC_IKE_TIMED_OUT, ErrorType::eErrorTypeWin32);105  if (!skip)106    EXPECT_STREQ("Negotiation timed out ", s.AsCString());107 108  s = Status(16000, ErrorType::eErrorTypeWin32);109  if (!skip)110    EXPECT_STREQ("unknown error", s.AsCString());111}112#endif113