arg_test.cc
Go to the documentation of this file.
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 //
10 
11 #include <ostream>
12 #include <string>
13 #include "gtest/gtest.h"
15 
16 namespace absl {
17 namespace str_format_internal {
18 namespace {
19 
20 class FormatArgImplTest : public ::testing::Test {
21  public:
22  enum Color { kRed, kGreen, kBlue };
23 
24  static const char *hi() { return "hi"; }
25 };
26 
27 TEST_F(FormatArgImplTest, ToInt) {
28  int out = 0;
29  EXPECT_TRUE(FormatArgImplFriend::ToInt(FormatArgImpl(1), &out));
30  EXPECT_EQ(1, out);
31  EXPECT_TRUE(FormatArgImplFriend::ToInt(FormatArgImpl(-1), &out));
32  EXPECT_EQ(-1, out);
33  EXPECT_TRUE(
34  FormatArgImplFriend::ToInt(FormatArgImpl(static_cast<char>(64)), &out));
35  EXPECT_EQ(64, out);
36  EXPECT_TRUE(FormatArgImplFriend::ToInt(
37  FormatArgImpl(static_cast<unsigned long long>(123456)), &out)); // NOLINT
38  EXPECT_EQ(123456, out);
39  EXPECT_TRUE(FormatArgImplFriend::ToInt(
40  FormatArgImpl(static_cast<unsigned long long>( // NOLINT
41  std::numeric_limits<int>::max()) +
42  1),
43  &out));
44  EXPECT_EQ(std::numeric_limits<int>::max(), out);
45  EXPECT_TRUE(FormatArgImplFriend::ToInt(
46  FormatArgImpl(static_cast<long long>( // NOLINT
47  std::numeric_limits<int>::min()) -
48  10),
49  &out));
50  EXPECT_EQ(std::numeric_limits<int>::min(), out);
51  EXPECT_TRUE(FormatArgImplFriend::ToInt(FormatArgImpl(false), &out));
52  EXPECT_EQ(0, out);
53  EXPECT_TRUE(FormatArgImplFriend::ToInt(FormatArgImpl(true), &out));
54  EXPECT_EQ(1, out);
55  EXPECT_FALSE(FormatArgImplFriend::ToInt(FormatArgImpl(2.2), &out));
56  EXPECT_FALSE(FormatArgImplFriend::ToInt(FormatArgImpl(3.2f), &out));
57  EXPECT_FALSE(FormatArgImplFriend::ToInt(
58  FormatArgImpl(static_cast<int *>(nullptr)), &out));
59  EXPECT_FALSE(FormatArgImplFriend::ToInt(FormatArgImpl(hi()), &out));
60  EXPECT_FALSE(FormatArgImplFriend::ToInt(FormatArgImpl("hi"), &out));
61  EXPECT_TRUE(FormatArgImplFriend::ToInt(FormatArgImpl(kBlue), &out));
62  EXPECT_EQ(2, out);
63 }
64 
65 extern const char kMyArray[];
66 
67 TEST_F(FormatArgImplTest, CharArraysDecayToCharPtr) {
68  const char* a = "";
69  EXPECT_EQ(FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl(a)),
70  FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl("")));
71  EXPECT_EQ(FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl(a)),
72  FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl("A")));
73  EXPECT_EQ(FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl(a)),
74  FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl("ABC")));
75  EXPECT_EQ(FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl(a)),
76  FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl(kMyArray)));
77 }
78 
79 TEST_F(FormatArgImplTest, OtherPtrDecayToVoidPtr) {
81  FormatArgImpl(static_cast<void *>(nullptr)));
83  FormatArgImpl(static_cast<int *>(nullptr))),
84  expected);
86  FormatArgImpl(static_cast<volatile int *>(nullptr))),
87  expected);
88 
89  auto p = static_cast<void (*)()>([] {});
90  EXPECT_EQ(FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl(p)),
91  expected);
92 }
93 
94 TEST_F(FormatArgImplTest, WorksWithCharArraysOfUnknownSize) {
95  std::string s;
96  FormatSinkImpl sink(&s);
97  ConversionSpec conv;
98  conv.set_conv(ConversionChar::FromChar('s'));
99  conv.set_flags(Flags());
100  conv.set_width(-1);
101  conv.set_precision(-1);
102  EXPECT_TRUE(
103  FormatArgImplFriend::Convert(FormatArgImpl(kMyArray), conv, &sink));
104  sink.Flush();
105  EXPECT_EQ("ABCDE", s);
106 }
107 const char kMyArray[] = "ABCDE";
108 
109 } // namespace
110 } // namespace str_format_internal
111 } // namespace absl
static Arg::Dispatcher GetVTablePtrForTest(Arg arg)
Definition: arg.h:213
Definition: algorithm.h:29
TEST_F(GraphCyclesTest, NoCycle)
static bool Convert(Arg arg, str_format_internal::ConversionSpec conv, FormatSinkImpl *out)
Definition: arg.h:207
static bool ToInt(Arg arg, int *out)
Definition: arg.h:200
static ConversionChar FromChar(char c)
Definition: extension.h:221
char * out
Definition: mutex.h:1013


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:19:56