stringbuffertest.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Tencent is pleased to support the open source community by making RapidJSON available.
  2. //
  3. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
  4. //
  5. // Licensed under the MIT License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // http://opensource.org/licenses/MIT
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include "unittest.h"
  15. #include "rapidjson/stringbuffer.h"
  16. #include "rapidjson/writer.h"
  17. #ifdef __clang__
  18. RAPIDJSON_DIAG_PUSH
  19. RAPIDJSON_DIAG_OFF(c++98-compat)
  20. #endif
  21. using namespace rapidjson;
  22. TEST(StringBuffer, InitialSize) {
  23. StringBuffer buffer;
  24. EXPECT_EQ(0u, buffer.GetSize());
  25. EXPECT_EQ(0u, buffer.GetLength());
  26. EXPECT_STREQ("", buffer.GetString());
  27. }
  28. TEST(StringBuffer, Put) {
  29. StringBuffer buffer;
  30. buffer.Put('A');
  31. EXPECT_EQ(1u, buffer.GetSize());
  32. EXPECT_EQ(1u, buffer.GetLength());
  33. EXPECT_STREQ("A", buffer.GetString());
  34. }
  35. TEST(StringBuffer, PutN_Issue672) {
  36. GenericStringBuffer<UTF8<>, MemoryPoolAllocator<> > buffer;
  37. EXPECT_EQ(0, buffer.GetSize());
  38. EXPECT_EQ(0, buffer.GetLength());
  39. rapidjson::PutN(buffer, ' ', 1);
  40. EXPECT_EQ(1, buffer.GetSize());
  41. EXPECT_EQ(1, buffer.GetLength());
  42. }
  43. TEST(StringBuffer, Clear) {
  44. StringBuffer buffer;
  45. buffer.Put('A');
  46. buffer.Put('B');
  47. buffer.Put('C');
  48. buffer.Clear();
  49. EXPECT_EQ(0u, buffer.GetSize());
  50. EXPECT_EQ(0u, buffer.GetLength());
  51. EXPECT_STREQ("", buffer.GetString());
  52. }
  53. TEST(StringBuffer, Push) {
  54. StringBuffer buffer;
  55. buffer.Push(5);
  56. EXPECT_EQ(5u, buffer.GetSize());
  57. EXPECT_EQ(5u, buffer.GetLength());
  58. // Causes sudden expansion to make the stack's capacity equal to size
  59. buffer.Push(65536u);
  60. EXPECT_EQ(5u + 65536u, buffer.GetSize());
  61. }
  62. TEST(StringBuffer, Pop) {
  63. StringBuffer buffer;
  64. buffer.Put('A');
  65. buffer.Put('B');
  66. buffer.Put('C');
  67. buffer.Put('D');
  68. buffer.Put('E');
  69. buffer.Pop(3);
  70. EXPECT_EQ(2u, buffer.GetSize());
  71. EXPECT_EQ(2u, buffer.GetLength());
  72. EXPECT_STREQ("AB", buffer.GetString());
  73. }
  74. TEST(StringBuffer, GetLength_Issue744) {
  75. GenericStringBuffer<UTF16<wchar_t> > buffer;
  76. buffer.Put('A');
  77. buffer.Put('B');
  78. buffer.Put('C');
  79. EXPECT_EQ(3u * sizeof(wchar_t), buffer.GetSize());
  80. EXPECT_EQ(3u, buffer.GetLength());
  81. }
  82. #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
  83. #if 0 // Many old compiler does not support these. Turn it off temporaily.
  84. #include <type_traits>
  85. TEST(StringBuffer, Traits) {
  86. static_assert( std::is_constructible<StringBuffer>::value, "");
  87. static_assert( std::is_default_constructible<StringBuffer>::value, "");
  88. #ifndef _MSC_VER
  89. static_assert(!std::is_copy_constructible<StringBuffer>::value, "");
  90. #endif
  91. static_assert( std::is_move_constructible<StringBuffer>::value, "");
  92. static_assert(!std::is_nothrow_constructible<StringBuffer>::value, "");
  93. static_assert(!std::is_nothrow_default_constructible<StringBuffer>::value, "");
  94. #if !defined(_MSC_VER) || _MSC_VER >= 1800
  95. static_assert(!std::is_nothrow_copy_constructible<StringBuffer>::value, "");
  96. static_assert(!std::is_nothrow_move_constructible<StringBuffer>::value, "");
  97. #endif
  98. static_assert( std::is_assignable<StringBuffer,StringBuffer>::value, "");
  99. #ifndef _MSC_VER
  100. static_assert(!std::is_copy_assignable<StringBuffer>::value, "");
  101. #endif
  102. static_assert( std::is_move_assignable<StringBuffer>::value, "");
  103. #if !defined(_MSC_VER) || _MSC_VER >= 1800
  104. static_assert(!std::is_nothrow_assignable<StringBuffer, StringBuffer>::value, "");
  105. #endif
  106. static_assert(!std::is_nothrow_copy_assignable<StringBuffer>::value, "");
  107. static_assert(!std::is_nothrow_move_assignable<StringBuffer>::value, "");
  108. static_assert( std::is_destructible<StringBuffer>::value, "");
  109. #ifndef _MSC_VER
  110. static_assert(std::is_nothrow_destructible<StringBuffer>::value, "");
  111. #endif
  112. }
  113. #endif
  114. TEST(StringBuffer, MoveConstructor) {
  115. StringBuffer x;
  116. x.Put('A');
  117. x.Put('B');
  118. x.Put('C');
  119. x.Put('D');
  120. EXPECT_EQ(4u, x.GetSize());
  121. EXPECT_EQ(4u, x.GetLength());
  122. EXPECT_STREQ("ABCD", x.GetString());
  123. // StringBuffer y(x); // does not compile (!is_copy_constructible)
  124. StringBuffer y(std::move(x));
  125. EXPECT_EQ(0u, x.GetSize());
  126. EXPECT_EQ(0u, x.GetLength());
  127. EXPECT_EQ(4u, y.GetSize());
  128. EXPECT_EQ(4u, y.GetLength());
  129. EXPECT_STREQ("ABCD", y.GetString());
  130. // StringBuffer z = y; // does not compile (!is_copy_assignable)
  131. StringBuffer z = std::move(y);
  132. EXPECT_EQ(0u, y.GetSize());
  133. EXPECT_EQ(0u, y.GetLength());
  134. EXPECT_EQ(4u, z.GetSize());
  135. EXPECT_EQ(4u, z.GetLength());
  136. EXPECT_STREQ("ABCD", z.GetString());
  137. }
  138. TEST(StringBuffer, MoveAssignment) {
  139. StringBuffer x;
  140. x.Put('A');
  141. x.Put('B');
  142. x.Put('C');
  143. x.Put('D');
  144. EXPECT_EQ(4u, x.GetSize());
  145. EXPECT_EQ(4u, x.GetLength());
  146. EXPECT_STREQ("ABCD", x.GetString());
  147. StringBuffer y;
  148. // y = x; // does not compile (!is_copy_assignable)
  149. y = std::move(x);
  150. EXPECT_EQ(0u, x.GetSize());
  151. EXPECT_EQ(4u, y.GetLength());
  152. EXPECT_STREQ("ABCD", y.GetString());
  153. }
  154. #endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS
  155. #ifdef __clang__
  156. RAPIDJSON_DIAG_POP
  157. #endif