arg_test.cc
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 #include "absl/strings/internal/str_format/arg.h"
00010 
00011 #include <ostream>
00012 #include <string>
00013 #include "gtest/gtest.h"
00014 #include "absl/strings/str_format.h"
00015 
00016 namespace absl {
00017 namespace str_format_internal {
00018 namespace {
00019 
00020 class FormatArgImplTest : public ::testing::Test {
00021  public:
00022   enum Color { kRed, kGreen, kBlue };
00023 
00024   static const char *hi() { return "hi"; }
00025 };
00026 
00027 TEST_F(FormatArgImplTest, ToInt) {
00028   int out = 0;
00029   EXPECT_TRUE(FormatArgImplFriend::ToInt(FormatArgImpl(1), &out));
00030   EXPECT_EQ(1, out);
00031   EXPECT_TRUE(FormatArgImplFriend::ToInt(FormatArgImpl(-1), &out));
00032   EXPECT_EQ(-1, out);
00033   EXPECT_TRUE(
00034       FormatArgImplFriend::ToInt(FormatArgImpl(static_cast<char>(64)), &out));
00035   EXPECT_EQ(64, out);
00036   EXPECT_TRUE(FormatArgImplFriend::ToInt(
00037       FormatArgImpl(static_cast<unsigned long long>(123456)), &out));  // NOLINT
00038   EXPECT_EQ(123456, out);
00039   EXPECT_TRUE(FormatArgImplFriend::ToInt(
00040       FormatArgImpl(static_cast<unsigned long long>(  // NOLINT
00041                         std::numeric_limits<int>::max()) +
00042                     1),
00043       &out));
00044   EXPECT_EQ(std::numeric_limits<int>::max(), out);
00045   EXPECT_TRUE(FormatArgImplFriend::ToInt(
00046       FormatArgImpl(static_cast<long long>(  // NOLINT
00047                         std::numeric_limits<int>::min()) -
00048                     10),
00049       &out));
00050   EXPECT_EQ(std::numeric_limits<int>::min(), out);
00051   EXPECT_TRUE(FormatArgImplFriend::ToInt(FormatArgImpl(false), &out));
00052   EXPECT_EQ(0, out);
00053   EXPECT_TRUE(FormatArgImplFriend::ToInt(FormatArgImpl(true), &out));
00054   EXPECT_EQ(1, out);
00055   EXPECT_FALSE(FormatArgImplFriend::ToInt(FormatArgImpl(2.2), &out));
00056   EXPECT_FALSE(FormatArgImplFriend::ToInt(FormatArgImpl(3.2f), &out));
00057   EXPECT_FALSE(FormatArgImplFriend::ToInt(
00058       FormatArgImpl(static_cast<int *>(nullptr)), &out));
00059   EXPECT_FALSE(FormatArgImplFriend::ToInt(FormatArgImpl(hi()), &out));
00060   EXPECT_FALSE(FormatArgImplFriend::ToInt(FormatArgImpl("hi"), &out));
00061   EXPECT_TRUE(FormatArgImplFriend::ToInt(FormatArgImpl(kBlue), &out));
00062   EXPECT_EQ(2, out);
00063 }
00064 
00065 extern const char kMyArray[];
00066 
00067 TEST_F(FormatArgImplTest, CharArraysDecayToCharPtr) {
00068   const char* a = "";
00069   EXPECT_EQ(FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl(a)),
00070             FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl("")));
00071   EXPECT_EQ(FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl(a)),
00072             FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl("A")));
00073   EXPECT_EQ(FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl(a)),
00074             FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl("ABC")));
00075   EXPECT_EQ(FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl(a)),
00076             FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl(kMyArray)));
00077 }
00078 
00079 TEST_F(FormatArgImplTest, OtherPtrDecayToVoidPtr) {
00080   auto expected = FormatArgImplFriend::GetVTablePtrForTest(
00081       FormatArgImpl(static_cast<void *>(nullptr)));
00082   EXPECT_EQ(FormatArgImplFriend::GetVTablePtrForTest(
00083                 FormatArgImpl(static_cast<int *>(nullptr))),
00084             expected);
00085   EXPECT_EQ(FormatArgImplFriend::GetVTablePtrForTest(
00086                 FormatArgImpl(static_cast<volatile int *>(nullptr))),
00087             expected);
00088 
00089   auto p = static_cast<void (*)()>([] {});
00090   EXPECT_EQ(FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl(p)),
00091             expected);
00092 }
00093 
00094 TEST_F(FormatArgImplTest, WorksWithCharArraysOfUnknownSize) {
00095   std::string s;
00096   FormatSinkImpl sink(&s);
00097   ConversionSpec conv;
00098   conv.set_conv(ConversionChar::FromChar('s'));
00099   conv.set_flags(Flags());
00100   conv.set_width(-1);
00101   conv.set_precision(-1);
00102   EXPECT_TRUE(
00103       FormatArgImplFriend::Convert(FormatArgImpl(kMyArray), conv, &sink));
00104   sink.Flush();
00105   EXPECT_EQ("ABCDE", s);
00106 }
00107 const char kMyArray[] = "ABCDE";
00108 
00109 }  // namespace
00110 }  // namespace str_format_internal
00111 }  // namespace absl


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