arenastring_unittest.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Based on mvels@'s frankenstring.
  31. #include <google/protobuf/arenastring.h>
  32. #include <algorithm>
  33. #include <cstdlib>
  34. #include <memory>
  35. #include <string>
  36. #include <vector>
  37. #include <google/protobuf/stubs/logging.h>
  38. #include <google/protobuf/stubs/common.h>
  39. #include <gtest/gtest.h>
  40. #include <google/protobuf/io/coded_stream.h>
  41. #include <google/protobuf/io/zero_copy_stream_impl.h>
  42. namespace google {
  43. using google::protobuf::internal::ArenaStringPtr;
  44. namespace protobuf {
  45. static string WrapString(const char* value) {
  46. return value;
  47. }
  48. // Test ArenaStringPtr with arena == NULL.
  49. TEST(ArenaStringPtrTest, ArenaStringPtrOnHeap) {
  50. ArenaStringPtr field;
  51. ::std::string default_value = "default";
  52. field.UnsafeSetDefault(&default_value);
  53. EXPECT_EQ(string("default"), field.Get());
  54. field.Set(&default_value, WrapString("Test short"), NULL);
  55. EXPECT_EQ(string("Test short"), field.Get());
  56. field.Set(&default_value, WrapString("Test long long long long value"), NULL);
  57. EXPECT_EQ(string("Test long long long long value"), field.Get());
  58. field.Set(&default_value, string(""), NULL);
  59. field.Destroy(&default_value, NULL);
  60. ArenaStringPtr field2;
  61. field2.UnsafeSetDefault(&default_value);
  62. ::std::string* mut = field2.Mutable(&default_value, NULL);
  63. EXPECT_EQ(mut, field2.Mutable(&default_value, NULL));
  64. EXPECT_EQ(mut, &field2.Get());
  65. EXPECT_NE(&default_value, mut);
  66. EXPECT_EQ(string("default"), *mut);
  67. *mut = "Test long long long long value"; // ensure string allocates storage
  68. EXPECT_EQ(string("Test long long long long value"), field2.Get());
  69. field2.Destroy(&default_value, NULL);
  70. }
  71. TEST(ArenaStringPtrTest, ArenaStringPtrOnArena) {
  72. google::protobuf::Arena arena;
  73. ArenaStringPtr field;
  74. ::std::string default_value = "default";
  75. field.UnsafeSetDefault(&default_value);
  76. EXPECT_EQ(string("default"), field.Get());
  77. field.Set(&default_value, WrapString("Test short"), &arena);
  78. EXPECT_EQ(string("Test short"), field.Get());
  79. field.Set(&default_value, WrapString("Test long long long long value"),
  80. &arena);
  81. EXPECT_EQ(string("Test long long long long value"), field.Get());
  82. field.Set(&default_value, string(""), &arena);
  83. field.Destroy(&default_value, &arena);
  84. ArenaStringPtr field2;
  85. field2.UnsafeSetDefault(&default_value);
  86. ::std::string* mut = field2.Mutable(&default_value, &arena);
  87. EXPECT_EQ(mut, field2.Mutable(&default_value, &arena));
  88. EXPECT_EQ(mut, &field2.Get());
  89. EXPECT_NE(&default_value, mut);
  90. EXPECT_EQ(string("default"), *mut);
  91. *mut = "Test long long long long value"; // ensure string allocates storage
  92. EXPECT_EQ(string("Test long long long long value"), field2.Get());
  93. field2.Destroy(&default_value, &arena);
  94. }
  95. TEST(ArenaStringPtrTest, ArenaStringPtrOnArenaNoSSO) {
  96. google::protobuf::Arena arena;
  97. ArenaStringPtr field;
  98. ::std::string default_value = "default";
  99. field.UnsafeSetDefault(&default_value);
  100. EXPECT_EQ(string("default"), field.Get());
  101. // Avoid triggering the SSO optimization by setting the string to something
  102. // larger than the internal buffer.
  103. field.Set(&default_value, WrapString("Test long long long long value"),
  104. &arena);
  105. EXPECT_EQ(string("Test long long long long value"), field.Get());
  106. field.Set(&default_value, string(""), &arena);
  107. field.Destroy(&default_value, &arena);
  108. ArenaStringPtr field2;
  109. field2.UnsafeSetDefault(&default_value);
  110. ::std::string* mut = field2.Mutable(&default_value, &arena);
  111. EXPECT_EQ(mut, field2.Mutable(&default_value, &arena));
  112. EXPECT_EQ(mut, &field2.Get());
  113. EXPECT_NE(&default_value, mut);
  114. EXPECT_EQ(string("default"), *mut);
  115. *mut = "Test long long long long value"; // ensure string allocates storage
  116. EXPECT_EQ(string("Test long long long long value"), field2.Get());
  117. field2.Destroy(&default_value, &arena);
  118. }
  119. } // namespace protobuf
  120. } // namespace google