ostringstream_test.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #include "absl/strings/internal/ostringstream.h"
  15. #include <memory>
  16. #include <ostream>
  17. #include <string>
  18. #include <type_traits>
  19. #include "gtest/gtest.h"
  20. namespace {
  21. TEST(OStringStream, IsOStream) {
  22. static_assert(
  23. std::is_base_of<std::ostream, absl::strings_internal::OStringStream>(),
  24. "");
  25. }
  26. TEST(OStringStream, ConstructDestroy) {
  27. {
  28. absl::strings_internal::OStringStream strm(nullptr);
  29. EXPECT_EQ(nullptr, strm.str());
  30. }
  31. {
  32. std::string s = "abc";
  33. {
  34. absl::strings_internal::OStringStream strm(&s);
  35. EXPECT_EQ(&s, strm.str());
  36. }
  37. EXPECT_EQ("abc", s);
  38. }
  39. {
  40. std::unique_ptr<std::string> s(new std::string);
  41. absl::strings_internal::OStringStream strm(s.get());
  42. s.reset();
  43. }
  44. }
  45. TEST(OStringStream, Str) {
  46. std::string s1;
  47. absl::strings_internal::OStringStream strm(&s1);
  48. const absl::strings_internal::OStringStream& c_strm(strm);
  49. static_assert(std::is_same<decltype(strm.str()), std::string*>(), "");
  50. static_assert(std::is_same<decltype(c_strm.str()), const std::string*>(), "");
  51. EXPECT_EQ(&s1, strm.str());
  52. EXPECT_EQ(&s1, c_strm.str());
  53. strm.str(&s1);
  54. EXPECT_EQ(&s1, strm.str());
  55. EXPECT_EQ(&s1, c_strm.str());
  56. std::string s2;
  57. strm.str(&s2);
  58. EXPECT_EQ(&s2, strm.str());
  59. EXPECT_EQ(&s2, c_strm.str());
  60. strm.str(nullptr);
  61. EXPECT_EQ(nullptr, strm.str());
  62. EXPECT_EQ(nullptr, c_strm.str());
  63. }
  64. TEST(OStreamStream, WriteToLValue) {
  65. std::string s = "abc";
  66. {
  67. absl::strings_internal::OStringStream strm(&s);
  68. EXPECT_EQ("abc", s);
  69. strm << "";
  70. EXPECT_EQ("abc", s);
  71. strm << 42;
  72. EXPECT_EQ("abc42", s);
  73. strm << 'x' << 'y';
  74. EXPECT_EQ("abc42xy", s);
  75. }
  76. EXPECT_EQ("abc42xy", s);
  77. }
  78. TEST(OStreamStream, WriteToRValue) {
  79. std::string s = "abc";
  80. absl::strings_internal::OStringStream(&s) << "";
  81. EXPECT_EQ("abc", s);
  82. absl::strings_internal::OStringStream(&s) << 42;
  83. EXPECT_EQ("abc42", s);
  84. absl::strings_internal::OStringStream(&s) << 'x' << 'y';
  85. EXPECT_EQ("abc42xy", s);
  86. }
  87. } // namespace