documenttest.cpp 16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Copyright (C) 2011 Milo Yip
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

21 22 23
#include "unittest.h"
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
24 25 26
#include "rapidjson/filereadstream.h"
#include "rapidjson/encodedstream.h"
#include "rapidjson/stringbuffer.h"
27 28 29 30
#include <sstream>

using namespace rapidjson;

31 32 33
template <typename Allocator, typename StackAllocator>
void ParseTest() {
    typedef GenericDocument<UTF8<>, Allocator, StackAllocator> DocumentType;
Milo Yip's avatar
Milo Yip committed
34
    typedef typename DocumentType::ValueType ValueType;
35
    DocumentType doc;
36

37
    doc.Parse(" { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } ");
38

39
    EXPECT_TRUE(doc.IsObject());
40

41
    EXPECT_TRUE(doc.HasMember("hello"));
42
    const ValueType& hello = doc["hello"];
43 44
    EXPECT_TRUE(hello.IsString());
    EXPECT_STREQ("world", hello.GetString());
45

46
    EXPECT_TRUE(doc.HasMember("t"));
47
    const ValueType& t = doc["t"];
48
    EXPECT_TRUE(t.IsTrue());
49

50
    EXPECT_TRUE(doc.HasMember("f"));
51
    const ValueType& f = doc["f"];
52
    EXPECT_TRUE(f.IsFalse());
53

54
    EXPECT_TRUE(doc.HasMember("n"));
55
    const ValueType& n = doc["n"];
56
    EXPECT_TRUE(n.IsNull());
57

58
    EXPECT_TRUE(doc.HasMember("i"));
59
    const ValueType& i = doc["i"];
60 61
    EXPECT_TRUE(i.IsNumber());
    EXPECT_EQ(123, i.GetInt());
62

63
    EXPECT_TRUE(doc.HasMember("pi"));
64
    const ValueType& pi = doc["pi"];
65 66
    EXPECT_TRUE(pi.IsNumber());
    EXPECT_EQ(3.1416, pi.GetDouble());
67

68
    EXPECT_TRUE(doc.HasMember("a"));
69
    const ValueType& a = doc["a"];
70 71 72 73
    EXPECT_TRUE(a.IsArray());
    EXPECT_EQ(4u, a.Size());
    for (SizeType i = 0; i < 4; i++)
        EXPECT_EQ(i + 1, a[i].GetUint());
74 75
}

76 77 78 79 80 81 82
TEST(Document, Parse) {
    ParseTest<MemoryPoolAllocator<>, CrtAllocator>();
    ParseTest<MemoryPoolAllocator<>, MemoryPoolAllocator<> >();
    ParseTest<CrtAllocator, MemoryPoolAllocator<> >();
    ParseTest<CrtAllocator, CrtAllocator>();
}

83
static FILE* OpenEncodedFile(const char* filename) {
84 85 86 87 88 89 90 91
    char buffer[1024];
    sprintf(buffer, "encodings/%s", filename);
    FILE *fp = fopen(buffer, "rb");
    if (!fp) {
        sprintf(buffer, "../../bin/encodings/%s", filename);
        fp = fopen(buffer, "rb");
    }
    return fp;
92 93 94
}

TEST(Document, ParseStream_EncodedInputStream) {
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    // UTF8 -> UTF16
    FILE* fp = OpenEncodedFile("utf8.json");
    char buffer[256];
    FileReadStream bis(fp, buffer, sizeof(buffer));
    EncodedInputStream<UTF8<>, FileReadStream> eis(bis);

    GenericDocument<UTF16<> > d;
    d.ParseStream<0, UTF8<> >(eis);
    EXPECT_FALSE(d.HasParseError());

    fclose(fp);

    wchar_t expected[] = L"I can eat glass and it doesn't hurt me.";
    GenericValue<UTF16<> >& v = d[L"en"];
    EXPECT_TRUE(v.IsString());
    EXPECT_EQ(sizeof(expected) / sizeof(wchar_t) - 1, v.GetStringLength());
    EXPECT_EQ(0, StrCmp(expected, v.GetString()));

    // UTF16 -> UTF8 in memory
    StringBuffer bos;
    typedef EncodedOutputStream<UTF8<>, StringBuffer> OutputStream;
    OutputStream eos(bos, false);   // Not writing BOM
    Writer<OutputStream, UTF16<>, UTF8<> > writer(eos);
    d.Accept(writer);

    {
        // Condense the original file and compare.
        FILE *fp = OpenEncodedFile("utf8.json");
        FileReadStream is(fp, buffer, sizeof(buffer));
        Reader reader;
        StringBuffer bos2;
        Writer<StringBuffer> writer(bos2);
        reader.Parse(is, writer);

        EXPECT_EQ(bos.GetSize(), bos2.GetSize());
        EXPECT_EQ(0, memcmp(bos.GetString(), bos2.GetString(), bos2.GetSize()));
    }
132 133 134
}

TEST(Document, ParseStream_AutoUTFInputStream) {
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    // Any -> UTF8
    FILE* fp = OpenEncodedFile("utf32be.json");
    char buffer[256];
    FileReadStream bis(fp, buffer, sizeof(buffer));
    AutoUTFInputStream<unsigned, FileReadStream> eis(bis);

    Document d;
    d.ParseStream<0, AutoUTF<unsigned> >(eis);
    EXPECT_FALSE(d.HasParseError());

    fclose(fp);

    char expected[] = "I can eat glass and it doesn't hurt me.";
    Value& v = d["en"];
    EXPECT_TRUE(v.IsString());
    EXPECT_EQ(sizeof(expected) - 1, v.GetStringLength());
    EXPECT_EQ(0, StrCmp(expected, v.GetString()));

    // UTF8 -> UTF8 in memory
    StringBuffer bos;
    Writer<StringBuffer> writer(bos);
    d.Accept(writer);

    {
        // Condense the original file and compare.
        FILE *fp = OpenEncodedFile("utf8.json");
        FileReadStream is(fp, buffer, sizeof(buffer));
        Reader reader;
        StringBuffer bos2;
        Writer<StringBuffer> writer(bos2);
        reader.Parse(is, writer);

        EXPECT_EQ(bos.GetSize(), bos2.GetSize());
        EXPECT_EQ(0, memcmp(bos.GetString(), bos2.GetString(), bos2.GetSize()));
    }
170 171
}

172
TEST(Document, Swap) {
173 174
    Document d1;
    Document::AllocatorType& a = d1.GetAllocator();
175

176
    d1.SetArray().PushBack(1, a).PushBack(2, a);
177

178 179
    Value o;
    o.SetObject().AddMember("a", 1, a);
180

181 182 183 184
    // Swap between Document and Value
    d1.Swap(o);
    EXPECT_TRUE(d1.IsObject());
    EXPECT_TRUE(o.IsArray());
185

186 187 188 189 190 191
    // Swap between Document and Document
    Document d2;
    d2.SetArray().PushBack(3, a);
    d1.Swap(d2);
    EXPECT_TRUE(d1.IsArray());
    EXPECT_TRUE(d2.IsObject());
192 193
}

194
// This should be slow due to assignment in inner-loop.
195
struct OutputStringStream : public std::ostringstream {
196
    typedef char Ch;
197

198 199 200 201
    void Put(char c) {
        put(c);
    }
    void Flush() {}
202 203 204
};

TEST(Document, AcceptWriter) {
205 206
    Document doc;
    doc.Parse(" { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } ");
207

208 209 210
    OutputStringStream os;
    Writer<OutputStringStream> writer(os);
    doc.Accept(writer);
211

212
    EXPECT_EQ("{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3,4]}", os.str());
213
}
214

215 216 217 218 219 220 221 222 223 224 225 226
// Issue 226: Value of string type should not point to NULL
TEST(Document, AssertAcceptInvalidNameType) {
    Document doc;
    doc.SetObject();
    doc.AddMember("a", 0, doc.GetAllocator());
    doc.FindMember("a")->name.SetNull(); // Change name to non-string type.

    OutputStringStream os;
    Writer<OutputStringStream> writer(os);
    ASSERT_THROW(doc.Accept(writer), AssertException);
}

227
// Issue 44:    SetStringRaw doesn't work with wchar_t
228
TEST(Document, UTF16_Document) {
229 230
    GenericDocument< UTF16<> > json;
    json.Parse<kParseValidateEncodingFlag>(L"[{\"created_at\":\"Wed Oct 30 17:13:20 +0000 2012\"}]");
231

232
    ASSERT_TRUE(json.IsArray());
233
    GenericValue< UTF16<> >& v = json[0];
234
    ASSERT_TRUE(v.IsObject());
235

236 237
    GenericValue< UTF16<> >& s = v[L"created_at"];
    ASSERT_TRUE(s.IsString());
238

239
    EXPECT_EQ(0, wcscmp(L"Wed Oct 30 17:13:20 +0000 2012", s.GetString()));
240
}
241

242 243
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS

244 245 246
#include <type_traits>

TEST(Document, Traits) {
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
    static_assert(std::is_constructible<Document>::value, "");
    static_assert(std::is_default_constructible<Document>::value, "");
#ifndef _MSC_VER
    static_assert(!std::is_copy_constructible<Document>::value, "");
#endif
    static_assert(std::is_move_constructible<Document>::value, "");

    static_assert(!std::is_nothrow_constructible<Document>::value, "");
    static_assert(!std::is_nothrow_default_constructible<Document>::value, "");
    static_assert(!std::is_nothrow_copy_constructible<Document>::value, "");
#ifndef _MSC_VER
    static_assert(std::is_nothrow_move_constructible<Document>::value, "");
#endif

    static_assert(std::is_assignable<Document,Document>::value, "");
#ifndef _MSC_VER
263
  static_assert(!std::is_copy_assignable<Document>::value, "");
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
#endif
    static_assert(std::is_move_assignable<Document>::value, "");

#ifndef _MSC_VER
    static_assert(std::is_nothrow_assignable<Document, Document>::value, "");
#endif
    static_assert(!std::is_nothrow_copy_assignable<Document>::value, "");
#ifndef _MSC_VER
    static_assert(std::is_nothrow_move_assignable<Document>::value, "");
#endif

    static_assert( std::is_destructible<Document>::value, "");
#ifndef _MSC_VER
    static_assert(std::is_nothrow_destructible<Document>::value, "");
#endif
279 280
}

281 282 283 284 285 286 287 288 289 290 291
template <typename Allocator>
struct DocumentMove: public ::testing::Test {
};

typedef ::testing::Types< CrtAllocator, MemoryPoolAllocator<> > MoveAllocatorTypes;
TYPED_TEST_CASE(DocumentMove, MoveAllocatorTypes);

TYPED_TEST(DocumentMove, MoveConstructor) {
    typedef TypeParam Allocator;
    typedef GenericDocument<UTF8<>, Allocator> Document;
    Allocator allocator;
292 293 294 295 296 297 298 299

    Document a(&allocator);
    a.Parse("[\"one\", \"two\", \"three\"]");
    EXPECT_FALSE(a.HasParseError());
    EXPECT_TRUE(a.IsArray());
    EXPECT_EQ(3u, a.Size());
    EXPECT_EQ(&a.GetAllocator(), &allocator);

300
    // Document b(a); // does not compile (!is_copy_constructible)
301 302 303 304 305 306 307 308 309 310 311 312
    Document b(std::move(a));
    EXPECT_TRUE(a.IsNull());
    EXPECT_TRUE(b.IsArray());
    EXPECT_EQ(3u, b.Size());
    EXPECT_EQ(&a.GetAllocator(), (void*)0);
    EXPECT_EQ(&b.GetAllocator(), &allocator);

    b.Parse("{\"Foo\": \"Bar\", \"Baz\": 42}");
    EXPECT_FALSE(b.HasParseError());
    EXPECT_TRUE(b.IsObject());
    EXPECT_EQ(2u, b.MemberCount());

313
    // Document c = a; // does not compile (!is_copy_constructible)
314 315 316 317 318 319 320 321
    Document c = std::move(b);
    EXPECT_TRUE(b.IsNull());
    EXPECT_TRUE(c.IsObject());
    EXPECT_EQ(2u, c.MemberCount());
    EXPECT_EQ(&b.GetAllocator(), (void*)0);
    EXPECT_EQ(&c.GetAllocator(), &allocator);
}

322 323 324
TYPED_TEST(DocumentMove, MoveConstructorParseError) {
    typedef TypeParam Allocator;
    typedef GenericDocument<UTF8<>, Allocator> Document;
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350

    ParseResult noError;
    Document a;
    a.Parse("{ 4 = 4]");
    ParseResult error(a.GetParseError(), a.GetErrorOffset());
    EXPECT_TRUE(a.HasParseError());
    EXPECT_NE(error.Code(), noError.Code());
    EXPECT_NE(error.Offset(), noError.Offset());

    Document b(std::move(a));
    EXPECT_FALSE(a.HasParseError());
    EXPECT_TRUE(b.HasParseError());
    EXPECT_EQ(a.GetParseError(), noError.Code());
    EXPECT_EQ(b.GetParseError(), error.Code());
    EXPECT_EQ(a.GetErrorOffset(), noError.Offset());
    EXPECT_EQ(b.GetErrorOffset(), error.Offset());

    Document c(std::move(b));
    EXPECT_FALSE(b.HasParseError());
    EXPECT_TRUE(c.HasParseError());
    EXPECT_EQ(b.GetParseError(), noError.Code());
    EXPECT_EQ(c.GetParseError(), error.Code());
    EXPECT_EQ(b.GetErrorOffset(), noError.Offset());
    EXPECT_EQ(c.GetErrorOffset(), error.Offset());
}

351 352 353 354
// This test does not properly use parsing, just for testing.
// It must call ClearStack() explicitly to prevent memory leak.
// But here we cannot as ClearStack() is private.
#if 0
355 356
TYPED_TEST(DocumentMove, MoveConstructorStack) {
    typedef TypeParam Allocator;
357
    typedef UTF8<> Encoding;
358
    typedef GenericDocument<Encoding, Allocator> Document;
359 360 361 362 363

    Document a;
    size_t defaultCapacity = a.GetStackCapacity();

    // Trick Document into getting GetStackCapacity() to return non-zero
364
    typedef GenericReader<Encoding, Encoding, Allocator> Reader;
365 366
    Reader reader(&a.GetAllocator());
    GenericStringStream<Encoding> is("[\"one\", \"two\", \"three\"]");
367
    reader.template Parse<kParseDefaultFlags>(is, a);
368
    size_t capacity = a.GetStackCapacity();
369
    EXPECT_GT(capacity, 0u);
370 371 372 373 374 375 376 377 378

    Document b(std::move(a));
    EXPECT_EQ(a.GetStackCapacity(), defaultCapacity);
    EXPECT_EQ(b.GetStackCapacity(), capacity);

    Document c = std::move(b);
    EXPECT_EQ(b.GetStackCapacity(), defaultCapacity);
    EXPECT_EQ(c.GetStackCapacity(), capacity);
}
379
#endif
380

381 382 383 384
TYPED_TEST(DocumentMove, MoveAssignment) {
    typedef TypeParam Allocator;
    typedef GenericDocument<UTF8<>, Allocator> Document;
    Allocator allocator;
385 386 387 388 389 390 391 392

    Document a(&allocator);
    a.Parse("[\"one\", \"two\", \"three\"]");
    EXPECT_FALSE(a.HasParseError());
    EXPECT_TRUE(a.IsArray());
    EXPECT_EQ(3u, a.Size());
    EXPECT_EQ(&a.GetAllocator(), &allocator);

393
    // Document b; b = a; // does not compile (!is_copy_assignable)
394 395 396 397 398 399 400 401 402 403 404 405 406
    Document b;
    b = std::move(a);
    EXPECT_TRUE(a.IsNull());
    EXPECT_TRUE(b.IsArray());
    EXPECT_EQ(3u, b.Size());
    EXPECT_EQ(&a.GetAllocator(), (void*)0);
    EXPECT_EQ(&b.GetAllocator(), &allocator);

    b.Parse("{\"Foo\": \"Bar\", \"Baz\": 42}");
    EXPECT_FALSE(b.HasParseError());
    EXPECT_TRUE(b.IsObject());
    EXPECT_EQ(2u, b.MemberCount());

407
    // Document c; c = a; // does not compile (see static_assert)
408 409 410 411 412 413 414 415 416
    Document c;
    c = std::move(b);
    EXPECT_TRUE(b.IsNull());
    EXPECT_TRUE(c.IsObject());
    EXPECT_EQ(2u, c.MemberCount());
    EXPECT_EQ(&b.GetAllocator(), (void*)0);
    EXPECT_EQ(&c.GetAllocator(), &allocator);
}

417 418 419
TYPED_TEST(DocumentMove, MoveAssignmentParseError) {
    typedef TypeParam Allocator;
    typedef GenericDocument<UTF8<>, Allocator> Document;
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447

    ParseResult noError;
    Document a;
    a.Parse("{ 4 = 4]");
    ParseResult error(a.GetParseError(), a.GetErrorOffset());
    EXPECT_TRUE(a.HasParseError());
    EXPECT_NE(error.Code(), noError.Code());
    EXPECT_NE(error.Offset(), noError.Offset());

    Document b;
    b = std::move(a);
    EXPECT_FALSE(a.HasParseError());
    EXPECT_TRUE(b.HasParseError());
    EXPECT_EQ(a.GetParseError(), noError.Code());
    EXPECT_EQ(b.GetParseError(), error.Code());
    EXPECT_EQ(a.GetErrorOffset(), noError.Offset());
    EXPECT_EQ(b.GetErrorOffset(), error.Offset());

    Document c;
    c = std::move(b);
    EXPECT_FALSE(b.HasParseError());
    EXPECT_TRUE(c.HasParseError());
    EXPECT_EQ(b.GetParseError(), noError.Code());
    EXPECT_EQ(c.GetParseError(), error.Code());
    EXPECT_EQ(b.GetErrorOffset(), noError.Offset());
    EXPECT_EQ(c.GetErrorOffset(), error.Offset());
}

448 449 450 451
// This test does not properly use parsing, just for testing.
// It must call ClearStack() explicitly to prevent memory leak.
// But here we cannot as ClearStack() is private.
#if 0
452 453
TYPED_TEST(DocumentMove, MoveAssignmentStack) {
    typedef TypeParam Allocator;
454
    typedef UTF8<> Encoding;
455
    typedef GenericDocument<Encoding, Allocator> Document;
456 457 458 459 460

    Document a;
    size_t defaultCapacity = a.GetStackCapacity();

    // Trick Document into getting GetStackCapacity() to return non-zero
461
    typedef GenericReader<Encoding, Encoding, Allocator> Reader;
462 463
    Reader reader(&a.GetAllocator());
    GenericStringStream<Encoding> is("[\"one\", \"two\", \"three\"]");
464
    reader.template Parse<kParseDefaultFlags>(is, a);
465
    size_t capacity = a.GetStackCapacity();
466
    EXPECT_GT(capacity, 0u);
467 468 469 470 471 472 473 474 475 476 477

    Document b;
    b = std::move(a);
    EXPECT_EQ(a.GetStackCapacity(), defaultCapacity);
    EXPECT_EQ(b.GetStackCapacity(), capacity);

    Document c;
    c = std::move(b);
    EXPECT_EQ(b.GetStackCapacity(), defaultCapacity);
    EXPECT_EQ(c.GetStackCapacity(), capacity);
}
478
#endif
479 480 481

#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS

482 483 484
// Issue 22: Memory corruption via operator=
// Fixed by making unimplemented assignment operator private.
//TEST(Document, Assignment) {
485 486 487
//  Document d1;
//  Document d2;
//  d1 = d2;
488
//}