| 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. |
| 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 |
|
| 33 |
TEST(StringBuffer, Put) { |
| 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) { |
| 43 |
GenericStringBuffer<UTF8<>, MemoryPoolAllocator<> > buffer; |
| 44 |
EXPECT_EQ(0u, buffer.GetSize()); |
| 45 |
EXPECT_EQ(0u, buffer.GetLength()); |
| 46 |
rapidjson::PutN(buffer, ' ', 1); |
| 47 |
EXPECT_EQ(1u, buffer.GetSize()); |
| 48 |
EXPECT_EQ(1u, 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 |
|
| 63 |
TEST(StringBuffer, Push) { |
| 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 |
|
| 75 |
TEST(StringBuffer, Pop) { |
| 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) { |
| 90 |
GenericStringBuffer<UTF16<wchar_t> > buffer; |
| 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 |
|
| 104 |
TEST(StringBuffer, Traits) { |
| 105 |
static_assert( std::is_constructible<StringBuffer>::value, ""); |
| 106 |
static_assert( std::is_default_constructible<StringBuffer>::value, ""); |
| 107 |
#ifndef _MSC_VER |
| 108 |
static_assert(!std::is_copy_constructible<StringBuffer>::value, ""); |
| 109 |
#endif |
| 110 |
static_assert( std::is_move_constructible<StringBuffer>::value, ""); |
| 111 |
|
| 112 |
static_assert(!std::is_nothrow_constructible<StringBuffer>::value, ""); |
| 113 |
static_assert(!std::is_nothrow_default_constructible<StringBuffer>::value, ""); |
| 114 |
|
| 115 |
#if !defined(_MSC_VER) || _MSC_VER >= 1800 |
| 116 |
static_assert(!std::is_nothrow_copy_constructible<StringBuffer>::value, ""); |
| 117 |
static_assert(!std::is_nothrow_move_constructible<StringBuffer>::value, ""); |
| 118 |
#endif |
| 119 |
|
| 120 |
static_assert( std::is_assignable<StringBuffer,StringBuffer>::value, ""); |
| 121 |
#ifndef _MSC_VER |
| 122 |
static_assert(!std::is_copy_assignable<StringBuffer>::value, ""); |
| 123 |
#endif |
| 124 |
static_assert( std::is_move_assignable<StringBuffer>::value, ""); |
| 125 |
|
| 126 |
#if !defined(_MSC_VER) || _MSC_VER >= 1800 |
| 127 |
static_assert(!std::is_nothrow_assignable<StringBuffer, StringBuffer>::value, ""); |
| 128 |
#endif |
| 129 |
|
| 130 |
static_assert(!std::is_nothrow_copy_assignable<StringBuffer>::value, ""); |
| 131 |
static_assert(!std::is_nothrow_move_assignable<StringBuffer>::value, ""); |
| 132 |
|
| 133 |
static_assert( std::is_destructible<StringBuffer>::value, ""); |
| 134 |
#ifndef _MSC_VER |
| 135 |
static_assert(std::is_nothrow_destructible<StringBuffer>::value, ""); |
| 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 |