Electroneum
Toggle main menu visibility
Loading...
Searching...
No Matches
istreamwrappertest.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/istreamwrapper.h
"
18
#include "
rapidjson/encodedstream.h
"
19
#include "
rapidjson/document.h
"
20
#include <sstream>
21
#include <fstream>
22
23
#if defined(_MSC_VER) && !defined(__clang__)
24
RAPIDJSON_DIAG_PUSH
25
RAPIDJSON_DIAG_OFF(4702)
// unreachable code
26
#endif
27
28
using namespace
rapidjson
;
29
using namespace
std
;
30
31
template
<
typename
StringStreamType>
32
static
void
TestStringStream() {
33
typedef
typename
StringStreamType::char_type
Ch
;
34
35
{
36
StringStreamType iss;
37
BasicIStreamWrapper<StringStreamType>
is(iss);
38
EXPECT_EQ
(0, is.Tell());
39
if
(
sizeof
(
Ch
) == 1) {
40
EXPECT_EQ
(0, is.Peek4());
41
EXPECT_EQ
(0, is.Tell());
42
}
43
EXPECT_EQ
(0, is.Peek());
44
EXPECT_EQ
(0, is.Take());
45
EXPECT_EQ
(0, is.Tell());
46
}
47
48
{
49
Ch
s[] = {
'A'
,
'B'
,
'C'
,
'\0'
};
50
StringStreamType iss(s);
51
BasicIStreamWrapper<StringStreamType>
is(iss);
52
EXPECT_EQ
(0, is.Tell());
53
if
(
sizeof
(
Ch
) == 1) {
54
EXPECT_EQ
(0, is.Peek4());
// less than 4 bytes
55
}
56
for
(
int
i = 0; i < 3; i++) {
57
EXPECT_EQ
(
static_cast<
size_t
>
(i), is.Tell());
58
EXPECT_EQ
(
'A'
+ i, is.Peek());
59
EXPECT_EQ
(
'A'
+ i, is.Peek());
60
EXPECT_EQ
(
'A'
+ i, is.Take());
61
}
62
EXPECT_EQ
(3, is.Tell());
63
EXPECT_EQ
(0, is.Peek());
64
EXPECT_EQ
(0, is.Take());
65
}
66
67
{
68
Ch
s[] = {
'A'
,
'B'
,
'C'
,
'D'
,
'E'
,
'\0'
};
69
StringStreamType iss(s);
70
BasicIStreamWrapper<StringStreamType>
is(iss);
71
if
(
sizeof
(
Ch
) == 1) {
72
const
Ch
* c = is.Peek4();
73
for
(
int
i = 0; i < 4; i++)
74
EXPECT_EQ
(
'A'
+ i, c[i]);
75
EXPECT_EQ
(0, is.Tell());
76
}
77
for
(
int
i = 0; i < 5; i++) {
78
EXPECT_EQ
(
static_cast<
size_t
>
(i), is.Tell());
79
EXPECT_EQ
(
'A'
+ i, is.Peek());
80
EXPECT_EQ
(
'A'
+ i, is.Peek());
81
EXPECT_EQ
(
'A'
+ i, is.Take());
82
}
83
EXPECT_EQ
(5, is.Tell());
84
EXPECT_EQ
(0, is.Peek());
85
EXPECT_EQ
(0, is.Take());
86
}
87
}
88
89
TEST
(
IStreamWrapper
, istringstream) {
90
TestStringStream<istringstream>();
91
}
92
93
TEST
(
IStreamWrapper
, stringstream) {
94
TestStringStream<stringstream>();
95
}
96
97
TEST
(
IStreamWrapper
, wistringstream) {
98
TestStringStream<wistringstream>();
99
}
100
101
TEST
(
IStreamWrapper
, wstringstream) {
102
TestStringStream<wstringstream>();
103
}
104
105
template
<
typename
FileStreamType>
106
static
bool
Open(FileStreamType& fs,
const
char
* filename) {
107
const
char
*paths[] = {
108
"encodings"
,
109
"bin/encodings"
,
110
"../bin/encodings"
,
111
"../../bin/encodings"
,
112
"../../../bin/encodings"
113
};
114
char
buffer[1024];
115
for
(
size_t
i = 0; i <
sizeof
(paths) /
sizeof
(paths[0]); i++) {
116
sprintf(buffer,
"%s/%s"
, paths[i], filename);
117
fs.open(buffer, ios_base::in | ios_base::binary);
118
if
(fs.is_open())
119
return
true
;
120
}
121
return
false
;
122
}
123
124
TEST
(
IStreamWrapper
, ifstream) {
125
ifstream ifs;
126
ASSERT_TRUE
(Open(ifs,
"utf8bom.json"
));
127
IStreamWrapper
isw(ifs);
128
EncodedInputStream<UTF8<>
,
IStreamWrapper
> eis(isw);
129
Document
d;
130
EXPECT_TRUE
(!d.
ParseStream
(eis).
HasParseError
());
131
EXPECT_TRUE
(d.IsObject());
132
EXPECT_EQ
(5, d.MemberCount());
133
}
134
135
TEST
(
IStreamWrapper
, fstream) {
136
fstream fs;
137
ASSERT_TRUE
(Open(fs,
"utf8bom.json"
));
138
IStreamWrapper
isw(fs);
139
EncodedInputStream<UTF8<>
,
IStreamWrapper
> eis(isw);
140
Document
d;
141
EXPECT_TRUE
(!d.
ParseStream
(eis).
HasParseError
());
142
EXPECT_TRUE
(d.IsObject());
143
EXPECT_EQ
(5, d.MemberCount());
144
}
145
146
// wifstream/wfstream only works on C++11 with codecvt_utf16
147
// But many C++11 library still not have it.
148
#if 0
149
#include <codecvt>
150
151
TEST
(
IStreamWrapper
, wifstream) {
152
wifstream ifs;
153
ASSERT_TRUE
(Open(ifs,
"utf16bebom.json"
));
154
ifs.imbue(std::locale(ifs.getloc(),
155
new
std::codecvt_utf16<wchar_t, 0x10ffff, std::consume_header>));
156
WIStreamWrapper
isw(ifs);
157
GenericDocument<UTF16<>
> d;
158
d.
ParseStream
<
kParseDefaultFlags
,
UTF16<>
,
WIStreamWrapper
>(isw);
159
EXPECT_TRUE
(!d.
HasParseError
());
160
EXPECT_TRUE
(d.IsObject());
161
EXPECT_EQ
(5, d.MemberCount());
162
}
163
164
TEST
(
IStreamWrapper
, wfstream) {
165
wfstream fs;
166
ASSERT_TRUE
(Open(fs,
"utf16bebom.json"
));
167
fs.imbue(std::locale(fs.getloc(),
168
new
std::codecvt_utf16<wchar_t, 0x10ffff, std::consume_header>));
169
WIStreamWrapper
isw(fs);
170
GenericDocument<UTF16<>
> d;
171
d.
ParseStream
<
kParseDefaultFlags
,
UTF16<>
,
WIStreamWrapper
>(isw);
172
EXPECT_TRUE
(!d.
HasParseError
());
173
EXPECT_TRUE
(d.IsObject());
174
EXPECT_EQ
(5, d.MemberCount());
175
}
176
177
#endif
178
179
#if defined(_MSC_VER) && !defined(__clang__)
180
RAPIDJSON_DIAG_POP
181
#endif
BasicIStreamWrapper
Wrapper of std::basic_istream into RapidJSON's Stream concept.
Definition
istreamwrapper.h:48
EncodedInputStream
Input byte stream wrapper with a statically bound encoding.
Definition
encodedstream.h:39
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::ParseStream
GenericDocument & ParseStream(InputStream &is)
Parse JSON text from an input stream (with Encoding conversion).
Definition
document.h:2265
IStreamWrapper
Definition
readertest.cpp:1362
document.h
Document
GenericDocument< UTF8<> > Document
GenericDocument with UTF8 encoding.
Definition
document.h:2512
encodedstream.h
EXPECT_EQ
#define EXPECT_EQ(val1, val2)
Definition
gtest.h:1922
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition
gtest.h:1859
TEST
#define TEST(test_case_name, test_name)
Definition
gtest.h:2187
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition
gtest.h:1865
Ch
#define Ch(x, y, z)
Definition
hash_impl.h:17
istreamwrapper.h
WIStreamWrapper
BasicIStreamWrapper< std::wistream > WIStreamWrapper
Definition
istreamwrapper.h:105
rapidjson
main RapidJSON namespace
std
STL namespace.
kParseDefaultFlags
@ kParseDefaultFlags
Default parse flags. Can be customized by defining RAPIDJSON_PARSE_DEFAULT_FLAGS.
Definition
reader.h:156
UTF16
UTF-16 encoding.
Definition
encodings.h:269
unittest.h
external
rapidjson
test
unittest
istreamwrappertest.cpp
Generated on
for Electroneum by
1.17.0