Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
R
rapidjson
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
rapidjson
Commits
85c8b657
Commit
85c8b657
authored
May 17, 2015
by
miloyip
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Achieve zero heap allocation for SchemaValidator.TestSuite
parent
e20645f0
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
13 deletions
+28
-13
schema.h
include/rapidjson/schema.h
+0
-0
schematest.cpp
test/unittest/schematest.cpp
+28
-13
No files found.
include/rapidjson/schema.h
View file @
85c8b657
This diff is collapsed.
Click to expand it.
test/unittest/schematest.cpp
View file @
85c8b657
...
...
@@ -837,7 +837,8 @@ TEST(SchemaValidator, AllOf_Nested) {
INVALIDATE
(
s
,
"123"
,
""
,
"allOf"
,
""
);
}
static
char
*
ReadFile
(
const
char
*
filename
)
{
template
<
typename
Allocator
=
CrtAllocator
>
static
char
*
ReadFile
(
const
char
*
filename
,
Allocator
&
allocator
)
{
const
char
*
paths
[]
=
{
"%s"
,
"bin/%s"
,
...
...
@@ -860,7 +861,7 @@ static char* ReadFile(const char* filename) {
fseek
(
fp
,
0
,
SEEK_END
);
size_t
length
=
(
size_t
)
ftell
(
fp
);
fseek
(
fp
,
0
,
SEEK_SET
);
char
*
json
=
(
char
*
)
m
alloc
(
length
+
1
);
char
*
json
=
(
char
*
)
allocator
.
M
alloc
(
length
+
1
);
size_t
readLength
=
fread
(
json
,
1
,
length
,
fp
);
json
[
readLength
]
=
'\0'
;
fclose
(
fp
);
...
...
@@ -868,7 +869,8 @@ static char* ReadFile(const char* filename) {
}
TEST
(
SchemaValidator
,
ValidateMetaSchema
)
{
char
*
json
=
ReadFile
(
"draft-04/schema"
);
CrtAllocator
allocator
;
char
*
json
=
ReadFile
(
"draft-04/schema"
,
allocator
);
Document
d
;
d
.
Parse
(
json
);
ASSERT_FALSE
(
d
.
HasParseError
());
...
...
@@ -884,7 +886,7 @@ TEST(SchemaValidator, ValidateMetaSchema) {
printf
(
"Invalid document: %s
\n
"
,
sb
.
GetString
());
ADD_FAILURE
();
}
f
ree
(
json
);
CrtAllocator
::
F
ree
(
json
);
}
TEST
(
SchemaValidator
,
ValidateMetaSchema_UTF16
)
{
...
...
@@ -892,7 +894,8 @@ TEST(SchemaValidator, ValidateMetaSchema_UTF16) {
typedef
GenericSchemaDocument
<
D
::
ValueType
>
SD
;
typedef
GenericSchemaValidator
<
SD
>
SV
;
char
*
json
=
ReadFile
(
"draft-04/schema"
);
CrtAllocator
allocator
;
char
*
json
=
ReadFile
(
"draft-04/schema"
,
allocator
);
D
d
;
StringStream
ss
(
json
);
...
...
@@ -910,13 +913,16 @@ TEST(SchemaValidator, ValidateMetaSchema_UTF16) {
wprintf
(
L"Invalid document: %ls
\n
"
,
sb
.
GetString
());
ADD_FAILURE
();
}
f
ree
(
json
);
CrtAllocator
::
F
ree
(
json
);
}
template
<
typename
SchemaDocumentType
=
SchemaDocument
>
class
RemoteSchemaDocumentProvider
:
public
IGenericRemoteSchemaDocumentProvider
<
SchemaDocumentType
>
{
public
:
RemoteSchemaDocumentProvider
()
:
documentAllocator_
(),
schemaAllocator_
()
{
RemoteSchemaDocumentProvider
()
:
documentAllocator_
(
documentBuffer_
,
sizeof
(
documentBuffer_
)),
schemaAllocator_
(
schemaBuffer_
,
sizeof
(
schemaBuffer_
))
{
const
char
*
filenames
[
kCount
]
=
{
"jsonschema/remotes/integer.json"
,
"jsonschema/remotes/subSchemas.json"
,
...
...
@@ -927,16 +933,20 @@ public:
for
(
size_t
i
=
0
;
i
<
kCount
;
i
++
)
{
sd_
[
i
]
=
0
;
char
*
json
=
ReadFile
(
filenames
[
i
]);
char
jsonBuffer
[
8192
];
MemoryPoolAllocator
<>
jsonAllocator
(
jsonBuffer
,
sizeof
(
jsonBuffer
));
char
*
json
=
ReadFile
(
filenames
[
i
],
jsonAllocator
);
if
(
!
json
)
{
printf
(
"json remote file %s not found"
,
filenames
[
i
]);
ADD_FAILURE
();
}
else
{
DocumentType
d
(
&
documentAllocator_
);
char
stackBuffer
[
4096
];
MemoryPoolAllocator
<>
stackAllocator
(
stackBuffer
,
sizeof
(
stackBuffer
));
DocumentType
d
(
&
documentAllocator_
,
1024
,
&
stackAllocator
);
d
.
Parse
(
json
);
sd_
[
i
]
=
new
SchemaDocumentType
(
d
,
0
,
&
schemaAllocator_
);
f
ree
(
json
);
MemoryPoolAllocator
<>::
F
ree
(
json
);
}
};
}
...
...
@@ -961,7 +971,7 @@ public:
}
private
:
typedef
GenericDocument
<
typename
SchemaDocumentType
::
EncodingType
,
MemoryPoolAllocator
<>
>
DocumentType
;
typedef
GenericDocument
<
typename
SchemaDocumentType
::
EncodingType
,
MemoryPoolAllocator
<>
,
MemoryPoolAllocator
<>
>
DocumentType
;
RemoteSchemaDocumentProvider
(
const
RemoteSchemaDocumentProvider
&
);
RemoteSchemaDocumentProvider
&
operator
=
(
const
RemoteSchemaDocumentProvider
&
);
...
...
@@ -970,6 +980,8 @@ private:
SchemaDocumentType
*
sd_
[
kCount
];
typename
DocumentType
::
AllocatorType
documentAllocator_
;
typename
SchemaDocumentType
::
AllocatorType
schemaAllocator_
;
char
documentBuffer_
[
16384
];
char
schemaBuffer_
[
128
*
1024
];
};
TEST
(
SchemaValidator
,
TestSuite
)
{
...
...
@@ -1013,10 +1025,12 @@ TEST(SchemaValidator, TestSuite) {
typedef
GenericSchemaDocument
<
Value
,
MemoryPoolAllocator
<>
>
SchemaDocumentType
;
RemoteSchemaDocumentProvider
<
SchemaDocumentType
>
provider
;
char
jsonBuffer
[
65536
];
char
documentBuffer
[
65536
];
char
documentStackBuffer
[
65536
];
char
schemaBuffer
[
65536
];
char
validatorBuffer
[
65536
];
MemoryPoolAllocator
<>
jsonAllocator
(
jsonBuffer
,
sizeof
(
jsonBuffer
));
MemoryPoolAllocator
<>
documentAllocator
(
documentBuffer
,
sizeof
(
documentBuffer
));
MemoryPoolAllocator
<>
documentStackAllocator
(
documentStackBuffer
,
sizeof
(
documentStackBuffer
));
MemoryPoolAllocator
<>
schemaAllocator
(
schemaBuffer
,
sizeof
(
schemaBuffer
));
...
...
@@ -1025,7 +1039,7 @@ TEST(SchemaValidator, TestSuite) {
for
(
size_t
i
=
0
;
i
<
sizeof
(
filenames
)
/
sizeof
(
filenames
[
0
]);
i
++
)
{
char
filename
[
FILENAME_MAX
];
sprintf
(
filename
,
"jsonschema/tests/draft4/%s"
,
filenames
[
i
]);
char
*
json
=
ReadFile
(
filename
);
char
*
json
=
ReadFile
(
filename
,
jsonAllocator
);
if
(
!
json
)
{
printf
(
"json test suite file %s not found"
,
filename
);
ADD_FAILURE
();
...
...
@@ -1066,7 +1080,8 @@ TEST(SchemaValidator, TestSuite) {
}
}
documentAllocator
.
Clear
();
free
(
json
);
MemoryPoolAllocator
<>::
Free
(
json
);
jsonAllocator
.
Clear
();
}
printf
(
"%d / %d passed (%2d%%)
\n
"
,
passCount
,
testCount
,
passCount
*
100
/
testCount
);
// if (passCount != testCount)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment