stringbuffertest.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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_STREQ("", buffer.GetString());
  26. }
  27. TEST(StringBuffer, Put) {
  28. StringBuffer buffer;
  29. buffer.Put('A');
  30. EXPECT_EQ(1u, buffer.GetSize());
  31. EXPECT_STREQ("A", buffer.GetString());
  32. }
  33. TEST(StringBuffer, PutN_Issue672) {
  34. GenericStringBuffer<UTF8<>, MemoryPoolAllocator<> > buffer;
  35. EXPECT_EQ(0, buffer.GetSize());
  36. rapidjson::PutN(buffer, ' ', 1);
  37. EXPECT_EQ(1, buffer.GetSize());
  38. }
  39. TEST(StringBuffer, Clear) {
  40. StringBuffer buffer;
  41. buffer.Put('A');
  42. buffer.Put('B');
  43. buffer.Put('C');
  44. buffer.Clear();
  45. EXPECT_EQ(0u, buffer.GetSize());
  46. EXPECT_STREQ("", buffer.GetString());
  47. }
  48. TEST(StringBuffer, Push) {
  49. StringBuffer buffer;
  50. buffer.Push(5);
  51. EXPECT_EQ(5u, buffer.GetSize());
  52. // Causes sudden expansion to make the stack's capacity equal to size
  53. buffer.Push(65536u);
  54. EXPECT_EQ(5u + 65536u, buffer.GetSize());
  55. }
  56. TEST(StringBuffer, Pop) {
  57. StringBuffer buffer;
  58. buffer.Put('A');
  59. buffer.Put('B');
  60. buffer.Put('C');
  61. buffer.Put('D');
  62. buffer.Put('E');
  63. buffer.Pop(3);
  64. EXPECT_EQ(2u, buffer.GetSize());
  65. EXPECT_STREQ("AB", buffer.GetString());
  66. }
  67. #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
  68. #if 0 // Many old compiler does not support these. Turn it off temporaily.
  69. #include <type_traits>
  70. TEST(StringBuffer, Traits) {
  71. static_assert( std::is_constructible<StringBuffer>::value, "");
  72. static_assert( std::is_default_constructible<StringBuffer>::value, "");
  73. #ifndef _MSC_VER
  74. static_assert(!std::is_copy_constructible<StringBuffer>::value, "");
  75. #endif
  76. static_assert( std::is_move_constructible<StringBuffer>::value, "");
  77. static_assert(!std::is_nothrow_constructible<StringBuffer>::value, "");
  78. static_assert(!std::is_nothrow_default_constructible<StringBuffer>::value, "");
  79. #if !defined(_MSC_VER) || _MSC_VER >= 1800
  80. static_assert(!std::is_nothrow_copy_constructible<StringBuffer>::value, "");
  81. static_assert(!std::is_nothrow_move_constructible<StringBuffer>::value, "");
  82. #endif
  83. static_assert( std::is_assignable<StringBuffer,StringBuffer>::value, "");
  84. #ifndef _MSC_VER
  85. static_assert(!std::is_copy_assignable<StringBuffer>::value, "");
  86. #endif
  87. static_assert( std::is_move_assignable<StringBuffer>::value, "");
  88. #if !defined(_MSC_VER) || _MSC_VER >= 1800
  89. static_assert(!std::is_nothrow_assignable<StringBuffer, StringBuffer>::value, "");
  90. #endif
  91. static_assert(!std::is_nothrow_copy_assignable<StringBuffer>::value, "");
  92. static_assert(!std::is_nothrow_move_assignable<StringBuffer>::value, "");
  93. static_assert( std::is_destructible<StringBuffer>::value, "");
  94. #ifndef _MSC_VER
  95. static_assert(std::is_nothrow_destructible<StringBuffer>::value, "");
  96. #endif
  97. }
  98. #endif
  99. TEST(StringBuffer, MoveConstructor) {
  100. StringBuffer x;
  101. x.Put('A');
  102. x.Put('B');
  103. x.Put('C');
  104. x.Put('D');
  105. EXPECT_EQ(4u, x.GetSize());
  106. EXPECT_STREQ("ABCD", x.GetString());
  107. // StringBuffer y(x); // does not compile (!is_copy_constructible)
  108. StringBuffer y(std::move(x));
  109. EXPECT_EQ(0u, x.GetSize());
  110. EXPECT_EQ(4u, y.GetSize());
  111. EXPECT_STREQ("ABCD", y.GetString());
  112. // StringBuffer z = y; // does not compile (!is_copy_assignable)
  113. StringBuffer z = std::move(y);
  114. EXPECT_EQ(0u, y.GetSize());
  115. EXPECT_EQ(4u, z.GetSize());
  116. EXPECT_STREQ("ABCD", z.GetString());
  117. }
  118. TEST(StringBuffer, MoveAssignment) {
  119. StringBuffer x;
  120. x.Put('A');
  121. x.Put('B');
  122. x.Put('C');
  123. x.Put('D');
  124. EXPECT_EQ(4u, x.GetSize());
  125. EXPECT_STREQ("ABCD", x.GetString());
  126. StringBuffer y;
  127. // y = x; // does not compile (!is_copy_assignable)
  128. y = std::move(x);
  129. EXPECT_EQ(0u, x.GetSize());
  130. EXPECT_EQ(4u, y.GetSize());
  131. EXPECT_STREQ("ABCD", y.GetString());
  132. }
  133. #endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS
  134. #ifdef __clang__
  135. RAPIDJSON_DIAG_POP
  136. #endif