stringbuffertest.cpp 4.5 KB

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