Electroneum
Toggle main menu visibility
Loading...
Searching...
No Matches
jsoncheckertest.cpp
Go to the documentation of this file.
1
// Tencent is pleased to support the open source community by making RapidJSON available.
2
//
3
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
4
//
5
// Licensed under the MIT License (the "License"); you may not use this file except
6
// in compliance with the License. You may obtain a copy of the License at
7
//
8
// http://opensource.org/licenses/MIT
9
//
10
// Unless required by applicable law or agreed to in writing, software distributed
11
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13
// specific language governing permissions and limitations under the License.
14
15
#include "
unittest.h
"
16
17
#include "
rapidjson/document.h
"
18
19
using namespace
rapidjson
;
20
21
static
char
* ReadFile(
const
char
* filename,
size_t
& length) {
22
const
char
*paths[] = {
23
"jsonchecker"
,
24
"bin/jsonchecker"
,
25
"../bin/jsonchecker"
,
26
"../../bin/jsonchecker"
,
27
"../../../bin/jsonchecker"
28
};
29
char
buffer[1024];
30
FILE *fp = 0;
31
for
(
size_t
i = 0; i <
sizeof
(paths) /
sizeof
(paths[0]); i++) {
32
sprintf(buffer,
"%s/%s"
, paths[i], filename);
33
fp = fopen(buffer,
"rb"
);
34
if
(fp)
35
break
;
36
}
37
38
if
(!fp)
39
return
0;
40
41
fseek(fp, 0, SEEK_END);
42
length =
static_cast<
size_t
>
(ftell(fp));
43
fseek(fp, 0, SEEK_SET);
44
char
*
json
=
static_cast<
char
*
>
(malloc(length + 1));
45
size_t
readLength = fread(
json
, 1, length, fp);
46
json
[readLength] =
'\0'
;
47
fclose(fp);
48
return
json
;
49
}
50
51
struct
NoOpHandler
{
52
bool
Null
() {
return
true
; }
53
bool
Bool
(
bool
) {
return
true
; }
54
bool
Int
(
int
) {
return
true
; }
55
bool
Uint
(
unsigned
) {
return
true
; }
56
bool
Int64
(
int64_t
) {
return
true
; }
57
bool
Uint64
(
uint64_t
) {
return
true
; }
58
bool
Double
(
double
) {
return
true
; }
59
bool
RawNumber
(
const
char
*,
SizeType
,
bool
) {
return
true
; }
60
bool
String
(
const
char
*,
SizeType
,
bool
) {
return
true
; }
61
bool
StartObject
() {
return
true
; }
62
bool
Key
(
const
char
*,
SizeType
,
bool
) {
return
true
; }
63
bool
EndObject
(
SizeType
) {
return
true
; }
64
bool
StartArray
() {
return
true
; }
65
bool
EndArray
(
SizeType
) {
return
true
; }
66
};
67
68
69
TEST
(JsonChecker,
Reader
) {
70
char
filename[256];
71
72
// jsonchecker/failXX.json
73
for
(
int
i = 1; i <= 33; i++) {
74
if
(i == 1)
// fail1.json is valid in rapidjson, which has no limitation on type of root element (RFC 7159).
75
continue
;
76
if
(i == 18)
// fail18.json is valid in rapidjson, which has no limitation on depth of nesting.
77
continue
;
78
79
sprintf(filename,
"fail%d.json"
, i);
80
size_t
length;
81
char
*
json
= ReadFile(filename, length);
82
if
(!
json
) {
83
printf(
"jsonchecker file %s not found"
, filename);
84
ADD_FAILURE
();
85
continue
;
86
}
87
88
// Test stack-based parsing.
89
GenericDocument<UTF8<>
,
CrtAllocator
> document;
// Use Crt allocator to check exception-safety (no memory leak)
90
document.
Parse
(
json
);
91
EXPECT_TRUE
(document.
HasParseError
()) << filename;
92
93
// Test iterative parsing.
94
document.
Parse
<
kParseIterativeFlag
>(
json
);
95
EXPECT_TRUE
(document.
HasParseError
()) << filename;
96
97
// Test iterative pull-parsing.
98
Reader
reader;
99
StringStream
ss(
json
);
100
NoOpHandler
h;
101
reader.
IterativeParseInit
();
102
while
(!reader.
IterativeParseComplete
()) {
103
if
(!reader.
IterativeParseNext
<
kParseDefaultFlags
>(ss, h))
104
break
;
105
}
106
EXPECT_TRUE
(reader.
HasParseError
()) << filename;
107
108
free(
json
);
109
}
110
111
// passX.json
112
for
(
int
i = 1; i <= 3; i++) {
113
sprintf(filename,
"pass%d.json"
, i);
114
size_t
length;
115
char
*
json
= ReadFile(filename, length);
116
if
(!
json
) {
117
printf(
"jsonchecker file %s not found"
, filename);
118
continue
;
119
}
120
121
// Test stack-based parsing.
122
GenericDocument<UTF8<>
,
CrtAllocator
> document;
// Use Crt allocator to check exception-safety (no memory leak)
123
document.
Parse
(
json
);
124
EXPECT_FALSE
(document.
HasParseError
()) << filename;
125
126
// Test iterative parsing.
127
document.
Parse
<
kParseIterativeFlag
>(
json
);
128
EXPECT_FALSE
(document.
HasParseError
()) << filename;
129
130
// Test iterative pull-parsing.
131
Reader
reader;
132
StringStream
ss(
json
);
133
NoOpHandler
h;
134
reader.
IterativeParseInit
();
135
while
(!reader.
IterativeParseComplete
()) {
136
if
(!reader.
IterativeParseNext
<
kParseDefaultFlags
>(ss, h))
137
break
;
138
}
139
EXPECT_FALSE
(reader.
HasParseError
()) << filename;
140
141
free(
json
);
142
}
143
}
CrtAllocator
C-runtime library allocator.
Definition
allocators.h:75
GenericDocument
A document for parsing JSON text as DOM.
Definition
document.h:2130
GenericDocument::HasParseError
bool HasParseError() const
Whether a parse error has occurred in the last parsing.
Definition
document.h:2394
GenericDocument::Parse
GenericDocument & Parse(const typename SourceEncoding::Ch *str)
Parse JSON text from a read-only string (with Encoding conversion).
Definition
document.h:2331
GenericReader::IterativeParseNext
bool IterativeParseNext(InputStream &is, Handler &handler)
Parse one token from JSON text.
Definition
reader.h:618
GenericReader::IterativeParseInit
void IterativeParseInit()
Initialize JSON text token-by-token parsing.
Definition
reader.h:605
GenericReader::IterativeParseComplete
RAPIDJSON_FORCEINLINE bool IterativeParseComplete() const
Check if token-by-token parsing JSON text is complete.
Definition
reader.h:675
GenericReader::HasParseError
bool HasParseError() const
Whether a parse error has occurred in the last parsing.
Definition
reader.h:680
document.h
StringStream
GenericStringStream< UTF8< char > > StringStream
Definition
fwd.h:49
Reader
GenericReader< UTF8< char >, UTF8< char >, CrtAllocator > Reader
Definition
fwd.h:90
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition
gtest.h:1859
TEST
#define TEST(test_case_name, test_name)
Definition
gtest.h:2187
ADD_FAILURE
#define ADD_FAILURE()
Definition
gtest.h:1808
EXPECT_FALSE
#define EXPECT_FALSE(condition)
Definition
gtest.h:1862
rapidjson
main RapidJSON namespace
SizeType
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.).
Definition
rapidjson.h:389
kParseDefaultFlags
@ kParseDefaultFlags
Default parse flags. Can be customized by defining RAPIDJSON_PARSE_DEFAULT_FLAGS.
Definition
reader.h:156
kParseIterativeFlag
@ kParseIterativeFlag
Iterative(constant complexity in terms of function call stack size) parsing.
Definition
reader.h:149
int64_t
signed __int64 int64_t
Definition
stdint.h:135
uint64_t
unsigned __int64 uint64_t
Definition
stdint.h:136
NoOpHandler
Definition
jsoncheckertest.cpp:51
NoOpHandler::StartObject
bool StartObject()
Definition
jsoncheckertest.cpp:61
NoOpHandler::String
bool String(const char *, SizeType, bool)
Definition
jsoncheckertest.cpp:60
NoOpHandler::StartArray
bool StartArray()
Definition
jsoncheckertest.cpp:64
NoOpHandler::Key
bool Key(const char *, SizeType, bool)
Definition
jsoncheckertest.cpp:62
NoOpHandler::RawNumber
bool RawNumber(const char *, SizeType, bool)
Definition
jsoncheckertest.cpp:59
NoOpHandler::EndObject
bool EndObject(SizeType)
Definition
jsoncheckertest.cpp:63
NoOpHandler::EndArray
bool EndArray(SizeType)
Definition
jsoncheckertest.cpp:65
NoOpHandler::Double
bool Double(double)
Definition
jsoncheckertest.cpp:58
NoOpHandler::Null
bool Null()
Definition
jsoncheckertest.cpp:52
NoOpHandler::Bool
bool Bool(bool)
Definition
jsoncheckertest.cpp:53
NoOpHandler::Uint
bool Uint(unsigned)
Definition
jsoncheckertest.cpp:55
NoOpHandler::Uint64
bool Uint64(uint64_t)
Definition
jsoncheckertest.cpp:57
NoOpHandler::Int
bool Int(int)
Definition
jsoncheckertest.cpp:54
NoOpHandler::Int64
bool Int64(int64_t)
Definition
jsoncheckertest.cpp:56
json
rapidjson::Document json
Definition
transport.cpp:49
unittest.h
external
rapidjson
test
unittest
jsoncheckertest.cpp
Generated on
for Electroneum by
1.17.0