• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 using namespace rapidjson;
20 
TEST(StringBuffer,InitialSize)21 TEST(StringBuffer, InitialSize) {
22     StringBuffer buffer;
23     EXPECT_EQ(0u, buffer.GetSize());
24     EXPECT_STREQ("", buffer.GetString());
25 }
26 
TEST(StringBuffer,Put)27 TEST(StringBuffer, Put) {
28     StringBuffer buffer;
29     buffer.Put('A');
30 
31     EXPECT_EQ(1u, buffer.GetSize());
32     EXPECT_STREQ("A", buffer.GetString());
33 }
34 
TEST(StringBuffer,Clear)35 TEST(StringBuffer, Clear) {
36     StringBuffer buffer;
37     buffer.Put('A');
38     buffer.Put('B');
39     buffer.Put('C');
40     buffer.Clear();
41 
42     EXPECT_EQ(0u, buffer.GetSize());
43     EXPECT_STREQ("", buffer.GetString());
44 }
45 
TEST(StringBuffer,Push)46 TEST(StringBuffer, Push) {
47     StringBuffer buffer;
48     buffer.Push(5);
49 
50     EXPECT_EQ(5u, buffer.GetSize());
51 
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 TEST(StringBuffer, Pop) {
58     StringBuffer buffer;
59     buffer.Put('A');
60     buffer.Put('B');
61     buffer.Put('C');
62     buffer.Put('D');
63     buffer.Put('E');
64     buffer.Pop(3);
65 
66     EXPECT_EQ(2u, buffer.GetSize());
67     EXPECT_STREQ("AB", buffer.GetString());
68 }
69 
70 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
71 
72 #include <type_traits>
73 
TEST(StringBuffer,Traits)74 TEST(StringBuffer, Traits) {
75     static_assert( std::is_constructible<StringBuffer>::value, "");
76     static_assert( std::is_default_constructible<StringBuffer>::value, "");
77 #ifndef _MSC_VER
78     static_assert(!std::is_copy_constructible<StringBuffer>::value, "");
79 #endif
80     static_assert( std::is_move_constructible<StringBuffer>::value, "");
81 
82     static_assert(!std::is_nothrow_constructible<StringBuffer>::value, "");
83     static_assert(!std::is_nothrow_default_constructible<StringBuffer>::value, "");
84 
85 #if !defined(_MSC_VER) || _MSC_VER >= 1800
86     static_assert(!std::is_nothrow_copy_constructible<StringBuffer>::value, "");
87     static_assert(!std::is_nothrow_move_constructible<StringBuffer>::value, "");
88 #endif
89 
90     static_assert( std::is_assignable<StringBuffer,StringBuffer>::value, "");
91 #ifndef _MSC_VER
92     static_assert(!std::is_copy_assignable<StringBuffer>::value, "");
93 #endif
94     static_assert( std::is_move_assignable<StringBuffer>::value, "");
95 
96 #if !defined(_MSC_VER) || _MSC_VER >= 1800
97     static_assert(!std::is_nothrow_assignable<StringBuffer, StringBuffer>::value, "");
98 #endif
99 
100     static_assert(!std::is_nothrow_copy_assignable<StringBuffer>::value, "");
101     static_assert(!std::is_nothrow_move_assignable<StringBuffer>::value, "");
102 
103     static_assert( std::is_destructible<StringBuffer>::value, "");
104 #ifndef _MSC_VER
105     static_assert(std::is_nothrow_destructible<StringBuffer>::value, "");
106 #endif
107 }
108 
TEST(StringBuffer,MoveConstructor)109 TEST(StringBuffer, MoveConstructor) {
110     StringBuffer x;
111     x.Put('A');
112     x.Put('B');
113     x.Put('C');
114     x.Put('D');
115 
116     EXPECT_EQ(4u, x.GetSize());
117     EXPECT_STREQ("ABCD", x.GetString());
118 
119     // StringBuffer y(x); // does not compile (!is_copy_constructible)
120     StringBuffer y(std::move(x));
121     EXPECT_EQ(0u, x.GetSize());
122     EXPECT_EQ(4u, y.GetSize());
123     EXPECT_STREQ("ABCD", y.GetString());
124 
125     // StringBuffer z = y; // does not compile (!is_copy_assignable)
126     StringBuffer z = std::move(y);
127     EXPECT_EQ(0u, y.GetSize());
128     EXPECT_EQ(4u, z.GetSize());
129     EXPECT_STREQ("ABCD", z.GetString());
130 }
131 
TEST(StringBuffer,MoveAssignment)132 TEST(StringBuffer, MoveAssignment) {
133     StringBuffer x;
134     x.Put('A');
135     x.Put('B');
136     x.Put('C');
137     x.Put('D');
138 
139     EXPECT_EQ(4u, x.GetSize());
140     EXPECT_STREQ("ABCD", x.GetString());
141 
142     StringBuffer y;
143     // y = x; // does not compile (!is_copy_assignable)
144     y = std::move(x);
145     EXPECT_EQ(0u, x.GetSize());
146     EXPECT_EQ(4u, y.GetSize());
147     EXPECT_STREQ("ABCD", y.GetString());
148 }
149 
150 #endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS
151