brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.0 KiB · c4e421f Raw
57 lines · cpp
1//===----------------------------------------------------------------------===//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 "llvm/Support/Format.h"10#include "gtest/gtest.h"11 12using namespace llvm;13 14namespace {15 16template <typename FormatTy>17std::string printToString(unsigned MaxN, FormatTy &&Fmt) {18  std::vector<char> Dst(MaxN + 2);19  int N = Fmt.snprint(Dst.data(), Dst.size());20  Dst.back() = 0;21  return N < 0 ? "" : Dst.data();22}23 24template <typename Expected, typename Arg>25constexpr bool checkDecayTypeEq(const Arg &arg) {26  return std::is_same_v<detail::decay_if_c_char_array_t<Arg>, Expected>;27}28 29TEST(Format, DecayIfCCharArray) {30  char Array[] = "Array";31  const char ConstArray[] = "ConstArray";32  char PtrBuf[] = "Ptr";33  char *Ptr = PtrBuf;34  const char *PtrToConst = "PtrToConst";35 36  EXPECT_EQ("        Literal", printToString(20, format("%15s", "Literal")));37  EXPECT_EQ("          Array", printToString(20, format("%15s", Array)));38  EXPECT_EQ("     ConstArray", printToString(20, format("%15s", ConstArray)));39  EXPECT_EQ("            Ptr", printToString(20, format("%15s", Ptr)));40  EXPECT_EQ("     PtrToConst", printToString(20, format("%15s", PtrToConst)));41 42  EXPECT_TRUE(checkDecayTypeEq<const char *>("Literal"));43  EXPECT_TRUE(checkDecayTypeEq<const char *>(Array));44  EXPECT_TRUE(checkDecayTypeEq<const char *>(ConstArray));45  EXPECT_TRUE(checkDecayTypeEq<char *>(Ptr));46  EXPECT_TRUE(checkDecayTypeEq<const char *>(PtrToConst));47  EXPECT_TRUE(checkDecayTypeEq<char>(PtrToConst[0]));48  EXPECT_TRUE(49      checkDecayTypeEq<const char *>(static_cast<const char *>("Literal")));50 51  wchar_t WCharArray[] = L"WCharArray";52  EXPECT_TRUE(checkDecayTypeEq<wchar_t[11]>(WCharArray));53  EXPECT_TRUE(checkDecayTypeEq<wchar_t>(WCharArray[0]));54}55 56} // namespace57