Bump rapidjson version (#6906)

This commit is contained in:
Siarhei Fedartsou
2024-05-27 08:31:59 +02:00
committed by GitHub
parent 667fd198ac
commit 163a2cfe3c
151 changed files with 14287 additions and 2363 deletions
+2
View File
@@ -19,6 +19,8 @@ if(CCACHE_FOUND)
endif()
endif(CCACHE_FOUND)
set_property(DIRECTORY PROPERTY COMPILE_OPTIONS ${EXTRA_CXX_FLAGS})
IF(NOT (CMAKE_BUILD_TYPE STREQUAL "Debug"))
add_test(NAME perftest
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/perftest
+3 -3
View File
@@ -1,6 +1,6 @@
// 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.
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
//
// 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
@@ -432,7 +432,7 @@ bool Writer1<rapidjson::StringBuffer>::WriteUint(unsigned u) {
return true;
}
// Using digits LUT to reduce divsion/modulo
// Using digits LUT to reduce division/modulo
template<typename OutputStream>
class Writer2 {
public:
@@ -616,7 +616,7 @@ inline bool Writer3<rapidjson::InsituStringStream>::WriteUint64(uint64_t u) {
return true;
}
// Using digits LUT to reduce divsion/modulo, two passes
// Using digits LUT to reduce division/modulo, two passes
template<typename OutputStream>
class Writer4 {
public:
+1 -1
View File
@@ -1,6 +1,6 @@
// 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.
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
//
// 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
+9 -5
View File
@@ -1,6 +1,6 @@
// 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.
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
//
// 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
@@ -24,10 +24,13 @@
// __SSE2__ and __SSE4_2__ are recognized by gcc, clang, and the Intel compiler.
// We use -march=native with gmake to enable -msse2 and -msse4.2, if supported.
// Likewise, __ARM_NEON is used to detect Neon.
#if defined(__SSE4_2__)
# define RAPIDJSON_SSE42
#elif defined(__SSE2__)
# define RAPIDJSON_SSE2
#elif defined(__ARM_NEON)
# define RAPIDJSON_NEON
#endif
#define RAPIDJSON_HAS_STDSTRING 1
@@ -127,7 +130,8 @@ public:
"integers.json",
"mixed.json",
"nulls.json",
"paragraphs.json"
"paragraphs.json",
"alotofkeys.json"
};
for (size_t j = 0; j < sizeof(typesfilenames) / sizeof(typesfilenames[0]); j++) {
@@ -155,7 +159,7 @@ public:
free(whitespace_);
json_ = 0;
whitespace_ = 0;
for (size_t i = 0; i < 7; i++) {
for (size_t i = 0; i < 8; i++) {
free(types_[i]);
types_[i] = 0;
}
@@ -171,8 +175,8 @@ protected:
size_t length_;
char *whitespace_;
size_t whitespace_length_;
char *types_[7];
size_t typesLength_[7];
char *types_[8];
size_t typesLength_[8];
static const size_t kTrialCount = 1000;
};
+2 -2
View File
@@ -1,6 +1,6 @@
// 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.
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
//
// 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
@@ -76,7 +76,7 @@ TEST_F(Platform, strlen) {
TEST_F(Platform, memcmp) {
for (int i = 0; i < kTrialCount; i++) {
EXPECT_EQ(0, memcmp(temp_, json_, length_));
EXPECT_EQ(0u, memcmp(temp_, json_, length_));
}
}
+126 -3
View File
@@ -1,6 +1,6 @@
// 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.
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
//
// 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
@@ -21,13 +21,19 @@
#include "rapidjson/prettywriter.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/filereadstream.h"
#include "rapidjson/istreamwrapper.h"
#include "rapidjson/encodedstream.h"
#include "rapidjson/memorystream.h"
#include <fstream>
#include <vector>
#ifdef RAPIDJSON_SSE2
#define SIMD_SUFFIX(name) name##_SSE2
#elif defined(RAPIDJSON_SSE42)
#define SIMD_SUFFIX(name) name##_SSE42
#elif defined(RAPIDJSON_NEON)
#define SIMD_SUFFIX(name) name##_NEON
#else
#define SIMD_SUFFIX(name) name
#endif
@@ -47,7 +53,7 @@ public:
// Parse as a document
EXPECT_FALSE(doc_.Parse(json_).HasParseError());
for (size_t i = 0; i < 7; i++)
for (size_t i = 0; i < 8; i++)
EXPECT_FALSE(typesDoc_[i].Parse(types_[i]).HasParseError());
}
@@ -63,7 +69,7 @@ private:
protected:
char *temp_;
Document doc_;
Document typesDoc_[7];
Document typesDoc_[8];
};
TEST_F(RapidJson, SIMD_SUFFIX(ReaderParseInsitu_DummyHandler)) {
@@ -152,6 +158,35 @@ TEST_F(RapidJson, SIMD_SUFFIX(ReaderParseIterativeInsitu_DummyHandler)) {
}
}
TEST_F(RapidJson, SIMD_SUFFIX(ReaderParseIterativePull_DummyHandler)) {
for (size_t i = 0; i < kTrialCount; i++) {
StringStream s(json_);
BaseReaderHandler<> h;
Reader reader;
reader.IterativeParseInit();
while (!reader.IterativeParseComplete()) {
if (!reader.IterativeParseNext<kParseDefaultFlags>(s, h))
break;
}
EXPECT_FALSE(reader.HasParseError());
}
}
TEST_F(RapidJson, SIMD_SUFFIX(ReaderParseIterativePullInsitu_DummyHandler)) {
for (size_t i = 0; i < kTrialCount; i++) {
memcpy(temp_, json_, length_ + 1);
InsituStringStream s(temp_);
BaseReaderHandler<> h;
Reader reader;
reader.IterativeParseInit();
while (!reader.IterativeParseComplete()) {
if (!reader.IterativeParseNext<kParseDefaultFlags|kParseInsituFlag>(s, h))
break;
}
EXPECT_FALSE(reader.HasParseError());
}
}
TEST_F(RapidJson, SIMD_SUFFIX(ReaderParse_DummyHandler_ValidateEncoding)) {
for (size_t i = 0; i < kTrialCount; i++) {
StringStream s(json_);
@@ -301,6 +336,23 @@ TEST_F(RapidJson, DocumentAccept) {
}
}
TEST_F(RapidJson, DocumentFind) {
typedef Document::ValueType ValueType;
typedef ValueType::ConstMemberIterator ConstMemberIterator;
const Document &doc = typesDoc_[7]; // alotofkeys.json
if (doc.IsObject()) {
std::vector<const ValueType*> keys;
for (ConstMemberIterator it = doc.MemberBegin(); it != doc.MemberEnd(); ++it) {
keys.push_back(&it->name);
}
for (size_t i = 0; i < kTrialCount; i++) {
for (size_t j = 0; j < keys.size(); j++) {
EXPECT_TRUE(doc.FindMember(*keys[j]) != doc.MemberEnd());
}
}
}
}
struct NullStream {
typedef char Ch;
@@ -432,6 +484,77 @@ TEST_F(RapidJson, SIMD_SUFFIX(ReaderParse_DummyHandler_FileReadStream)) {
}
}
TEST_F(RapidJson, IStreamWrapper) {
for (size_t i = 0; i < kTrialCount; i++) {
std::ifstream is(filename_, std::ios::in | std::ios::binary);
char buffer[65536];
IStreamWrapper isw(is, buffer, sizeof(buffer));
while (isw.Take() != '\0')
;
is.close();
}
}
TEST_F(RapidJson, IStreamWrapper_Unbuffered) {
for (size_t i = 0; i < kTrialCount; i++) {
std::ifstream is(filename_, std::ios::in | std::ios::binary);
IStreamWrapper isw(is);
while (isw.Take() != '\0')
;
is.close();
}
}
TEST_F(RapidJson, IStreamWrapper_Setbuffered) {
for (size_t i = 0; i < kTrialCount; i++) {
std::ifstream is;
char buffer[65536];
is.rdbuf()->pubsetbuf(buffer, sizeof(buffer));
is.open(filename_, std::ios::in | std::ios::binary);
IStreamWrapper isw(is);
while (isw.Take() != '\0')
;
is.close();
}
}
TEST_F(RapidJson, SIMD_SUFFIX(ReaderParse_DummyHandler_IStreamWrapper)) {
for (size_t i = 0; i < kTrialCount; i++) {
std::ifstream is(filename_, std::ios::in | std::ios::binary);
char buffer[65536];
IStreamWrapper isw(is, buffer, sizeof(buffer));
BaseReaderHandler<> h;
Reader reader;
reader.Parse(isw, h);
is.close();
}
}
TEST_F(RapidJson, SIMD_SUFFIX(ReaderParse_DummyHandler_IStreamWrapper_Unbuffered)) {
for (size_t i = 0; i < kTrialCount; i++) {
std::ifstream is(filename_, std::ios::in | std::ios::binary);
IStreamWrapper isw(is);
BaseReaderHandler<> h;
Reader reader;
reader.Parse(isw, h);
is.close();
}
}
TEST_F(RapidJson, SIMD_SUFFIX(ReaderParse_DummyHandler_IStreamWrapper_Setbuffered)) {
for (size_t i = 0; i < kTrialCount; i++) {
std::ifstream is;
char buffer[65536];
is.rdbuf()->pubsetbuf(buffer, sizeof(buffer));
is.open(filename_, std::ios::in | std::ios::binary);
IStreamWrapper isw(is);
BaseReaderHandler<> h;
Reader reader;
reader.Parse(isw, h);
is.close();
}
}
TEST_F(RapidJson, StringBuffer) {
StringBuffer sb;
for (int i = 0; i < 32 * 1024 * 1024; i++)
+7
View File
@@ -11,6 +11,11 @@
using namespace rapidjson;
RAPIDJSON_DIAG_PUSH
#if defined(__GNUC__) && __GNUC__ >= 7
RAPIDJSON_DIAG_OFF(format-overflow)
#endif
template <typename Allocator>
static char* ReadFile(const char* filename, Allocator& allocator) {
const char *paths[] = {
@@ -42,6 +47,8 @@ static char* ReadFile(const char* filename, Allocator& allocator) {
return json;
}
RAPIDJSON_DIAG_POP
class Schema : public PerfTest {
public:
Schema() {}
+8 -5
View File
@@ -3,6 +3,8 @@ include(CheckCXXCompilerFlag)
set(UNITTEST_SOURCES
allocatorstest.cpp
bigintegertest.cpp
clzlltest.cpp
cursorstreamwrappertest.cpp
documenttest.cpp
dtoatest.cpp
encodedstreamtest.cpp
@@ -14,6 +16,7 @@ set(UNITTEST_SOURCES
jsoncheckertest.cpp
namespacetest.cpp
pointertest.cpp
platformtest.cpp
prettywritertest.cpp
ostreamwrappertest.cpp
readertest.cpp
@@ -24,6 +27,7 @@ set(UNITTEST_SOURCES
stringbuffertest.cpp
strtodtest.cpp
unittest.cpp
uritest.cpp
valuetest.cpp
writertest.cpp)
@@ -36,10 +40,9 @@ if(CCACHE_FOUND)
endif()
endif(CCACHE_FOUND)
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wall -Wextra -Weffc++ -Wswitch-default -Wfloat-equal")
elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wall -Wextra -Weffc++ -Wswitch-default -Wfloat-equal -Wimplicit-fallthrough -Weverything")
set_property(DIRECTORY PROPERTY COMPILE_OPTIONS ${EXTRA_CXX_FLAGS})
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
# If the user is running a newer version of Clang that includes the
# -Wdouble-promotion, we will ignore that warning.
if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 3.7)
@@ -80,7 +83,7 @@ add_test(NAME unittest
if(NOT MSVC)
# Not running SIMD.* unit test cases for Valgrind
add_test(NAME valgrind_unittest
COMMAND valgrind --leak-check=full --error-exitcode=1 ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/unittest --gtest_filter=-SIMD.*
COMMAND valgrind --suppressions=${CMAKE_SOURCE_DIR}/test/valgrind.supp --leak-check=full --error-exitcode=1 ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/unittest --gtest_filter=-SIMD.*
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
+206 -16
View File
@@ -1,6 +1,6 @@
// 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.
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
//
// 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
@@ -16,6 +16,11 @@
#include "rapidjson/allocators.h"
#include <map>
#include <string>
#include <utility>
#include <functional>
using namespace rapidjson;
template <typename Allocator>
@@ -47,39 +52,224 @@ void TestAllocator(Allocator& a) {
EXPECT_TRUE(a.Realloc(a.Malloc(1), 1, 0) == 0);
}
struct TestStdAllocatorData {
TestStdAllocatorData(int &constructions, int &destructions) :
constructions_(&constructions),
destructions_(&destructions)
{
++*constructions_;
}
TestStdAllocatorData(const TestStdAllocatorData& rhs) :
constructions_(rhs.constructions_),
destructions_(rhs.destructions_)
{
++*constructions_;
}
TestStdAllocatorData& operator=(const TestStdAllocatorData& rhs)
{
this->~TestStdAllocatorData();
constructions_ = rhs.constructions_;
destructions_ = rhs.destructions_;
++*constructions_;
return *this;
}
~TestStdAllocatorData()
{
++*destructions_;
}
private:
TestStdAllocatorData();
int *constructions_,
*destructions_;
};
template <typename Allocator>
void TestStdAllocator(const Allocator& a) {
#if RAPIDJSON_HAS_CXX17
typedef StdAllocator<bool, Allocator> BoolAllocator;
#else
typedef StdAllocator<void, Allocator> VoidAllocator;
typedef typename VoidAllocator::template rebind<bool>::other BoolAllocator;
#endif
BoolAllocator ba(a), ba2(a);
EXPECT_TRUE(ba == ba2);
EXPECT_FALSE(ba!= ba2);
ba.deallocate(ba.allocate());
EXPECT_TRUE(ba == ba2);
EXPECT_FALSE(ba != ba2);
unsigned long long ll = 0, *llp = &ll;
const unsigned long long cll = 0, *cllp = &cll;
StdAllocator<unsigned long long, Allocator> lla(a);
EXPECT_EQ(lla.address(ll), llp);
EXPECT_EQ(lla.address(cll), cllp);
EXPECT_TRUE(lla.max_size() > 0 && lla.max_size() <= SIZE_MAX / sizeof(unsigned long long));
int *arr;
StdAllocator<int, Allocator> ia(a);
arr = ia.allocate(10 * sizeof(int));
EXPECT_TRUE(arr != 0);
for (int i = 0; i < 10; ++i) {
arr[i] = 0x0f0f0f0f;
}
ia.deallocate(arr, 10);
arr = Malloc<int>(ia, 10);
EXPECT_TRUE(arr != 0);
for (int i = 0; i < 10; ++i) {
arr[i] = 0x0f0f0f0f;
}
arr = Realloc<int>(ia, arr, 10, 20);
EXPECT_TRUE(arr != 0);
for (int i = 0; i < 10; ++i) {
EXPECT_EQ(arr[i], 0x0f0f0f0f);
}
for (int i = 10; i < 20; i++) {
arr[i] = 0x0f0f0f0f;
}
Free<int>(ia, arr, 20);
int cons = 0, dest = 0;
StdAllocator<TestStdAllocatorData, Allocator> da(a);
for (int i = 1; i < 10; i++) {
TestStdAllocatorData *d = da.allocate();
EXPECT_TRUE(d != 0);
da.destroy(new(d) TestStdAllocatorData(cons, dest));
EXPECT_EQ(cons, i);
EXPECT_EQ(dest, i);
da.deallocate(d);
}
typedef StdAllocator<char, Allocator> CharAllocator;
typedef std::basic_string<char, std::char_traits<char>, CharAllocator> String;
#if RAPIDJSON_HAS_CXX11
String s(CharAllocator{a});
#else
CharAllocator ca(a);
String s(ca);
#endif
for (int i = 0; i < 26; i++) {
s.push_back(static_cast<char>('A' + i));
}
EXPECT_TRUE(s == "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
typedef StdAllocator<std::pair<const int, bool>, Allocator> MapAllocator;
typedef std::map<int, bool, std::less<int>, MapAllocator> Map;
#if RAPIDJSON_HAS_CXX11
Map map(std::less<int>(), MapAllocator{a});
#else
MapAllocator ma(a);
Map map(std::less<int>(), ma);
#endif
for (int i = 0; i < 10; i++) {
map.insert(std::make_pair(i, (i % 2) == 0));
}
EXPECT_TRUE(map.size() == 10);
for (int i = 0; i < 10; i++) {
typename Map::iterator it = map.find(i);
EXPECT_TRUE(it != map.end());
EXPECT_TRUE(it->second == ((i % 2) == 0));
}
}
TEST(Allocator, CrtAllocator) {
CrtAllocator a;
TestAllocator(a);
TestStdAllocator(a);
CrtAllocator a2;
EXPECT_TRUE(a == a2);
EXPECT_FALSE(a != a2);
a2.Free(a2.Malloc(1));
EXPECT_TRUE(a == a2);
EXPECT_FALSE(a != a2);
}
TEST(Allocator, MemoryPoolAllocator) {
MemoryPoolAllocator<> a;
const size_t capacity = RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY;
MemoryPoolAllocator<> a(capacity);
a.Clear(); // noop
EXPECT_EQ(a.Size(), 0u);
EXPECT_EQ(a.Capacity(), 0u);
EXPECT_EQ(a.Shared(), false);
{
MemoryPoolAllocator<> a2(a);
EXPECT_EQ(a2.Shared(), true);
EXPECT_EQ(a.Shared(), true);
EXPECT_TRUE(a == a2);
EXPECT_FALSE(a != a2);
a2.Free(a2.Malloc(1));
EXPECT_TRUE(a == a2);
EXPECT_FALSE(a != a2);
}
EXPECT_EQ(a.Shared(), false);
EXPECT_EQ(a.Capacity(), capacity);
EXPECT_EQ(a.Size(), 8u); // aligned
a.Clear();
EXPECT_EQ(a.Capacity(), 0u);
EXPECT_EQ(a.Size(), 0u);
TestAllocator(a);
TestStdAllocator(a);
for (size_t i = 1; i < 1000; i++) {
EXPECT_TRUE(a.Malloc(i) != 0);
EXPECT_LE(a.Size(), a.Capacity());
}
CrtAllocator baseAllocator;
a = MemoryPoolAllocator<>(capacity, &baseAllocator);
EXPECT_EQ(a.Capacity(), 0u);
EXPECT_EQ(a.Size(), 0u);
a.Free(a.Malloc(1));
EXPECT_EQ(a.Capacity(), capacity);
EXPECT_EQ(a.Size(), 8u); // aligned
{
a.Clear();
const size_t bufSize = 1024;
char *buffer = static_cast<char *>(a.Malloc(bufSize));
MemoryPoolAllocator<> aligned_a(buffer, bufSize);
EXPECT_TRUE(aligned_a.Capacity() > 0 && aligned_a.Capacity() <= bufSize);
EXPECT_EQ(aligned_a.Size(), 0u);
aligned_a.Free(aligned_a.Malloc(1));
EXPECT_TRUE(aligned_a.Capacity() > 0 && aligned_a.Capacity() <= bufSize);
EXPECT_EQ(aligned_a.Size(), 8u); // aligned
}
{
a.Clear();
const size_t bufSize = 1024;
char *buffer = static_cast<char *>(a.Malloc(bufSize));
RAPIDJSON_ASSERT(bufSize % sizeof(void*) == 0);
MemoryPoolAllocator<> unaligned_a(buffer + 1, bufSize - 1);
EXPECT_TRUE(unaligned_a.Capacity() > 0 && unaligned_a.Capacity() <= bufSize - sizeof(void*));
EXPECT_EQ(unaligned_a.Size(), 0u);
unaligned_a.Free(unaligned_a.Malloc(1));
EXPECT_TRUE(unaligned_a.Capacity() > 0 && unaligned_a.Capacity() <= bufSize - sizeof(void*));
EXPECT_EQ(unaligned_a.Size(), 8u); // aligned
}
}
TEST(Allocator, Alignment) {
#if RAPIDJSON_64BIT == 1
EXPECT_EQ(RAPIDJSON_UINT64_C2(0x00000000, 0x00000000), RAPIDJSON_ALIGN(0));
for (uint64_t i = 1; i < 8; i++) {
EXPECT_EQ(RAPIDJSON_UINT64_C2(0x00000000, 0x00000008), RAPIDJSON_ALIGN(i));
EXPECT_EQ(RAPIDJSON_UINT64_C2(0x00000000, 0x00000010), RAPIDJSON_ALIGN(RAPIDJSON_UINT64_C2(0x00000000, 0x00000008) + i));
EXPECT_EQ(RAPIDJSON_UINT64_C2(0x00000001, 0x00000000), RAPIDJSON_ALIGN(RAPIDJSON_UINT64_C2(0x00000000, 0xFFFFFFF8) + i));
EXPECT_EQ(RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0xFFFFFFF8), RAPIDJSON_ALIGN(RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0xFFFFFFF0) + i));
if (sizeof(size_t) >= 8) {
EXPECT_EQ(RAPIDJSON_UINT64_C2(0x00000000, 0x00000000), RAPIDJSON_ALIGN(0));
for (uint64_t i = 1; i < 8; i++) {
EXPECT_EQ(RAPIDJSON_UINT64_C2(0x00000000, 0x00000008), RAPIDJSON_ALIGN(i));
EXPECT_EQ(RAPIDJSON_UINT64_C2(0x00000000, 0x00000010), RAPIDJSON_ALIGN(RAPIDJSON_UINT64_C2(0x00000000, 0x00000008) + i));
EXPECT_EQ(RAPIDJSON_UINT64_C2(0x00000001, 0x00000000), RAPIDJSON_ALIGN(RAPIDJSON_UINT64_C2(0x00000000, 0xFFFFFFF8) + i));
EXPECT_EQ(RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0xFFFFFFF8), RAPIDJSON_ALIGN(RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0xFFFFFFF0) + i));
}
}
#else
EXPECT_EQ(0u, RAPIDJSON_ALIGN(0u));
for (uint32_t i = 1; i < 4; i++) {
EXPECT_EQ(4u, RAPIDJSON_ALIGN(i));
EXPECT_EQ(8u, RAPIDJSON_ALIGN(4u + i));
EXPECT_EQ(0xFFFFFFF8u, RAPIDJSON_ALIGN(0xFFFFFFF4u + i));
EXPECT_EQ(0xFFFFFFFCu, RAPIDJSON_ALIGN(0xFFFFFFF8u + i));
for (uint32_t i = 1; i < 8; i++) {
EXPECT_EQ(8u, RAPIDJSON_ALIGN(i));
EXPECT_EQ(0xFFFFFFF8u, RAPIDJSON_ALIGN(0xFFFFFFF0u + i));
}
#endif
}
TEST(Allocator, Issue399) {
+6 -1
View File
@@ -1,6 +1,6 @@
// 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.
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
//
// 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
@@ -120,6 +120,11 @@ TEST(BigInteger, LeftShift) {
EXPECT_TRUE(BIGINTEGER_LITERAL("4537899042132549697536") == a);
a <<= 99;
EXPECT_TRUE(BIGINTEGER_LITERAL("2876235222267216943024851750785644982682875244576768") == a);
a = 1;
a <<= 64; // a.count_ != 1
a <<= 256; // interShift == 0
EXPECT_TRUE(BIGINTEGER_LITERAL("2135987035920910082395021706169552114602704522356652769947041607822219725780640550022962086936576") == a);
}
TEST(BigInteger, Compare) {
+34
View File
@@ -0,0 +1,34 @@
// 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.
//
// 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
//
// http://opensource.org/licenses/MIT
//
// 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.
#include "unittest.h"
#include "rapidjson/internal/clzll.h"
#ifdef __GNUC__
RAPIDJSON_DIAG_PUSH
#endif
using namespace rapidjson::internal;
TEST(clzll, normal) {
EXPECT_EQ(clzll(1), 63U);
EXPECT_EQ(clzll(2), 62U);
EXPECT_EQ(clzll(12), 60U);
EXPECT_EQ(clzll(0x0000000080000001UL), 32U);
EXPECT_EQ(clzll(0x8000000000000001UL), 0U);
}
#ifdef __GNUC__
RAPIDJSON_DIAG_POP
#endif
@@ -0,0 +1,115 @@
// 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.
//
// 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
//
// http://opensource.org/licenses/MIT
//
// 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.
#include "unittest.h"
#include "rapidjson/document.h"
#include "rapidjson/cursorstreamwrapper.h"
using namespace rapidjson;
// static const char json[] = "{\"string\"\n\n:\"my string\",\"array\"\n:[\"1\", \"2\", \"3\"]}";
static bool testJson(const char *json, size_t &line, size_t &col) {
StringStream ss(json);
CursorStreamWrapper<StringStream> csw(ss);
Document document;
document.ParseStream(csw);
bool ret = document.HasParseError();
if (ret) {
col = csw.GetColumn();
line = csw.GetLine();
}
return ret;
}
TEST(CursorStreamWrapper, MissingFirstBracket) {
const char json[] = "\"string\"\n\n:\"my string\",\"array\"\n:[\"1\", \"2\", \"3\"]}";
size_t col, line;
bool ret = testJson(json, line, col);
EXPECT_TRUE(ret);
EXPECT_EQ(line, 3u);
EXPECT_EQ(col, 0u);
}
TEST(CursorStreamWrapper, MissingQuotes) {
const char json[] = "{\"string\n\n:\"my string\",\"array\"\n:[\"1\", \"2\", \"3\"]}";
size_t col, line;
bool ret = testJson(json, line, col);
EXPECT_TRUE(ret);
EXPECT_EQ(line, 1u);
EXPECT_EQ(col, 8u);
}
TEST(CursorStreamWrapper, MissingColon) {
const char json[] = "{\"string\"\n\n\"my string\",\"array\"\n:[\"1\", \"2\", \"3\"]}";
size_t col, line;
bool ret = testJson(json, line, col);
EXPECT_TRUE(ret);
EXPECT_EQ(line, 3u);
EXPECT_EQ(col, 0u);
}
TEST(CursorStreamWrapper, MissingSecondQuotes) {
const char json[] = "{\"string\"\n\n:my string\",\"array\"\n:[\"1\", \"2\", \"3\"]}";
size_t col, line;
bool ret = testJson(json, line, col);
EXPECT_TRUE(ret);
EXPECT_EQ(line, 3u);
EXPECT_EQ(col, 1u);
}
TEST(CursorStreamWrapper, MissingComma) {
const char json[] = "{\"string\"\n\n:\"my string\"\"array\"\n:[\"1\", \"2\", \"3\"]}";
size_t col, line;
bool ret = testJson(json, line, col);
EXPECT_TRUE(ret);
EXPECT_EQ(line, 3u);
EXPECT_EQ(col, 12u);
}
TEST(CursorStreamWrapper, MissingArrayBracket) {
const char json[] = "{\"string\"\n\n:\"my string\",\"array\"\n:\"1\", \"2\", \"3\"]}";
size_t col, line;
bool ret = testJson(json, line, col);
EXPECT_TRUE(ret);
EXPECT_EQ(line, 4u);
EXPECT_EQ(col, 9u);
}
TEST(CursorStreamWrapper, MissingArrayComma) {
const char json[] = "{\"string\"\n\n:\"my string\",\"array\"\n:[\"1\" \"2\", \"3\"]}";
size_t col, line;
bool ret = testJson(json, line, col);
EXPECT_TRUE(ret);
EXPECT_EQ(line, 4u);
EXPECT_EQ(col, 6u);
}
TEST(CursorStreamWrapper, MissingLastArrayBracket) {
const char json8[] = "{\"string\"\n\n:\"my string\",\"array\"\n:[\"1\", \"2\", \"3\"}";
size_t col, line;
bool ret = testJson(json8, line, col);
EXPECT_TRUE(ret);
EXPECT_EQ(line, 4u);
EXPECT_EQ(col, 15u);
}
TEST(CursorStreamWrapper, MissingLastBracket) {
const char json9[] = "{\"string\"\n\n:\"my string\",\"array\"\n:[\"1\", \"2\", \"3\"]";
size_t col, line;
bool ret = testJson(json9, line, col);
EXPECT_TRUE(ret);
EXPECT_EQ(line, 4u);
EXPECT_EQ(col, 16u);
}
+25 -3
View File
@@ -1,6 +1,6 @@
// 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.
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
//
// 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
@@ -128,8 +128,14 @@ TEST(Document, UnchangedOnParseError) {
Document doc;
doc.SetArray().PushBack(0, doc.GetAllocator());
ParseResult noError;
EXPECT_TRUE(noError);
ParseResult err = doc.Parse("{]");
EXPECT_TRUE(doc.HasParseError());
EXPECT_NE(err, noError);
EXPECT_NE(err.Code(), noError);
EXPECT_NE(noError, doc.GetParseError());
EXPECT_EQ(err.Code(), doc.GetParseError());
EXPECT_EQ(err.Offset(), doc.GetErrorOffset());
EXPECT_TRUE(doc.IsArray());
@@ -138,6 +144,9 @@ TEST(Document, UnchangedOnParseError) {
err = doc.Parse("{}");
EXPECT_FALSE(doc.HasParseError());
EXPECT_FALSE(err.IsError());
EXPECT_TRUE(err);
EXPECT_EQ(err, noError);
EXPECT_EQ(err.Code(), noError);
EXPECT_EQ(err.Code(), doc.GetParseError());
EXPECT_EQ(err.Offset(), doc.GetErrorOffset());
EXPECT_TRUE(doc.IsObject());
@@ -291,7 +300,14 @@ TEST(Document, Swap) {
o.SetObject().AddMember("a", 1, a);
// Swap between Document and Value
// d1.Swap(o); // doesn't compile
d1.Swap(o);
EXPECT_TRUE(d1.IsObject());
EXPECT_TRUE(o.IsArray());
d1.Swap(o);
EXPECT_TRUE(d1.IsArray());
EXPECT_TRUE(o.IsObject());
o.Swap(d1);
EXPECT_TRUE(d1.IsObject());
EXPECT_TRUE(o.IsArray());
@@ -309,6 +325,8 @@ TEST(Document, Swap) {
EXPECT_TRUE(d1.IsNull());
// reset document, including allocator
// so clear o before so that it doesnt contain dangling elements
o.Clear();
Document().Swap(d2);
EXPECT_TRUE(d2.IsNull());
EXPECT_NE(&d2.GetAllocator(), &a);
@@ -488,15 +506,19 @@ TYPED_TEST(DocumentMove, MoveConstructorParseError) {
a.Parse("{ 4 = 4]");
ParseResult error(a.GetParseError(), a.GetErrorOffset());
EXPECT_TRUE(a.HasParseError());
EXPECT_NE(error, noError);
EXPECT_NE(error.Code(), noError);
EXPECT_NE(error.Code(), noError.Code());
EXPECT_NE(error.Offset(), noError.Offset());
D b(std::move(a));
EXPECT_FALSE(a.HasParseError());
EXPECT_TRUE(b.HasParseError());
EXPECT_EQ(a.GetParseError(), noError);
EXPECT_EQ(a.GetParseError(), noError.Code());
EXPECT_EQ(b.GetParseError(), error.Code());
EXPECT_EQ(a.GetErrorOffset(), noError.Offset());
EXPECT_EQ(b.GetParseError(), error);
EXPECT_EQ(b.GetParseError(), error.Code());
EXPECT_EQ(b.GetErrorOffset(), error.Offset());
D c(std::move(b));
+2 -1
View File
@@ -1,6 +1,6 @@
// 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.
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
//
// 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
@@ -38,6 +38,7 @@ TEST(dtoa, normal) {
TEST_DTOA(0.123456789012, "0.123456789012");
TEST_DTOA(1234567.8, "1234567.8");
TEST_DTOA(-79.39773355813419, "-79.39773355813419");
TEST_DTOA(-36.973846435546875, "-36.973846435546875");
TEST_DTOA(0.000001, "0.000001");
TEST_DTOA(0.0000001, "1e-7");
TEST_DTOA(1e30, "1e30");
+2 -2
View File
@@ -1,6 +1,6 @@
// 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.
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
//
// 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
@@ -113,8 +113,8 @@ protected:
EXPECT_EQ(expected, actual);
}
EXPECT_EQ('\0', s.Peek());
free(data);
EXPECT_EQ(size, eis.Tell());
free(data);
}
}
+2 -2
View File
@@ -1,6 +1,6 @@
// 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.
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
//
// 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
@@ -267,7 +267,7 @@ static unsigned inline decode(unsigned* state, unsigned* codep, unsigned byte) {
*codep = (*state != UTF8_ACCEPT) ?
(byte & 0x3fu) | (*codep << 6) :
(0xff >> type) & (byte);
(0xffu >> type) & (byte);
*state = utf8d[256 + *state + type];
return *state;
+45 -2
View File
@@ -1,6 +1,6 @@
// 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.
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
//
// 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
@@ -21,7 +21,7 @@ using namespace rapidjson;
class FileStreamTest : public ::testing::Test {
public:
FileStreamTest() : filename_(), json_(), length_() {}
FileStreamTest() : filename_(), json_(), length_(), abcde_() {}
virtual ~FileStreamTest();
virtual void SetUp() {
@@ -49,6 +49,24 @@ public:
size_t readLength = fread(json_, 1, length_, fp);
json_[readLength] = '\0';
fclose(fp);
const char *abcde_paths[] = {
"data/abcde.txt",
"bin/data/abcde.txt",
"../bin/data/abcde.txt",
"../../bin/data/abcde.txt",
"../../../bin/data/abcde.txt"
};
fp = 0;
for (size_t i = 0; i < sizeof(abcde_paths) / sizeof(abcde_paths[0]); i++) {
fp = fopen(abcde_paths[i], "rb");
if (fp) {
abcde_ = abcde_paths[i];
break;
}
}
ASSERT_TRUE(fp != 0);
fclose(fp);
}
virtual void TearDown() {
@@ -64,6 +82,7 @@ protected:
const char* filename_;
char *json_;
size_t length_;
const char* abcde_;
};
FileStreamTest::~FileStreamTest() {}
@@ -86,6 +105,30 @@ TEST_F(FileStreamTest, FileReadStream) {
fclose(fp);
}
TEST_F(FileStreamTest, FileReadStream_Peek4) {
FILE *fp = fopen(abcde_, "rb");
ASSERT_TRUE(fp != 0);
char buffer[4];
FileReadStream s(fp, buffer, sizeof(buffer));
const char* c = s.Peek4();
for (int i = 0; i < 4; i++)
EXPECT_EQ('a' + i, c[i]);
EXPECT_EQ(0u, s.Tell());
for (int i = 0; i < 5; i++) {
EXPECT_EQ(static_cast<size_t>(i), s.Tell());
EXPECT_EQ('a' + i, s.Peek());
EXPECT_EQ('a' + i, s.Peek());
EXPECT_EQ('a' + i, s.Take());
}
EXPECT_EQ(5u, s.Tell());
EXPECT_EQ(0, s.Peek());
EXPECT_EQ(0, s.Take());
fclose(fp);
}
TEST_F(FileStreamTest, FileWriteStream) {
char filename[L_tmpnam];
FILE* fp = TempFile(filename);
+15 -12
View File
@@ -1,6 +1,6 @@
// 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.
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
//
// 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
@@ -100,6 +100,9 @@ struct Foo {
#include "rapidjson/prettywriter.h"
#include "rapidjson/schema.h" // -> pointer.h
typedef Transcoder<UTF8<>, UTF8<> > TranscoderUtf8ToUtf8;
typedef BaseReaderHandler<UTF8<>, void> BaseReaderHandlerUtf8Void;
Foo::Foo() :
// encodings.h
utf8(RAPIDJSON_NEW(UTF8<>)),
@@ -111,40 +114,40 @@ Foo::Foo() :
utf32le(RAPIDJSON_NEW(UTF32LE<>)),
ascii(RAPIDJSON_NEW(ASCII<>)),
autoutf(RAPIDJSON_NEW(AutoUTF<unsigned>)),
transcoder(RAPIDJSON_NEW((Transcoder<UTF8<>, UTF8<> >))),
transcoder(RAPIDJSON_NEW(TranscoderUtf8ToUtf8)),
// allocators.h
crtallocator(RAPIDJSON_NEW(CrtAllocator)),
memorypoolallocator(RAPIDJSON_NEW(MemoryPoolAllocator<>)),
// stream.h
stringstream(RAPIDJSON_NEW(StringStream(0))),
insitustringstream(RAPIDJSON_NEW(InsituStringStream(0))),
stringstream(RAPIDJSON_NEW(StringStream)(NULL)),
insitustringstream(RAPIDJSON_NEW(InsituStringStream)(NULL)),
// stringbuffer.h
stringbuffer(RAPIDJSON_NEW(StringBuffer)),
// // filereadstream.h
// filereadstream(RAPIDJSON_NEW(FileReadStream(stdout, buffer, sizeof(buffer)))),
// filereadstream(RAPIDJSON_NEW(FileReadStream)(stdout, buffer, sizeof(buffer))),
// // filewritestream.h
// filewritestream(RAPIDJSON_NEW(FileWriteStream(stdout, buffer, sizeof(buffer)))),
// filewritestream(RAPIDJSON_NEW(FileWriteStream)(stdout, buffer, sizeof(buffer))),
// memorybuffer.h
memorybuffer(RAPIDJSON_NEW(MemoryBuffer)),
// memorystream.h
memorystream(RAPIDJSON_NEW(MemoryStream(0, 0))),
memorystream(RAPIDJSON_NEW(MemoryStream)(NULL, 0)),
// reader.h
basereaderhandler(RAPIDJSON_NEW((BaseReaderHandler<UTF8<>, void>))),
basereaderhandler(RAPIDJSON_NEW(BaseReaderHandlerUtf8Void)),
reader(RAPIDJSON_NEW(Reader)),
// writer.h
writer(RAPIDJSON_NEW((Writer<StringBuffer>))),
writer(RAPIDJSON_NEW(Writer<StringBuffer>)),
// prettywriter.h
prettywriter(RAPIDJSON_NEW((PrettyWriter<StringBuffer>))),
prettywriter(RAPIDJSON_NEW(PrettyWriter<StringBuffer>)),
// document.h
value(RAPIDJSON_NEW(Value)),
@@ -154,8 +157,8 @@ Foo::Foo() :
pointer(RAPIDJSON_NEW(Pointer)),
// schema.h
schemadocument(RAPIDJSON_NEW(SchemaDocument(*document))),
schemavalidator(RAPIDJSON_NEW(SchemaValidator(*schemadocument)))
schemadocument(RAPIDJSON_NEW(SchemaDocument)(*document)),
schemavalidator(RAPIDJSON_NEW(SchemaValidator)(*schemadocument))
{
}
+12 -12
View File
@@ -1,6 +1,6 @@
// 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.
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
//
// 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
@@ -20,7 +20,7 @@
#include <sstream>
#include <fstream>
#ifdef _MSC_VER
#if defined(_MSC_VER) && !defined(__clang__)
RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(4702) // unreachable code
#endif
@@ -35,21 +35,21 @@ static void TestStringStream() {
{
StringStreamType iss;
BasicIStreamWrapper<StringStreamType> is(iss);
EXPECT_EQ(0, is.Tell());
EXPECT_EQ(0u, is.Tell());
if (sizeof(Ch) == 1) {
EXPECT_EQ(0, is.Peek4());
EXPECT_EQ(0, is.Tell());
EXPECT_EQ(0u, is.Tell());
}
EXPECT_EQ(0, is.Peek());
EXPECT_EQ(0, is.Take());
EXPECT_EQ(0, is.Tell());
EXPECT_EQ(0u, is.Tell());
}
{
Ch s[] = { 'A', 'B', 'C', '\0' };
StringStreamType iss(s);
BasicIStreamWrapper<StringStreamType> is(iss);
EXPECT_EQ(0, is.Tell());
EXPECT_EQ(0u, is.Tell());
if (sizeof(Ch) == 1) {
EXPECT_EQ(0, is.Peek4()); // less than 4 bytes
}
@@ -59,7 +59,7 @@ static void TestStringStream() {
EXPECT_EQ('A' + i, is.Peek());
EXPECT_EQ('A' + i, is.Take());
}
EXPECT_EQ(3, is.Tell());
EXPECT_EQ(3u, is.Tell());
EXPECT_EQ(0, is.Peek());
EXPECT_EQ(0, is.Take());
}
@@ -72,7 +72,7 @@ static void TestStringStream() {
const Ch* c = is.Peek4();
for (int i = 0; i < 4; i++)
EXPECT_EQ('A' + i, c[i]);
EXPECT_EQ(0, is.Tell());
EXPECT_EQ(0u, is.Tell());
}
for (int i = 0; i < 5; i++) {
EXPECT_EQ(static_cast<size_t>(i), is.Tell());
@@ -80,7 +80,7 @@ static void TestStringStream() {
EXPECT_EQ('A' + i, is.Peek());
EXPECT_EQ('A' + i, is.Take());
}
EXPECT_EQ(5, is.Tell());
EXPECT_EQ(5u, is.Tell());
EXPECT_EQ(0, is.Peek());
EXPECT_EQ(0, is.Take());
}
@@ -129,7 +129,7 @@ TEST(IStreamWrapper, ifstream) {
Document d;
EXPECT_TRUE(!d.ParseStream(eis).HasParseError());
EXPECT_TRUE(d.IsObject());
EXPECT_EQ(5, d.MemberCount());
EXPECT_EQ(5u, d.MemberCount());
}
TEST(IStreamWrapper, fstream) {
@@ -140,7 +140,7 @@ TEST(IStreamWrapper, fstream) {
Document d;
EXPECT_TRUE(!d.ParseStream(eis).HasParseError());
EXPECT_TRUE(d.IsObject());
EXPECT_EQ(5, d.MemberCount());
EXPECT_EQ(5u, d.MemberCount());
}
// wifstream/wfstream only works on C++11 with codecvt_utf16
@@ -176,6 +176,6 @@ TEST(IStreamWrapper, wfstream) {
#endif
#ifdef _MSC_VER
#if defined(_MSC_VER) && !defined(__clang__)
RAPIDJSON_DIAG_POP
#endif
+12 -12
View File
@@ -1,15 +1,15 @@
// 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.
//
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
//
// 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
//
// http://opensource.org/licenses/MIT
//
// 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
// 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.
#include "unittest.h"
@@ -61,7 +61,7 @@ static void VerifyValue(T value, void(*f)(T, char*), char* (*g)(T, char*)) {
f(value, buffer1);
*g(value, buffer2) = '\0';
EXPECT_STREQ(buffer1, buffer2);
}
@@ -70,23 +70,23 @@ template <typename T>
static void Verify(void(*f)(T, char*), char* (*g)(T, char*)) {
// Boundary cases
VerifyValue<T>(0, f, g);
VerifyValue<T>(std::numeric_limits<T>::min(), f, g);
VerifyValue<T>(std::numeric_limits<T>::max(), f, g);
VerifyValue<T>((std::numeric_limits<T>::min)(), f, g);
VerifyValue<T>((std::numeric_limits<T>::max)(), f, g);
// 2^n - 1, 2^n, 10^n - 1, 10^n until overflow
for (uint32_t power = 2; power <= 10; power += 8) {
for (int power = 2; power <= 10; power += 8) {
T i = 1, last;
do {
VerifyValue<T>(i - 1, f, g);
VerifyValue<T>(i, f, g);
if (std::numeric_limits<T>::min() < 0) {
if ((std::numeric_limits<T>::min)() < 0) {
VerifyValue<T>(Traits<T>::Negate(i), f, g);
VerifyValue<T>(Traits<T>::Negate(i + 1), f, g);
}
last = i;
if (i > static_cast<T>(std::numeric_limits<T>::max() / static_cast<T>(power)))
if (i > static_cast<T>((std::numeric_limits<T>::max)() / static_cast<T>(power)))
break;
i *= power;
i *= static_cast<T>(power);
} while (last < i);
}
}
+49 -5
View File
@@ -1,6 +1,6 @@
// 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.
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
//
// 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
@@ -48,6 +48,24 @@ static char* ReadFile(const char* filename, size_t& length) {
return json;
}
struct NoOpHandler {
bool Null() { return true; }
bool Bool(bool) { return true; }
bool Int(int) { return true; }
bool Uint(unsigned) { return true; }
bool Int64(int64_t) { return true; }
bool Uint64(uint64_t) { return true; }
bool Double(double) { return true; }
bool RawNumber(const char*, SizeType, bool) { return true; }
bool String(const char*, SizeType, bool) { return true; }
bool StartObject() { return true; }
bool Key(const char*, SizeType, bool) { return true; }
bool EndObject(SizeType) { return true; }
bool StartArray() { return true; }
bool EndArray(SizeType) { return true; }
};
TEST(JsonChecker, Reader) {
char filename[256];
@@ -67,13 +85,26 @@ TEST(JsonChecker, Reader) {
continue;
}
// Test stack-based parsing.
GenericDocument<UTF8<>, CrtAllocator> document; // Use Crt allocator to check exception-safety (no memory leak)
document.Parse(json);
EXPECT_TRUE(document.HasParseError());
EXPECT_TRUE(document.HasParseError()) << filename;
// Test iterative parsing.
document.Parse<kParseIterativeFlag>(json);
EXPECT_TRUE(document.HasParseError());
EXPECT_TRUE(document.HasParseError()) << filename;
// Test iterative pull-parsing.
Reader reader;
StringStream ss(json);
NoOpHandler h;
reader.IterativeParseInit();
while (!reader.IterativeParseComplete()) {
if (!reader.IterativeParseNext<kParseDefaultFlags>(ss, h))
break;
}
EXPECT_TRUE(reader.HasParseError()) << filename;
free(json);
}
@@ -87,12 +118,25 @@ TEST(JsonChecker, Reader) {
continue;
}
// Test stack-based parsing.
GenericDocument<UTF8<>, CrtAllocator> document; // Use Crt allocator to check exception-safety (no memory leak)
document.Parse(json);
EXPECT_FALSE(document.HasParseError());
EXPECT_FALSE(document.HasParseError()) << filename;
// Test iterative parsing.
document.Parse<kParseIterativeFlag>(json);
EXPECT_FALSE(document.HasParseError());
EXPECT_FALSE(document.HasParseError()) << filename;
// Test iterative pull-parsing.
Reader reader;
StringStream ss(json);
NoOpHandler h;
reader.IterativeParseInit();
while (!reader.IterativeParseComplete()) {
if (!reader.IterativeParseNext<kParseDefaultFlags>(ss, h))
break;
}
EXPECT_FALSE(reader.HasParseError()) << filename;
free(json);
}
+1 -1
View File
@@ -1,6 +1,6 @@
// 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.
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
//
// 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
+4 -3
View File
@@ -1,6 +1,6 @@
// 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.
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
//
// 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
@@ -69,14 +69,15 @@ static void TestFileStream() {
const char* s = "Hello World!\n";
{
ofstream ofs(filename, ios::out | ios::binary);
BasicOStreamWrapper<ofstream> osw(ofs);
FileStreamType ofs(filename, ios::out | ios::binary);
BasicOStreamWrapper<FileStreamType> osw(ofs);
for (const char* p = s; *p; p++)
osw.Put(*p);
osw.Flush();
}
fp = fopen(filename, "r");
ASSERT_TRUE( fp != NULL );
for (const char* p = s; *p; p++)
EXPECT_EQ(*p, static_cast<char>(fgetc(fp)));
fclose(fp);
+40
View File
@@ -0,0 +1,40 @@
// Tencent is pleased to support the open source community by making RapidJSON available.
//
// Copyright (C) 2021 THL A29 Limited, a Tencent company, and Milo Yip.
//
// 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
//
// http://opensource.org/licenses/MIT
//
// 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.
#include "unittest.h"
// see https://github.com/Tencent/rapidjson/issues/1448
// including windows.h on purpose to provoke a compile time problem as GetObject is a
// macro that gets defined when windows.h is included
#ifdef _WIN32
#include <windows.h>
#endif
#include "rapidjson/document.h"
#undef GetObject
using namespace rapidjson;
TEST(Platform, GetObject) {
Document doc;
doc.Parse(" { \"object\" : { \"pi\": 3.1416} } ");
EXPECT_TRUE(doc.IsObject());
EXPECT_TRUE(doc.HasMember("object"));
const Document::ValueType& o = doc["object"];
EXPECT_TRUE(o.IsObject());
Value::ConstObject sub = o.GetObject();
EXPECT_TRUE(sub.HasMember("pi"));
Value::ConstObject sub2 = o.GetObj();
EXPECT_TRUE(sub2.HasMember("pi"));
}
+231 -25
View File
@@ -1,6 +1,6 @@
// 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.
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
//
// 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
@@ -15,7 +15,10 @@
#include "unittest.h"
#include "rapidjson/pointer.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/ostreamwrapper.h"
#include <sstream>
#include <map>
#include <algorithm>
using namespace rapidjson;
@@ -300,7 +303,7 @@ TEST(Pointer, Parse_URIFragment) {
}
{
// Decode UTF-8 perecent encoding to UTF-8
// Decode UTF-8 percent encoding to UTF-8
Pointer p("#/%C2%A2");
EXPECT_TRUE(p.IsValid());
EXPECT_EQ(1u, p.GetTokenCount());
@@ -308,7 +311,7 @@ TEST(Pointer, Parse_URIFragment) {
}
{
// Decode UTF-8 perecent encoding to UTF-16
// Decode UTF-8 percent encoding to UTF-16
GenericPointer<GenericValue<UTF16<> > > p(L"#/%C2%A2");
EXPECT_TRUE(p.IsValid());
EXPECT_EQ(1u, p.GetTokenCount());
@@ -317,7 +320,7 @@ TEST(Pointer, Parse_URIFragment) {
}
{
// Decode UTF-8 perecent encoding to UTF-16
// Decode UTF-8 percent encoding to UTF-16
GenericPointer<GenericValue<UTF16<> > > p(L"#/%E2%82%AC");
EXPECT_TRUE(p.IsValid());
EXPECT_EQ(1u, p.GetTokenCount());
@@ -441,8 +444,8 @@ TEST(Pointer, Stringify) {
}
// Construct a Pointer with static tokens, no dynamic allocation involved.
#define NAME(s) { s, sizeof(s) / sizeof(s[0]) - 1, kPointerInvalidIndex }
#define INDEX(i) { #i, sizeof(#i) - 1, i }
#define NAME(s) { s, static_cast<SizeType>(sizeof(s) / sizeof(s[0]) - 1), kPointerInvalidIndex }
#define INDEX(i) { #i, static_cast<SizeType>(sizeof(#i) - 1), i }
static const Pointer::Token kTokens[] = { NAME("foo"), INDEX(0) }; // equivalent to "/foo/0"
@@ -462,7 +465,8 @@ TEST(Pointer, ConstructorWithToken) {
TEST(Pointer, CopyConstructor) {
{
Pointer p("/foo/0");
CrtAllocator allocator;
Pointer p("/foo/0", &allocator);
Pointer q(p);
EXPECT_TRUE(q.IsValid());
EXPECT_EQ(2u, q.GetTokenCount());
@@ -471,6 +475,9 @@ TEST(Pointer, CopyConstructor) {
EXPECT_EQ(1u, q.GetTokens()[1].length);
EXPECT_STREQ("0", q.GetTokens()[1].name);
EXPECT_EQ(0u, q.GetTokens()[1].index);
// Copied pointer needs to have its own allocator
EXPECT_NE(&p.GetAllocator(), &q.GetAllocator());
}
// Static tokens
@@ -489,7 +496,8 @@ TEST(Pointer, CopyConstructor) {
TEST(Pointer, Assignment) {
{
Pointer p("/foo/0");
CrtAllocator allocator;
Pointer p("/foo/0", &allocator);
Pointer q;
q = p;
EXPECT_TRUE(q.IsValid());
@@ -499,7 +507,8 @@ TEST(Pointer, Assignment) {
EXPECT_EQ(1u, q.GetTokens()[1].length);
EXPECT_STREQ("0", q.GetTokens()[1].name);
EXPECT_EQ(0u, q.GetTokens()[1].index);
q = q;
EXPECT_NE(&p.GetAllocator(), &q.GetAllocator());
q = static_cast<const Pointer &>(q);
EXPECT_TRUE(q.IsValid());
EXPECT_EQ(2u, q.GetTokenCount());
EXPECT_EQ(3u, q.GetTokens()[0].length);
@@ -507,6 +516,7 @@ TEST(Pointer, Assignment) {
EXPECT_EQ(1u, q.GetTokens()[1].length);
EXPECT_STREQ("0", q.GetTokens()[1].name);
EXPECT_EQ(0u, q.GetTokens()[1].index);
EXPECT_NE(&p.GetAllocator(), &q.GetAllocator());
}
// Static tokens
@@ -524,6 +534,36 @@ TEST(Pointer, Assignment) {
}
}
TEST(Pointer, Swap) {
Pointer p("/foo/0");
Pointer q(&p.GetAllocator());
q.Swap(p);
EXPECT_EQ(&q.GetAllocator(), &p.GetAllocator());
EXPECT_TRUE(p.IsValid());
EXPECT_TRUE(q.IsValid());
EXPECT_EQ(0u, p.GetTokenCount());
EXPECT_EQ(2u, q.GetTokenCount());
EXPECT_EQ(3u, q.GetTokens()[0].length);
EXPECT_STREQ("foo", q.GetTokens()[0].name);
EXPECT_EQ(1u, q.GetTokens()[1].length);
EXPECT_STREQ("0", q.GetTokens()[1].name);
EXPECT_EQ(0u, q.GetTokens()[1].index);
// std::swap compatibility
std::swap(p, q);
EXPECT_EQ(&p.GetAllocator(), &q.GetAllocator());
EXPECT_TRUE(q.IsValid());
EXPECT_TRUE(p.IsValid());
EXPECT_EQ(0u, q.GetTokenCount());
EXPECT_EQ(2u, p.GetTokenCount());
EXPECT_EQ(3u, p.GetTokens()[0].length);
EXPECT_STREQ("foo", p.GetTokens()[0].name);
EXPECT_EQ(1u, p.GetTokens()[1].length);
EXPECT_STREQ("0", p.GetTokens()[1].name);
EXPECT_EQ(0u, p.GetTokens()[1].index);
}
TEST(Pointer, Append) {
{
Pointer p;
@@ -610,6 +650,52 @@ TEST(Pointer, Create) {
}
}
static const char kJsonIds[] = "{\n"
" \"id\": \"/root/\","
" \"foo\":[\"bar\", \"baz\", {\"id\": \"inarray\", \"child\": 1}],\n"
" \"int\" : 2,\n"
" \"str\" : \"val\",\n"
" \"obj\": {\"id\": \"inobj\", \"child\": 3},\n"
" \"jbo\": {\"id\": true, \"child\": 4}\n"
"}";
TEST(Pointer, GetUri) {
CrtAllocator allocator;
Document d;
d.Parse(kJsonIds);
Pointer::UriType doc("http://doc");
Pointer::UriType root("http://doc/root/");
Pointer::UriType empty = Pointer::UriType();
EXPECT_TRUE(Pointer("").GetUri(d, doc) == doc);
EXPECT_TRUE(Pointer("/foo").GetUri(d, doc) == root);
EXPECT_TRUE(Pointer("/foo/0").GetUri(d, doc) == root);
EXPECT_TRUE(Pointer("/foo/2").GetUri(d, doc) == root);
EXPECT_TRUE(Pointer("/foo/2/child").GetUri(d, doc) == Pointer::UriType("http://doc/root/inarray"));
EXPECT_TRUE(Pointer("/int").GetUri(d, doc) == root);
EXPECT_TRUE(Pointer("/str").GetUri(d, doc) == root);
EXPECT_TRUE(Pointer("/obj").GetUri(d, doc) == root);
EXPECT_TRUE(Pointer("/obj/child").GetUri(d, doc) == Pointer::UriType("http://doc/root/inobj"));
EXPECT_TRUE(Pointer("/jbo").GetUri(d, doc) == root);
EXPECT_TRUE(Pointer("/jbo/child").GetUri(d, doc) == root); // id not string
size_t unresolvedTokenIndex;
EXPECT_TRUE(Pointer("/abc").GetUri(d, doc, &unresolvedTokenIndex, &allocator) == empty); // Out of boundary
EXPECT_EQ(0u, unresolvedTokenIndex);
EXPECT_TRUE(Pointer("/foo/3").GetUri(d, doc, &unresolvedTokenIndex, &allocator) == empty); // Out of boundary
EXPECT_EQ(1u, unresolvedTokenIndex);
EXPECT_TRUE(Pointer("/foo/a").GetUri(d, doc, &unresolvedTokenIndex, &allocator) == empty); // "/foo" is an array, cannot query by "a"
EXPECT_EQ(1u, unresolvedTokenIndex);
EXPECT_TRUE(Pointer("/foo/0/0").GetUri(d, doc, &unresolvedTokenIndex, &allocator) == empty); // "/foo/0" is an string, cannot further query
EXPECT_EQ(2u, unresolvedTokenIndex);
EXPECT_TRUE(Pointer("/foo/0/a").GetUri(d, doc, &unresolvedTokenIndex, &allocator) == empty); // "/foo/0" is an string, cannot further query
EXPECT_EQ(2u, unresolvedTokenIndex);
Pointer::Token tokens[] = { { "foo ...", 3, kPointerInvalidIndex } };
EXPECT_TRUE(Pointer(tokens, 1).GetUri(d, doc) == root);
}
TEST(Pointer, Get) {
Document d;
d.Parse(kJson);
@@ -626,16 +712,20 @@ TEST(Pointer, Get) {
EXPECT_EQ(&d["k\"l"], Pointer("/k\"l").Get(d));
EXPECT_EQ(&d[" "], Pointer("/ ").Get(d));
EXPECT_EQ(&d["m~n"], Pointer("/m~0n").Get(d));
EXPECT_TRUE(Pointer("/abc").Get(d) == 0);
EXPECT_TRUE(Pointer("/abc").Get(d) == 0); // Out of boundary
size_t unresolvedTokenIndex;
EXPECT_TRUE(Pointer("/foo/2").Get(d, &unresolvedTokenIndex) == 0); // Out of boundary
EXPECT_EQ(1, unresolvedTokenIndex);
EXPECT_EQ(1u, unresolvedTokenIndex);
EXPECT_TRUE(Pointer("/foo/a").Get(d, &unresolvedTokenIndex) == 0); // "/foo" is an array, cannot query by "a"
EXPECT_EQ(1, unresolvedTokenIndex);
EXPECT_EQ(1u, unresolvedTokenIndex);
EXPECT_TRUE(Pointer("/foo/0/0").Get(d, &unresolvedTokenIndex) == 0); // "/foo/0" is an string, cannot further query
EXPECT_EQ(2, unresolvedTokenIndex);
EXPECT_EQ(2u, unresolvedTokenIndex);
EXPECT_TRUE(Pointer("/foo/0/a").Get(d, &unresolvedTokenIndex) == 0); // "/foo/0" is an string, cannot further query
EXPECT_EQ(2, unresolvedTokenIndex);
EXPECT_EQ(2u, unresolvedTokenIndex);
Pointer::Token tokens[] = { { "foo ...", 3, kPointerInvalidIndex } };
EXPECT_EQ(&d["foo"], Pointer(tokens, 1).Get(d));
}
TEST(Pointer, GetWithDefault) {
@@ -862,7 +952,7 @@ TEST(Pointer, Set_NoAllocator) {
#endif
}
TEST(Pointer, Swap) {
TEST(Pointer, Swap_Value) {
Document d;
d.Parse(kJson);
Document::AllocatorType& a = d.GetAllocator();
@@ -871,7 +961,7 @@ TEST(Pointer, Swap) {
EXPECT_STREQ("bar", d["foo"][1].GetString());
}
TEST(Pointer, Swap_NoAllocator) {
TEST(Pointer, Swap_Value_NoAllocator) {
Document d;
d.Parse(kJson);
Pointer("/foo/0").Swap(d, *Pointer("/foo/1").Get(d));
@@ -954,13 +1044,13 @@ TEST(Pointer, GetValueByPointer) {
size_t unresolvedTokenIndex;
EXPECT_TRUE(GetValueByPointer(d, "/foo/2", &unresolvedTokenIndex) == 0); // Out of boundary
EXPECT_EQ(1, unresolvedTokenIndex);
EXPECT_EQ(1u, unresolvedTokenIndex);
EXPECT_TRUE(GetValueByPointer(d, "/foo/a", &unresolvedTokenIndex) == 0); // "/foo" is an array, cannot query by "a"
EXPECT_EQ(1, unresolvedTokenIndex);
EXPECT_EQ(1u, unresolvedTokenIndex);
EXPECT_TRUE(GetValueByPointer(d, "/foo/0/0", &unresolvedTokenIndex) == 0); // "/foo/0" is an string, cannot further query
EXPECT_EQ(2, unresolvedTokenIndex);
EXPECT_EQ(2u, unresolvedTokenIndex);
EXPECT_TRUE(GetValueByPointer(d, "/foo/0/a", &unresolvedTokenIndex) == 0); // "/foo/0" is an string, cannot further query
EXPECT_EQ(2, unresolvedTokenIndex);
EXPECT_EQ(2u, unresolvedTokenIndex);
// const version
const Value& v = d;
@@ -968,13 +1058,13 @@ TEST(Pointer, GetValueByPointer) {
EXPECT_EQ(&d["foo"][0], GetValueByPointer(v, "/foo/0"));
EXPECT_TRUE(GetValueByPointer(v, "/foo/2", &unresolvedTokenIndex) == 0); // Out of boundary
EXPECT_EQ(1, unresolvedTokenIndex);
EXPECT_EQ(1u, unresolvedTokenIndex);
EXPECT_TRUE(GetValueByPointer(v, "/foo/a", &unresolvedTokenIndex) == 0); // "/foo" is an array, cannot query by "a"
EXPECT_EQ(1, unresolvedTokenIndex);
EXPECT_EQ(1u, unresolvedTokenIndex);
EXPECT_TRUE(GetValueByPointer(v, "/foo/0/0", &unresolvedTokenIndex) == 0); // "/foo/0" is an string, cannot further query
EXPECT_EQ(2, unresolvedTokenIndex);
EXPECT_EQ(2u, unresolvedTokenIndex);
EXPECT_TRUE(GetValueByPointer(v, "/foo/0/a", &unresolvedTokenIndex) == 0); // "/foo/0" is an string, cannot further query
EXPECT_EQ(2, unresolvedTokenIndex);
EXPECT_EQ(2u, unresolvedTokenIndex);
}
@@ -1488,7 +1578,112 @@ TEST(Pointer, Ambiguity) {
}
}
// https://github.com/miloyip/rapidjson/issues/483
TEST(Pointer, ResolveOnObject) {
Document d;
EXPECT_FALSE(d.Parse("{\"a\": 123}").HasParseError());
{
Value::ConstObject o = static_cast<const Document&>(d).GetObject();
EXPECT_EQ(123, Pointer("/a").Get(o)->GetInt());
}
{
Value::Object o = d.GetObject();
Pointer("/a").Set(o, 456, d.GetAllocator());
EXPECT_EQ(456, Pointer("/a").Get(o)->GetInt());
}
}
TEST(Pointer, ResolveOnArray) {
Document d;
EXPECT_FALSE(d.Parse("[1, 2, 3]").HasParseError());
{
Value::ConstArray a = static_cast<const Document&>(d).GetArray();
EXPECT_EQ(2, Pointer("/1").Get(a)->GetInt());
}
{
Value::Array a = d.GetArray();
Pointer("/1").Set(a, 123, d.GetAllocator());
EXPECT_EQ(123, Pointer("/1").Get(a)->GetInt());
}
}
TEST(Pointer, LessThan) {
static const struct {
const char *str;
bool valid;
} pointers[] = {
{ "/a/b", true },
{ "/a", true },
{ "/d/1", true },
{ "/d/2/z", true },
{ "/d/2/3", true },
{ "/d/2", true },
{ "/a/c", true },
{ "/e/f~g", false },
{ "/d/2/zz", true },
{ "/d/1", true },
{ "/d/2/z", true },
{ "/e/f~~g", false },
{ "/e/f~0g", true },
{ "/e/f~1g", true },
{ "/e/f.g", true },
{ "", true }
};
static const char *ordered_pointers[] = {
"",
"/a",
"/a/b",
"/a/c",
"/d/1",
"/d/1",
"/d/2",
"/e/f.g",
"/e/f~1g",
"/e/f~0g",
"/d/2/3",
"/d/2/z",
"/d/2/z",
"/d/2/zz",
NULL, // was invalid "/e/f~g"
NULL // was invalid "/e/f~~g"
};
typedef MemoryPoolAllocator<> AllocatorType;
typedef GenericPointer<Value, AllocatorType> PointerType;
typedef std::multimap<PointerType, size_t> PointerMap;
PointerMap map;
PointerMap::iterator it;
AllocatorType allocator;
size_t i;
EXPECT_EQ(sizeof(pointers) / sizeof(pointers[0]),
sizeof(ordered_pointers) / sizeof(ordered_pointers[0]));
for (i = 0; i < sizeof(pointers) / sizeof(pointers[0]); ++i) {
it = map.insert(PointerMap::value_type(PointerType(pointers[i].str, &allocator), i));
if (!it->first.IsValid()) {
EXPECT_EQ(++it, map.end());
}
}
for (i = 0, it = map.begin(); it != map.end(); ++it, ++i) {
EXPECT_TRUE(it->second < sizeof(pointers) / sizeof(pointers[0]));
EXPECT_EQ(it->first.IsValid(), pointers[it->second].valid);
EXPECT_TRUE(i < sizeof(ordered_pointers) / sizeof(ordered_pointers[0]));
EXPECT_EQ(it->first.IsValid(), !!ordered_pointers[i]);
if (it->first.IsValid()) {
std::stringstream ss;
OStreamWrapper os(ss);
EXPECT_TRUE(it->first.Stringify(os));
EXPECT_EQ(ss.str(), pointers[it->second].str);
EXPECT_EQ(ss.str(), ordered_pointers[i]);
}
}
}
// https://github.com/Tencent/rapidjson/issues/483
namespace myjson {
class MyAllocator
@@ -1522,3 +1717,14 @@ TEST(Pointer, Issue483) {
value.SetString(mystr.c_str(), static_cast<SizeType>(mystr.length()), document.GetAllocator());
myjson::Pointer(path.c_str()).Set(document, value, document.GetAllocator());
}
TEST(Pointer, Issue1899) {
typedef GenericPointer<Value, MemoryPoolAllocator<> > PointerType;
PointerType p;
PointerType q = p.Append("foo");
EXPECT_TRUE(PointerType("/foo") == q);
q = q.Append(1234);
EXPECT_TRUE(PointerType("/foo/1234") == q);
q = q.Append("");
EXPECT_TRUE(PointerType("/foo/1234/") == q);
}
+171 -1
View File
@@ -1,6 +1,6 @@
// 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.
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
//
// 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
@@ -18,6 +18,11 @@
#include "rapidjson/stringbuffer.h"
#include "rapidjson/filewritestream.h"
#ifdef __clang__
RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(c++98-compat)
#endif
using namespace rapidjson;
static const char kJson[] = "{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3,-1],\"u64\":1234567890123456789,\"i64\":-1234567890123456789}";
@@ -162,6 +167,7 @@ TEST(PrettyWriter, OStreamWrapper) {
TEST(PrettyWriter, FileWriteStream) {
char filename[L_tmpnam];
FILE* fp = TempFile(filename);
ASSERT_TRUE(fp!=NULL);
char buffer[16];
FileWriteStream os(fp, buffer, sizeof(buffer));
PrettyWriter<FileWriteStream> writer(os);
@@ -201,3 +207,167 @@ TEST(PrettyWriter, RawValue) {
"}",
buffer.GetString());
}
TEST(PrettyWriter, InvalidEventSequence) {
// {]
{
StringBuffer buffer;
PrettyWriter<StringBuffer> writer(buffer);
writer.StartObject();
EXPECT_THROW(writer.EndArray(), AssertException);
EXPECT_FALSE(writer.IsComplete());
}
// [}
{
StringBuffer buffer;
PrettyWriter<StringBuffer> writer(buffer);
writer.StartArray();
EXPECT_THROW(writer.EndObject(), AssertException);
EXPECT_FALSE(writer.IsComplete());
}
// { 1:
{
StringBuffer buffer;
PrettyWriter<StringBuffer> writer(buffer);
writer.StartObject();
EXPECT_THROW(writer.Int(1), AssertException);
EXPECT_FALSE(writer.IsComplete());
}
// { 'a' }
{
StringBuffer buffer;
PrettyWriter<StringBuffer> writer(buffer);
writer.StartObject();
writer.Key("a");
EXPECT_THROW(writer.EndObject(), AssertException);
EXPECT_FALSE(writer.IsComplete());
}
// { 'a':'b','c' }
{
StringBuffer buffer;
PrettyWriter<StringBuffer> writer(buffer);
writer.StartObject();
writer.Key("a");
writer.String("b");
writer.Key("c");
EXPECT_THROW(writer.EndObject(), AssertException);
EXPECT_FALSE(writer.IsComplete());
}
}
TEST(PrettyWriter, NaN) {
double nan = std::numeric_limits<double>::quiet_NaN();
EXPECT_TRUE(internal::Double(nan).IsNan());
StringBuffer buffer;
{
PrettyWriter<StringBuffer> writer(buffer);
EXPECT_FALSE(writer.Double(nan));
}
{
PrettyWriter<StringBuffer, UTF8<>, UTF8<>, CrtAllocator, kWriteNanAndInfFlag> writer(buffer);
EXPECT_TRUE(writer.Double(nan));
EXPECT_STREQ("NaN", buffer.GetString());
}
GenericStringBuffer<UTF16<> > buffer2;
PrettyWriter<GenericStringBuffer<UTF16<> > > writer2(buffer2);
EXPECT_FALSE(writer2.Double(nan));
}
TEST(PrettyWriter, Inf) {
double inf = std::numeric_limits<double>::infinity();
EXPECT_TRUE(internal::Double(inf).IsInf());
StringBuffer buffer;
{
PrettyWriter<StringBuffer> writer(buffer);
EXPECT_FALSE(writer.Double(inf));
}
{
PrettyWriter<StringBuffer> writer(buffer);
EXPECT_FALSE(writer.Double(-inf));
}
{
PrettyWriter<StringBuffer, UTF8<>, UTF8<>, CrtAllocator, kWriteNanAndInfFlag> writer(buffer);
EXPECT_TRUE(writer.Double(inf));
}
{
PrettyWriter<StringBuffer, UTF8<>, UTF8<>, CrtAllocator, kWriteNanAndInfFlag> writer(buffer);
EXPECT_TRUE(writer.Double(-inf));
}
EXPECT_STREQ("Infinity-Infinity", buffer.GetString());
}
TEST(PrettyWriter, Issue_889) {
char buf[100] = "Hello";
StringBuffer buffer;
PrettyWriter<StringBuffer> writer(buffer);
writer.StartArray();
writer.String(buf);
writer.EndArray();
EXPECT_STREQ("[\n \"Hello\"\n]", buffer.GetString());
EXPECT_TRUE(writer.IsComplete()); \
}
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
static PrettyWriter<StringBuffer> WriterGen(StringBuffer &target) {
PrettyWriter<StringBuffer> writer(target);
writer.StartObject();
writer.Key("a");
writer.Int(1);
return writer;
}
TEST(PrettyWriter, MoveCtor) {
StringBuffer buffer;
PrettyWriter<StringBuffer> writer(WriterGen(buffer));
writer.EndObject();
EXPECT_TRUE(writer.IsComplete());
EXPECT_STREQ(
"{\n"
" \"a\": 1\n"
"}",
buffer.GetString());
}
#endif
TEST(PrettyWriter, Issue_1336) {
#define T(meth, val, expected) \
{ \
StringBuffer buffer; \
PrettyWriter<StringBuffer> writer(buffer); \
writer.meth(val); \
\
EXPECT_STREQ(expected, buffer.GetString()); \
EXPECT_TRUE(writer.IsComplete()); \
}
T(Bool, false, "false");
T(Bool, true, "true");
T(Int, 0, "0");
T(Uint, 0, "0");
T(Int64, 0, "0");
T(Uint64, 0, "0");
T(Double, 0, "0.0");
T(String, "Hello", "\"Hello\"");
#undef T
StringBuffer buffer;
PrettyWriter<StringBuffer> writer(buffer);
writer.Null();
EXPECT_STREQ("null", buffer.GetString());
EXPECT_TRUE(writer.IsComplete());
}
#ifdef __clang__
RAPIDJSON_DIAG_POP
#endif
+615 -89
View File
@@ -1,6 +1,6 @@
// 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.
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
//
// 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
@@ -207,6 +207,7 @@ static void TestParseDouble() {
TEST_DOUBLE(fullPrecision, "0.0", 0.0);
TEST_DOUBLE(fullPrecision, "-0.0", -0.0); // For checking issue #289
TEST_DOUBLE(fullPrecision, "0e100", 0.0); // For checking issue #1249
TEST_DOUBLE(fullPrecision, "1.0", 1.0);
TEST_DOUBLE(fullPrecision, "-1.0", -1.0);
TEST_DOUBLE(fullPrecision, "1.5", 1.5);
@@ -233,7 +234,7 @@ static void TestParseDouble() {
TEST_DOUBLE(fullPrecision, "1e-10000", 0.0); // must underflow
TEST_DOUBLE(fullPrecision, "18446744073709551616", 18446744073709551616.0); // 2^64 (max of uint64_t + 1, force to use double)
TEST_DOUBLE(fullPrecision, "-9223372036854775809", -9223372036854775809.0); // -2^63 - 1(min of int64_t + 1, force to use double)
TEST_DOUBLE(fullPrecision, "0.9868011474609375", 0.9868011474609375); // https://github.com/miloyip/rapidjson/issues/120
TEST_DOUBLE(fullPrecision, "0.9868011474609375", 0.9868011474609375); // https://github.com/Tencent/rapidjson/issues/120
TEST_DOUBLE(fullPrecision, "123e34", 123e34); // Fast Path Cases In Disguise
TEST_DOUBLE(fullPrecision, "45913141877270640000.0", 45913141877270640000.0);
TEST_DOUBLE(fullPrecision, "2.2250738585072011e-308", 2.2250738585072011e-308); // http://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/
@@ -242,16 +243,18 @@ static void TestParseDouble() {
TEST_DOUBLE(fullPrecision, "1e-214748363", 0.0); // Maximum supported negative exponent
TEST_DOUBLE(fullPrecision, "1e-214748364", 0.0);
TEST_DOUBLE(fullPrecision, "1e-21474836311", 0.0);
TEST_DOUBLE(fullPrecision, "1.00000000001e-2147483638", 0.0);
TEST_DOUBLE(fullPrecision, "0.017976931348623157e+310", 1.7976931348623157e+308); // Max double in another form
TEST_DOUBLE(fullPrecision, "128.74836467836484838364836483643636483648e-336", 0.0); // Issue #1251
// Since
// abs((2^-1022 - 2^-1074) - 2.2250738585072012e-308) = 3.109754131239141401123495768877590405345064751974375599... 10^-324
// abs((2^-1022) - 2.2250738585072012e-308) = 1.830902327173324040642192159804623318305533274168872044... 10 ^ -324
// abs((2^-1022 - 2^-1074) - 2.2250738585072012e-308) = 3.109754131239141401123495768877590405345064751974375599... x 10^-324
// abs((2^-1022) - 2.2250738585072012e-308) = 1.830902327173324040642192159804623318305533274168872044... x 10 ^ -324
// So 2.2250738585072012e-308 should round to 2^-1022 = 2.2250738585072014e-308
TEST_DOUBLE(fullPrecision, "2.2250738585072012e-308", 2.2250738585072014e-308); // http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/
// More closer to normal/subnormal boundary
// boundary = 2^-1022 - 2^-1075 = 2.225073858507201136057409796709131975934819546351645648... 10^-308
// boundary = 2^-1022 - 2^-1075 = 2.225073858507201136057409796709131975934819546351645648... x 10^-308
TEST_DOUBLE(fullPrecision, "2.22507385850720113605740979670913197593481954635164564e-308", 2.2250738585072009e-308);
TEST_DOUBLE(fullPrecision, "2.22507385850720113605740979670913197593481954635164565e-308", 2.2250738585072014e-308);
@@ -376,6 +379,208 @@ static void TestParseDouble() {
d = d.Value() * 0.5;
}
}
// Issue 1249
TEST_DOUBLE(fullPrecision, "0e100", 0.0);
// Issue 1251
TEST_DOUBLE(fullPrecision, "128.74836467836484838364836483643636483648e-336", 0.0);
// Issue 1256
TEST_DOUBLE(fullPrecision,
"6223372036854775296.1701512723685473547372536854755293372036854685477"
"529752233737201701512337200972013723685473123372036872036854236854737"
"247372368372367752975258547752975254729752547372368737201701512354737"
"83723677529752585477247372368372368547354737253685475529752",
6223372036854775808.0);
#if 0
// Test (length + exponent) overflow
TEST_DOUBLE(fullPrecision, "0e+2147483647", 0.0);
TEST_DOUBLE(fullPrecision, "0e-2147483648", 0.0);
TEST_DOUBLE(fullPrecision, "1e-2147483648", 0.0);
TEST_DOUBLE(fullPrecision, "0e+9223372036854775807", 0.0);
TEST_DOUBLE(fullPrecision, "0e-9223372036854775808", 0.0);
#endif
if (fullPrecision)
{
TEST_DOUBLE(fullPrecision, "1e-325", 0.0);
TEST_DOUBLE(fullPrecision, "1e-324", 0.0);
TEST_DOUBLE(fullPrecision, "2e-324", 0.0);
TEST_DOUBLE(fullPrecision, "2.4703282292062327e-324", 0.0);
TEST_DOUBLE(fullPrecision, "2.4703282292062328e-324", 5e-324);
TEST_DOUBLE(fullPrecision, "2.48e-324",5e-324);
TEST_DOUBLE(fullPrecision, "2.5e-324", 5e-324);
// Slightly above max-normal
TEST_DOUBLE(fullPrecision, "1.7976931348623158e+308", 1.7976931348623158e+308);
TEST_DOUBLE(fullPrecision,
"17976931348623157081452742373170435679807056752584499659891747680315726"
"07800285387605895586327668781715404589535143824642343213268894641827684"
"67546703537516986049910576551282076245490090389328944075868508455133942"
"30458323690322294816580855933212334827479782620414472316873817718091929"
"9881250404026184124858368",
(std::numeric_limits<double>::max)());
TEST_DOUBLE(fullPrecision,
"243546080556034731077856379609316893158278902575447060151047"
"212703405344938119816206067372775299130836050315842578309818"
"316450894337978612745889730079163798234256495613858256849283"
"467066859489192118352020514036083287319232435355752493038825"
"828481044358810649108367633313557305310641892225870327827273"
"41408256.000000",
2.4354608055603473e+307);
// 9007199254740991 * 2^971 (max normal)
TEST_DOUBLE(fullPrecision,
"1.797693134862315708145274237317043567980705675258449965989174768031572607800285"
"38760589558632766878171540458953514382464234321326889464182768467546703537516986"
"04991057655128207624549009038932894407586850845513394230458323690322294816580855"
"9332123348274797826204144723168738177180919299881250404026184124858368e+308",
1.797693134862315708e+308 // 0x1.fffffffffffffp1023
);
#if 0
// TODO:
// Should work at least in full-precision mode...
TEST_DOUBLE(fullPrecision,
"0.00000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000024703282292062327208828439643411068618252"
"9901307162382212792841250337753635104375932649918180817996189"
"8982823477228588654633283551779698981993873980053909390631503"
"5659515570226392290858392449105184435931802849936536152500319"
"3704576782492193656236698636584807570015857692699037063119282"
"7955855133292783433840935197801553124659726357957462276646527"
"2827220056374006485499977096599470454020828166226237857393450"
"7363390079677619305775067401763246736009689513405355374585166"
"6113422376667860416215968046191446729184030053005753084904876"
"5391711386591646239524912623653881879636239373280423891018672"
"3484976682350898633885879256283027559956575244555072551893136"
"9083625477918694866799496832404970582102851318545139621383772"
"2826145437693412532098591327667236328125",
0.0);
#endif
// 9007199254740991 * 2^-1074 = (2^53 - 1) * 2^-1074
TEST_DOUBLE(fullPrecision,
"4.450147717014402272114819593418263951869639092703291296046852219449644444042153"
"89103305904781627017582829831782607924221374017287738918929105531441481564124348"
"67599762821265346585071045737627442980259622449029037796981144446145705102663115"
"10031828794952795966823603998647925096578034214163701381261333311989876551545144"
"03152612538132666529513060001849177663286607555958373922409899478075565940981010"
"21612198814605258742579179000071675999344145086087205681577915435923018910334964"
"86942061405218289243144579760516365090360651414037721744226256159024466852576737"
"24464300755133324500796506867194913776884780053099639677097589658441378944337966"
"21993967316936280457084866613206797017728916080020698679408551343728867675409720"
"757232455434770912461317493580281734466552734375e-308",
4.450147717014402272e-308 // 0x1.fffffffffffffp-1022
);
// 9007199254740990 * 2^-1074
TEST_DOUBLE(fullPrecision,
"4.450147717014401778049173752171719775300846224481918930987049605124880018456471"
"39035755177760751831052846195619008686241717547743167145836439860405887584484471"
"19639655002484083577939142623582164522087943959208000909794783876158397872163051"
"22622675229968408654350206725478309956546318828765627255022767720818849892988457"
"26333908582101604036318532842699932130356061901518261174396928478121372742040102"
"17446565569357687263889031732270082446958029584739170416643195242132750803227473"
"16608838720742955671061336566907126801014814608027120593609275183716632624844904"
"31985250929886016737037234388448352929102742708402644340627409931664203093081360"
"70794835812045179006047003875039546061891526346421705014598610179523165038319441"
"51446491086954182492263498716056346893310546875e-308",
4.450147717014401778e-308 // 0x1.ffffffffffffep-1022
);
// half way between the two numbers above.
// round to nearest even.
TEST_DOUBLE(fullPrecision,
"4.450147717014402025081996672794991863585242658592605113516950912287262231249312"
"64069530541271189424317838013700808305231545782515453032382772695923684574304409"
"93619708911874715081505094180604803751173783204118519353387964161152051487413083"
"16327252012460602310586905362063117526562176521464664318142050516404363222266800"
"64743260560117135282915796422274554896821334728738317548403413978098469341510556"
"19529382191981473003234105366170879223151087335413188049110555339027884856781219"
"01775450062980622457102958163711745945687733011032421168917765671370549738710820"
"78224775842509670618916870627821633352993761380751142008862499795052791018709663"
"46394401564490729731565935244123171539810221213221201847003580761626016356864581"
"1358486831521563686919762403704226016998291015625e-308",
4.450147717014401778e-308 // 0x1.ffffffffffffep-1022
);
TEST_DOUBLE(fullPrecision,
"4.450147717014402025081996672794991863585242658592605113516950912287262231249312"
"64069530541271189424317838013700808305231545782515453032382772695923684574304409"
"93619708911874715081505094180604803751173783204118519353387964161152051487413083"
"16327252012460602310586905362063117526562176521464664318142050516404363222266800"
"64743260560117135282915796422274554896821334728738317548403413978098469341510556"
"19529382191981473003234105366170879223151087335413188049110555339027884856781219"
"01775450062980622457102958163711745945687733011032421168917765671370549738710820"
"78224775842509670618916870627821633352993761380751142008862499795052791018709663"
"46394401564490729731565935244123171539810221213221201847003580761626016356864581"
"13584868315215636869197624037042260169982910156250000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000e-308",
4.450147717014401778e-308 // 0x1.ffffffffffffep-1022
);
#if 0
// ... round up
// TODO:
// Should work at least in full-precision mode...
TEST_DOUBLE(fullPrecision,
"4.450147717014402025081996672794991863585242658592605113516950912287262231249312"
"64069530541271189424317838013700808305231545782515453032382772695923684574304409"
"93619708911874715081505094180604803751173783204118519353387964161152051487413083"
"16327252012460602310586905362063117526562176521464664318142050516404363222266800"
"64743260560117135282915796422274554896821334728738317548403413978098469341510556"
"19529382191981473003234105366170879223151087335413188049110555339027884856781219"
"01775450062980622457102958163711745945687733011032421168917765671370549738710820"
"78224775842509670618916870627821633352993761380751142008862499795052791018709663"
"46394401564490729731565935244123171539810221213221201847003580761626016356864581"
"13584868315215636869197624037042260169982910156250000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000001e-308",
4.450147717014402272e-308 // 0x1.fffffffffffffp-1022
);
#endif
// ... round down
TEST_DOUBLE(fullPrecision,
"4.450147717014402025081996672794991863585242658592605113516950912287262231249312"
"64069530541271189424317838013700808305231545782515453032382772695923684574304409"
"93619708911874715081505094180604803751173783204118519353387964161152051487413083"
"16327252012460602310586905362063117526562176521464664318142050516404363222266800"
"64743260560117135282915796422274554896821334728738317548403413978098469341510556"
"19529382191981473003234105366170879223151087335413188049110555339027884856781219"
"01775450062980622457102958163711745945687733011032421168917765671370549738710820"
"78224775842509670618916870627821633352993761380751142008862499795052791018709663"
"46394401564490729731565935244123171539810221213221201847003580761626016356864581"
"13584868315215636869197624037042260169982910156249999999999999999999999999999999"
"99999999999999999999999999999999999999999999999999999999999999999999999999999999"
"99999999999999999999999999999999999999999999999999999999999999999999999999999999e-308",
4.450147717014401778e-308 // 0x1.ffffffffffffep-1022
);
// Slightly below half way between max-normal and infinity.
// Should round down.
TEST_DOUBLE(fullPrecision,
"1.797693134862315807937289714053034150799341327100378269361737789804449682927647"
"50946649017977587207096330286416692887910946555547851940402630657488671505820681"
"90890200070838367627385484581771153176447573027006985557136695962284291481986083"
"49364752927190741684443655107043427115596995080930428801779041744977919999999999"
"99999999999999999999999999999999999999999999999999999999999999999999999999999999"
"99999999999999999999999999999999999999999999999999999999999999999999999999999999"
"99999999999999999999999999999999999999999999999999999999999999999999999999999999"
"99999999999999999999999999999999999999999999999999999999999999999999999999999999"
"99999999999999999999999999999999999999999999999999999999999999999999999999999999"
"99999999999999999999999999999999999999999999999999999999999999999999999999999999"
"99999999999999999999999999999999999999999999999999999999999999999999999999999999"
"99999999999999999999999999999999999999999999999999999999999999999999999999999999"
"99999999999999999999999999999999999999999999999999999999999999999999999999999999"
"99999999999999999999999999999999999999999999999999999999999999999999999999999999"
"99999999999999999999999999999999999999999999999999999999999999999999999999999999"
"99999999999999999999999999999999999999999999999999999999999999999999999999999999e+308",
1.797693134862315708e+308 // 0x1.fffffffffffffp1023
);
}
#undef TEST_DOUBLE
}
@@ -415,21 +620,23 @@ TEST(Reader, ParseNumber_NormalPrecisionError) {
uint64_t bias1 = e.ToBias();
uint64_t bias2 = a.ToBias();
double ulp = static_cast<double>(bias1 >= bias2 ? bias1 - bias2 : bias2 - bias1);
ulpMax = std::max(ulpMax, ulp);
ulpMax = (std::max)(ulpMax, ulp);
ulpSum += ulp;
}
printf("ULP Average = %g, Max = %g \n", ulpSum / count, ulpMax);
}
TEST(Reader, ParseNumber_Error) {
template<bool fullPrecision>
static void TestParseNumberError() {
#define TEST_NUMBER_ERROR(errorCode, str, errorOffset, streamPos) \
{ \
char buffer[1001]; \
char buffer[2048]; \
ASSERT_LT(std::strlen(str), 2048u); \
sprintf(buffer, "%s", str); \
InsituStringStream s(buffer); \
BaseReaderHandler<> h; \
Reader reader; \
EXPECT_FALSE(reader.Parse(s, h)); \
EXPECT_FALSE(reader.Parse<fullPrecision ? kParseFullPrecisionFlag : 0>(s, h)); \
EXPECT_EQ(errorCode, reader.GetParseErrorCode());\
EXPECT_EQ(errorOffset, reader.GetErrorOffset());\
EXPECT_EQ(streamPos, s.Tell());\
@@ -442,21 +649,109 @@ TEST(Reader, ParseNumber_Error) {
for (int i = 1; i < 310; i++)
n1e309[i] = '0';
n1e309[310] = '\0';
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, n1e309, 0, 309);
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, n1e309, 0u, 310u);
}
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "1e309", 0, 5);
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "1e309", 0u, 5u);
// Miss fraction part in number.
TEST_NUMBER_ERROR(kParseErrorNumberMissFraction, "1.", 2, 2);
TEST_NUMBER_ERROR(kParseErrorNumberMissFraction, "1.a", 2, 2);
TEST_NUMBER_ERROR(kParseErrorNumberMissFraction, "1.", 2u, 2u);
TEST_NUMBER_ERROR(kParseErrorNumberMissFraction, "1.a", 2u, 2u);
// Miss exponent in number.
TEST_NUMBER_ERROR(kParseErrorNumberMissExponent, "1e", 2, 2);
TEST_NUMBER_ERROR(kParseErrorNumberMissExponent, "1e_", 2, 2);
TEST_NUMBER_ERROR(kParseErrorNumberMissExponent, "1e", 2u, 2u);
TEST_NUMBER_ERROR(kParseErrorNumberMissExponent, "1e_", 2u, 2u);
// Issue 849
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "1.8e308", 0u, 7u);
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "5e308", 0u, 5u);
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "1e309", 0u, 5u);
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "1.0e310", 0u, 7u);
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "1.00e310", 0u, 8u);
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "-1.8e308", 0u, 8u);
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "-1e309", 0u, 6u);
// Issue 1253
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "2e308", 0u, 5u);
// Issue 1259
TEST_NUMBER_ERROR(kParseErrorNumberTooBig,
"88474320368547737236837236775298547354737253685475547552933720368546854775297525"
"29337203685468547770151233720097201372368547312337203687203685423685123372036872"
"03685473724737236837236775297525854775297525472975254737236873720170151235473783"
"7236737247372368772473723683723456789012E66", 0u, 283u);
#if 0
// Test (length + exponent) overflow
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "1e+2147483647", 0u, 13u);
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "1e+9223372036854775807", 0u, 22u);
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "1e+10000", 0u, 8u);
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "1e+50000", 0u, 8u);
#endif
// 9007199254740992 * 2^971 ("infinity")
TEST_NUMBER_ERROR(kParseErrorNumberTooBig,
"1.797693134862315907729305190789024733617976978942306572734300811577326758055009"
"63132708477322407536021120113879871393357658789768814416622492847430639474124377"
"76789342486548527630221960124609411945308295208500576883815068234246288147391311"
"0540827237163350510684586298239947245938479716304835356329624224137216e+308", 0u, 315u);
// TODO:
// These tests (currently) fail in normal-precision mode
if (fullPrecision)
{
// Half way between max-normal and infinity
// Should round to infinity in nearest-even mode.
TEST_NUMBER_ERROR(kParseErrorNumberTooBig,
"1.797693134862315807937289714053034150799341327100378269361737789804449682927647"
"50946649017977587207096330286416692887910946555547851940402630657488671505820681"
"90890200070838367627385484581771153176447573027006985557136695962284291481986083"
"49364752927190741684443655107043427115596995080930428801779041744977920000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000e+308", 0u, 1125u);
// ...round up
TEST_NUMBER_ERROR(kParseErrorNumberTooBig,
"1.797693134862315807937289714053034150799341327100378269361737789804449682927647"
"50946649017977587207096330286416692887910946555547851940402630657488671505820681"
"90890200070838367627385484581771153176447573027006985557136695962284291481986083"
"49364752927190741684443655107043427115596995080930428801779041744977920000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000001e+308", 0u, 1205u);
}
TEST_NUMBER_ERROR(kParseErrorNumberTooBig,
"10000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000000001", 0u, 310u);
#undef TEST_NUMBER_ERROR
}
TEST(Reader, ParseNumberError_NormalPrecisionDouble) {
TestParseNumberError<false>();
}
TEST(Reader, ParseNumberError_FullPrecisionDouble) {
TestParseNumberError<true>();
}
template <typename Encoding>
struct ParseStringHandler : BaseReaderHandler<Encoding, ParseStringHandler<Encoding> > {
ParseStringHandler() : str_(0), length_(0), copy_() {}
@@ -636,21 +931,24 @@ TEST(Reader, ParseString_Error) {
}
// Invalid escape character in string.
TEST_STRING_ERROR(kParseErrorStringEscapeInvalid, "[\"\\a\"]", 2, 3);
TEST_STRING_ERROR(kParseErrorStringEscapeInvalid, "[\"\\a\"]", 2u, 3u);
// Incorrect hex digit after \\u escape in string.
TEST_STRING_ERROR(kParseErrorStringUnicodeEscapeInvalidHex, "[\"\\uABCG\"]", 2, 7);
TEST_STRING_ERROR(kParseErrorStringUnicodeEscapeInvalidHex, "[\"\\uABCG\"]", 2u, 7u);
// Quotation in \\u escape in string (Issue #288)
TEST_STRING_ERROR(kParseErrorStringUnicodeEscapeInvalidHex, "[\"\\uaaa\"]", 2, 7);
TEST_STRING_ERROR(kParseErrorStringUnicodeEscapeInvalidHex, "[\"\\uD800\\uFFF\"]", 2, 13);
TEST_STRING_ERROR(kParseErrorStringUnicodeEscapeInvalidHex, "[\"\\uaaa\"]", 2u, 7u);
TEST_STRING_ERROR(kParseErrorStringUnicodeEscapeInvalidHex, "[\"\\uD800\\uFFF\"]", 2u, 13u);
// The surrogate pair in string is invalid.
TEST_STRING_ERROR(kParseErrorStringUnicodeSurrogateInvalid, "[\"\\uD800X\"]", 2, 8);
TEST_STRING_ERROR(kParseErrorStringUnicodeSurrogateInvalid, "[\"\\uD800\\uFFFF\"]", 2, 14);
TEST_STRING_ERROR(kParseErrorStringUnicodeSurrogateInvalid, "[\"\\uD800X\"]", 2u, 8u);
TEST_STRING_ERROR(kParseErrorStringUnicodeSurrogateInvalid, "[\"\\uD800\\uFFFF\"]", 2u, 14u);
// Single low surrogate pair in string is invalid.
TEST_STRING_ERROR(kParseErrorStringUnicodeSurrogateInvalid, "[\"\\udc4d\"]", 2u, 8u);
// Missing a closing quotation mark in string.
TEST_STRING_ERROR(kParseErrorStringMissQuotationMark, "[\"Test]", 7, 7);
TEST_STRING_ERROR(kParseErrorStringMissQuotationMark, "[\"Test]", 7u, 7u);
// http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
@@ -673,7 +971,7 @@ TEST(Reader, ParseString_Error) {
char e[] = { '[', '\"', 0, ' ', '\"', ']', '\0' };
for (unsigned c = 0xC0u; c <= 0xFFu; c++) {
e[2] = static_cast<char>(c);
int streamPos;
unsigned streamPos;
if (c <= 0xC1u)
streamPos = 3; // 0xC0 - 0xC1
else if (c <= 0xDFu)
@@ -684,7 +982,7 @@ TEST(Reader, ParseString_Error) {
streamPos = 6; // 0xF0 - 0xF4
else
streamPos = 3; // 0xF5 - 0xFF
TEST_STRING_ERROR(kParseErrorStringInvalidEncoding, e, 2, streamPos);
TEST_STRING_ERROR(kParseErrorStringInvalidEncoding, e, 2u, streamPos);
}
}
@@ -725,6 +1023,8 @@ TEST(Reader, ParseString_Error) {
// Malform ASCII sequence
TEST_STRINGENCODING_ERROR(ASCII<>, UTF8<>, char, ARRAY('[', '\"', char(0x80u), '\"', ']', '\0'));
TEST_STRINGENCODING_ERROR(ASCII<>, UTF8<>, char, ARRAY('[', '\"', char(0x01u), '\"', ']', '\0'));
TEST_STRINGENCODING_ERROR(ASCII<>, UTF8<>, char, ARRAY('[', '\"', char(0x1Cu), '\"', ']', '\0'));
#undef ARRAY
#undef TEST_STRINGARRAY_ERROR
@@ -765,7 +1065,7 @@ TEST(Reader, ParseArray) {
TEST(Reader, ParseArray_Error) {
#define TEST_ARRAY_ERROR(errorCode, str, errorOffset) \
{ \
int streamPos = errorOffset; \
unsigned streamPos = errorOffset; \
char buffer[1001]; \
strncpy(buffer, str, 1000); \
InsituStringStream s(buffer); \
@@ -778,13 +1078,13 @@ TEST(Reader, ParseArray_Error) {
}
// Missing a comma or ']' after an array element.
TEST_ARRAY_ERROR(kParseErrorArrayMissCommaOrSquareBracket, "[1", 2);
TEST_ARRAY_ERROR(kParseErrorArrayMissCommaOrSquareBracket, "[1}", 2);
TEST_ARRAY_ERROR(kParseErrorArrayMissCommaOrSquareBracket, "[1 2]", 3);
TEST_ARRAY_ERROR(kParseErrorArrayMissCommaOrSquareBracket, "[1", 2u);
TEST_ARRAY_ERROR(kParseErrorArrayMissCommaOrSquareBracket, "[1}", 2u);
TEST_ARRAY_ERROR(kParseErrorArrayMissCommaOrSquareBracket, "[1 2]", 3u);
// Array cannot have a trailing comma (without kParseTrailingCommasFlag);
// a value must follow a comma
TEST_ARRAY_ERROR(kParseErrorValueInvalid, "[1,]", 3);
TEST_ARRAY_ERROR(kParseErrorValueInvalid, "[1,]", 3u);
#undef TEST_ARRAY_ERROR
}
@@ -933,7 +1233,7 @@ TEST(Reader, ParseInsituIterative_MultipleRoot) {
#define TEST_ERROR(errorCode, str, errorOffset) \
{ \
int streamPos = errorOffset; \
unsigned streamPos = errorOffset; \
char buffer[1001]; \
strncpy(buffer, str, 1000); \
InsituStringStream s(buffer); \
@@ -947,48 +1247,48 @@ TEST(Reader, ParseInsituIterative_MultipleRoot) {
TEST(Reader, ParseDocument_Error) {
// The document is empty.
TEST_ERROR(kParseErrorDocumentEmpty, "", 0);
TEST_ERROR(kParseErrorDocumentEmpty, " ", 1);
TEST_ERROR(kParseErrorDocumentEmpty, " \n", 2);
TEST_ERROR(kParseErrorDocumentEmpty, "", 0u);
TEST_ERROR(kParseErrorDocumentEmpty, " ", 1u);
TEST_ERROR(kParseErrorDocumentEmpty, " \n", 2u);
// The document root must not follow by other values.
TEST_ERROR(kParseErrorDocumentRootNotSingular, "[] 0", 3);
TEST_ERROR(kParseErrorDocumentRootNotSingular, "{} 0", 3);
TEST_ERROR(kParseErrorDocumentRootNotSingular, "null []", 5);
TEST_ERROR(kParseErrorDocumentRootNotSingular, "0 {}", 2);
TEST_ERROR(kParseErrorDocumentRootNotSingular, "[] 0", 3u);
TEST_ERROR(kParseErrorDocumentRootNotSingular, "{} 0", 3u);
TEST_ERROR(kParseErrorDocumentRootNotSingular, "null []", 5u);
TEST_ERROR(kParseErrorDocumentRootNotSingular, "0 {}", 2u);
}
TEST(Reader, ParseValue_Error) {
// Invalid value.
TEST_ERROR(kParseErrorValueInvalid, "nulL", 3);
TEST_ERROR(kParseErrorValueInvalid, "truE", 3);
TEST_ERROR(kParseErrorValueInvalid, "falsE", 4);
TEST_ERROR(kParseErrorValueInvalid, "a]", 0);
TEST_ERROR(kParseErrorValueInvalid, ".1", 0);
TEST_ERROR(kParseErrorValueInvalid, "nulL", 3u);
TEST_ERROR(kParseErrorValueInvalid, "truE", 3u);
TEST_ERROR(kParseErrorValueInvalid, "falsE", 4u);
TEST_ERROR(kParseErrorValueInvalid, "a]", 0u);
TEST_ERROR(kParseErrorValueInvalid, ".1", 0u);
}
TEST(Reader, ParseObject_Error) {
// Missing a name for object member.
TEST_ERROR(kParseErrorObjectMissName, "{1}", 1);
TEST_ERROR(kParseErrorObjectMissName, "{:1}", 1);
TEST_ERROR(kParseErrorObjectMissName, "{null:1}", 1);
TEST_ERROR(kParseErrorObjectMissName, "{true:1}", 1);
TEST_ERROR(kParseErrorObjectMissName, "{false:1}", 1);
TEST_ERROR(kParseErrorObjectMissName, "{1:1}", 1);
TEST_ERROR(kParseErrorObjectMissName, "{[]:1}", 1);
TEST_ERROR(kParseErrorObjectMissName, "{{}:1}", 1);
TEST_ERROR(kParseErrorObjectMissName, "{xyz:1}", 1);
TEST_ERROR(kParseErrorObjectMissName, "{1}", 1u);
TEST_ERROR(kParseErrorObjectMissName, "{:1}", 1u);
TEST_ERROR(kParseErrorObjectMissName, "{null:1}", 1u);
TEST_ERROR(kParseErrorObjectMissName, "{true:1}", 1u);
TEST_ERROR(kParseErrorObjectMissName, "{false:1}", 1u);
TEST_ERROR(kParseErrorObjectMissName, "{1:1}", 1u);
TEST_ERROR(kParseErrorObjectMissName, "{[]:1}", 1u);
TEST_ERROR(kParseErrorObjectMissName, "{{}:1}", 1u);
TEST_ERROR(kParseErrorObjectMissName, "{xyz:1}", 1u);
// Missing a colon after a name of object member.
TEST_ERROR(kParseErrorObjectMissColon, "{\"a\" 1}", 5);
TEST_ERROR(kParseErrorObjectMissColon, "{\"a\",1}", 4);
TEST_ERROR(kParseErrorObjectMissColon, "{\"a\" 1}", 5u);
TEST_ERROR(kParseErrorObjectMissColon, "{\"a\",1}", 4u);
// Must be a comma or '}' after an object member
TEST_ERROR(kParseErrorObjectMissCommaOrCurlyBracket, "{\"a\":1]", 6);
TEST_ERROR(kParseErrorObjectMissCommaOrCurlyBracket, "{\"a\":1]", 6u);
// Object cannot have a trailing comma (without kParseTrailingCommasFlag);
// an object member name must follow a comma
TEST_ERROR(kParseErrorObjectMissName, "{\"a\":1,}", 7);
TEST_ERROR(kParseErrorObjectMissName, "{\"a\":1,}", 7u);
// This tests that MemoryStream is checking the length in Peek().
{
@@ -1092,6 +1392,36 @@ private:
std::istream& is_;
};
class WIStreamWrapper {
public:
typedef wchar_t Ch;
WIStreamWrapper(std::wistream& is) : is_(is) {}
Ch Peek() const {
unsigned c = is_.peek();
return c == std::char_traits<wchar_t>::eof() ? Ch('\0') : static_cast<Ch>(c);
}
Ch Take() {
unsigned c = is_.get();
return c == std::char_traits<wchar_t>::eof() ? Ch('\0') : static_cast<Ch>(c);
}
size_t Tell() const { return static_cast<size_t>(is_.tellg()); }
Ch* PutBegin() { assert(false); return 0; }
void Put(Ch) { assert(false); }
void Flush() { assert(false); }
size_t PutEnd(Ch*) { assert(false); return 0; }
private:
WIStreamWrapper(const WIStreamWrapper&);
WIStreamWrapper& operator=(const WIStreamWrapper&);
std::wistream& is_;
};
TEST(Reader, Parse_IStreamWrapper_StringStream) {
const char* json = "[1,2,3,4]";
@@ -1108,7 +1438,7 @@ TEST(Reader, Parse_IStreamWrapper_StringStream) {
#define TESTERRORHANDLING(text, errorCode, offset)\
{\
int streamPos = offset; \
unsigned streamPos = offset; \
StringStream json(text); \
BaseReaderHandler<> handler; \
Reader reader; \
@@ -1157,22 +1487,22 @@ template<typename Encoding = UTF8<> >
struct IterativeParsingReaderHandler {
typedef typename Encoding::Ch Ch;
const static int LOG_NULL = -1;
const static int LOG_BOOL = -2;
const static int LOG_INT = -3;
const static int LOG_UINT = -4;
const static int LOG_INT64 = -5;
const static int LOG_UINT64 = -6;
const static int LOG_DOUBLE = -7;
const static int LOG_STRING = -8;
const static int LOG_STARTOBJECT = -9;
const static int LOG_KEY = -10;
const static int LOG_ENDOBJECT = -11;
const static int LOG_STARTARRAY = -12;
const static int LOG_ENDARRAY = -13;
const static uint32_t LOG_NULL = 0x10000000;
const static uint32_t LOG_BOOL = 0x20000000;
const static uint32_t LOG_INT = 0x30000000;
const static uint32_t LOG_UINT = 0x40000000;
const static uint32_t LOG_INT64 = 0x50000000;
const static uint32_t LOG_UINT64 = 0x60000000;
const static uint32_t LOG_DOUBLE = 0x70000000;
const static uint32_t LOG_STRING = 0x80000000;
const static uint32_t LOG_STARTOBJECT = 0x90000000;
const static uint32_t LOG_KEY = 0xA0000000;
const static uint32_t LOG_ENDOBJECT = 0xB0000000;
const static uint32_t LOG_STARTARRAY = 0xC0000000;
const static uint32_t LOG_ENDARRAY = 0xD0000000;
const static size_t LogCapacity = 256;
int Logs[LogCapacity];
uint32_t Logs[LogCapacity];
size_t LogCount;
IterativeParsingReaderHandler() : LogCount(0) {
@@ -1202,8 +1532,8 @@ struct IterativeParsingReaderHandler {
bool EndObject(SizeType c) {
RAPIDJSON_ASSERT(LogCount < LogCapacity);
Logs[LogCount++] = LOG_ENDOBJECT;
Logs[LogCount++] = static_cast<int>(c);
RAPIDJSON_ASSERT((static_cast<uint32_t>(c) & 0xF0000000) == 0);
Logs[LogCount++] = LOG_ENDOBJECT | static_cast<uint32_t>(c);
return true;
}
@@ -1211,8 +1541,8 @@ struct IterativeParsingReaderHandler {
bool EndArray(SizeType c) {
RAPIDJSON_ASSERT(LogCount < LogCapacity);
Logs[LogCount++] = LOG_ENDARRAY;
Logs[LogCount++] = static_cast<int>(c);
RAPIDJSON_ASSERT((static_cast<uint32_t>(c) & 0xF0000000) == 0);
Logs[LogCount++] = LOG_ENDARRAY | static_cast<uint32_t>(c);
return true;
}
};
@@ -1228,7 +1558,7 @@ TEST(Reader, IterativeParsing_General) {
EXPECT_FALSE(r.IsError());
EXPECT_FALSE(reader.HasParseError());
int e[] = {
uint32_t e[] = {
handler.LOG_STARTARRAY,
handler.LOG_INT,
handler.LOG_STARTOBJECT,
@@ -1236,14 +1566,14 @@ TEST(Reader, IterativeParsing_General) {
handler.LOG_STARTARRAY,
handler.LOG_INT,
handler.LOG_INT,
handler.LOG_ENDARRAY, 2,
handler.LOG_ENDOBJECT, 1,
handler.LOG_ENDARRAY | 2,
handler.LOG_ENDOBJECT | 1,
handler.LOG_NULL,
handler.LOG_BOOL,
handler.LOG_BOOL,
handler.LOG_STRING,
handler.LOG_DOUBLE,
handler.LOG_ENDARRAY, 7
handler.LOG_ENDARRAY | 7
};
EXPECT_EQ(sizeof(e) / sizeof(int), handler.LogCount);
@@ -1265,20 +1595,20 @@ TEST(Reader, IterativeParsing_Count) {
EXPECT_FALSE(r.IsError());
EXPECT_FALSE(reader.HasParseError());
int e[] = {
uint32_t e[] = {
handler.LOG_STARTARRAY,
handler.LOG_STARTOBJECT,
handler.LOG_ENDOBJECT, 0,
handler.LOG_ENDOBJECT | 0,
handler.LOG_STARTOBJECT,
handler.LOG_KEY,
handler.LOG_INT,
handler.LOG_ENDOBJECT, 1,
handler.LOG_ENDOBJECT | 1,
handler.LOG_STARTARRAY,
handler.LOG_INT,
handler.LOG_ENDARRAY, 1,
handler.LOG_ENDARRAY | 1,
handler.LOG_STARTARRAY,
handler.LOG_ENDARRAY, 0,
handler.LOG_ENDARRAY, 4
handler.LOG_ENDARRAY | 0,
handler.LOG_ENDARRAY | 4
};
EXPECT_EQ(sizeof(e) / sizeof(int), handler.LogCount);
@@ -1289,6 +1619,51 @@ TEST(Reader, IterativeParsing_Count) {
}
}
TEST(Reader, IterativePullParsing_General) {
{
IterativeParsingReaderHandler<> handler;
uint32_t e[] = {
handler.LOG_STARTARRAY,
handler.LOG_INT,
handler.LOG_STARTOBJECT,
handler.LOG_KEY,
handler.LOG_STARTARRAY,
handler.LOG_INT,
handler.LOG_INT,
handler.LOG_ENDARRAY | 2,
handler.LOG_ENDOBJECT | 1,
handler.LOG_NULL,
handler.LOG_BOOL,
handler.LOG_BOOL,
handler.LOG_STRING,
handler.LOG_DOUBLE,
handler.LOG_ENDARRAY | 7
};
StringStream is("[1, {\"k\": [1, 2]}, null, false, true, \"string\", 1.2]");
Reader reader;
reader.IterativeParseInit();
while (!reader.IterativeParseComplete()) {
size_t oldLogCount = handler.LogCount;
EXPECT_TRUE(oldLogCount < sizeof(e) / sizeof(int)) << "overrun";
EXPECT_TRUE(reader.IterativeParseNext<kParseDefaultFlags>(is, handler)) << "parse fail";
EXPECT_EQ(handler.LogCount, oldLogCount + 1) << "handler should be invoked exactly once each time";
EXPECT_EQ(e[oldLogCount], handler.Logs[oldLogCount]) << "wrong event returned";
}
EXPECT_FALSE(reader.HasParseError());
EXPECT_EQ(sizeof(e) / sizeof(int), handler.LogCount) << "handler invoked wrong number of times";
// The handler should not be invoked when the JSON has been fully read, but it should not fail
size_t oldLogCount = handler.LogCount;
EXPECT_TRUE(reader.IterativeParseNext<kParseDefaultFlags>(is, handler)) << "parse-next past complete is allowed";
EXPECT_EQ(handler.LogCount, oldLogCount) << "parse-next past complete should not invoke handler";
EXPECT_FALSE(reader.HasParseError()) << "parse-next past complete should not generate parse error";
}
}
// Test iterative parsing on kParseErrorTermination.
struct HandlerTerminateAtStartObject : public IterativeParsingReaderHandler<> {
bool StartObject() { return false; }
@@ -1633,6 +2008,129 @@ TEST(Reader, NumbersAsStrings) {
Reader reader;
EXPECT_TRUE(reader.Parse<kParseNumbersAsStringsFlag>(s, h));
}
{
char n1e319[321]; // '1' followed by 319 '0'
n1e319[0] = '1';
for (int i = 1; i < 320; i++)
n1e319[i] = '0';
n1e319[320] = '\0';
StringStream s(n1e319);
NumbersAsStringsHandler h(n1e319);
Reader reader;
EXPECT_TRUE(reader.Parse<kParseNumbersAsStringsFlag>(s, h));
}
}
struct NumbersAsStringsHandlerWChar_t {
bool Null() { return true; }
bool Bool(bool) { return true; }
bool Int(int) { return true; }
bool Uint(unsigned) { return true; }
bool Int64(int64_t) { return true; }
bool Uint64(uint64_t) { return true; }
bool Double(double) { return true; }
// 'str' is not null-terminated
bool RawNumber(const wchar_t* str, SizeType length, bool) {
EXPECT_TRUE(str != 0);
EXPECT_TRUE(expected_len_ == length);
EXPECT_TRUE(wcsncmp(str, expected_, length) == 0);
return true;
}
bool String(const wchar_t*, SizeType, bool) { return true; }
bool StartObject() { return true; }
bool Key(const wchar_t*, SizeType, bool) { return true; }
bool EndObject(SizeType) { return true; }
bool StartArray() { return true; }
bool EndArray(SizeType) { return true; }
NumbersAsStringsHandlerWChar_t(const wchar_t* expected)
: expected_(expected)
, expected_len_(wcslen(expected)) {}
const wchar_t* expected_;
size_t expected_len_;
};
TEST(Reader, NumbersAsStringsWChar_t) {
{
const wchar_t* json = L"{ \"pi\": 3.1416 } ";
GenericStringStream<UTF16<> > s(json);
NumbersAsStringsHandlerWChar_t h(L"3.1416");
GenericReader<UTF16<>, UTF16<> > reader;
EXPECT_TRUE(reader.Parse<kParseNumbersAsStringsFlag>(s, h));
}
{
wchar_t* json = StrDup(L"{ \"pi\": 3.1416 } ");
GenericInsituStringStream<UTF16<> > s(json);
NumbersAsStringsHandlerWChar_t h(L"3.1416");
GenericReader<UTF16<>, UTF16<> > reader;
EXPECT_TRUE(reader.Parse<kParseInsituFlag | kParseNumbersAsStringsFlag>(s, h));
free(json);
}
{
const wchar_t* json = L"{ \"gigabyte\": 1.0e9 } ";
GenericStringStream<UTF16<> > s(json);
NumbersAsStringsHandlerWChar_t h(L"1.0e9");
GenericReader<UTF16<>, UTF16<> > reader;
EXPECT_TRUE(reader.Parse<kParseNumbersAsStringsFlag>(s, h));
}
{
wchar_t* json = StrDup(L"{ \"gigabyte\": 1.0e9 } ");
GenericInsituStringStream<UTF16<> > s(json);
NumbersAsStringsHandlerWChar_t h(L"1.0e9");
GenericReader<UTF16<>, UTF16<> > reader;
EXPECT_TRUE(reader.Parse<kParseInsituFlag | kParseNumbersAsStringsFlag>(s, h));
free(json);
}
{
const wchar_t* json = L"{ \"pi\": 314.159e-2 } ";
GenericStringStream<UTF16<> > s(json);
NumbersAsStringsHandlerWChar_t h(L"314.159e-2");
GenericReader<UTF16<>, UTF16<> > reader;
EXPECT_TRUE(reader.Parse<kParseNumbersAsStringsFlag>(s, h));
}
{
wchar_t* json = StrDup(L"{ \"gigabyte\": 314.159e-2 } ");
GenericInsituStringStream<UTF16<> > s(json);
NumbersAsStringsHandlerWChar_t h(L"314.159e-2");
GenericReader<UTF16<>, UTF16<> > reader;
EXPECT_TRUE(reader.Parse<kParseInsituFlag | kParseNumbersAsStringsFlag>(s, h));
free(json);
}
{
const wchar_t* json = L"{ \"negative\": -1.54321 } ";
GenericStringStream<UTF16<> > s(json);
NumbersAsStringsHandlerWChar_t h(L"-1.54321");
GenericReader<UTF16<>, UTF16<> > reader;
EXPECT_TRUE(reader.Parse<kParseNumbersAsStringsFlag>(s, h));
}
{
wchar_t* json = StrDup(L"{ \"negative\": -1.54321 } ");
GenericInsituStringStream<UTF16<> > s(json);
NumbersAsStringsHandlerWChar_t h(L"-1.54321");
GenericReader<UTF16<>, UTF16<> > reader;
EXPECT_TRUE(reader.Parse<kParseInsituFlag | kParseNumbersAsStringsFlag>(s, h));
free(json);
}
{
const wchar_t* json = L"{ \"pi\": 314.159e-2 } ";
std::wstringstream ss(json);
WIStreamWrapper s(ss);
NumbersAsStringsHandlerWChar_t h(L"314.159e-2");
GenericReader<UTF16<>, UTF16<> > reader;
EXPECT_TRUE(reader.Parse<kParseNumbersAsStringsFlag>(s, h));
}
{
wchar_t n1e319[321]; // '1' followed by 319 '0'
n1e319[0] = L'1';
for(int i = 1; i < 320; i++)
n1e319[i] = L'0';
n1e319[320] = L'\0';
GenericStringStream<UTF16<> > s(n1e319);
NumbersAsStringsHandlerWChar_t h(n1e319);
GenericReader<UTF16<>, UTF16<> > reader;
EXPECT_TRUE(reader.Parse<kParseNumbersAsStringsFlag>(s, h));
}
}
template <unsigned extraFlags>
@@ -1811,7 +2309,7 @@ TEST(Reader, ParseNanAndInfinity) {
}
#define TEST_NAN_INF_ERROR(errorCode, str, errorOffset) \
{ \
int streamPos = errorOffset; \
unsigned streamPos = errorOffset; \
char buffer[1001]; \
strncpy(buffer, str, 1000); \
InsituStringStream s(buffer); \
@@ -1832,13 +2330,41 @@ TEST(Reader, ParseNanAndInfinity) {
TEST_NAN_INF("Infinity", inf);
TEST_NAN_INF("-Inf", -inf);
TEST_NAN_INF("-Infinity", -inf);
TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "nan", 1);
TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "-nan", 1);
TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "NAN", 1);
TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "-Infinty", 6);
TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "NInf", 1u);
TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "NaInf", 2u);
TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "INan", 1u);
TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "InNan", 2u);
TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "nan", 1u);
TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "-nan", 1u);
TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "NAN", 1u);
TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "-Infinty", 6u);
#undef TEST_NAN_INF_ERROR
#undef TEST_NAN_INF
}
TEST(Reader, EscapedApostrophe) {
const char json[] = " { \"foo\": \"bar\\'buzz\" } ";
BaseReaderHandler<> h;
{
StringStream s(json);
Reader reader;
ParseResult r = reader.Parse<kParseNoFlags>(s, h);
EXPECT_TRUE(reader.HasParseError());
EXPECT_EQ(kParseErrorStringEscapeInvalid, r.Code());
EXPECT_EQ(14u, r.Offset());
}
{
StringStream s(json);
Reader reader;
ParseResult r = reader.Parse<kParseEscapedApostropheFlag>(s, h);
EXPECT_FALSE(reader.HasParseError());
EXPECT_EQ(kParseErrorNone, r.Code());
EXPECT_EQ(0u, r.Offset());
}
}
RAPIDJSON_DIAG_POP
+336 -289
View File
@@ -1,6 +1,6 @@
// 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.
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
//
// 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
@@ -20,523 +20,569 @@ using namespace rapidjson::internal;
TEST(Regex, Single) {
Regex re("a");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("a"));
EXPECT_FALSE(re.Match(""));
EXPECT_FALSE(re.Match("b"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("a"));
EXPECT_FALSE(rs.Match(""));
EXPECT_FALSE(rs.Match("b"));
}
TEST(Regex, Concatenation) {
Regex re("abc");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("abc"));
EXPECT_FALSE(re.Match(""));
EXPECT_FALSE(re.Match("a"));
EXPECT_FALSE(re.Match("b"));
EXPECT_FALSE(re.Match("ab"));
EXPECT_FALSE(re.Match("abcd"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("abc"));
EXPECT_FALSE(rs.Match(""));
EXPECT_FALSE(rs.Match("a"));
EXPECT_FALSE(rs.Match("b"));
EXPECT_FALSE(rs.Match("ab"));
EXPECT_FALSE(rs.Match("abcd"));
}
TEST(Regex, Alternation1) {
Regex re("abab|abbb");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("abab"));
EXPECT_TRUE(re.Match("abbb"));
EXPECT_FALSE(re.Match(""));
EXPECT_FALSE(re.Match("ab"));
EXPECT_FALSE(re.Match("ababa"));
EXPECT_FALSE(re.Match("abb"));
EXPECT_FALSE(re.Match("abbbb"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("abab"));
EXPECT_TRUE(rs.Match("abbb"));
EXPECT_FALSE(rs.Match(""));
EXPECT_FALSE(rs.Match("ab"));
EXPECT_FALSE(rs.Match("ababa"));
EXPECT_FALSE(rs.Match("abb"));
EXPECT_FALSE(rs.Match("abbbb"));
}
TEST(Regex, Alternation2) {
Regex re("a|b|c");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("a"));
EXPECT_TRUE(re.Match("b"));
EXPECT_TRUE(re.Match("c"));
EXPECT_FALSE(re.Match(""));
EXPECT_FALSE(re.Match("aa"));
EXPECT_FALSE(re.Match("ab"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("a"));
EXPECT_TRUE(rs.Match("b"));
EXPECT_TRUE(rs.Match("c"));
EXPECT_FALSE(rs.Match(""));
EXPECT_FALSE(rs.Match("aa"));
EXPECT_FALSE(rs.Match("ab"));
}
TEST(Regex, Parenthesis1) {
Regex re("(ab)c");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("abc"));
EXPECT_FALSE(re.Match(""));
EXPECT_FALSE(re.Match("a"));
EXPECT_FALSE(re.Match("b"));
EXPECT_FALSE(re.Match("ab"));
EXPECT_FALSE(re.Match("abcd"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("abc"));
EXPECT_FALSE(rs.Match(""));
EXPECT_FALSE(rs.Match("a"));
EXPECT_FALSE(rs.Match("b"));
EXPECT_FALSE(rs.Match("ab"));
EXPECT_FALSE(rs.Match("abcd"));
}
TEST(Regex, Parenthesis2) {
Regex re("a(bc)");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("abc"));
EXPECT_FALSE(re.Match(""));
EXPECT_FALSE(re.Match("a"));
EXPECT_FALSE(re.Match("b"));
EXPECT_FALSE(re.Match("ab"));
EXPECT_FALSE(re.Match("abcd"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("abc"));
EXPECT_FALSE(rs.Match(""));
EXPECT_FALSE(rs.Match("a"));
EXPECT_FALSE(rs.Match("b"));
EXPECT_FALSE(rs.Match("ab"));
EXPECT_FALSE(rs.Match("abcd"));
}
TEST(Regex, Parenthesis3) {
Regex re("(a|b)(c|d)");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("ac"));
EXPECT_TRUE(re.Match("ad"));
EXPECT_TRUE(re.Match("bc"));
EXPECT_TRUE(re.Match("bd"));
EXPECT_FALSE(re.Match(""));
EXPECT_FALSE(re.Match("ab"));
EXPECT_FALSE(re.Match("cd"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("ac"));
EXPECT_TRUE(rs.Match("ad"));
EXPECT_TRUE(rs.Match("bc"));
EXPECT_TRUE(rs.Match("bd"));
EXPECT_FALSE(rs.Match(""));
EXPECT_FALSE(rs.Match("ab"));
EXPECT_FALSE(rs.Match("cd"));
}
TEST(Regex, ZeroOrOne1) {
Regex re("a?");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match(""));
EXPECT_TRUE(re.Match("a"));
EXPECT_FALSE(re.Match("aa"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match(""));
EXPECT_TRUE(rs.Match("a"));
EXPECT_FALSE(rs.Match("aa"));
}
TEST(Regex, ZeroOrOne2) {
Regex re("a?b");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("b"));
EXPECT_TRUE(re.Match("ab"));
EXPECT_FALSE(re.Match("a"));
EXPECT_FALSE(re.Match("aa"));
EXPECT_FALSE(re.Match("bb"));
EXPECT_FALSE(re.Match("ba"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("b"));
EXPECT_TRUE(rs.Match("ab"));
EXPECT_FALSE(rs.Match("a"));
EXPECT_FALSE(rs.Match("aa"));
EXPECT_FALSE(rs.Match("bb"));
EXPECT_FALSE(rs.Match("ba"));
}
TEST(Regex, ZeroOrOne3) {
Regex re("ab?");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("a"));
EXPECT_TRUE(re.Match("ab"));
EXPECT_FALSE(re.Match("b"));
EXPECT_FALSE(re.Match("aa"));
EXPECT_FALSE(re.Match("bb"));
EXPECT_FALSE(re.Match("ba"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("a"));
EXPECT_TRUE(rs.Match("ab"));
EXPECT_FALSE(rs.Match("b"));
EXPECT_FALSE(rs.Match("aa"));
EXPECT_FALSE(rs.Match("bb"));
EXPECT_FALSE(rs.Match("ba"));
}
TEST(Regex, ZeroOrOne4) {
Regex re("a?b?");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match(""));
EXPECT_TRUE(re.Match("a"));
EXPECT_TRUE(re.Match("b"));
EXPECT_TRUE(re.Match("ab"));
EXPECT_FALSE(re.Match("aa"));
EXPECT_FALSE(re.Match("bb"));
EXPECT_FALSE(re.Match("ba"));
EXPECT_FALSE(re.Match("abc"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match(""));
EXPECT_TRUE(rs.Match("a"));
EXPECT_TRUE(rs.Match("b"));
EXPECT_TRUE(rs.Match("ab"));
EXPECT_FALSE(rs.Match("aa"));
EXPECT_FALSE(rs.Match("bb"));
EXPECT_FALSE(rs.Match("ba"));
EXPECT_FALSE(rs.Match("abc"));
}
TEST(Regex, ZeroOrOne5) {
Regex re("a(ab)?b");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("ab"));
EXPECT_TRUE(re.Match("aabb"));
EXPECT_FALSE(re.Match("aab"));
EXPECT_FALSE(re.Match("abb"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("ab"));
EXPECT_TRUE(rs.Match("aabb"));
EXPECT_FALSE(rs.Match("aab"));
EXPECT_FALSE(rs.Match("abb"));
}
TEST(Regex, ZeroOrMore1) {
Regex re("a*");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match(""));
EXPECT_TRUE(re.Match("a"));
EXPECT_TRUE(re.Match("aa"));
EXPECT_FALSE(re.Match("b"));
EXPECT_FALSE(re.Match("ab"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match(""));
EXPECT_TRUE(rs.Match("a"));
EXPECT_TRUE(rs.Match("aa"));
EXPECT_FALSE(rs.Match("b"));
EXPECT_FALSE(rs.Match("ab"));
}
TEST(Regex, ZeroOrMore2) {
Regex re("a*b");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("b"));
EXPECT_TRUE(re.Match("ab"));
EXPECT_TRUE(re.Match("aab"));
EXPECT_FALSE(re.Match(""));
EXPECT_FALSE(re.Match("bb"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("b"));
EXPECT_TRUE(rs.Match("ab"));
EXPECT_TRUE(rs.Match("aab"));
EXPECT_FALSE(rs.Match(""));
EXPECT_FALSE(rs.Match("bb"));
}
TEST(Regex, ZeroOrMore3) {
Regex re("a*b*");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match(""));
EXPECT_TRUE(re.Match("a"));
EXPECT_TRUE(re.Match("aa"));
EXPECT_TRUE(re.Match("b"));
EXPECT_TRUE(re.Match("bb"));
EXPECT_TRUE(re.Match("ab"));
EXPECT_TRUE(re.Match("aabb"));
EXPECT_FALSE(re.Match("ba"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match(""));
EXPECT_TRUE(rs.Match("a"));
EXPECT_TRUE(rs.Match("aa"));
EXPECT_TRUE(rs.Match("b"));
EXPECT_TRUE(rs.Match("bb"));
EXPECT_TRUE(rs.Match("ab"));
EXPECT_TRUE(rs.Match("aabb"));
EXPECT_FALSE(rs.Match("ba"));
}
TEST(Regex, ZeroOrMore4) {
Regex re("a(ab)*b");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("ab"));
EXPECT_TRUE(re.Match("aabb"));
EXPECT_TRUE(re.Match("aababb"));
EXPECT_FALSE(re.Match(""));
EXPECT_FALSE(re.Match("aa"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("ab"));
EXPECT_TRUE(rs.Match("aabb"));
EXPECT_TRUE(rs.Match("aababb"));
EXPECT_FALSE(rs.Match(""));
EXPECT_FALSE(rs.Match("aa"));
}
TEST(Regex, OneOrMore1) {
Regex re("a+");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("a"));
EXPECT_TRUE(re.Match("aa"));
EXPECT_FALSE(re.Match(""));
EXPECT_FALSE(re.Match("b"));
EXPECT_FALSE(re.Match("ab"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("a"));
EXPECT_TRUE(rs.Match("aa"));
EXPECT_FALSE(rs.Match(""));
EXPECT_FALSE(rs.Match("b"));
EXPECT_FALSE(rs.Match("ab"));
}
TEST(Regex, OneOrMore2) {
Regex re("a+b");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("ab"));
EXPECT_TRUE(re.Match("aab"));
EXPECT_FALSE(re.Match(""));
EXPECT_FALSE(re.Match("b"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("ab"));
EXPECT_TRUE(rs.Match("aab"));
EXPECT_FALSE(rs.Match(""));
EXPECT_FALSE(rs.Match("b"));
}
TEST(Regex, OneOrMore3) {
Regex re("a+b+");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("ab"));
EXPECT_TRUE(re.Match("aab"));
EXPECT_TRUE(re.Match("abb"));
EXPECT_TRUE(re.Match("aabb"));
EXPECT_FALSE(re.Match(""));
EXPECT_FALSE(re.Match("b"));
EXPECT_FALSE(re.Match("ba"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("ab"));
EXPECT_TRUE(rs.Match("aab"));
EXPECT_TRUE(rs.Match("abb"));
EXPECT_TRUE(rs.Match("aabb"));
EXPECT_FALSE(rs.Match(""));
EXPECT_FALSE(rs.Match("b"));
EXPECT_FALSE(rs.Match("ba"));
}
TEST(Regex, OneOrMore4) {
Regex re("a(ab)+b");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("aabb"));
EXPECT_TRUE(re.Match("aababb"));
EXPECT_FALSE(re.Match(""));
EXPECT_FALSE(re.Match("ab"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("aabb"));
EXPECT_TRUE(rs.Match("aababb"));
EXPECT_FALSE(rs.Match(""));
EXPECT_FALSE(rs.Match("ab"));
}
TEST(Regex, QuantifierExact1) {
Regex re("ab{3}c");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("abbbc"));
EXPECT_FALSE(re.Match("ac"));
EXPECT_FALSE(re.Match("abc"));
EXPECT_FALSE(re.Match("abbc"));
EXPECT_FALSE(re.Match("abbbbc"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("abbbc"));
EXPECT_FALSE(rs.Match("ac"));
EXPECT_FALSE(rs.Match("abc"));
EXPECT_FALSE(rs.Match("abbc"));
EXPECT_FALSE(rs.Match("abbbbc"));
}
TEST(Regex, QuantifierExact2) {
Regex re("a(bc){3}d");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("abcbcbcd"));
EXPECT_FALSE(re.Match("ad"));
EXPECT_FALSE(re.Match("abcd"));
EXPECT_FALSE(re.Match("abcbcd"));
EXPECT_FALSE(re.Match("abcbcbcbcd"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("abcbcbcd"));
EXPECT_FALSE(rs.Match("ad"));
EXPECT_FALSE(rs.Match("abcd"));
EXPECT_FALSE(rs.Match("abcbcd"));
EXPECT_FALSE(rs.Match("abcbcbcbcd"));
}
TEST(Regex, QuantifierExact3) {
Regex re("a(b|c){3}d");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("abbbd"));
EXPECT_TRUE(re.Match("acccd"));
EXPECT_TRUE(re.Match("abcbd"));
EXPECT_FALSE(re.Match("ad"));
EXPECT_FALSE(re.Match("abbd"));
EXPECT_FALSE(re.Match("accccd"));
EXPECT_FALSE(re.Match("abbbbd"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("abbbd"));
EXPECT_TRUE(rs.Match("acccd"));
EXPECT_TRUE(rs.Match("abcbd"));
EXPECT_FALSE(rs.Match("ad"));
EXPECT_FALSE(rs.Match("abbd"));
EXPECT_FALSE(rs.Match("accccd"));
EXPECT_FALSE(rs.Match("abbbbd"));
}
TEST(Regex, QuantifierMin1) {
Regex re("ab{3,}c");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("abbbc"));
EXPECT_TRUE(re.Match("abbbbc"));
EXPECT_TRUE(re.Match("abbbbbc"));
EXPECT_FALSE(re.Match("ac"));
EXPECT_FALSE(re.Match("abc"));
EXPECT_FALSE(re.Match("abbc"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("abbbc"));
EXPECT_TRUE(rs.Match("abbbbc"));
EXPECT_TRUE(rs.Match("abbbbbc"));
EXPECT_FALSE(rs.Match("ac"));
EXPECT_FALSE(rs.Match("abc"));
EXPECT_FALSE(rs.Match("abbc"));
}
TEST(Regex, QuantifierMin2) {
Regex re("a(bc){3,}d");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("abcbcbcd"));
EXPECT_TRUE(re.Match("abcbcbcbcd"));
EXPECT_FALSE(re.Match("ad"));
EXPECT_FALSE(re.Match("abcd"));
EXPECT_FALSE(re.Match("abcbcd"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("abcbcbcd"));
EXPECT_TRUE(rs.Match("abcbcbcbcd"));
EXPECT_FALSE(rs.Match("ad"));
EXPECT_FALSE(rs.Match("abcd"));
EXPECT_FALSE(rs.Match("abcbcd"));
}
TEST(Regex, QuantifierMin3) {
Regex re("a(b|c){3,}d");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("abbbd"));
EXPECT_TRUE(re.Match("acccd"));
EXPECT_TRUE(re.Match("abcbd"));
EXPECT_TRUE(re.Match("accccd"));
EXPECT_TRUE(re.Match("abbbbd"));
EXPECT_FALSE(re.Match("ad"));
EXPECT_FALSE(re.Match("abbd"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("abbbd"));
EXPECT_TRUE(rs.Match("acccd"));
EXPECT_TRUE(rs.Match("abcbd"));
EXPECT_TRUE(rs.Match("accccd"));
EXPECT_TRUE(rs.Match("abbbbd"));
EXPECT_FALSE(rs.Match("ad"));
EXPECT_FALSE(rs.Match("abbd"));
}
TEST(Regex, QuantifierMinMax1) {
Regex re("ab{3,5}c");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("abbbc"));
EXPECT_TRUE(re.Match("abbbbc"));
EXPECT_TRUE(re.Match("abbbbbc"));
EXPECT_FALSE(re.Match("ac"));
EXPECT_FALSE(re.Match("abc"));
EXPECT_FALSE(re.Match("abbc"));
EXPECT_FALSE(re.Match("abbbbbbc"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("abbbc"));
EXPECT_TRUE(rs.Match("abbbbc"));
EXPECT_TRUE(rs.Match("abbbbbc"));
EXPECT_FALSE(rs.Match("ac"));
EXPECT_FALSE(rs.Match("abc"));
EXPECT_FALSE(rs.Match("abbc"));
EXPECT_FALSE(rs.Match("abbbbbbc"));
}
TEST(Regex, QuantifierMinMax2) {
Regex re("a(bc){3,5}d");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("abcbcbcd"));
EXPECT_TRUE(re.Match("abcbcbcbcd"));
EXPECT_TRUE(re.Match("abcbcbcbcbcd"));
EXPECT_FALSE(re.Match("ad"));
EXPECT_FALSE(re.Match("abcd"));
EXPECT_FALSE(re.Match("abcbcd"));
EXPECT_FALSE(re.Match("abcbcbcbcbcbcd"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("abcbcbcd"));
EXPECT_TRUE(rs.Match("abcbcbcbcd"));
EXPECT_TRUE(rs.Match("abcbcbcbcbcd"));
EXPECT_FALSE(rs.Match("ad"));
EXPECT_FALSE(rs.Match("abcd"));
EXPECT_FALSE(rs.Match("abcbcd"));
EXPECT_FALSE(rs.Match("abcbcbcbcbcbcd"));
}
TEST(Regex, QuantifierMinMax3) {
Regex re("a(b|c){3,5}d");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("abbbd"));
EXPECT_TRUE(re.Match("acccd"));
EXPECT_TRUE(re.Match("abcbd"));
EXPECT_TRUE(re.Match("accccd"));
EXPECT_TRUE(re.Match("abbbbd"));
EXPECT_TRUE(re.Match("acccccd"));
EXPECT_TRUE(re.Match("abbbbbd"));
EXPECT_FALSE(re.Match("ad"));
EXPECT_FALSE(re.Match("abbd"));
EXPECT_FALSE(re.Match("accccccd"));
EXPECT_FALSE(re.Match("abbbbbbd"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("abbbd"));
EXPECT_TRUE(rs.Match("acccd"));
EXPECT_TRUE(rs.Match("abcbd"));
EXPECT_TRUE(rs.Match("accccd"));
EXPECT_TRUE(rs.Match("abbbbd"));
EXPECT_TRUE(rs.Match("acccccd"));
EXPECT_TRUE(rs.Match("abbbbbd"));
EXPECT_FALSE(rs.Match("ad"));
EXPECT_FALSE(rs.Match("abbd"));
EXPECT_FALSE(rs.Match("accccccd"));
EXPECT_FALSE(rs.Match("abbbbbbd"));
}
// Issue538
TEST(Regex, QuantifierMinMax4) {
Regex re("a(b|c){0,3}d");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("ad"));
EXPECT_TRUE(re.Match("abd"));
EXPECT_TRUE(re.Match("acd"));
EXPECT_TRUE(re.Match("abbd"));
EXPECT_TRUE(re.Match("accd"));
EXPECT_TRUE(re.Match("abcd"));
EXPECT_TRUE(re.Match("abbbd"));
EXPECT_TRUE(re.Match("acccd"));
EXPECT_FALSE(re.Match("abbbbd"));
EXPECT_FALSE(re.Match("add"));
EXPECT_FALSE(re.Match("accccd"));
EXPECT_FALSE(re.Match("abcbcd"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("ad"));
EXPECT_TRUE(rs.Match("abd"));
EXPECT_TRUE(rs.Match("acd"));
EXPECT_TRUE(rs.Match("abbd"));
EXPECT_TRUE(rs.Match("accd"));
EXPECT_TRUE(rs.Match("abcd"));
EXPECT_TRUE(rs.Match("abbbd"));
EXPECT_TRUE(rs.Match("acccd"));
EXPECT_FALSE(rs.Match("abbbbd"));
EXPECT_FALSE(rs.Match("add"));
EXPECT_FALSE(rs.Match("accccd"));
EXPECT_FALSE(rs.Match("abcbcd"));
}
// Issue538
TEST(Regex, QuantifierMinMax5) {
Regex re("a(b|c){0,}d");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("ad"));
EXPECT_TRUE(re.Match("abd"));
EXPECT_TRUE(re.Match("acd"));
EXPECT_TRUE(re.Match("abbd"));
EXPECT_TRUE(re.Match("accd"));
EXPECT_TRUE(re.Match("abcd"));
EXPECT_TRUE(re.Match("abbbd"));
EXPECT_TRUE(re.Match("acccd"));
EXPECT_TRUE(re.Match("abbbbd"));
EXPECT_TRUE(re.Match("accccd"));
EXPECT_TRUE(re.Match("abcbcd"));
EXPECT_FALSE(re.Match("add"));
EXPECT_FALSE(re.Match("aad"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("ad"));
EXPECT_TRUE(rs.Match("abd"));
EXPECT_TRUE(rs.Match("acd"));
EXPECT_TRUE(rs.Match("abbd"));
EXPECT_TRUE(rs.Match("accd"));
EXPECT_TRUE(rs.Match("abcd"));
EXPECT_TRUE(rs.Match("abbbd"));
EXPECT_TRUE(rs.Match("acccd"));
EXPECT_TRUE(rs.Match("abbbbd"));
EXPECT_TRUE(rs.Match("accccd"));
EXPECT_TRUE(rs.Match("abcbcd"));
EXPECT_FALSE(rs.Match("add"));
EXPECT_FALSE(rs.Match("aad"));
}
#define EURO "\xE2\x82\xAC" // "\xE2\x82\xAC" is UTF-8 sequence of Euro sign U+20AC
#define EURO "\xE2\x82\xAC" // "\xE2\x82\xAC" is UTF-8 rsquence of Euro sign U+20AC
TEST(Regex, Unicode) {
Regex re("a" EURO "+b");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("a" EURO "b"));
EXPECT_TRUE(re.Match("a" EURO EURO "b"));
EXPECT_FALSE(re.Match("a?b"));
EXPECT_FALSE(re.Match("a" EURO "\xAC" "b")); // unaware of UTF-8 will match
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("a" EURO "b"));
EXPECT_TRUE(rs.Match("a" EURO EURO "b"));
EXPECT_FALSE(rs.Match("a?b"));
EXPECT_FALSE(rs.Match("a" EURO "\xAC" "b")); // unaware of UTF-8 will match
}
TEST(Regex, AnyCharacter) {
Regex re(".");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("a"));
EXPECT_TRUE(re.Match("b"));
EXPECT_TRUE(re.Match(EURO));
EXPECT_FALSE(re.Match(""));
EXPECT_FALSE(re.Match("aa"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("a"));
EXPECT_TRUE(rs.Match("b"));
EXPECT_TRUE(rs.Match(EURO));
EXPECT_FALSE(rs.Match(""));
EXPECT_FALSE(rs.Match("aa"));
}
TEST(Regex, CharacterRange1) {
Regex re("[abc]");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("a"));
EXPECT_TRUE(re.Match("b"));
EXPECT_TRUE(re.Match("c"));
EXPECT_FALSE(re.Match(""));
EXPECT_FALSE(re.Match("`"));
EXPECT_FALSE(re.Match("d"));
EXPECT_FALSE(re.Match("aa"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("a"));
EXPECT_TRUE(rs.Match("b"));
EXPECT_TRUE(rs.Match("c"));
EXPECT_FALSE(rs.Match(""));
EXPECT_FALSE(rs.Match("`"));
EXPECT_FALSE(rs.Match("d"));
EXPECT_FALSE(rs.Match("aa"));
}
TEST(Regex, CharacterRange2) {
Regex re("[^abc]");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("`"));
EXPECT_TRUE(re.Match("d"));
EXPECT_FALSE(re.Match("a"));
EXPECT_FALSE(re.Match("b"));
EXPECT_FALSE(re.Match("c"));
EXPECT_FALSE(re.Match(""));
EXPECT_FALSE(re.Match("aa"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("`"));
EXPECT_TRUE(rs.Match("d"));
EXPECT_FALSE(rs.Match("a"));
EXPECT_FALSE(rs.Match("b"));
EXPECT_FALSE(rs.Match("c"));
EXPECT_FALSE(rs.Match(""));
EXPECT_FALSE(rs.Match("aa"));
}
TEST(Regex, CharacterRange3) {
Regex re("[a-c]");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("a"));
EXPECT_TRUE(re.Match("b"));
EXPECT_TRUE(re.Match("c"));
EXPECT_FALSE(re.Match(""));
EXPECT_FALSE(re.Match("`"));
EXPECT_FALSE(re.Match("d"));
EXPECT_FALSE(re.Match("aa"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("a"));
EXPECT_TRUE(rs.Match("b"));
EXPECT_TRUE(rs.Match("c"));
EXPECT_FALSE(rs.Match(""));
EXPECT_FALSE(rs.Match("`"));
EXPECT_FALSE(rs.Match("d"));
EXPECT_FALSE(rs.Match("aa"));
}
TEST(Regex, CharacterRange4) {
Regex re("[^a-c]");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("`"));
EXPECT_TRUE(re.Match("d"));
EXPECT_FALSE(re.Match("a"));
EXPECT_FALSE(re.Match("b"));
EXPECT_FALSE(re.Match("c"));
EXPECT_FALSE(re.Match(""));
EXPECT_FALSE(re.Match("aa"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("`"));
EXPECT_TRUE(rs.Match("d"));
EXPECT_FALSE(rs.Match("a"));
EXPECT_FALSE(rs.Match("b"));
EXPECT_FALSE(rs.Match("c"));
EXPECT_FALSE(rs.Match(""));
EXPECT_FALSE(rs.Match("aa"));
}
TEST(Regex, CharacterRange5) {
Regex re("[-]");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("-"));
EXPECT_FALSE(re.Match(""));
EXPECT_FALSE(re.Match("a"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("-"));
EXPECT_FALSE(rs.Match(""));
EXPECT_FALSE(rs.Match("a"));
}
TEST(Regex, CharacterRange6) {
Regex re("[a-]");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("a"));
EXPECT_TRUE(re.Match("-"));
EXPECT_FALSE(re.Match(""));
EXPECT_FALSE(re.Match("`"));
EXPECT_FALSE(re.Match("b"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("a"));
EXPECT_TRUE(rs.Match("-"));
EXPECT_FALSE(rs.Match(""));
EXPECT_FALSE(rs.Match("`"));
EXPECT_FALSE(rs.Match("b"));
}
TEST(Regex, CharacterRange7) {
Regex re("[-a]");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("a"));
EXPECT_TRUE(re.Match("-"));
EXPECT_FALSE(re.Match(""));
EXPECT_FALSE(re.Match("`"));
EXPECT_FALSE(re.Match("b"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("a"));
EXPECT_TRUE(rs.Match("-"));
EXPECT_FALSE(rs.Match(""));
EXPECT_FALSE(rs.Match("`"));
EXPECT_FALSE(rs.Match("b"));
}
TEST(Regex, CharacterRange8) {
Regex re("[a-zA-Z0-9]*");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("Milo"));
EXPECT_TRUE(re.Match("MT19937"));
EXPECT_TRUE(re.Match("43"));
EXPECT_FALSE(re.Match("a_b"));
EXPECT_FALSE(re.Match("!"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("Milo"));
EXPECT_TRUE(rs.Match("MT19937"));
EXPECT_TRUE(rs.Match("43"));
EXPECT_FALSE(rs.Match("a_b"));
EXPECT_FALSE(rs.Match("!"));
}
TEST(Regex, Search) {
Regex re("abc");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Search("abc"));
EXPECT_TRUE(re.Search("_abc"));
EXPECT_TRUE(re.Search("abc_"));
EXPECT_TRUE(re.Search("_abc_"));
EXPECT_TRUE(re.Search("__abc__"));
EXPECT_TRUE(re.Search("abcabc"));
EXPECT_FALSE(re.Search("a"));
EXPECT_FALSE(re.Search("ab"));
EXPECT_FALSE(re.Search("bc"));
EXPECT_FALSE(re.Search("cba"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Search("abc"));
EXPECT_TRUE(rs.Search("_abc"));
EXPECT_TRUE(rs.Search("abc_"));
EXPECT_TRUE(rs.Search("_abc_"));
EXPECT_TRUE(rs.Search("__abc__"));
EXPECT_TRUE(rs.Search("abcabc"));
EXPECT_FALSE(rs.Search("a"));
EXPECT_FALSE(rs.Search("ab"));
EXPECT_FALSE(rs.Search("bc"));
EXPECT_FALSE(rs.Search("cba"));
}
TEST(Regex, Search_BeginAnchor) {
Regex re("^abc");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Search("abc"));
EXPECT_TRUE(re.Search("abc_"));
EXPECT_TRUE(re.Search("abcabc"));
EXPECT_FALSE(re.Search("_abc"));
EXPECT_FALSE(re.Search("_abc_"));
EXPECT_FALSE(re.Search("a"));
EXPECT_FALSE(re.Search("ab"));
EXPECT_FALSE(re.Search("bc"));
EXPECT_FALSE(re.Search("cba"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Search("abc"));
EXPECT_TRUE(rs.Search("abc_"));
EXPECT_TRUE(rs.Search("abcabc"));
EXPECT_FALSE(rs.Search("_abc"));
EXPECT_FALSE(rs.Search("_abc_"));
EXPECT_FALSE(rs.Search("a"));
EXPECT_FALSE(rs.Search("ab"));
EXPECT_FALSE(rs.Search("bc"));
EXPECT_FALSE(rs.Search("cba"));
}
TEST(Regex, Search_EndAnchor) {
Regex re("abc$");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Search("abc"));
EXPECT_TRUE(re.Search("_abc"));
EXPECT_TRUE(re.Search("abcabc"));
EXPECT_FALSE(re.Search("abc_"));
EXPECT_FALSE(re.Search("_abc_"));
EXPECT_FALSE(re.Search("a"));
EXPECT_FALSE(re.Search("ab"));
EXPECT_FALSE(re.Search("bc"));
EXPECT_FALSE(re.Search("cba"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Search("abc"));
EXPECT_TRUE(rs.Search("_abc"));
EXPECT_TRUE(rs.Search("abcabc"));
EXPECT_FALSE(rs.Search("abc_"));
EXPECT_FALSE(rs.Search("_abc_"));
EXPECT_FALSE(rs.Search("a"));
EXPECT_FALSE(rs.Search("ab"));
EXPECT_FALSE(rs.Search("bc"));
EXPECT_FALSE(rs.Search("cba"));
}
TEST(Regex, Search_BothAnchor) {
Regex re("^abc$");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Search("abc"));
EXPECT_FALSE(re.Search(""));
EXPECT_FALSE(re.Search("a"));
EXPECT_FALSE(re.Search("b"));
EXPECT_FALSE(re.Search("ab"));
EXPECT_FALSE(re.Search("abcd"));
RegexSearch rs(re);
EXPECT_TRUE(rs.Search("abc"));
EXPECT_FALSE(rs.Search(""));
EXPECT_FALSE(rs.Search("a"));
EXPECT_FALSE(rs.Search("b"));
EXPECT_FALSE(rs.Search("ab"));
EXPECT_FALSE(rs.Search("abcd"));
}
TEST(Regex, Escape) {
const char* s = "\\^\\$\\|\\(\\)\\?\\*\\+\\.\\[\\]\\{\\}\\\\\\f\\n\\r\\t\\v[\\b][\\[][\\]]";
Regex re(s);
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("^$|()?*+.[]{}\\\x0C\n\r\t\x0B\b[]"));
EXPECT_FALSE(re.Match(s)); // Not escaping
RegexSearch rs(re);
EXPECT_TRUE(rs.Match("^$|()?*+.[]{}\\\x0C\n\r\t\x0B\b[]"));
EXPECT_FALSE(rs.Match(s)); // Not escaping
}
TEST(Regex, Invalid) {
@@ -549,6 +595,7 @@ TEST(Regex, Invalid) {
TEST_INVALID("");
TEST_INVALID("a|");
TEST_INVALID("()");
TEST_INVALID("(");
TEST_INVALID(")");
TEST_INVALID("(a))");
TEST_INVALID("(a|)");
File diff suppressed because it is too large Load Diff
+12 -8
View File
@@ -1,6 +1,6 @@
// 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.
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
//
// 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
@@ -21,6 +21,8 @@
# define RAPIDJSON_SSE42
#elif defined(__SSE2__)
# define RAPIDJSON_SSE2
#elif defined(__ARM_NEON)
# define RAPIDJSON_NEON
#endif
#define RAPIDJSON_NAMESPACE rapidjson_simd
@@ -41,14 +43,18 @@ using namespace rapidjson_simd;
#define SIMD_SUFFIX(name) name##_SSE2
#elif defined(RAPIDJSON_SSE42)
#define SIMD_SUFFIX(name) name##_SSE42
#elif defined(RAPIDJSON_NEON)
#define SIMD_SUFFIX(name) name##_NEON
#else
#define SIMD_SUFFIX(name) name
#endif
#define SIMD_SIZE_ALIGN(n) ((size_t(n) + 15) & ~size_t(15))
template <typename StreamType>
void TestSkipWhitespace() {
for (size_t step = 1; step < 32; step++) {
char buffer[1025];
char buffer[SIMD_SIZE_ALIGN(1025)];
for (size_t i = 0; i < 1024; i++)
buffer[i] = " \t\r\n"[i % 4];
for (size_t i = 0; i < 1024; i += step)
@@ -75,7 +81,7 @@ TEST(SIMD, SIMD_SUFFIX(SkipWhitespace)) {
TEST(SIMD, SIMD_SUFFIX(SkipWhitespace_EncodedMemoryStream)) {
for (size_t step = 1; step < 32; step++) {
char buffer[1024];
char buffer[SIMD_SIZE_ALIGN(1024)];
for (size_t i = 0; i < 1024; i++)
buffer[i] = " \t\r\n"[i % 4];
for (size_t i = 0; i < 1024; i += step)
@@ -83,14 +89,12 @@ TEST(SIMD, SIMD_SUFFIX(SkipWhitespace_EncodedMemoryStream)) {
MemoryStream ms(buffer, 1024);
EncodedInputStream<UTF8<>, MemoryStream> s(ms);
size_t i = 0;
for (;;) {
SkipWhitespace(s);
if (s.Peek() == '\0')
break;
//EXPECT_EQ(i, s.Tell());
EXPECT_EQ('X', s.Take());
i += step;
}
}
}
@@ -105,8 +109,8 @@ struct ScanCopyUnescapedStringHandler : BaseReaderHandler<UTF8<>, ScanCopyUnesca
template <unsigned parseFlags, typename StreamType>
void TestScanCopyUnescapedString() {
char buffer[1024 + 5 + 32];
char backup[1024 + 5 + 32];
char buffer[SIMD_SIZE_ALIGN(1024u + 5 + 32)];
char backup[SIMD_SIZE_ALIGN(1024u + 5 + 32)];
// Test "ABCDABCD...\\"
for (size_t offset = 0; offset < 32; offset++) {
@@ -163,7 +167,7 @@ TEST(SIMD, SIMD_SUFFIX(ScanCopyUnescapedString)) {
}
TEST(SIMD, SIMD_SUFFIX(ScanWriteUnescapedString)) {
char buffer[2048 + 1 + 32];
char buffer[SIMD_SIZE_ALIGN(2048 + 1 + 32)];
for (size_t offset = 0; offset < 32; offset++) {
for (size_t step = 0; step < 1024; step++) {
char* s = buffer + offset;
+1 -1
View File
@@ -1,6 +1,6 @@
// 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.
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
//
// 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
+26 -4
View File
@@ -1,6 +1,6 @@
// 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.
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
//
// 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
@@ -26,6 +26,7 @@ using namespace rapidjson;
TEST(StringBuffer, InitialSize) {
StringBuffer buffer;
EXPECT_EQ(0u, buffer.GetSize());
EXPECT_EQ(0u, buffer.GetLength());
EXPECT_STREQ("", buffer.GetString());
}
@@ -34,14 +35,17 @@ TEST(StringBuffer, Put) {
buffer.Put('A');
EXPECT_EQ(1u, buffer.GetSize());
EXPECT_EQ(1u, buffer.GetLength());
EXPECT_STREQ("A", buffer.GetString());
}
TEST(StringBuffer, PutN_Issue672) {
GenericStringBuffer<UTF8<>, MemoryPoolAllocator<> > buffer;
EXPECT_EQ(0, buffer.GetSize());
EXPECT_EQ(0u, buffer.GetSize());
EXPECT_EQ(0u, buffer.GetLength());
rapidjson::PutN(buffer, ' ', 1);
EXPECT_EQ(1, buffer.GetSize());
EXPECT_EQ(1u, buffer.GetSize());
EXPECT_EQ(1u, buffer.GetLength());
}
TEST(StringBuffer, Clear) {
@@ -52,6 +56,7 @@ TEST(StringBuffer, Clear) {
buffer.Clear();
EXPECT_EQ(0u, buffer.GetSize());
EXPECT_EQ(0u, buffer.GetLength());
EXPECT_STREQ("", buffer.GetString());
}
@@ -60,6 +65,7 @@ TEST(StringBuffer, Push) {
buffer.Push(5);
EXPECT_EQ(5u, buffer.GetSize());
EXPECT_EQ(5u, buffer.GetLength());
// Causes sudden expansion to make the stack's capacity equal to size
buffer.Push(65536u);
@@ -76,9 +82,19 @@ TEST(StringBuffer, Pop) {
buffer.Pop(3);
EXPECT_EQ(2u, buffer.GetSize());
EXPECT_EQ(2u, buffer.GetLength());
EXPECT_STREQ("AB", buffer.GetString());
}
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());
}
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
#if 0 // Many old compiler does not support these. Turn it off temporaily.
@@ -130,18 +146,23 @@ TEST(StringBuffer, MoveConstructor) {
x.Put('D');
EXPECT_EQ(4u, x.GetSize());
EXPECT_EQ(4u, x.GetLength());
EXPECT_STREQ("ABCD", x.GetString());
// StringBuffer y(x); // does not compile (!is_copy_constructible)
StringBuffer y(std::move(x));
EXPECT_EQ(0u, x.GetSize());
EXPECT_EQ(0u, x.GetLength());
EXPECT_EQ(4u, y.GetSize());
EXPECT_EQ(4u, y.GetLength());
EXPECT_STREQ("ABCD", y.GetString());
// StringBuffer z = y; // does not compile (!is_copy_assignable)
StringBuffer z = std::move(y);
EXPECT_EQ(0u, y.GetSize());
EXPECT_EQ(0u, y.GetLength());
EXPECT_EQ(4u, z.GetSize());
EXPECT_EQ(4u, z.GetLength());
EXPECT_STREQ("ABCD", z.GetString());
}
@@ -153,13 +174,14 @@ TEST(StringBuffer, MoveAssignment) {
x.Put('D');
EXPECT_EQ(4u, x.GetSize());
EXPECT_EQ(4u, x.GetLength());
EXPECT_STREQ("ABCD", x.GetString());
StringBuffer y;
// y = x; // does not compile (!is_copy_assignable)
y = std::move(x);
EXPECT_EQ(0u, x.GetSize());
EXPECT_EQ(4u, y.GetSize());
EXPECT_EQ(4u, y.GetLength());
EXPECT_STREQ("ABCD", y.GetString());
}
+2 -2
View File
@@ -1,6 +1,6 @@
// 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.
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
//
// 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
@@ -91,7 +91,7 @@ TEST(Strtod, CheckApproximationCase) {
}
// Remove common power of two factor from all three scaled values
int common_Exp2 = std::min(dS_Exp2, std::min(bS_Exp2, hS_Exp2));
int common_Exp2 = (std::min)(dS_Exp2, (std::min)(bS_Exp2, hS_Exp2));
dS_Exp2 -= common_Exp2;
bS_Exp2 -= common_Exp2;
hS_Exp2 -= common_Exp2;
+1 -1
View File
@@ -1,6 +1,6 @@
// 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.
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
//
// 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
+11 -3
View File
@@ -1,6 +1,6 @@
// 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.
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
//
// 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
@@ -78,7 +78,7 @@ inline Ch* StrDup(const Ch* str) {
}
inline FILE* TempFile(char *filename) {
#ifdef _MSC_VER
#if defined(__WIN32__) || defined(_MSC_VER)
filename = tmpnam(filename);
// For Visual Studio, tmpnam() adds a backslash in front. Remove it.
@@ -117,7 +117,15 @@ public:
#pragma GCC diagnostic pop
#endif
#define RAPIDJSON_ASSERT(x) if (!(x)) throw AssertException(RAPIDJSON_STRINGIFY(x))
// Not using noexcept for testing RAPIDJSON_ASSERT()
#define RAPIDJSON_HAS_CXX11_NOEXCEPT 0
#ifndef RAPIDJSON_ASSERT
#define RAPIDJSON_ASSERT(x) (!(x) ? throw AssertException(RAPIDJSON_STRINGIFY(x)) : (void)0u)
#ifndef RAPIDJSON_ASSERT_THROWS
#define RAPIDJSON_ASSERT_THROWS
#endif
#endif
class Random {
public:
+725
View File
@@ -0,0 +1,725 @@
// Tencent is pleased to support the open source community by making RapidJSON available.
//
// (C) Copyright IBM Corporation 2021
//
// 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
//
// http://opensource.org/licenses/MIT
//
// 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.
#define RAPIDJSON_SCHEMA_VERBOSE 0
#define RAPIDJSON_HAS_STDSTRING 1
#include "unittest.h"
#include "rapidjson/document.h"
#include "rapidjson/uri.h"
#ifdef __clang__
RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(variadic-macros)
#elif defined(_MSC_VER)
RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(4822) // local class member function does not have a body
#endif
using namespace rapidjson;
TEST(Uri, DefaultConstructor) {
typedef GenericUri<Value> UriType;
UriType u;
EXPECT_TRUE(u.GetSchemeString() == 0);
EXPECT_TRUE(u.GetAuthString() == 0);
EXPECT_TRUE(u.GetPathString() == 0);
EXPECT_TRUE(u.GetBaseString() == 0);
EXPECT_TRUE(u.GetQueryString() == 0);
EXPECT_TRUE(u.GetFragString() == 0);
EXPECT_TRUE(u.GetString() == 0);
EXPECT_TRUE(u.GetSchemeStringLength() == 0);
EXPECT_TRUE(u.GetAuthStringLength() == 0);
EXPECT_TRUE(u.GetPathStringLength() == 0);
EXPECT_TRUE(u.GetBaseStringLength() == 0);
EXPECT_TRUE(u.GetQueryStringLength() == 0);
EXPECT_TRUE(u.GetFragStringLength() == 0);
EXPECT_TRUE(u.GetStringLength() == 0);
}
TEST(Uri, Parse) {
typedef GenericUri<Value, MemoryPoolAllocator<> > UriType;
MemoryPoolAllocator<CrtAllocator> allocator;
Value v;
Value w;
v.SetString("http://auth/path/xxx?query#frag", allocator);
UriType u = UriType(v, &allocator);
EXPECT_TRUE(StrCmp(u.GetSchemeString(), "http:") == 0);
EXPECT_TRUE(StrCmp(u.GetAuthString(), "//auth") == 0);
EXPECT_TRUE(StrCmp(u.GetPathString(), "/path/xxx") == 0);
EXPECT_TRUE(StrCmp(u.GetBaseString(), "http://auth/path/xxx?query") == 0);
EXPECT_TRUE(StrCmp(u.GetQueryString(), "?query") == 0);
EXPECT_TRUE(StrCmp(u.GetFragString(), "#frag") == 0);
u.Get(w, allocator);
EXPECT_TRUE(*w.GetString() == *v.GetString());
v.SetString("urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f", allocator);
u = UriType(v, &allocator);
EXPECT_TRUE(StrCmp(u.GetSchemeString(), "urn:") == 0);
EXPECT_TRUE(u.GetAuthStringLength() == 0);
EXPECT_TRUE(StrCmp(u.GetPathString(), "uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f") == 0);
EXPECT_TRUE(StrCmp(u.GetBaseString(), "urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f") == 0);
EXPECT_TRUE(u.GetQueryStringLength() == 0);
EXPECT_TRUE(u.GetFragStringLength() == 0);
u.Get(w, allocator);
EXPECT_TRUE(*w.GetString() == *v.GetString());
v.SetString("", allocator);
u = UriType(v, &allocator);
EXPECT_TRUE(u.GetSchemeStringLength() == 0);
EXPECT_TRUE(u.GetAuthStringLength() == 0);
EXPECT_TRUE(u.GetPathStringLength() == 0);
EXPECT_TRUE(u.GetBaseStringLength() == 0);
EXPECT_TRUE(u.GetQueryStringLength() == 0);
EXPECT_TRUE(u.GetFragStringLength() == 0);
v.SetString("http://auth/", allocator);
u = UriType(v, &allocator);
EXPECT_TRUE(StrCmp(u.GetSchemeString(), "http:") == 0);
EXPECT_TRUE(StrCmp(u.GetAuthString(), "//auth") == 0);
EXPECT_TRUE(StrCmp(u.GetPathString(), "/") == 0);
EXPECT_TRUE(StrCmp(u.GetBaseString(), "http://auth/") == 0);
EXPECT_TRUE(u.GetQueryStringLength() == 0);
EXPECT_TRUE(u.GetFragStringLength() == 0);
u = UriType("/path/sub");
EXPECT_TRUE(u.GetSchemeStringLength() == 0);
EXPECT_TRUE(u.GetAuthStringLength() == 0);
EXPECT_TRUE(StrCmp(u.GetPathString(), "/path/sub") == 0);
EXPECT_TRUE(StrCmp(u.GetBaseString(), "/path/sub") == 0);
EXPECT_TRUE(u.GetQueryStringLength() == 0);
EXPECT_TRUE(u.GetFragStringLength() == 0);
// absolute path gets normalized
u = UriType("/path/../sub/");
EXPECT_TRUE(u.GetSchemeStringLength() == 0);
EXPECT_TRUE(u.GetAuthStringLength() == 0);
EXPECT_TRUE(StrCmp(u.GetPathString(), "/sub/") == 0);
EXPECT_TRUE(StrCmp(u.GetBaseString(), "/sub/") == 0);
EXPECT_TRUE(u.GetQueryStringLength() == 0);
EXPECT_TRUE(u.GetFragStringLength() == 0);
// relative path does not
u = UriType("path/../sub");
EXPECT_TRUE(u.GetSchemeStringLength() == 0);
EXPECT_TRUE(u.GetAuthStringLength() == 0);
EXPECT_TRUE(StrCmp(u.GetPathString(), "path/../sub") == 0);
EXPECT_TRUE(StrCmp(u.GetBaseString(), "path/../sub") == 0);
EXPECT_TRUE(u.GetQueryStringLength() == 0);
EXPECT_TRUE(u.GetFragStringLength() == 0);
u = UriType("http://auth#frag/stuff");
EXPECT_TRUE(StrCmp(u.GetSchemeString(), "http:") == 0);
EXPECT_TRUE(StrCmp(u.GetAuthString(), "//auth") == 0);
EXPECT_TRUE(u.GetPathStringLength() == 0);
EXPECT_TRUE(StrCmp(u.GetBaseString(), "http://auth") == 0);
EXPECT_TRUE(u.GetQueryStringLength() == 0);
EXPECT_TRUE(StrCmp(u.GetFragString(), "#frag/stuff") == 0);
EXPECT_TRUE(StrCmp(u.GetString(), "http://auth#frag/stuff") == 0);
const Value::Ch c[] = { '#', 'f', 'r', 'a', 'g', '/', 's', 't', 'u', 'f', 'f', '\0'};
SizeType len = internal::StrLen<Value::Ch>(c);
u = UriType(c, len);
EXPECT_TRUE(StrCmp(u.GetString(), "#frag/stuff") == 0);
EXPECT_TRUE(u.GetStringLength() == len);
EXPECT_TRUE(StrCmp(u.GetBaseString(), "") == 0);
EXPECT_TRUE(u.GetBaseStringLength() == 0);
EXPECT_TRUE(StrCmp(u.GetFragString(), "#frag/stuff") == 0);
EXPECT_TRUE(u.GetFragStringLength() == len);
u = UriType(c);
EXPECT_TRUE(StrCmp(u.GetString(), "#frag/stuff") == 0);
EXPECT_TRUE(u.GetStringLength() == len);
EXPECT_TRUE(StrCmp(u.GetBaseString(), "") == 0);
EXPECT_TRUE(u.GetBaseStringLength() == 0);
EXPECT_TRUE(StrCmp(u.GetFragString(), "#frag/stuff") == 0);
EXPECT_TRUE(u.GetFragStringLength() == len);
// Incomplete auth treated as path
u = UriType("http:/");
EXPECT_TRUE(StrCmp(u.GetSchemeString(), "http:") == 0);
EXPECT_TRUE(u.GetAuthStringLength() == 0);
EXPECT_TRUE(StrCmp(u.GetPathString(), "/") == 0);
EXPECT_TRUE(StrCmp(u.GetBaseString(), "http:/") == 0);
}
TEST(Uri, Parse_UTF16) {
typedef GenericValue<UTF16<> > Value16;
typedef GenericUri<Value16, MemoryPoolAllocator<> > UriType;
MemoryPoolAllocator<CrtAllocator> allocator;
Value16 v;
Value16 w;
v.SetString(L"http://auth/path/xxx?query#frag", allocator);
UriType u = UriType(v, &allocator);
EXPECT_TRUE(StrCmp(u.GetSchemeString(), L"http:") == 0);
EXPECT_TRUE(StrCmp(u.GetAuthString(), L"//auth") == 0);
EXPECT_TRUE(StrCmp(u.GetPathString(), L"/path/xxx") == 0);
EXPECT_TRUE(StrCmp(u.GetBaseString(), L"http://auth/path/xxx?query") == 0);
EXPECT_TRUE(StrCmp(u.GetQueryString(), L"?query") == 0);
EXPECT_TRUE(StrCmp(u.GetFragString(), L"#frag") == 0);
u.Get(w, allocator);
EXPECT_TRUE(*w.GetString() == *v.GetString());
v.SetString(L"urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f", allocator);
u = UriType(v, &allocator);
EXPECT_TRUE(StrCmp(u.GetSchemeString(), L"urn:") == 0);
EXPECT_TRUE(u.GetAuthStringLength() == 0);
EXPECT_TRUE(StrCmp(u.GetPathString(), L"uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f") == 0);
EXPECT_TRUE(StrCmp(u.GetBaseString(), L"urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f") == 0);
EXPECT_TRUE(u.GetQueryStringLength() == 0);
EXPECT_TRUE(u.GetFragStringLength() == 0);
u.Get(w, allocator);
EXPECT_TRUE(*w.GetString() == *v.GetString());
v.SetString(L"", allocator);
u = UriType(v, &allocator);
EXPECT_TRUE(u.GetSchemeStringLength() == 0);
EXPECT_TRUE(u.GetAuthStringLength() == 0);
EXPECT_TRUE(u.GetPathStringLength() == 0);
EXPECT_TRUE(u.GetBaseStringLength() == 0);
EXPECT_TRUE(u.GetQueryStringLength() == 0);
EXPECT_TRUE(u.GetFragStringLength() == 0);
v.SetString(L"http://auth/", allocator);
u = UriType(v, &allocator);
EXPECT_TRUE(StrCmp(u.GetSchemeString(), L"http:") == 0);
EXPECT_TRUE(StrCmp(u.GetAuthString(), L"//auth") == 0);
EXPECT_TRUE(StrCmp(u.GetPathString(), L"/") == 0);
EXPECT_TRUE(StrCmp(u.GetBaseString(), L"http://auth/") == 0);
EXPECT_TRUE(u.GetQueryStringLength() == 0);
EXPECT_TRUE(u.GetFragStringLength() == 0);
u = UriType(L"/path/sub");
EXPECT_TRUE(u.GetSchemeStringLength() == 0);
EXPECT_TRUE(u.GetAuthStringLength() == 0);
EXPECT_TRUE(StrCmp(u.GetPathString(), L"/path/sub") == 0);
EXPECT_TRUE(StrCmp(u.GetBaseString(), L"/path/sub") == 0);
EXPECT_TRUE(u.GetQueryStringLength() == 0);
EXPECT_TRUE(u.GetFragStringLength() == 0);
// absolute path gets normalized
u = UriType(L"/path/../sub/");
EXPECT_TRUE(u.GetSchemeStringLength() == 0);
EXPECT_TRUE(u.GetAuthStringLength() == 0);
EXPECT_TRUE(StrCmp(u.GetPathString(), L"/sub/") == 0);
EXPECT_TRUE(StrCmp(u.GetBaseString(), L"/sub/") == 0);
EXPECT_TRUE(u.GetQueryStringLength() == 0);
EXPECT_TRUE(u.GetFragStringLength() == 0);
// relative path does not
u = UriType(L"path/../sub");
EXPECT_TRUE(u.GetSchemeStringLength() == 0);
EXPECT_TRUE(u.GetAuthStringLength() == 0);
EXPECT_TRUE(StrCmp(u.GetPathString(), L"path/../sub") == 0);
EXPECT_TRUE(StrCmp(u.GetBaseString(), L"path/../sub") == 0);
EXPECT_TRUE(u.GetQueryStringLength() == 0);
EXPECT_TRUE(u.GetFragStringLength() == 0);
u = UriType(L"http://auth#frag/stuff");
EXPECT_TRUE(StrCmp(u.GetSchemeString(), L"http:") == 0);
EXPECT_TRUE(StrCmp(u.GetAuthString(), L"//auth") == 0);
EXPECT_TRUE(u.GetPathStringLength() == 0);
EXPECT_TRUE(StrCmp(u.GetBaseString(), L"http://auth") == 0);
EXPECT_TRUE(u.GetQueryStringLength() == 0);
EXPECT_TRUE(StrCmp(u.GetFragString(), L"#frag/stuff") == 0);
EXPECT_TRUE(StrCmp(u.GetString(), L"http://auth#frag/stuff") == 0);
const Value16::Ch c[] = { '#', 'f', 'r', 'a', 'g', '/', 's', 't', 'u', 'f', 'f', '\0'};
SizeType len = internal::StrLen<Value16::Ch>(c);
u = UriType(c, len);
EXPECT_TRUE(StrCmp(u.GetString(), L"#frag/stuff") == 0);
EXPECT_TRUE(u.GetStringLength() == len);
EXPECT_TRUE(StrCmp(u.GetBaseString(), L"") == 0);
EXPECT_TRUE(u.GetBaseStringLength() == 0);
EXPECT_TRUE(StrCmp(u.GetFragString(), L"#frag/stuff") == 0);
EXPECT_TRUE(u.GetFragStringLength() == len);
u = UriType(c);
EXPECT_TRUE(StrCmp(u.GetString(), L"#frag/stuff") == 0);
EXPECT_TRUE(u.GetStringLength() == len);
EXPECT_TRUE(StrCmp(u.GetBaseString(), L"") == 0);
EXPECT_TRUE(u.GetBaseStringLength() == 0);
EXPECT_TRUE(StrCmp(u.GetFragString(), L"#frag/stuff") == 0);
EXPECT_TRUE(u.GetFragStringLength() == len);
// Incomplete auth treated as path
u = UriType(L"http:/");
EXPECT_TRUE(StrCmp(u.GetSchemeString(), L"http:") == 0);
EXPECT_TRUE(u.GetAuthStringLength() == 0);
EXPECT_TRUE(StrCmp(u.GetPathString(), L"/") == 0);
EXPECT_TRUE(StrCmp(u.GetBaseString(), L"http:/") == 0);
}
#if RAPIDJSON_HAS_STDSTRING
TEST(Uri, Parse_Std) {
typedef GenericUri<Value, MemoryPoolAllocator<> > UriType;
MemoryPoolAllocator<CrtAllocator> allocator;
typedef std::basic_string<Value::Ch> String;
String str = "http://auth/path/xxx?query#frag";
const UriType uri = UriType(str, &allocator);
EXPECT_TRUE(UriType::GetScheme(uri) == "http:");
EXPECT_TRUE(UriType::GetAuth(uri) == "//auth");
EXPECT_TRUE(UriType::GetPath(uri) == "/path/xxx");
EXPECT_TRUE(UriType::GetBase(uri) == "http://auth/path/xxx?query");
EXPECT_TRUE(UriType::GetQuery(uri) == "?query");
EXPECT_TRUE(UriType::GetFrag(uri) == "#frag");
EXPECT_TRUE(UriType::Get(uri) == str);
}
TEST(Uri, Parse_UTF16_Std) {
typedef GenericValue<UTF16<> > Value16;
typedef GenericUri<Value16, MemoryPoolAllocator<> > UriType;
MemoryPoolAllocator<CrtAllocator> allocator;
typedef std::basic_string<Value16::Ch> String;
String str = L"http://auth/path/xxx?query#frag";
const UriType uri = UriType(str, &allocator);
EXPECT_TRUE(UriType::GetScheme(uri) == L"http:");
EXPECT_TRUE(UriType::GetAuth(uri) == L"//auth");
EXPECT_TRUE(UriType::GetPath(uri) == L"/path/xxx");
EXPECT_TRUE(UriType::GetBase(uri) == L"http://auth/path/xxx?query");
EXPECT_TRUE(UriType::GetQuery(uri) == L"?query");
EXPECT_TRUE(UriType::GetFrag(uri) == L"#frag");
EXPECT_TRUE(UriType::Get(uri) == str);
}
#endif
TEST(Uri, CopyConstructor) {
typedef GenericUri<Value> UriType;
CrtAllocator allocator;
UriType u("http://auth/path/xxx?query#frag", &allocator);
UriType u2(u);
EXPECT_TRUE(u == u2);
EXPECT_NE(&u.GetAllocator(), &u2.GetAllocator());
}
TEST(Uri, Assignment) {
typedef GenericUri<Value> UriType;
CrtAllocator allocator;
UriType u("http://auth/path/xxx?query#frag", &allocator);
UriType u2;
u2 = u;
EXPECT_TRUE(u == u2);
EXPECT_NE(&u.GetAllocator(), &u2.GetAllocator());
}
TEST(Uri, Resolve) {
typedef GenericUri<Value> UriType;
CrtAllocator allocator;
// ref is full uri
UriType base = UriType("http://auth/path/#frag");
UriType ref = UriType("http://newauth/newpath#newfrag");
UriType res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://newauth/newpath#newfrag") == 0);
base = UriType("/path/#frag", &allocator);
ref = UriType("http://newauth/newpath#newfrag", &allocator);
res = ref.Resolve(base, &allocator);
EXPECT_TRUE(StrCmp(res.GetString(), "http://newauth/newpath#newfrag") == 0);
// ref is alternate uri
base = UriType("http://auth/path/#frag");
ref = UriType("urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f") == 0);
// ref is absolute path
base = UriType("http://auth/path/#");
ref = UriType("/newpath#newfrag");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://auth/newpath#newfrag") == 0);
// ref is relative path
base = UriType("http://auth/path/file.json#frag");
ref = UriType("newfile.json#");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://auth/path/newfile.json#") == 0);
base = UriType("http://auth/path/file.json#frag/stuff");
ref = UriType("newfile.json#newfrag/newstuff");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://auth/path/newfile.json#newfrag/newstuff") == 0);
base = UriType("file.json", &allocator);
ref = UriType("newfile.json", &base.GetAllocator());
res = ref.Resolve(base, &ref.GetAllocator());
EXPECT_TRUE(StrCmp(res.GetString(), "newfile.json") == 0);
base = UriType("file.json", &allocator);
ref = UriType("./newfile.json", &allocator);
res = ref.Resolve(base, &allocator);
EXPECT_TRUE(StrCmp(res.GetString(), "newfile.json") == 0);
base = UriType("file.json");
ref = UriType("parent/../newfile.json");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "newfile.json") == 0);
base = UriType("file.json");
ref = UriType("parent/./newfile.json");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "parent/newfile.json") == 0);
base = UriType("file.json");
ref = UriType("../../parent/.././newfile.json");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "newfile.json") == 0);
// This adds a joining slash so resolved length is base length + ref length + 1
base = UriType("http://auth");
ref = UriType("newfile.json");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://auth/newfile.json") == 0);
// ref is fragment
base = UriType("#frag/stuff");
ref = UriType("#newfrag/newstuff");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "#newfrag/newstuff") == 0);
// test ref fragment always wins
base = UriType("/path#frag");
ref = UriType("");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "/path") == 0);
// Examples from RFC3896
base = UriType("http://a/b/c/d;p?q");
ref = UriType("g:h");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "g:h") == 0);
ref = UriType("g");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://a/b/c/g") == 0);
ref = UriType("./g");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://a/b/c/g") == 0);
ref = UriType("g/");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://a/b/c/g/") == 0);
ref = UriType("/g");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://a/g") == 0);
ref = UriType("//g");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://g") == 0);
ref = UriType("?y");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://a/b/c/d;p?y") == 0);
ref = UriType("g?y");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://a/b/c/g?y") == 0);
ref = UriType("#s");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://a/b/c/d;p?q#s") == 0);
ref = UriType("g#s");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://a/b/c/g#s") == 0);
ref = UriType("g?y#s");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://a/b/c/g?y#s") == 0);
ref = UriType(";x");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://a/b/c/;x") == 0);
ref = UriType("g;x");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://a/b/c/g;x") == 0);
ref = UriType("g;x?y#s");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://a/b/c/g;x?y#s") == 0);
ref = UriType("");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://a/b/c/d;p?q") == 0);
ref = UriType(".");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://a/b/c/") == 0);
ref = UriType("./");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://a/b/c/") == 0);
ref = UriType("..");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://a/b/") == 0);
ref = UriType("../");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://a/b/") == 0);
ref = UriType("../g");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://a/b/g") == 0);
ref = UriType("../..");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://a/") == 0);
ref = UriType("../../");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://a/") == 0);
ref = UriType("../../g");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://a/g") == 0);
ref = UriType("../../../g");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://a/g") == 0);
ref = UriType("../../../../g");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://a/g") == 0);
ref = UriType("/./g");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://a/g") == 0);
ref = UriType("/../g");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://a/g") == 0);
ref = UriType("g.");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://a/b/c/g.") == 0);
ref = UriType(".g");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://a/b/c/.g") == 0);
ref = UriType("g..");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://a/b/c/g..") == 0);
ref = UriType("..g");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://a/b/c/..g") == 0);
ref = UriType("g#s/../x");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://a/b/c/g#s/../x") == 0);
}
TEST(Uri, Resolve_UTF16) {
typedef GenericValue<UTF16<> > Value16;
typedef GenericUri<Value16> UriType;
CrtAllocator allocator;
// ref is full uri
UriType base = UriType(L"http://auth/path/#frag");
UriType ref = UriType(L"http://newauth/newpath#newfrag");
UriType res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://newauth/newpath#newfrag") == 0);
base = UriType(L"/path/#frag");
ref = UriType(L"http://newauth/newpath#newfrag");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://newauth/newpath#newfrag") == 0);
// ref is alternate uri
base = UriType(L"http://auth/path/#frag");
ref = UriType(L"urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f") == 0);
// ref is absolute path
base = UriType(L"http://auth/path/#");
ref = UriType(L"/newpath#newfrag");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://auth/newpath#newfrag") == 0);
// ref is relative path
base = UriType(L"http://auth/path/file.json#frag");
ref = UriType(L"newfile.json#");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://auth/path/newfile.json#") == 0);
base = UriType(L"http://auth/path/file.json#frag/stuff");
ref = UriType(L"newfile.json#newfrag/newstuff");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://auth/path/newfile.json#newfrag/newstuff") == 0);
base = UriType(L"file.json", &allocator);
ref = UriType(L"newfile.json", &base.GetAllocator());
res = ref.Resolve(base, &ref.GetAllocator());
EXPECT_TRUE(StrCmp(res.GetString(), L"newfile.json") == 0);
base = UriType(L"file.json", &allocator);
ref = UriType(L"./newfile.json", &allocator);
res = ref.Resolve(base, &allocator);
EXPECT_TRUE(StrCmp(res.GetString(), L"newfile.json") == 0);
base = UriType(L"file.json");
ref = UriType(L"parent/../newfile.json");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"newfile.json") == 0);
base = UriType(L"file.json");
ref = UriType(L"parent/./newfile.json");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"parent/newfile.json") == 0);
base = UriType(L"file.json");
ref = UriType(L"../../parent/.././newfile.json");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"newfile.json") == 0);
// This adds a joining slash so resolved length is base length + ref length + 1
base = UriType(L"http://auth");
ref = UriType(L"newfile.json");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://auth/newfile.json") == 0);
// ref is fragment
base = UriType(L"#frag/stuff");
ref = UriType(L"#newfrag/newstuff");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"#newfrag/newstuff") == 0);
// test ref fragment always wins
base = UriType(L"/path#frag");
ref = UriType(L"");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"/path") == 0);
// Examples from RFC3896
base = UriType(L"http://a/b/c/d;p?q");
ref = UriType(L"g:h");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"g:h") == 0);
ref = UriType(L"g");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://a/b/c/g") == 0);
ref = UriType(L"./g");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://a/b/c/g") == 0);
ref = UriType(L"g/");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://a/b/c/g/") == 0);
ref = UriType(L"/g");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://a/g") == 0);
ref = UriType(L"//g");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://g") == 0);
ref = UriType(L"?y");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://a/b/c/d;p?y") == 0);
ref = UriType(L"g?y");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://a/b/c/g?y") == 0);
ref = UriType(L"#s");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://a/b/c/d;p?q#s") == 0);
ref = UriType(L"g#s");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://a/b/c/g#s") == 0);
ref = UriType(L"g?y#s");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://a/b/c/g?y#s") == 0);
ref = UriType(L";x");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://a/b/c/;x") == 0);
ref = UriType(L"g;x");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://a/b/c/g;x") == 0);
ref = UriType(L"g;x?y#s");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://a/b/c/g;x?y#s") == 0);
ref = UriType(L"");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://a/b/c/d;p?q") == 0);
ref = UriType(L".");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://a/b/c/") == 0);
ref = UriType(L"./");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://a/b/c/") == 0);
ref = UriType(L"..");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://a/b/") == 0);
ref = UriType(L"../");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://a/b/") == 0);
ref = UriType(L"../g");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://a/b/g") == 0);
ref = UriType(L"../..");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://a/") == 0);
ref = UriType(L"../../");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://a/") == 0);
ref = UriType(L"../../g");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://a/g") == 0);
ref = UriType(L"../../../g");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://a/g") == 0);
ref = UriType(L"../../../../g");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://a/g") == 0);
ref = UriType(L"/./g");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://a/g") == 0);
ref = UriType(L"/../g");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://a/g") == 0);
ref = UriType(L"g.");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://a/b/c/g.") == 0);
ref = UriType(L".g");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://a/b/c/.g") == 0);
ref = UriType(L"g..");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://a/b/c/g..") == 0);
ref = UriType(L"..g");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://a/b/c/..g") == 0);
ref = UriType(L"g#s/../x");
res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), L"http://a/b/c/g#s/../x") == 0);
}
TEST(Uri, Equals) {
typedef GenericUri<Value> UriType;
UriType a = UriType("http://a/a#a");
UriType b = UriType("http://a/a#b");
UriType c = a;
EXPECT_TRUE(a == a);
EXPECT_TRUE(a == c);
EXPECT_TRUE(a != b);
}
TEST(Uri, Match) {
typedef GenericUri<Value> UriType;
UriType a = UriType("http://a/a#a");
UriType b = UriType("http://a/a#b");
UriType c = a;
UriType d;
EXPECT_TRUE(a.Match(a));
EXPECT_TRUE(a.Match(c));
EXPECT_FALSE(a.Match(b));
EXPECT_FALSE(a.Match(b, true));
EXPECT_TRUE(a.Match(b, false)); // Base Uri same
EXPECT_FALSE(a.Match(d));
EXPECT_FALSE(d.Match(a));
}
TEST(Uri, Issue1899) {
typedef GenericUri<Value, MemoryPoolAllocator<> > UriType;
UriType base = UriType("http://auth/path/#frag");
UriType ref = UriType("http://newauth/newpath#newfrag");
UriType res = ref.Resolve(base);
EXPECT_TRUE(StrCmp(res.GetString(), "http://newauth/newpath#newfrag") == 0);
}
#if defined(_MSC_VER) || defined(__clang__)
RAPIDJSON_DIAG_POP
#endif
+109 -39
View File
@@ -1,6 +1,6 @@
// 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.
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
//
// 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
@@ -26,11 +26,11 @@ using namespace rapidjson;
TEST(Value, Size) {
if (sizeof(SizeType) == 4) {
#if RAPIDJSON_48BITPOINTER_OPTIMIZATION
EXPECT_EQ(16, sizeof(Value));
EXPECT_EQ(16u, sizeof(Value));
#elif RAPIDJSON_64BIT
EXPECT_EQ(24, sizeof(Value));
EXPECT_EQ(24u, sizeof(Value));
#else
EXPECT_EQ(16, sizeof(Value));
EXPECT_EQ(16u, sizeof(Value));
#endif
}
}
@@ -439,6 +439,17 @@ TEST(Value, Int) {
EXPECT_EQ(5678, z.Get<int>());
EXPECT_EQ(5679, z.Set(5679).Get<int>());
EXPECT_EQ(5680, z.Set<int>(5680).Get<int>());
#ifdef _MSC_VER
// long as int on MSC platforms
RAPIDJSON_STATIC_ASSERT(sizeof(long) == sizeof(int));
z.SetInt(2222);
EXPECT_TRUE(z.Is<long>());
EXPECT_EQ(2222l, z.Get<long>());
EXPECT_EQ(3333l, z.Set(3333l).Get<long>());
EXPECT_EQ(4444l, z.Set<long>(4444l).Get<long>());
EXPECT_TRUE(z.IsInt());
#endif
}
TEST(Value, Uint) {
@@ -485,6 +496,17 @@ TEST(Value, Uint) {
EXPECT_EQ(2147483648u, z.Get<unsigned>());
EXPECT_EQ(2147483649u, z.Set(2147483649u).Get<unsigned>());
EXPECT_EQ(2147483650u, z.Set<unsigned>(2147483650u).Get<unsigned>());
#ifdef _MSC_VER
// unsigned long as unsigned on MSC platforms
RAPIDJSON_STATIC_ASSERT(sizeof(unsigned long) == sizeof(unsigned));
z.SetUint(2222);
EXPECT_TRUE(z.Is<unsigned long>());
EXPECT_EQ(2222ul, z.Get<unsigned long>());
EXPECT_EQ(3333ul, z.Set(3333ul).Get<unsigned long>());
EXPECT_EQ(4444ul, z.Set<unsigned long>(4444ul).Get<unsigned long>());
EXPECT_TRUE(x.IsUint());
#endif
}
TEST(Value, Int64) {
@@ -857,9 +879,46 @@ TEST(Value, String) {
}
// Issue 226: Value of string type should not point to NULL
TEST(Value, SetStringNullException) {
Value v;
EXPECT_THROW(v.SetString(0, 0), AssertException);
TEST(Value, SetStringNull) {
MemoryPoolAllocator<> allocator;
const char* nullPtr = 0;
{
// Construction with string type creates empty string
Value v(kStringType);
EXPECT_NE(v.GetString(), nullPtr); // non-null string returned
EXPECT_EQ(v.GetStringLength(), 0u);
// Construction from/setting to null without length not allowed
EXPECT_THROW(Value(StringRef(nullPtr)), AssertException);
EXPECT_THROW(Value(StringRef(nullPtr), allocator), AssertException);
EXPECT_THROW(v.SetString(nullPtr, allocator), AssertException);
// Non-empty length with null string is not allowed
EXPECT_THROW(v.SetString(nullPtr, 17u), AssertException);
EXPECT_THROW(v.SetString(nullPtr, 42u, allocator), AssertException);
// Setting to null string with empty length is allowed
v.SetString(nullPtr, 0u);
EXPECT_NE(v.GetString(), nullPtr); // non-null string returned
EXPECT_EQ(v.GetStringLength(), 0u);
v.SetNull();
v.SetString(nullPtr, 0u, allocator);
EXPECT_NE(v.GetString(), nullPtr); // non-null string returned
EXPECT_EQ(v.GetStringLength(), 0u);
}
// Construction with null string and empty length is allowed
{
Value v(nullPtr,0u);
EXPECT_NE(v.GetString(), nullPtr); // non-null string returned
EXPECT_EQ(v.GetStringLength(), 0u);
}
{
Value v(nullPtr, 0u, allocator);
EXPECT_NE(v.GetString(), nullPtr); // non-null string returned
EXPECT_EQ(v.GetStringLength(), 0u);
}
}
template <typename T, typename Allocator>
@@ -1001,7 +1060,7 @@ static void TestArray(T& x, Allocator& allocator) {
x.Clear();
for (unsigned i = 0; i < n; i++)
x.PushBack(Value(kArrayType).PushBack(i, allocator).Move(), allocator);
itr = x.Erase(x.Begin() + first, x.Begin() + last);
if (last == n)
EXPECT_EQ(x.End(), itr);
@@ -1019,9 +1078,9 @@ static void TestArray(T& x, Allocator& allocator) {
}
TEST(Value, Array) {
Value::AllocatorType allocator;
Value x(kArrayType);
const Value& y = x;
Value::AllocatorType allocator;
EXPECT_EQ(kArrayType, x.GetType());
EXPECT_TRUE(x.IsArray());
@@ -1060,6 +1119,16 @@ TEST(Value, Array) {
z.SetArray();
EXPECT_TRUE(z.IsArray());
EXPECT_TRUE(z.Empty());
// PR #1503: assign from inner Value
{
CrtAllocator a; // Free() is not a noop
GenericValue<UTF8<>, CrtAllocator> nullValue;
GenericValue<UTF8<>, CrtAllocator> arrayValue(kArrayType);
arrayValue.PushBack(nullValue, a);
arrayValue = arrayValue[0]; // shouldn't crash (use after free)
EXPECT_TRUE(arrayValue.IsNull());
}
}
TEST(Value, ArrayHelper) {
@@ -1076,10 +1145,10 @@ TEST(Value, ArrayHelper) {
a.PushBack(1, allocator);
Value::Array a2(a); // copy constructor
EXPECT_EQ(1, a2.Size());
EXPECT_EQ(1u, a2.Size());
Value::Array a3 = a;
EXPECT_EQ(1, a3.Size());
EXPECT_EQ(1u, a3.Size());
Value::ConstArray y = static_cast<const Value&>(x).GetArray();
(void)y;
@@ -1116,7 +1185,7 @@ TEST(Value, ArrayHelper) {
y.PushBack(123, allocator);
x.PushBack(y.GetArray(), allocator); // Implicit constructor to convert Array to GenericValue
EXPECT_EQ(1, x.Size());
EXPECT_EQ(1u, x.Size());
EXPECT_EQ(123, x[0][0].GetInt());
EXPECT_TRUE(y.IsArray());
EXPECT_TRUE(y.Empty());
@@ -1365,7 +1434,7 @@ static void TestObject(T& x, Allocator& allocator) {
for (; itr != x.MemberEnd(); ++itr) {
size_t i = static_cast<size_t>((itr - x.MemberBegin())) + 1;
EXPECT_STREQ(itr->name.GetString(), keys[i]);
EXPECT_EQ(i, itr->value[0].GetInt());
EXPECT_EQ(static_cast<int>(i), itr->value[0].GetInt());
}
// Erase the last
@@ -1376,7 +1445,7 @@ static void TestObject(T& x, Allocator& allocator) {
for (; itr != x.MemberEnd(); ++itr) {
size_t i = static_cast<size_t>(itr - x.MemberBegin()) + 1;
EXPECT_STREQ(itr->name.GetString(), keys[i]);
EXPECT_EQ(i, itr->value[0].GetInt());
EXPECT_EQ(static_cast<int>(i), itr->value[0].GetInt());
}
// Erase the middle
@@ -1388,7 +1457,7 @@ static void TestObject(T& x, Allocator& allocator) {
size_t i = static_cast<size_t>(itr - x.MemberBegin());
i += (i < 4) ? 1 : 2;
EXPECT_STREQ(itr->name.GetString(), keys[i]);
EXPECT_EQ(i, itr->value[0].GetInt());
EXPECT_EQ(static_cast<int>(i), itr->value[0].GetInt());
}
// EraseMember(ConstMemberIterator, ConstMemberIterator)
@@ -1422,9 +1491,9 @@ static void TestObject(T& x, Allocator& allocator) {
}
TEST(Value, Object) {
Value::AllocatorType allocator;
Value x(kObjectType);
const Value& y = x; // const version
Value::AllocatorType allocator;
EXPECT_EQ(kObjectType, x.GetType());
EXPECT_TRUE(x.IsObject());
@@ -1457,10 +1526,10 @@ TEST(Value, ObjectHelper) {
o.AddMember("1", 1, allocator);
Value::Object o2(o); // copy constructor
EXPECT_EQ(1, o2.MemberCount());
EXPECT_EQ(1u, o2.MemberCount());
Value::Object o3 = o;
EXPECT_EQ(1, o3.MemberCount());
EXPECT_EQ(1u, o3.MemberCount());
Value::ConstObject y = static_cast<const Value&>(x).GetObject();
(void)y;
@@ -1487,7 +1556,7 @@ TEST(Value, ObjectHelper) {
EXPECT_STREQ("apple", y["a"].GetString());
EXPECT_TRUE(x.IsObject()); // Invariant
}
{
Value x(kObjectType);
x.AddMember("a", "apple", allocator);
@@ -1512,7 +1581,7 @@ TEST(Value, ObjectHelperRangeFor) {
{
int i = 0;
for (auto& m : x.GetObject()) {
char name[10];
char name[11];
sprintf(name, "%d", i);
EXPECT_STREQ(name, m.name.GetString());
EXPECT_EQ(i, m.value.GetInt());
@@ -1523,7 +1592,7 @@ TEST(Value, ObjectHelperRangeFor) {
{
int i = 0;
for (const auto& m : const_cast<const Value&>(x).GetObject()) {
char name[10];
char name[11];
sprintf(name, "%d", i);
EXPECT_STREQ(name, m.name.GetString());
EXPECT_EQ(i, m.value.GetInt());
@@ -1580,10 +1649,11 @@ TEST(Value, BigNestedObject) {
MemoryPoolAllocator<> allocator;
Value x(kObjectType);
static const SizeType n = 200;
const char* format = std::numeric_limits<SizeType>::is_signed ? "%d" : "%u";
for (SizeType i = 0; i < n; i++) {
char name1[10];
sprintf(name1, "%d", i);
sprintf(name1, format, i);
// Value name(name1); // should not compile
Value name(name1, static_cast<SizeType>(strlen(name1)), allocator);
@@ -1591,7 +1661,7 @@ TEST(Value, BigNestedObject) {
for (SizeType j = 0; j < n; j++) {
char name2[10];
sprintf(name2, "%d", j);
sprintf(name2, format, j);
Value name3(name2, static_cast<SizeType>(strlen(name2)), allocator);
Value number(static_cast<int>(i * n + j));
@@ -1604,11 +1674,11 @@ TEST(Value, BigNestedObject) {
for (SizeType i = 0; i < n; i++) {
char name1[10];
sprintf(name1, "%d", i);
sprintf(name1, format, i);
for (SizeType j = 0; j < n; j++) {
char name2[10];
sprintf(name2, "%d", j);
sprintf(name2, format, j);
x[name1];
EXPECT_EQ(static_cast<int>(i * n + j), x[name1][name2].GetInt());
}
@@ -1620,8 +1690,8 @@ TEST(Value, BigNestedObject) {
TEST(Value, RemoveLastElement) {
rapidjson::Document doc;
rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
rapidjson::Value objVal(rapidjson::kObjectType);
objVal.AddMember("var1", 123, allocator);
rapidjson::Value objVal(rapidjson::kObjectType);
objVal.AddMember("var1", 123, allocator);
objVal.AddMember("var2", "444", allocator);
objVal.AddMember("var3", 555, allocator);
EXPECT_TRUE(objVal.HasMember("var3"));
@@ -1643,22 +1713,22 @@ TEST(Document, CrtAllocator) {
static void TestShortStringOptimization(const char* str) {
const rapidjson::SizeType len = static_cast<rapidjson::SizeType>(strlen(str));
rapidjson::Document doc;
rapidjson::Value val;
val.SetString(str, len, doc.GetAllocator());
EXPECT_EQ(val.GetStringLength(), len);
EXPECT_STREQ(val.GetString(), str);
EXPECT_EQ(val.GetStringLength(), len);
EXPECT_STREQ(val.GetString(), str);
}
TEST(Value, AllocateShortString) {
TestShortStringOptimization(""); // edge case: empty string
TestShortStringOptimization("12345678"); // regular case for short strings: 8 chars
TestShortStringOptimization("12345678901"); // edge case: 11 chars in 32-bit mode (=> short string)
TestShortStringOptimization("123456789012"); // edge case: 12 chars in 32-bit mode (=> regular string)
TestShortStringOptimization("123456789012345"); // edge case: 15 chars in 64-bit mode (=> short string)
TestShortStringOptimization("1234567890123456"); // edge case: 16 chars in 64-bit mode (=> regular string)
TestShortStringOptimization(""); // edge case: empty string
TestShortStringOptimization("12345678"); // regular case for short strings: 8 chars
TestShortStringOptimization("12345678901"); // edge case: 11 chars in 32-bit mode (=> short string)
TestShortStringOptimization("123456789012"); // edge case: 12 chars in 32-bit mode (=> regular string)
TestShortStringOptimization("123456789012345"); // edge case: 15 chars in 64-bit mode (=> short string)
TestShortStringOptimization("1234567890123456"); // edge case: 16 chars in 64-bit mode (=> regular string)
}
template <int e>
@@ -1733,7 +1803,7 @@ static void MergeDuplicateKey(Value& v, Value::AllocatorType& a) {
// Convert all key:value into key:[value]
for (Value::MemberIterator itr = v.MemberBegin(); itr != v.MemberEnd(); ++itr)
itr->value = Value(kArrayType).Move().PushBack(itr->value, a);
// Merge arrays if key is duplicated
for (Value::MemberIterator itr = v.MemberBegin(); itr != v.MemberEnd();) {
Value::MemberIterator itr2 = v.FindMember(itr->name);
+133 -2
View File
@@ -1,6 +1,6 @@
// 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.
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
//
// 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
@@ -20,6 +20,11 @@
#include "rapidjson/stringbuffer.h"
#include "rapidjson/memorybuffer.h"
#ifdef __clang__
RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(c++98-compat)
#endif
using namespace rapidjson;
TEST(Writer, Compact) {
@@ -95,6 +100,19 @@ TEST(Writer, String) {
#endif
}
TEST(Writer, Issue_889) {
char buf[100] = "Hello";
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
writer.StartArray();
writer.String(buf);
writer.EndArray();
EXPECT_STREQ("[\"Hello\"]", buffer.GetString());
EXPECT_TRUE(writer.IsComplete()); \
}
TEST(Writer, ScanWriteUnescapedString) {
const char json[] = "[\" \\\"0123456789ABCDEF\"]";
// ^ scanning stops here.
@@ -394,8 +412,10 @@ TEST(Writer, ValidateEncoding) {
EXPECT_TRUE(writer.String("\xC2\xA2")); // Cents sign U+00A2
EXPECT_TRUE(writer.String("\xE2\x82\xAC")); // Euro sign U+20AC
EXPECT_TRUE(writer.String("\xF0\x9D\x84\x9E")); // G clef sign U+1D11E
EXPECT_TRUE(writer.String("\x01")); // SOH control U+0001
EXPECT_TRUE(writer.String("\x1B")); // Escape control U+001B
writer.EndArray();
EXPECT_STREQ("[\"\x24\",\"\xC2\xA2\",\"\xE2\x82\xAC\",\"\xF0\x9D\x84\x9E\"]", buffer.GetString());
EXPECT_STREQ("[\"\x24\",\"\xC2\xA2\",\"\xE2\x82\xAC\",\"\xF0\x9D\x84\x9E\",\"\\u0001\",\"\\u001B\"]", buffer.GetString());
}
// Fail in decoding invalid UTF-8 sequence http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
@@ -437,6 +457,28 @@ TEST(Writer, InvalidEventSequence) {
EXPECT_THROW(writer.Int(1), AssertException);
EXPECT_FALSE(writer.IsComplete());
}
// { 'a' }
{
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
writer.StartObject();
writer.Key("a");
EXPECT_THROW(writer.EndObject(), AssertException);
EXPECT_FALSE(writer.IsComplete());
}
// { 'a':'b','c' }
{
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
writer.StartObject();
writer.Key("a");
writer.String("b");
writer.Key("c");
EXPECT_THROW(writer.EndObject(), AssertException);
EXPECT_FALSE(writer.IsComplete());
}
}
TEST(Writer, NaN) {
@@ -458,6 +500,18 @@ TEST(Writer, NaN) {
EXPECT_FALSE(writer2.Double(nan));
}
TEST(Writer, NaNToNull) {
double nan = std::numeric_limits<double>::quiet_NaN();
EXPECT_TRUE(internal::Double(nan).IsNan());
{
StringBuffer buffer;
Writer<StringBuffer, UTF8<>, UTF8<>, CrtAllocator, kWriteNanAndInfNullFlag> writer(buffer);
EXPECT_TRUE(writer.Double(nan));
EXPECT_STREQ("null", buffer.GetString());
}
}
TEST(Writer, Inf) {
double inf = std::numeric_limits<double>::infinity();
@@ -482,6 +536,24 @@ TEST(Writer, Inf) {
EXPECT_STREQ("Infinity-Infinity", buffer.GetString());
}
TEST(Writer, InfToNull) {
double inf = std::numeric_limits<double>::infinity();
EXPECT_TRUE(internal::Double(inf).IsInf());
{
StringBuffer buffer;
Writer<StringBuffer, UTF8<>, UTF8<>, CrtAllocator, kWriteNanAndInfNullFlag> writer(buffer);
EXPECT_TRUE(writer.Double(inf));
EXPECT_STREQ("null", buffer.GetString());
}
{
StringBuffer buffer;
Writer<StringBuffer, UTF8<>, UTF8<>, CrtAllocator, kWriteNanAndInfNullFlag> writer(buffer);
EXPECT_TRUE(writer.Double(-inf));
EXPECT_STREQ("null", buffer.GetString());
}
}
TEST(Writer, RawValue) {
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
@@ -495,3 +567,62 @@ TEST(Writer, RawValue) {
EXPECT_TRUE(writer.IsComplete());
EXPECT_STREQ("{\"a\":1,\"raw\":[\"Hello\\nWorld\", 123.456]}", buffer.GetString());
}
TEST(Write, RawValue_Issue1152) {
{
GenericStringBuffer<UTF32<> > sb;
Writer<GenericStringBuffer<UTF32<> >, UTF8<>, UTF32<> > writer(sb);
writer.RawValue("null", 4, kNullType);
EXPECT_TRUE(writer.IsComplete());
const unsigned *out = sb.GetString();
EXPECT_EQ(static_cast<unsigned>('n'), out[0]);
EXPECT_EQ(static_cast<unsigned>('u'), out[1]);
EXPECT_EQ(static_cast<unsigned>('l'), out[2]);
EXPECT_EQ(static_cast<unsigned>('l'), out[3]);
EXPECT_EQ(static_cast<unsigned>(0 ), out[4]);
}
{
GenericStringBuffer<UTF8<> > sb;
Writer<GenericStringBuffer<UTF8<> >, UTF16<>, UTF8<> > writer(sb);
writer.RawValue(L"null", 4, kNullType);
EXPECT_TRUE(writer.IsComplete());
EXPECT_STREQ("null", sb.GetString());
}
{
// Fail in transcoding
GenericStringBuffer<UTF16<> > buffer;
Writer<GenericStringBuffer<UTF16<> >, UTF8<>, UTF16<> > writer(buffer);
EXPECT_FALSE(writer.RawValue("\"\xfe\"", 3, kStringType));
}
{
// Fail in encoding validation
StringBuffer buffer;
Writer<StringBuffer, UTF8<>, UTF8<>, CrtAllocator, kWriteValidateEncodingFlag> writer(buffer);
EXPECT_FALSE(writer.RawValue("\"\xfe\"", 3, kStringType));
}
}
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
static Writer<StringBuffer> WriterGen(StringBuffer &target) {
Writer<StringBuffer> writer(target);
writer.StartObject();
writer.Key("a");
writer.Int(1);
return writer;
}
TEST(Writer, MoveCtor) {
StringBuffer buffer;
Writer<StringBuffer> writer(WriterGen(buffer));
writer.EndObject();
EXPECT_TRUE(writer.IsComplete());
EXPECT_STREQ("{\"a\":1}", buffer.GetString());
}
#endif
#ifdef __clang__
RAPIDJSON_DIAG_POP
#endif
+26
View File
@@ -0,0 +1,26 @@
{
Suppress wcslen valgrind report 1
Memcheck:Cond
fun:__wcslen_sse2
}
{
Suppress wcslen valgrind report 2
Memcheck:Addr8
fun:__wcslen_sse2
}
{
Suppress wcslen valgrind report 3
Memcheck:Value8
fun:__wcslen_sse2
}
{
Suppress wmemcmp valgrind report 4
Memcheck:Addr32
fun:__wmemcmp_avx2_movbe
...
fun:*Uri*Parse_UTF16_Std*
}