ostringstream_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 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include "absl/strings/internal/ostringstream.h"
00016 
00017 #include <memory>
00018 #include <ostream>
00019 #include <string>
00020 #include <type_traits>
00021 
00022 #include "gtest/gtest.h"
00023 
00024 namespace {
00025 
00026 TEST(OStringStream, IsOStream) {
00027   static_assert(
00028       std::is_base_of<std::ostream, absl::strings_internal::OStringStream>(),
00029       "");
00030 }
00031 
00032 TEST(OStringStream, ConstructDestroy) {
00033   {
00034     absl::strings_internal::OStringStream strm(nullptr);
00035     EXPECT_EQ(nullptr, strm.str());
00036   }
00037   {
00038     std::string s = "abc";
00039     {
00040       absl::strings_internal::OStringStream strm(&s);
00041       EXPECT_EQ(&s, strm.str());
00042     }
00043     EXPECT_EQ("abc", s);
00044   }
00045   {
00046     std::unique_ptr<std::string> s(new std::string);
00047     absl::strings_internal::OStringStream strm(s.get());
00048     s.reset();
00049   }
00050 }
00051 
00052 TEST(OStringStream, Str) {
00053   std::string s1;
00054   absl::strings_internal::OStringStream strm(&s1);
00055   const absl::strings_internal::OStringStream& c_strm(strm);
00056 
00057   static_assert(std::is_same<decltype(strm.str()), std::string*>(), "");
00058   static_assert(std::is_same<decltype(c_strm.str()), const std::string*>(), "");
00059 
00060   EXPECT_EQ(&s1, strm.str());
00061   EXPECT_EQ(&s1, c_strm.str());
00062 
00063   strm.str(&s1);
00064   EXPECT_EQ(&s1, strm.str());
00065   EXPECT_EQ(&s1, c_strm.str());
00066 
00067   std::string s2;
00068   strm.str(&s2);
00069   EXPECT_EQ(&s2, strm.str());
00070   EXPECT_EQ(&s2, c_strm.str());
00071 
00072   strm.str(nullptr);
00073   EXPECT_EQ(nullptr, strm.str());
00074   EXPECT_EQ(nullptr, c_strm.str());
00075 }
00076 
00077 TEST(OStreamStream, WriteToLValue) {
00078   std::string s = "abc";
00079   {
00080     absl::strings_internal::OStringStream strm(&s);
00081     EXPECT_EQ("abc", s);
00082     strm << "";
00083     EXPECT_EQ("abc", s);
00084     strm << 42;
00085     EXPECT_EQ("abc42", s);
00086     strm << 'x' << 'y';
00087     EXPECT_EQ("abc42xy", s);
00088   }
00089   EXPECT_EQ("abc42xy", s);
00090 }
00091 
00092 TEST(OStreamStream, WriteToRValue) {
00093   std::string s = "abc";
00094   absl::strings_internal::OStringStream(&s) << "";
00095   EXPECT_EQ("abc", s);
00096   absl::strings_internal::OStringStream(&s) << 42;
00097   EXPECT_EQ("abc42", s);
00098   absl::strings_internal::OStringStream(&s) << 'x' << 'y';
00099   EXPECT_EQ("abc42xy", s);
00100 }
00101 
00102 }  // namespace


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