ostringstream_test.cc (3433B)
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 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "absl/strings/internal/ostringstream.h" 16 17 #include <ios> 18 #include <memory> 19 #include <ostream> 20 #include <string> 21 #include <type_traits> 22 #include <utility> 23 24 #include "gtest/gtest.h" 25 26 namespace { 27 28 TEST(OStringStream, IsOStream) { 29 static_assert( 30 std::is_base_of<std::ostream, absl::strings_internal::OStringStream>(), 31 ""); 32 } 33 34 TEST(OStringStream, ConstructNullptr) { 35 absl::strings_internal::OStringStream strm(nullptr); 36 EXPECT_EQ(nullptr, strm.str()); 37 } 38 39 TEST(OStringStream, ConstructStr) { 40 std::string s = "abc"; 41 { 42 absl::strings_internal::OStringStream strm(&s); 43 EXPECT_EQ(&s, strm.str()); 44 } 45 EXPECT_EQ("abc", s); 46 } 47 48 TEST(OStringStream, Destroy) { 49 std::unique_ptr<std::string> s(new std::string); 50 absl::strings_internal::OStringStream strm(s.get()); 51 s.reset(); 52 } 53 54 TEST(OStringStream, MoveConstruct) { 55 std::string s = "abc"; 56 { 57 absl::strings_internal::OStringStream strm1(&s); 58 strm1 << std::hex << 16; 59 EXPECT_EQ(&s, strm1.str()); 60 absl::strings_internal::OStringStream strm2(std::move(strm1)); 61 strm2 << 16; // We should still be in base 16. 62 EXPECT_EQ(&s, strm2.str()); 63 } 64 EXPECT_EQ("abc1010", s); 65 } 66 67 TEST(OStringStream, MoveAssign) { 68 std::string s = "abc"; 69 { 70 absl::strings_internal::OStringStream strm1(&s); 71 strm1 << std::hex << 16; 72 EXPECT_EQ(&s, strm1.str()); 73 absl::strings_internal::OStringStream strm2(nullptr); 74 strm2 = std::move(strm1); 75 strm2 << 16; // We should still be in base 16. 76 EXPECT_EQ(&s, strm2.str()); 77 } 78 EXPECT_EQ("abc1010", s); 79 } 80 81 TEST(OStringStream, Str) { 82 std::string s1; 83 absl::strings_internal::OStringStream strm(&s1); 84 const absl::strings_internal::OStringStream& c_strm(strm); 85 86 static_assert(std::is_same<decltype(strm.str()), std::string*>(), ""); 87 static_assert(std::is_same<decltype(c_strm.str()), const std::string*>(), ""); 88 89 EXPECT_EQ(&s1, strm.str()); 90 EXPECT_EQ(&s1, c_strm.str()); 91 92 strm.str(&s1); 93 EXPECT_EQ(&s1, strm.str()); 94 EXPECT_EQ(&s1, c_strm.str()); 95 96 std::string s2; 97 strm.str(&s2); 98 EXPECT_EQ(&s2, strm.str()); 99 EXPECT_EQ(&s2, c_strm.str()); 100 101 strm.str(nullptr); 102 EXPECT_EQ(nullptr, strm.str()); 103 EXPECT_EQ(nullptr, c_strm.str()); 104 } 105 106 TEST(OStreamStream, WriteToLValue) { 107 std::string s = "abc"; 108 { 109 absl::strings_internal::OStringStream strm(&s); 110 EXPECT_EQ("abc", s); 111 strm << ""; 112 EXPECT_EQ("abc", s); 113 strm << 42; 114 EXPECT_EQ("abc42", s); 115 strm << 'x' << 'y'; 116 EXPECT_EQ("abc42xy", s); 117 } 118 EXPECT_EQ("abc42xy", s); 119 } 120 121 TEST(OStreamStream, WriteToRValue) { 122 std::string s = "abc"; 123 absl::strings_internal::OStringStream(&s) << ""; 124 EXPECT_EQ("abc", s); 125 absl::strings_internal::OStringStream(&s) << 42; 126 EXPECT_EQ("abc42", s); 127 absl::strings_internal::OStringStream(&s) << 'x' << 'y'; 128 EXPECT_EQ("abc42xy", s); 129 } 130 131 } // namespace