stringbuffertest.cpp
Go to the documentation of this file.
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 
15 #include "unittest.h"
16 #include "rapidjson/stringbuffer.h"
17 #include "rapidjson/writer.h"
18 
19 #ifdef __clang__
20 RAPIDJSON_DIAG_PUSH
21 RAPIDJSON_DIAG_OFF(c++98-compat)
22 #endif
23 
24 using namespace rapidjson;
25 
26 TEST(StringBuffer, InitialSize) {
27  StringBuffer buffer;
28  EXPECT_EQ(0u, buffer.GetSize());
29  EXPECT_EQ(0u, buffer.GetLength());
30  EXPECT_STREQ("", buffer.GetString());
31 }
32 
34  StringBuffer buffer;
35  buffer.Put('A');
36 
37  EXPECT_EQ(1u, buffer.GetSize());
38  EXPECT_EQ(1u, buffer.GetLength());
39  EXPECT_STREQ("A", buffer.GetString());
40 }
41 
42 TEST(StringBuffer, PutN_Issue672) {
44  EXPECT_EQ(0, buffer.GetSize());
45  EXPECT_EQ(0, buffer.GetLength());
46  rapidjson::PutN(buffer, ' ', 1);
47  EXPECT_EQ(1, buffer.GetSize());
48  EXPECT_EQ(1, buffer.GetLength());
49 }
50 
51 TEST(StringBuffer, Clear) {
52  StringBuffer buffer;
53  buffer.Put('A');
54  buffer.Put('B');
55  buffer.Put('C');
56  buffer.Clear();
57 
58  EXPECT_EQ(0u, buffer.GetSize());
59  EXPECT_EQ(0u, buffer.GetLength());
60  EXPECT_STREQ("", buffer.GetString());
61 }
62 
64  StringBuffer buffer;
65  buffer.Push(5);
66 
67  EXPECT_EQ(5u, buffer.GetSize());
68  EXPECT_EQ(5u, buffer.GetLength());
69 
70  // Causes sudden expansion to make the stack's capacity equal to size
71  buffer.Push(65536u);
72  EXPECT_EQ(5u + 65536u, buffer.GetSize());
73 }
74 
76  StringBuffer buffer;
77  buffer.Put('A');
78  buffer.Put('B');
79  buffer.Put('C');
80  buffer.Put('D');
81  buffer.Put('E');
82  buffer.Pop(3);
83 
84  EXPECT_EQ(2u, buffer.GetSize());
85  EXPECT_EQ(2u, buffer.GetLength());
86  EXPECT_STREQ("AB", buffer.GetString());
87 }
88 
89 TEST(StringBuffer, GetLength_Issue744) {
91  buffer.Put('A');
92  buffer.Put('B');
93  buffer.Put('C');
94  EXPECT_EQ(3u * sizeof(wchar_t), buffer.GetSize());
95  EXPECT_EQ(3u, buffer.GetLength());
96 }
97 
98 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
99 
100 #if 0 // Many old compiler does not support these. Turn it off temporaily.
101 
102 #include <type_traits>
103 
105  static_assert( std::is_constructible<StringBuffer>::value, "");
107 #ifndef _MSC_VER
109 #endif
111 
114 
115 #if !defined(_MSC_VER) || _MSC_VER >= 1800
118 #endif
119 
121 #ifndef _MSC_VER
123 #endif
125 
126 #if !defined(_MSC_VER) || _MSC_VER >= 1800
128 #endif
129 
132 
133  static_assert( std::is_destructible<StringBuffer>::value, "");
134 #ifndef _MSC_VER
136 #endif
137 }
138 
139 #endif
140 
141 TEST(StringBuffer, MoveConstructor) {
142  StringBuffer x;
143  x.Put('A');
144  x.Put('B');
145  x.Put('C');
146  x.Put('D');
147 
148  EXPECT_EQ(4u, x.GetSize());
149  EXPECT_EQ(4u, x.GetLength());
150  EXPECT_STREQ("ABCD", x.GetString());
151 
152  // StringBuffer y(x); // does not compile (!is_copy_constructible)
153  StringBuffer y(std::move(x));
154  EXPECT_EQ(0u, x.GetSize());
155  EXPECT_EQ(0u, x.GetLength());
156  EXPECT_EQ(4u, y.GetSize());
157  EXPECT_EQ(4u, y.GetLength());
158  EXPECT_STREQ("ABCD", y.GetString());
159 
160  // StringBuffer z = y; // does not compile (!is_copy_assignable)
161  StringBuffer z = std::move(y);
162  EXPECT_EQ(0u, y.GetSize());
163  EXPECT_EQ(0u, y.GetLength());
164  EXPECT_EQ(4u, z.GetSize());
165  EXPECT_EQ(4u, z.GetLength());
166  EXPECT_STREQ("ABCD", z.GetString());
167 }
168 
169 TEST(StringBuffer, MoveAssignment) {
170  StringBuffer x;
171  x.Put('A');
172  x.Put('B');
173  x.Put('C');
174  x.Put('D');
175 
176  EXPECT_EQ(4u, x.GetSize());
177  EXPECT_EQ(4u, x.GetLength());
178  EXPECT_STREQ("ABCD", x.GetString());
179 
180  StringBuffer y;
181  // y = x; // does not compile (!is_copy_assignable)
182  y = std::move(x);
183  EXPECT_EQ(0u, x.GetSize());
184  EXPECT_EQ(4u, y.GetLength());
185  EXPECT_STREQ("ABCD", y.GetString());
186 }
187 
188 #endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS
189 
190 #ifdef __clang__
191 RAPIDJSON_DIAG_POP
192 #endif
size_t GetSize() const
Get the size of string in bytes in the string buffer.
Definition: stringbuffer.h:82
const Ch * GetString() const
Definition: stringbuffer.h:73
void PutN(FileWriteStream &stream, char c, size_t n)
Implement specialized version of PutN() with memset() for better performance.
Ch * Push(size_t count)
Definition: stringbuffer.h:69
main RapidJSON namespace
TEST(StringBuffer, InitialSize)
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1222
size_t GetLength() const
Get the length of string in Ch in the string buffer.
Definition: stringbuffer.h:85
void Pop(size_t count)
Definition: stringbuffer.h:71


choreo_rapidjson
Author(s):
autogenerated on Thu Jul 18 2019 03:59:09