| 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/document.h" |
| 17 |
#include "rapidjson/writer.h" |
| 18 |
#include "rapidjson/filereadstream.h" |
| 19 |
#include "rapidjson/encodedstream.h" |
| 20 |
#include "rapidjson/stringbuffer.h" |
| 21 |
#include <sstream> |
| 22 |
#include <algorithm> |
| 23 |
|
| 24 |
#ifdef __clang__ |
| 25 |
RAPIDJSON_DIAG_PUSH |
| 26 |
RAPIDJSON_DIAG_OFF(c++98-compat) |
| 27 |
RAPIDJSON_DIAG_OFF(missing-variable-declarations) |
| 28 |
#endif |
| 29 |
|
| 30 |
using namespace rapidjson; |
| 31 |
|
| 32 |
template <typename DocumentType> |
| 33 |
void ParseCheck(DocumentType& doc) { |
| 34 |
typedef typename DocumentType::ValueType ValueType; |
| 35 |
|
| 36 |
EXPECT_FALSE(doc.HasParseError()); |
| 37 |
if (doc.HasParseError()) |
| 38 |
printf("Error: %d at %zu\n", static_cast<int>(doc.GetParseError()), doc.GetErrorOffset()); |
| 39 |
EXPECT_TRUE(static_cast<ParseResult>(doc)); |
| 40 |
|
| 41 |
EXPECT_TRUE(doc.IsObject()); |
| 42 |
|
| 43 |
EXPECT_TRUE(doc.HasMember("hello")); |
| 44 |
const ValueType& hello = doc["hello"]; |
| 45 |
EXPECT_TRUE(hello.IsString()); |
| 46 |
EXPECT_STREQ("world", hello.GetString()); |
| 47 |
|
| 48 |
EXPECT_TRUE(doc.HasMember("t")); |
| 49 |
const ValueType& t = doc["t"]; |
| 50 |
EXPECT_TRUE(t.IsTrue()); |
| 51 |
|
| 52 |
EXPECT_TRUE(doc.HasMember("f")); |
| 53 |
const ValueType& f = doc["f"]; |
| 54 |
EXPECT_TRUE(f.IsFalse()); |
| 55 |
|
| 56 |
EXPECT_TRUE(doc.HasMember("n")); |
| 57 |
const ValueType& n = doc["n"]; |
| 58 |
EXPECT_TRUE(n.IsNull()); |
| 59 |
|
| 60 |
EXPECT_TRUE(doc.HasMember("i")); |
| 61 |
const ValueType& i = doc["i"]; |
| 62 |
EXPECT_TRUE(i.IsNumber()); |
| 63 |
EXPECT_EQ(123, i.GetInt()); |
| 64 |
|
| 65 |
EXPECT_TRUE(doc.HasMember("pi")); |
| 66 |
const ValueType& pi = doc["pi"]; |
| 67 |
EXPECT_TRUE(pi.IsNumber()); |
| 68 |
EXPECT_DOUBLE_EQ(3.1416, pi.GetDouble()); |
| 69 |
|
| 70 |
EXPECT_TRUE(doc.HasMember("a")); |
| 71 |
const ValueType& a = doc["a"]; |
| 72 |
EXPECT_TRUE(a.IsArray()); |
| 73 |
EXPECT_EQ(4u, a.Size()); |
| 74 |
for (SizeType j = 0; j < 4; j++) |
| 75 |
EXPECT_EQ(j + 1, a[j].GetUint()); |
| 76 |
} |
| 77 |
|
| 78 |
template <typename Allocator, typename StackAllocator> |
| 79 |
void ParseTest() { |
| 80 |
typedef GenericDocument<UTF8<>, Allocator, StackAllocator> DocumentType; |
| 81 |
DocumentType doc; |
| 82 |
|
| 83 |
const char* json = " { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } "; |
| 84 |
|
| 85 |
doc.Parse(json); |
| 86 |
ParseCheck(doc); |
| 87 |
|
| 88 |
doc.SetNull(); |
| 89 |
StringStream s(json); |
| 90 |
doc.template ParseStream<0>(s); |
| 91 |
ParseCheck(doc); |
| 92 |
|
| 93 |
doc.SetNull(); |
| 94 |
char *buffer = strdup(json); |
| 95 |
doc.ParseInsitu(buffer); |
| 96 |
ParseCheck(doc); |
| 97 |
free(buffer); |
| 98 |
|
| 99 |
// Parse(const Ch*, size_t) |
| 100 |
size_t length = strlen(json); |
| 101 |
buffer = reinterpret_cast<char*>(malloc(length * 2)); |
| 102 |
memcpy(buffer, json, length); |
| 103 |
memset(buffer + length, 'X', length); |
| 104 |
#if RAPIDJSON_HAS_STDSTRING |
| 105 |
std::string s2(buffer, length); // backup buffer |
| 106 |
#endif |
| 107 |
doc.SetNull(); |
| 108 |
doc.Parse(buffer, length); |
| 109 |
free(buffer); |
| 110 |
ParseCheck(doc); |
| 111 |
|
| 112 |
#if RAPIDJSON_HAS_STDSTRING |
| 113 |
// Parse(std::string) |
| 114 |
doc.SetNull(); |
| 115 |
doc.Parse(s2); |
| 116 |
ParseCheck(doc); |
| 117 |
#endif |
| 118 |
} |
| 119 |
|
| 120 |
TEST(Document, Parse) { |
| 121 |
ParseTest<MemoryPoolAllocator<>, CrtAllocator>(); |
| 122 |
ParseTest<MemoryPoolAllocator<>, MemoryPoolAllocator<> >(); |
| 123 |
ParseTest<CrtAllocator, MemoryPoolAllocator<> >(); |
| 124 |
ParseTest<CrtAllocator, CrtAllocator>(); |
| 125 |
} |
| 126 |
|
| 127 |
TEST(Document, UnchangedOnParseError) { |
| 128 |
Document doc; |
| 129 |
doc.SetArray().PushBack(0, doc.GetAllocator()); |
| 130 |
|
| 131 |
ParseResult noError; |
| 132 |
EXPECT_TRUE(noError); |
| 133 |
|
| 134 |
ParseResult err = doc.Parse("{]"); |
| 135 |
EXPECT_TRUE(doc.HasParseError()); |
| 136 |
EXPECT_NE(err, noError); |
| 137 |
EXPECT_NE(err.Code(), noError); |
| 138 |
EXPECT_NE(noError, doc.GetParseError()); |
| 139 |
EXPECT_EQ(err.Code(), doc.GetParseError()); |
| 140 |
EXPECT_EQ(err.Offset(), doc.GetErrorOffset()); |
| 141 |
EXPECT_TRUE(doc.IsArray()); |
| 142 |
EXPECT_EQ(doc.Size(), 1u); |
| 143 |
|
| 144 |
err = doc.Parse("{}"); |
| 145 |
EXPECT_FALSE(doc.HasParseError()); |
| 146 |
EXPECT_FALSE(err.IsError()); |
| 147 |
EXPECT_TRUE(err); |
| 148 |
EXPECT_EQ(err, noError); |
| 149 |
EXPECT_EQ(err.Code(), noError); |
| 150 |
EXPECT_EQ(err.Code(), doc.GetParseError()); |
| 151 |
EXPECT_EQ(err.Offset(), doc.GetErrorOffset()); |
| 152 |
EXPECT_TRUE(doc.IsObject()); |
| 153 |
EXPECT_EQ(doc.MemberCount(), 0u); |
| 154 |
} |
| 155 |
|
| 156 |
static FILE* OpenEncodedFile(const char* filename) { |
| 157 |
const char *paths[] = { |
| 158 |
"encodings", |
| 159 |
"bin/encodings", |
| 160 |
"../bin/encodings", |
| 161 |
"../../bin/encodings", |
| 162 |
"../../../bin/encodings" |
| 163 |
}; |
| 164 |
char buffer[1024]; |
| 165 |
for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) { |
| 166 |
sprintf(buffer, "%s/%s", paths[i], filename); |
| 167 |
FILE *fp = fopen(buffer, "rb"); |
| 168 |
if (fp) |
| 169 |
return fp; |
| 170 |
} |
| 171 |
return 0; |
| 172 |
} |
| 173 |
|
| 174 |
TEST(Document, Parse_Encoding) { |
| 175 |
const char* json = " { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } "; |
| 176 |
|
| 177 |
typedef GenericDocument<UTF16<> > DocumentType; |
| 178 |
DocumentType doc; |
| 179 |
|
| 180 |
// Parse<unsigned, SourceEncoding>(const SourceEncoding::Ch*) |
| 181 |
// doc.Parse<kParseDefaultFlags, UTF8<> >(json); |
| 182 |
// EXPECT_FALSE(doc.HasParseError()); |
| 183 |
// EXPECT_EQ(0, StrCmp(doc[L"hello"].GetString(), L"world")); |
| 184 |
|
| 185 |
// Parse<unsigned, SourceEncoding>(const SourceEncoding::Ch*, size_t) |
| 186 |
size_t length = strlen(json); |
| 187 |
char* buffer = reinterpret_cast<char*>(malloc(length * 2)); |
| 188 |
memcpy(buffer, json, length); |
| 189 |
memset(buffer + length, 'X', length); |
| 190 |
#if RAPIDJSON_HAS_STDSTRING |
| 191 |
std::string s2(buffer, length); // backup buffer |
| 192 |
#endif |
| 193 |
doc.SetNull(); |
| 194 |
doc.Parse<kParseDefaultFlags, UTF8<> >(buffer, length); |
| 195 |
free(buffer); |
| 196 |
EXPECT_FALSE(doc.HasParseError()); |
| 197 |
if (doc.HasParseError()) |
| 198 |
printf("Error: %d at %zu\n", static_cast<int>(doc.GetParseError()), doc.GetErrorOffset()); |
| 199 |
EXPECT_EQ(0, StrCmp(doc[L"hello"].GetString(), L"world")); |
| 200 |
|
| 201 |
#if RAPIDJSON_HAS_STDSTRING |
| 202 |
// Parse<unsigned, SourceEncoding>(std::string) |
| 203 |
doc.SetNull(); |
| 204 |
|
| 205 |
#if defined(_MSC_VER) && _MSC_VER < 1800 |
| 206 |
doc.Parse<kParseDefaultFlags, UTF8<> >(s2.c_str()); // VS2010 or below cannot handle templated function overloading. Use const char* instead. |
| 207 |
#else |
| 208 |
doc.Parse<kParseDefaultFlags, UTF8<> >(s2); |
| 209 |
#endif |
| 210 |
EXPECT_FALSE(doc.HasParseError()); |
| 211 |
EXPECT_EQ(0, StrCmp(doc[L"hello"].GetString(), L"world")); |
| 212 |
#endif |
| 213 |
} |
| 214 |
|
| 215 |
TEST(Document, ParseStream_EncodedInputStream) { |
| 216 |
// UTF8 -> UTF16 |
| 217 |
FILE* fp = OpenEncodedFile("utf8.json"); |
| 218 |
char buffer[256]; |
| 219 |
FileReadStream bis(fp, buffer, sizeof(buffer)); |
| 220 |
EncodedInputStream<UTF8<>, FileReadStream> eis(bis); |
| 221 |
|
| 222 |
GenericDocument<UTF16<> > d; |
| 223 |
d.ParseStream<0, UTF8<> >(eis); |
| 224 |
EXPECT_FALSE(d.HasParseError()); |
| 225 |
|
| 226 |
fclose(fp); |
| 227 |
|
| 228 |
wchar_t expected[] = L"I can eat glass and it doesn't hurt me."; |
| 229 |
GenericValue<UTF16<> >& v = d[L"en"]; |
| 230 |
EXPECT_TRUE(v.IsString()); |
| 231 |
EXPECT_EQ(sizeof(expected) / sizeof(wchar_t) - 1, v.GetStringLength()); |
| 232 |
EXPECT_EQ(0, StrCmp(expected, v.GetString())); |
| 233 |
|
| 234 |
// UTF16 -> UTF8 in memory |
| 235 |
StringBuffer bos; |
| 236 |
typedef EncodedOutputStream<UTF8<>, StringBuffer> OutputStream; |
| 237 |
OutputStream eos(bos, false); // Not writing BOM |
| 238 |
{ |
| 239 |
Writer<OutputStream, UTF16<>, UTF8<> > writer(eos); |
| 240 |
d.Accept(writer); |
| 241 |
} |
| 242 |
|
| 243 |
// Condense the original file and compare. |
| 244 |
fp = OpenEncodedFile("utf8.json"); |
| 245 |
FileReadStream is(fp, buffer, sizeof(buffer)); |
| 246 |
Reader reader; |
| 247 |
StringBuffer bos2; |
| 248 |
Writer<StringBuffer> writer2(bos2); |
| 249 |
reader.Parse(is, writer2); |
| 250 |
fclose(fp); |
| 251 |
|
| 252 |
EXPECT_EQ(bos.GetSize(), bos2.GetSize()); |
| 253 |
EXPECT_EQ(0, memcmp(bos.GetString(), bos2.GetString(), bos2.GetSize())); |
| 254 |
} |
| 255 |
|
| 256 |
TEST(Document, ParseStream_AutoUTFInputStream) { |
| 257 |
// Any -> UTF8 |
| 258 |
FILE* fp = OpenEncodedFile("utf32be.json"); |
| 259 |
char buffer[256]; |
| 260 |
FileReadStream bis(fp, buffer, sizeof(buffer)); |
| 261 |
AutoUTFInputStream<unsigned, FileReadStream> eis(bis); |
| 262 |
|
| 263 |
Document d; |
| 264 |
d.ParseStream<0, AutoUTF<unsigned> >(eis); |
| 265 |
EXPECT_FALSE(d.HasParseError()); |
| 266 |
|
| 267 |
fclose(fp); |
| 268 |
|
| 269 |
char expected[] = "I can eat glass and it doesn't hurt me."; |
| 270 |
Value& v = d["en"]; |
| 271 |
EXPECT_TRUE(v.IsString()); |
| 272 |
EXPECT_EQ(sizeof(expected) - 1, v.GetStringLength()); |
| 273 |
EXPECT_EQ(0, StrCmp(expected, v.GetString())); |
| 274 |
|
| 275 |
// UTF8 -> UTF8 in memory |
| 276 |
StringBuffer bos; |
| 277 |
Writer<StringBuffer> writer(bos); |
| 278 |
d.Accept(writer); |
| 279 |
|
| 280 |
// Condense the original file and compare. |
| 281 |
fp = OpenEncodedFile("utf8.json"); |
| 282 |
FileReadStream is(fp, buffer, sizeof(buffer)); |
| 283 |
Reader reader; |
| 284 |
StringBuffer bos2; |
| 285 |
Writer<StringBuffer> writer2(bos2); |
| 286 |
reader.Parse(is, writer2); |
| 287 |
fclose(fp); |
| 288 |
|
| 289 |
EXPECT_EQ(bos.GetSize(), bos2.GetSize()); |
| 290 |
EXPECT_EQ(0, memcmp(bos.GetString(), bos2.GetString(), bos2.GetSize())); |
| 291 |
} |
| 292 |
|
| 293 |
TEST(Document, Swap) { |
| 294 |
Document d1; |
| 295 |
Document::AllocatorType& a = d1.GetAllocator(); |
| 296 |
|
| 297 |
d1.SetArray().PushBack(1, a).PushBack(2, a); |
| 298 |
|
| 299 |
Value o; |
| 300 |
o.SetObject().AddMember("a", 1, a); |
| 301 |
|
| 302 |
// Swap between Document and Value |
| 303 |
d1.Swap(o); |
| 304 |
EXPECT_TRUE(d1.IsObject()); |
| 305 |
EXPECT_TRUE(o.IsArray()); |
| 306 |
|
| 307 |
d1.Swap(o); |
| 308 |
EXPECT_TRUE(d1.IsArray()); |
| 309 |
EXPECT_TRUE(o.IsObject()); |
| 310 |
|
| 311 |
o.Swap(d1); |
| 312 |
EXPECT_TRUE(d1.IsObject()); |
| 313 |
EXPECT_TRUE(o.IsArray()); |
| 314 |
|
| 315 |
// Swap between Document and Document |
| 316 |
Document d2; |
| 317 |
d2.SetArray().PushBack(3, a); |
| 318 |
d1.Swap(d2); |
| 319 |
EXPECT_TRUE(d1.IsArray()); |
| 320 |
EXPECT_TRUE(d2.IsObject()); |
| 321 |
EXPECT_EQ(&d2.GetAllocator(), &a); |
| 322 |
|
| 323 |
// reset value |
| 324 |
Value().Swap(d1); |
| 325 |
EXPECT_TRUE(d1.IsNull()); |
| 326 |
|
| 327 |
// reset document, including allocator |
| 328 |
// so clear o before so that it doesnt contain dangling elements |
| 329 |
o.Clear(); |
| 330 |
Document().Swap(d2); |
| 331 |
EXPECT_TRUE(d2.IsNull()); |
| 332 |
EXPECT_NE(&d2.GetAllocator(), &a); |
| 333 |
|
| 334 |
// testing std::swap compatibility |
| 335 |
d1.SetBool(true); |
| 336 |
using std::swap; |
| 337 |
swap(d1, d2); |
| 338 |
EXPECT_TRUE(d1.IsNull()); |
| 339 |
EXPECT_TRUE(d2.IsTrue()); |
| 340 |
|
| 341 |
swap(o, d2); |
| 342 |
EXPECT_TRUE(o.IsTrue()); |
| 343 |
EXPECT_TRUE(d2.IsArray()); |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
// This should be slow due to assignment in inner-loop. |
| 348 |
struct OutputStringStream : public std::ostringstream { |
| 349 |
typedef char Ch; |
| 350 |
|
| 351 |
virtual ~OutputStringStream(); |
| 352 |
|
| 353 |
void Put(char c) { |
| 354 |
put(c); |
| 355 |
} |
| 356 |
void Flush() {} |
| 357 |
}; |
| 358 |
|
| 359 |
OutputStringStream::~OutputStringStream() {} |
| 360 |
|
| 361 |
TEST(Document, AcceptWriter) { |
| 362 |
Document doc; |
| 363 |
doc.Parse(" { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } "); |
| 364 |
|
| 365 |
OutputStringStream os; |
| 366 |
Writer<OutputStringStream> writer(os); |
| 367 |
doc.Accept(writer); |
| 368 |
|
| 369 |
EXPECT_EQ("{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3,4]}", os.str()); |
| 370 |
} |
| 371 |
|
| 372 |
TEST(Document, UserBuffer) { |
| 373 |
typedef GenericDocument<UTF8<>, MemoryPoolAllocator<>, MemoryPoolAllocator<> > DocumentType; |
| 374 |
char valueBuffer[4096]; |
| 375 |
char parseBuffer[1024]; |
| 376 |
MemoryPoolAllocator<> valueAllocator(valueBuffer, sizeof(valueBuffer)); |
| 377 |
MemoryPoolAllocator<> parseAllocator(parseBuffer, sizeof(parseBuffer)); |
| 378 |
DocumentType doc(&valueAllocator, sizeof(parseBuffer) / 2, &parseAllocator); |
| 379 |
doc.Parse(" { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } "); |
| 380 |
EXPECT_FALSE(doc.HasParseError()); |
| 381 |
EXPECT_LE(valueAllocator.Size(), sizeof(valueBuffer)); |
| 382 |
EXPECT_LE(parseAllocator.Size(), sizeof(parseBuffer)); |
| 383 |
|
| 384 |
// Cover MemoryPoolAllocator::Capacity() |
| 385 |
EXPECT_LE(valueAllocator.Size(), valueAllocator.Capacity()); |
| 386 |
EXPECT_LE(parseAllocator.Size(), parseAllocator.Capacity()); |
| 387 |
} |
| 388 |
|
| 389 |
// Issue 226: Value of string type should not point to NULL |
| 390 |
TEST(Document, AssertAcceptInvalidNameType) { |
| 391 |
Document doc; |
| 392 |
doc.SetObject(); |
| 393 |
doc.AddMember("a", 0, doc.GetAllocator()); |
| 394 |
doc.FindMember("a")->name.SetNull(); // Change name to non-string type. |
| 395 |
|
| 396 |
OutputStringStream os; |
| 397 |
Writer<OutputStringStream> writer(os); |
| 398 |
ASSERT_THROW(doc.Accept(writer), AssertException); |
| 399 |
} |
| 400 |
|
| 401 |
// Issue 44: SetStringRaw doesn't work with wchar_t |
| 402 |
TEST(Document, UTF16_Document) { |
| 403 |
GenericDocument< UTF16<> > json; |
| 404 |
json.Parse<kParseValidateEncodingFlag>(L"[{\"created_at\":\"Wed Oct 30 17:13:20 +0000 2012\"}]"); |
| 405 |
|
| 406 |
ASSERT_TRUE(json.IsArray()); |
| 407 |
GenericValue< UTF16<> >& v = json[0]; |
| 408 |
ASSERT_TRUE(v.IsObject()); |
| 409 |
|
| 410 |
GenericValue< UTF16<> >& s = v[L"created_at"]; |
| 411 |
ASSERT_TRUE(s.IsString()); |
| 412 |
|
| 413 |
EXPECT_EQ(0, memcmp(L"Wed Oct 30 17:13:20 +0000 2012", s.GetString(), (s.GetStringLength() + 1) * sizeof(wchar_t))); |
| 414 |
} |
| 415 |
|
| 416 |
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS |
| 417 |
|
| 418 |
#if 0 // Many old compiler does not support these. Turn it off temporaily. |
| 419 |
|
| 420 |
#include <type_traits> |
| 421 |
|
| 422 |
TEST(Document, Traits) { |
| 423 |
static_assert(std::is_constructible<Document>::value, ""); |
| 424 |
static_assert(std::is_default_constructible<Document>::value, ""); |
| 425 |
#ifndef _MSC_VER |
| 426 |
static_assert(!std::is_copy_constructible<Document>::value, ""); |
| 427 |
#endif |
| 428 |
static_assert(std::is_move_constructible<Document>::value, ""); |
| 429 |
|
| 430 |
static_assert(!std::is_nothrow_constructible<Document>::value, ""); |
| 431 |
static_assert(!std::is_nothrow_default_constructible<Document>::value, ""); |
| 432 |
#ifndef _MSC_VER |
| 433 |
static_assert(!std::is_nothrow_copy_constructible<Document>::value, ""); |
| 434 |
static_assert(std::is_nothrow_move_constructible<Document>::value, ""); |
| 435 |
#endif |
| 436 |
|
| 437 |
static_assert(std::is_assignable<Document,Document>::value, ""); |
| 438 |
#ifndef _MSC_VER |
| 439 |
static_assert(!std::is_copy_assignable<Document>::value, ""); |
| 440 |
#endif |
| 441 |
static_assert(std::is_move_assignable<Document>::value, ""); |
| 442 |
|
| 443 |
#ifndef _MSC_VER |
| 444 |
static_assert(std::is_nothrow_assignable<Document, Document>::value, ""); |
| 445 |
#endif |
| 446 |
static_assert(!std::is_nothrow_copy_assignable<Document>::value, ""); |
| 447 |
#ifndef _MSC_VER |
| 448 |
static_assert(std::is_nothrow_move_assignable<Document>::value, ""); |
| 449 |
#endif |
| 450 |
|
| 451 |
static_assert( std::is_destructible<Document>::value, ""); |
| 452 |
#ifndef _MSC_VER |
| 453 |
static_assert(std::is_nothrow_destructible<Document>::value, ""); |
| 454 |
#endif |
| 455 |
} |
| 456 |
|
| 457 |
#endif |
| 458 |
|
| 459 |
template <typename Allocator> |
| 460 |
struct DocumentMove: public ::testing::Test { |
| 461 |
}; |
| 462 |
|
| 463 |
typedef ::testing::Types< CrtAllocator, MemoryPoolAllocator<> > MoveAllocatorTypes; |
| 464 |
TYPED_TEST_CASE(DocumentMove, MoveAllocatorTypes); |
| 465 |
|
| 466 |
TYPED_TEST(DocumentMove, MoveConstructor) { |
| 467 |
typedef TypeParam Allocator; |
| 468 |
typedef GenericDocument<UTF8<>, Allocator> D; |
| 469 |
Allocator allocator; |
| 470 |
|
| 471 |
D a(&allocator); |
| 472 |
a.Parse("[\"one\", \"two\", \"three\"]"); |
| 473 |
EXPECT_FALSE(a.HasParseError()); |
| 474 |
EXPECT_TRUE(a.IsArray()); |
| 475 |
EXPECT_EQ(3u, a.Size()); |
| 476 |
EXPECT_EQ(&a.GetAllocator(), &allocator); |
| 477 |
|
| 478 |
// Document b(a); // does not compile (!is_copy_constructible) |
| 479 |
D b(std::move(a)); |
| 480 |
EXPECT_TRUE(a.IsNull()); |
| 481 |
EXPECT_TRUE(b.IsArray()); |
| 482 |
EXPECT_EQ(3u, b.Size()); |
| 483 |
EXPECT_THROW(a.GetAllocator(), AssertException); |
| 484 |
EXPECT_EQ(&b.GetAllocator(), &allocator); |
| 485 |
|
| 486 |
b.Parse("{\"Foo\": \"Bar\", \"Baz\": 42}"); |
| 487 |
EXPECT_FALSE(b.HasParseError()); |
| 488 |
EXPECT_TRUE(b.IsObject()); |
| 489 |
EXPECT_EQ(2u, b.MemberCount()); |
| 490 |
|
| 491 |
// Document c = a; // does not compile (!is_copy_constructible) |
| 492 |
D c = std::move(b); |
| 493 |
EXPECT_TRUE(b.IsNull()); |
| 494 |
EXPECT_TRUE(c.IsObject()); |
| 495 |
EXPECT_EQ(2u, c.MemberCount()); |
| 496 |
EXPECT_THROW(b.GetAllocator(), AssertException); |
| 497 |
EXPECT_EQ(&c.GetAllocator(), &allocator); |
| 498 |
} |
| 499 |
|
| 500 |
TYPED_TEST(DocumentMove, MoveConstructorParseError) { |
| 501 |
typedef TypeParam Allocator; |
| 502 |
typedef GenericDocument<UTF8<>, Allocator> D; |
| 503 |
|
| 504 |
ParseResult noError; |
| 505 |
D a; |
| 506 |
a.Parse("{ 4 = 4]"); |
| 507 |
ParseResult error(a.GetParseError(), a.GetErrorOffset()); |
| 508 |
EXPECT_TRUE(a.HasParseError()); |
| 509 |
EXPECT_NE(error, noError); |
| 510 |
EXPECT_NE(error.Code(), noError); |
| 511 |
EXPECT_NE(error.Code(), noError.Code()); |
| 512 |
EXPECT_NE(error.Offset(), noError.Offset()); |
| 513 |
|
| 514 |
D b(std::move(a)); |
| 515 |
EXPECT_FALSE(a.HasParseError()); |
| 516 |
EXPECT_TRUE(b.HasParseError()); |
| 517 |
EXPECT_EQ(a.GetParseError(), noError); |
| 518 |
EXPECT_EQ(a.GetParseError(), noError.Code()); |
| 519 |
EXPECT_EQ(a.GetErrorOffset(), noError.Offset()); |
| 520 |
EXPECT_EQ(b.GetParseError(), error); |
| 521 |
EXPECT_EQ(b.GetParseError(), error.Code()); |
| 522 |
EXPECT_EQ(b.GetErrorOffset(), error.Offset()); |
| 523 |
|
| 524 |
D c(std::move(b)); |
| 525 |
EXPECT_FALSE(b.HasParseError()); |
| 526 |
EXPECT_TRUE(c.HasParseError()); |
| 527 |
EXPECT_EQ(b.GetParseError(), noError.Code()); |
| 528 |
EXPECT_EQ(c.GetParseError(), error.Code()); |
| 529 |
EXPECT_EQ(b.GetErrorOffset(), noError.Offset()); |
| 530 |
EXPECT_EQ(c.GetErrorOffset(), error.Offset()); |
| 531 |
} |
| 532 |
|
| 533 |
// This test does not properly use parsing, just for testing. |
| 534 |
// It must call ClearStack() explicitly to prevent memory leak. |
| 535 |
// But here we cannot as ClearStack() is private. |
| 536 |
#if 0 |
| 537 |
TYPED_TEST(DocumentMove, MoveConstructorStack) { |
| 538 |
typedef TypeParam Allocator; |
| 539 |
typedef UTF8<> Encoding; |
| 540 |
typedef GenericDocument<Encoding, Allocator> Document; |
| 541 |
|
| 542 |
Document a; |
| 543 |
size_t defaultCapacity = a.GetStackCapacity(); |
| 544 |
|
| 545 |
// Trick Document into getting GetStackCapacity() to return non-zero |
| 546 |
typedef GenericReader<Encoding, Encoding, Allocator> Reader; |
| 547 |
Reader reader(&a.GetAllocator()); |
| 548 |
GenericStringStream<Encoding> is("[\"one\", \"two\", \"three\"]"); |
| 549 |
reader.template Parse<kParseDefaultFlags>(is, a); |
| 550 |
size_t capacity = a.GetStackCapacity(); |
| 551 |
EXPECT_GT(capacity, 0u); |
| 552 |
|
| 553 |
Document b(std::move(a)); |
| 554 |
EXPECT_EQ(a.GetStackCapacity(), defaultCapacity); |
| 555 |
EXPECT_EQ(b.GetStackCapacity(), capacity); |
| 556 |
|
| 557 |
Document c = std::move(b); |
| 558 |
EXPECT_EQ(b.GetStackCapacity(), defaultCapacity); |
| 559 |
EXPECT_EQ(c.GetStackCapacity(), capacity); |
| 560 |
} |
| 561 |
#endif |
| 562 |
|
| 563 |
TYPED_TEST(DocumentMove, MoveAssignment) { |
| 564 |
typedef TypeParam Allocator; |
| 565 |
typedef GenericDocument<UTF8<>, Allocator> D; |
| 566 |
Allocator allocator; |
| 567 |
|
| 568 |
D a(&allocator); |
| 569 |
a.Parse("[\"one\", \"two\", \"three\"]"); |
| 570 |
EXPECT_FALSE(a.HasParseError()); |
| 571 |
EXPECT_TRUE(a.IsArray()); |
| 572 |
EXPECT_EQ(3u, a.Size()); |
| 573 |
EXPECT_EQ(&a.GetAllocator(), &allocator); |
| 574 |
|
| 575 |
// Document b; b = a; // does not compile (!is_copy_assignable) |
| 576 |
D b; |
| 577 |
b = std::move(a); |
| 578 |
EXPECT_TRUE(a.IsNull()); |
| 579 |
EXPECT_TRUE(b.IsArray()); |
| 580 |
EXPECT_EQ(3u, b.Size()); |
| 581 |
EXPECT_THROW(a.GetAllocator(), AssertException); |
| 582 |
EXPECT_EQ(&b.GetAllocator(), &allocator); |
| 583 |
|
| 584 |
b.Parse("{\"Foo\": \"Bar\", \"Baz\": 42}"); |
| 585 |
EXPECT_FALSE(b.HasParseError()); |
| 586 |
EXPECT_TRUE(b.IsObject()); |
| 587 |
EXPECT_EQ(2u, b.MemberCount()); |
| 588 |
|
| 589 |
// Document c; c = a; // does not compile (see static_assert) |
| 590 |
D c; |
| 591 |
c = std::move(b); |
| 592 |
EXPECT_TRUE(b.IsNull()); |
| 593 |
EXPECT_TRUE(c.IsObject()); |
| 594 |
EXPECT_EQ(2u, c.MemberCount()); |
| 595 |
EXPECT_THROW(b.GetAllocator(), AssertException); |
| 596 |
EXPECT_EQ(&c.GetAllocator(), &allocator); |
| 597 |
} |
| 598 |
|
| 599 |
TYPED_TEST(DocumentMove, MoveAssignmentParseError) { |
| 600 |
typedef TypeParam Allocator; |
| 601 |
typedef GenericDocument<UTF8<>, Allocator> D; |
| 602 |
|
| 603 |
ParseResult noError; |
| 604 |
D a; |
| 605 |
a.Parse("{ 4 = 4]"); |
| 606 |
ParseResult error(a.GetParseError(), a.GetErrorOffset()); |
| 607 |
EXPECT_TRUE(a.HasParseError()); |
| 608 |
EXPECT_NE(error.Code(), noError.Code()); |
| 609 |
EXPECT_NE(error.Offset(), noError.Offset()); |
| 610 |
|
| 611 |
D b; |
| 612 |
b = std::move(a); |
| 613 |
EXPECT_FALSE(a.HasParseError()); |
| 614 |
EXPECT_TRUE(b.HasParseError()); |
| 615 |
EXPECT_EQ(a.GetParseError(), noError.Code()); |
| 616 |
EXPECT_EQ(b.GetParseError(), error.Code()); |
| 617 |
EXPECT_EQ(a.GetErrorOffset(), noError.Offset()); |
| 618 |
EXPECT_EQ(b.GetErrorOffset(), error.Offset()); |
| 619 |
|
| 620 |
D c; |
| 621 |
c = std::move(b); |
| 622 |
EXPECT_FALSE(b.HasParseError()); |
| 623 |
EXPECT_TRUE(c.HasParseError()); |
| 624 |
EXPECT_EQ(b.GetParseError(), noError.Code()); |
| 625 |
EXPECT_EQ(c.GetParseError(), error.Code()); |
| 626 |
EXPECT_EQ(b.GetErrorOffset(), noError.Offset()); |
| 627 |
EXPECT_EQ(c.GetErrorOffset(), error.Offset()); |
| 628 |
} |
| 629 |
|
| 630 |
// This test does not properly use parsing, just for testing. |
| 631 |
// It must call ClearStack() explicitly to prevent memory leak. |
| 632 |
// But here we cannot as ClearStack() is private. |
| 633 |
#if 0 |
| 634 |
TYPED_TEST(DocumentMove, MoveAssignmentStack) { |
| 635 |
typedef TypeParam Allocator; |
| 636 |
typedef UTF8<> Encoding; |
| 637 |
typedef GenericDocument<Encoding, Allocator> D; |
| 638 |
|
| 639 |
D a; |
| 640 |
size_t defaultCapacity = a.GetStackCapacity(); |
| 641 |
|
| 642 |
// Trick Document into getting GetStackCapacity() to return non-zero |
| 643 |
typedef GenericReader<Encoding, Encoding, Allocator> Reader; |
| 644 |
Reader reader(&a.GetAllocator()); |
| 645 |
GenericStringStream<Encoding> is("[\"one\", \"two\", \"three\"]"); |
| 646 |
reader.template Parse<kParseDefaultFlags>(is, a); |
| 647 |
size_t capacity = a.GetStackCapacity(); |
| 648 |
EXPECT_GT(capacity, 0u); |
| 649 |
|
| 650 |
D b; |
| 651 |
b = std::move(a); |
| 652 |
EXPECT_EQ(a.GetStackCapacity(), defaultCapacity); |
| 653 |
EXPECT_EQ(b.GetStackCapacity(), capacity); |
| 654 |
|
| 655 |
D c; |
| 656 |
c = std::move(b); |
| 657 |
EXPECT_EQ(b.GetStackCapacity(), defaultCapacity); |
| 658 |
EXPECT_EQ(c.GetStackCapacity(), capacity); |
| 659 |
} |
| 660 |
#endif |
| 661 |
|
| 662 |
#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS |
| 663 |
|
| 664 |
// Issue 22: Memory corruption via operator= |
| 665 |
// Fixed by making unimplemented assignment operator private. |
| 666 |
//TEST(Document, Assignment) { |
| 667 |
// Document d1; |
| 668 |
// Document d2; |
| 669 |
// d1 = d2; |
| 670 |
//} |
| 671 |
|
| 672 |
#ifdef __clang__ |
| 673 |
RAPIDJSON_DIAG_POP |
| 674 |
#endif |