• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      https://www.apache.org/licenses/LICENSE-2.0
8 //
9 #include "absl/strings/internal/str_format/arg.h"
10 
11 #include <ostream>
12 #include <string>
13 #include "gtest/gtest.h"
14 #include "absl/strings/str_format.h"
15 
16 namespace absl {
17 ABSL_NAMESPACE_BEGIN
18 namespace str_format_internal {
19 namespace {
20 
21 class FormatArgImplTest : public ::testing::Test {
22  public:
23   enum Color { kRed, kGreen, kBlue };
24 
hi()25   static const char *hi() { return "hi"; }
26 
27   struct X {};
28 
29   X x_;
30 };
31 
AbslFormatConvert(const FormatArgImplTest::X &,const FormatConversionSpec &,FormatSink *)32 inline FormatConvertResult<FormatConversionCharSet{}> AbslFormatConvert(
33     const FormatArgImplTest::X &, const FormatConversionSpec &, FormatSink *) {
34   return {false};
35 }
36 
TEST_F(FormatArgImplTest,ToInt)37 TEST_F(FormatArgImplTest, ToInt) {
38   int out = 0;
39   EXPECT_TRUE(FormatArgImplFriend::ToInt(FormatArgImpl(1), &out));
40   EXPECT_EQ(1, out);
41   EXPECT_TRUE(FormatArgImplFriend::ToInt(FormatArgImpl(-1), &out));
42   EXPECT_EQ(-1, out);
43   EXPECT_TRUE(
44       FormatArgImplFriend::ToInt(FormatArgImpl(static_cast<char>(64)), &out));
45   EXPECT_EQ(64, out);
46   EXPECT_TRUE(FormatArgImplFriend::ToInt(
47       FormatArgImpl(static_cast<unsigned long long>(123456)), &out));  // NOLINT
48   EXPECT_EQ(123456, out);
49   EXPECT_TRUE(FormatArgImplFriend::ToInt(
50       FormatArgImpl(static_cast<unsigned long long>(  // NOLINT
51                         std::numeric_limits<int>::max()) +
52                     1),
53       &out));
54   EXPECT_EQ(std::numeric_limits<int>::max(), out);
55   EXPECT_TRUE(FormatArgImplFriend::ToInt(
56       FormatArgImpl(static_cast<long long>(  // NOLINT
57                         std::numeric_limits<int>::min()) -
58                     10),
59       &out));
60   EXPECT_EQ(std::numeric_limits<int>::min(), out);
61   EXPECT_TRUE(FormatArgImplFriend::ToInt(FormatArgImpl(false), &out));
62   EXPECT_EQ(0, out);
63   EXPECT_TRUE(FormatArgImplFriend::ToInt(FormatArgImpl(true), &out));
64   EXPECT_EQ(1, out);
65   EXPECT_FALSE(FormatArgImplFriend::ToInt(FormatArgImpl(2.2), &out));
66   EXPECT_FALSE(FormatArgImplFriend::ToInt(FormatArgImpl(3.2f), &out));
67   EXPECT_FALSE(FormatArgImplFriend::ToInt(
68       FormatArgImpl(static_cast<int *>(nullptr)), &out));
69   EXPECT_FALSE(FormatArgImplFriend::ToInt(FormatArgImpl(hi()), &out));
70   EXPECT_FALSE(FormatArgImplFriend::ToInt(FormatArgImpl("hi"), &out));
71   EXPECT_FALSE(FormatArgImplFriend::ToInt(FormatArgImpl(x_), &out));
72   EXPECT_TRUE(FormatArgImplFriend::ToInt(FormatArgImpl(kBlue), &out));
73   EXPECT_EQ(2, out);
74 }
75 
76 extern const char kMyArray[];
77 
TEST_F(FormatArgImplTest,CharArraysDecayToCharPtr)78 TEST_F(FormatArgImplTest, CharArraysDecayToCharPtr) {
79   const char* a = "";
80   EXPECT_EQ(FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl(a)),
81             FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl("")));
82   EXPECT_EQ(FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl(a)),
83             FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl("A")));
84   EXPECT_EQ(FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl(a)),
85             FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl("ABC")));
86   EXPECT_EQ(FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl(a)),
87             FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl(kMyArray)));
88 }
89 
TEST_F(FormatArgImplTest,OtherPtrDecayToVoidPtr)90 TEST_F(FormatArgImplTest, OtherPtrDecayToVoidPtr) {
91   auto expected = FormatArgImplFriend::GetVTablePtrForTest(
92       FormatArgImpl(static_cast<void *>(nullptr)));
93   EXPECT_EQ(FormatArgImplFriend::GetVTablePtrForTest(
94                 FormatArgImpl(static_cast<int *>(nullptr))),
95             expected);
96   EXPECT_EQ(FormatArgImplFriend::GetVTablePtrForTest(
97                 FormatArgImpl(static_cast<volatile int *>(nullptr))),
98             expected);
99 
100   auto p = static_cast<void (*)()>([] {});
101   EXPECT_EQ(FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl(p)),
102             expected);
103 }
104 
TEST_F(FormatArgImplTest,WorksWithCharArraysOfUnknownSize)105 TEST_F(FormatArgImplTest, WorksWithCharArraysOfUnknownSize) {
106   std::string s;
107   FormatSinkImpl sink(&s);
108   FormatConversionSpecImpl conv;
109   FormatConversionSpecImplFriend::SetConversionChar(
110       FormatConversionCharInternal::s, &conv);
111   FormatConversionSpecImplFriend::SetFlags(Flags(), &conv);
112   FormatConversionSpecImplFriend::SetWidth(-1, &conv);
113   FormatConversionSpecImplFriend::SetPrecision(-1, &conv);
114   EXPECT_TRUE(
115       FormatArgImplFriend::Convert(FormatArgImpl(kMyArray), conv, &sink));
116   sink.Flush();
117   EXPECT_EQ("ABCDE", s);
118 }
119 const char kMyArray[] = "ABCDE";
120 
121 }  // namespace
122 }  // namespace str_format_internal
123 ABSL_NAMESPACE_END
124 }  // namespace absl
125