stringbuffertest.cpp 5.43 KB
Newer Older
1 2 3
// Tencent is pleased to support the open source community by making RapidJSON available.
// 
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
4
//
5 6
// Licensed under the MIT License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
7
//
8
// http://opensource.org/licenses/MIT
9
//
10 11 12 13
// Unless required by applicable law or agreed to in writing, software distributed 
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
// CONDITIONS OF ANY KIND, either express or implied. See the License for the 
// specific language governing permissions and limitations under the License.
14 15 16 17 18

#include "unittest.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"

19 20 21 22 23
#ifdef __clang__
RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(c++98-compat)
#endif

24 25 26
using namespace rapidjson;

TEST(StringBuffer, InitialSize) {
27 28
    StringBuffer buffer;
    EXPECT_EQ(0u, buffer.GetSize());
29
    EXPECT_EQ(0u, buffer.GetLength());
30
    EXPECT_STREQ("", buffer.GetString());
31 32 33
}

TEST(StringBuffer, Put) {
34 35
    StringBuffer buffer;
    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 43 44
TEST(StringBuffer, PutN_Issue672) {
    GenericStringBuffer<UTF8<>, MemoryPoolAllocator<> > buffer;
    EXPECT_EQ(0, buffer.GetSize());
45
    EXPECT_EQ(0, buffer.GetLength());
46 47
    rapidjson::PutN(buffer, ' ', 1);
    EXPECT_EQ(1, buffer.GetSize());
48
    EXPECT_EQ(1, buffer.GetLength());
49 50
}

51
TEST(StringBuffer, Clear) {
52 53 54 55 56 57 58
    StringBuffer buffer;
    buffer.Put('A');
    buffer.Put('B');
    buffer.Put('C');
    buffer.Clear();

    EXPECT_EQ(0u, buffer.GetSize());
59
    EXPECT_EQ(0u, buffer.GetLength());
60
    EXPECT_STREQ("", buffer.GetString());
61 62 63
}

TEST(StringBuffer, Push) {
64 65
    StringBuffer buffer;
    buffer.Push(5);
66

67
    EXPECT_EQ(5u, buffer.GetSize());
68
    EXPECT_EQ(5u, buffer.GetLength());
miloyip's avatar
miloyip committed
69

miloyip's avatar
miloyip committed
70
    // Causes sudden expansion to make the stack's capacity equal to size
miloyip's avatar
miloyip committed
71 72
    buffer.Push(65536u);
    EXPECT_EQ(5u + 65536u, buffer.GetSize());
73 74 75
}

TEST(StringBuffer, Pop) {
76 77 78 79 80 81 82 83 84
    StringBuffer buffer;
    buffer.Put('A');
    buffer.Put('B');
    buffer.Put('C');
    buffer.Put('D');
    buffer.Put('E');
    buffer.Pop(3);

    EXPECT_EQ(2u, buffer.GetSize());
85
    EXPECT_EQ(2u, buffer.GetLength());
86
    EXPECT_STREQ("AB", buffer.GetString());
87 88
}

89 90 91 92 93 94 95 96 97
TEST(StringBuffer, GetLength_Issue744) {
    GenericStringBuffer<UTF16<wchar_t> > buffer;
    buffer.Put('A');
    buffer.Put('B');
    buffer.Put('C');
    EXPECT_EQ(3u * sizeof(wchar_t), buffer.GetSize());
    EXPECT_EQ(3u, buffer.GetLength());
}

98
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
99

Milo Yip's avatar
Milo Yip committed
100 101
#if 0 // Many old compiler does not support these. Turn it off temporaily.

102 103 104
#include <type_traits>

TEST(StringBuffer, Traits) {
105 106 107 108 109 110 111 112 113
    static_assert( std::is_constructible<StringBuffer>::value, "");
    static_assert( std::is_default_constructible<StringBuffer>::value, "");
#ifndef _MSC_VER
    static_assert(!std::is_copy_constructible<StringBuffer>::value, "");
#endif
    static_assert( std::is_move_constructible<StringBuffer>::value, "");

    static_assert(!std::is_nothrow_constructible<StringBuffer>::value, "");
    static_assert(!std::is_nothrow_default_constructible<StringBuffer>::value, "");
114 115

#if !defined(_MSC_VER) || _MSC_VER >= 1800
116 117
    static_assert(!std::is_nothrow_copy_constructible<StringBuffer>::value, "");
    static_assert(!std::is_nothrow_move_constructible<StringBuffer>::value, "");
118
#endif
119 120 121 122 123 124 125

    static_assert( std::is_assignable<StringBuffer,StringBuffer>::value, "");
#ifndef _MSC_VER
    static_assert(!std::is_copy_assignable<StringBuffer>::value, "");
#endif
    static_assert( std::is_move_assignable<StringBuffer>::value, "");

126 127 128 129
#if !defined(_MSC_VER) || _MSC_VER >= 1800
    static_assert(!std::is_nothrow_assignable<StringBuffer, StringBuffer>::value, "");
#endif

130 131 132 133 134 135 136
    static_assert(!std::is_nothrow_copy_assignable<StringBuffer>::value, "");
    static_assert(!std::is_nothrow_move_assignable<StringBuffer>::value, "");

    static_assert( std::is_destructible<StringBuffer>::value, "");
#ifndef _MSC_VER
    static_assert(std::is_nothrow_destructible<StringBuffer>::value, "");
#endif
137 138
}

Milo Yip's avatar
Milo Yip committed
139 140
#endif

141
TEST(StringBuffer, MoveConstructor) {
142 143 144 145 146 147 148
    StringBuffer x;
    x.Put('A');
    x.Put('B');
    x.Put('C');
    x.Put('D');

    EXPECT_EQ(4u, x.GetSize());
149
    EXPECT_EQ(4u, x.GetLength());
150 151 152 153 154
    EXPECT_STREQ("ABCD", x.GetString());

    // StringBuffer y(x); // does not compile (!is_copy_constructible)
    StringBuffer y(std::move(x));
    EXPECT_EQ(0u, x.GetSize());
155
    EXPECT_EQ(0u, x.GetLength());
156
    EXPECT_EQ(4u, y.GetSize());
157
    EXPECT_EQ(4u, y.GetLength());
158 159 160 161 162
    EXPECT_STREQ("ABCD", y.GetString());

    // StringBuffer z = y; // does not compile (!is_copy_assignable)
    StringBuffer z = std::move(y);
    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 171 172 173 174 175 176
    StringBuffer x;
    x.Put('A');
    x.Put('B');
    x.Put('C');
    x.Put('D');

    EXPECT_EQ(4u, x.GetSize());
177
    EXPECT_EQ(4u, x.GetLength());
178 179 180 181 182 183
    EXPECT_STREQ("ABCD", x.GetString());

    StringBuffer y;
    // y = x; // does not compile (!is_copy_assignable)
    y = std::move(x);
    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 191 192

#ifdef __clang__
RAPIDJSON_DIAG_POP
#endif