]>
git.proxmox.com Git - ceph.git/blob - ceph/src/s3select/rapidjson/test/unittest/encodedstreamtest.cpp
1 // Tencent is pleased to support the open source community by making RapidJSON available.
3 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
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
8 // http://opensource.org/licenses/MIT
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.
16 #include "rapidjson/filereadstream.h"
17 #include "rapidjson/filewritestream.h"
18 #include "rapidjson/encodedstream.h"
19 #include "rapidjson/stringbuffer.h"
20 #include "rapidjson/memorystream.h"
21 #include "rapidjson/memorybuffer.h"
23 using namespace rapidjson
;
25 class EncodedStreamTest
: public ::testing::Test
{
27 EncodedStreamTest() : json_(), length_() {}
28 virtual ~EncodedStreamTest();
30 virtual void SetUp() {
31 json_
= ReadFile("utf8.json", true, &length_
);
34 virtual void TearDown() {
40 EncodedStreamTest(const EncodedStreamTest
&);
41 EncodedStreamTest
& operator=(const EncodedStreamTest
&);
44 static FILE* Open(const char* filename
) {
45 const char *paths
[] = {
49 "../../bin/encodings",
50 "../../../bin/encodings"
53 for (size_t i
= 0; i
< sizeof(paths
) / sizeof(paths
[0]); i
++) {
54 sprintf(buffer
, "%s/%s", paths
[i
], filename
);
55 FILE *fp
= fopen(buffer
, "rb");
62 static char *ReadFile(const char* filename
, bool appendPath
, size_t* outLength
) {
63 FILE *fp
= appendPath
? Open(filename
) : fopen(filename
, "rb");
70 fseek(fp
, 0, SEEK_END
);
71 *outLength
= static_cast<size_t>(ftell(fp
));
72 fseek(fp
, 0, SEEK_SET
);
73 char* buffer
= static_cast<char*>(malloc(*outLength
+ 1));
74 size_t readLength
= fread(buffer
, 1, *outLength
, fp
);
75 buffer
[readLength
] = '\0';
80 template <typename FileEncoding
, typename MemoryEncoding
>
81 void TestEncodedInputStream(const char* filename
) {
82 // Test FileReadStream
85 FILE *fp
= Open(filename
);
87 FileReadStream
fs(fp
, buffer
, sizeof(buffer
));
88 EncodedInputStream
<FileEncoding
, FileReadStream
> eis(fs
);
89 StringStream
s(json_
);
91 while (eis
.Peek() != '\0') {
92 unsigned expected
, actual
;
93 EXPECT_TRUE(UTF8
<>::Decode(s
, &expected
));
94 EXPECT_TRUE(MemoryEncoding::Decode(eis
, &actual
));
95 EXPECT_EQ(expected
, actual
);
97 EXPECT_EQ('\0', s
.Peek());
104 char* data
= ReadFile(filename
, true, &size
);
105 MemoryStream
ms(data
, size
);
106 EncodedInputStream
<FileEncoding
, MemoryStream
> eis(ms
);
107 StringStream
s(json_
);
109 while (eis
.Peek() != '\0') {
110 unsigned expected
, actual
;
111 EXPECT_TRUE(UTF8
<>::Decode(s
, &expected
));
112 EXPECT_TRUE(MemoryEncoding::Decode(eis
, &actual
));
113 EXPECT_EQ(expected
, actual
);
115 EXPECT_EQ('\0', s
.Peek());
117 EXPECT_EQ(size
, eis
.Tell());
121 void TestAutoUTFInputStream(const char *filename
, bool expectHasBOM
) {
122 // Test FileReadStream
125 FILE *fp
= Open(filename
);
126 ASSERT_TRUE(fp
!= 0);
127 FileReadStream
fs(fp
, buffer
, sizeof(buffer
));
128 AutoUTFInputStream
<unsigned, FileReadStream
> eis(fs
);
129 EXPECT_EQ(expectHasBOM
, eis
.HasBOM());
130 StringStream
s(json_
);
131 while (eis
.Peek() != '\0') {
132 unsigned expected
, actual
;
133 EXPECT_TRUE(UTF8
<>::Decode(s
, &expected
));
134 EXPECT_TRUE(AutoUTF
<unsigned>::Decode(eis
, &actual
));
135 EXPECT_EQ(expected
, actual
);
137 EXPECT_EQ('\0', s
.Peek());
144 char* data
= ReadFile(filename
, true, &size
);
145 MemoryStream
ms(data
, size
);
146 AutoUTFInputStream
<unsigned, MemoryStream
> eis(ms
);
147 EXPECT_EQ(expectHasBOM
, eis
.HasBOM());
148 StringStream
s(json_
);
150 while (eis
.Peek() != '\0') {
151 unsigned expected
, actual
;
152 EXPECT_TRUE(UTF8
<>::Decode(s
, &expected
));
153 EXPECT_TRUE(AutoUTF
<unsigned>::Decode(eis
, &actual
));
154 EXPECT_EQ(expected
, actual
);
156 EXPECT_EQ('\0', s
.Peek());
158 EXPECT_EQ(size
, eis
.Tell());
162 template <typename FileEncoding
, typename MemoryEncoding
>
163 void TestEncodedOutputStream(const char* expectedFilename
, bool putBOM
) {
164 // Test FileWriteStream
166 char filename
[L_tmpnam
];
167 FILE* fp
= TempFile(filename
);
169 FileWriteStream
os(fp
, buffer
, sizeof(buffer
));
170 EncodedOutputStream
<FileEncoding
, FileWriteStream
> eos(os
, putBOM
);
171 StringStream
s(json_
);
172 while (s
.Peek() != '\0') {
173 bool success
= Transcoder
<UTF8
<>, MemoryEncoding
>::Transcode(s
, eos
);
174 EXPECT_TRUE(success
);
178 EXPECT_TRUE(CompareFile(filename
, expectedFilename
));
185 EncodedOutputStream
<FileEncoding
, MemoryBuffer
> eos(mb
, putBOM
);
186 StringStream
s(json_
);
187 while (s
.Peek() != '\0') {
188 bool success
= Transcoder
<UTF8
<>, MemoryEncoding
>::Transcode(s
, eos
);
189 EXPECT_TRUE(success
);
192 EXPECT_TRUE(CompareBufferFile(mb
.GetBuffer(), mb
.GetSize(), expectedFilename
));
196 void TestAutoUTFOutputStream(UTFType type
, bool putBOM
, const char *expectedFilename
) {
197 // Test FileWriteStream
199 char filename
[L_tmpnam
];
200 FILE* fp
= TempFile(filename
);
203 FileWriteStream
os(fp
, buffer
, sizeof(buffer
));
204 AutoUTFOutputStream
<unsigned, FileWriteStream
> eos(os
, type
, putBOM
);
205 StringStream
s(json_
);
206 while (s
.Peek() != '\0') {
207 bool success
= Transcoder
<UTF8
<>, AutoUTF
<unsigned> >::Transcode(s
, eos
);
208 EXPECT_TRUE(success
);
212 EXPECT_TRUE(CompareFile(filename
, expectedFilename
));
219 AutoUTFOutputStream
<unsigned, MemoryBuffer
> eos(mb
, type
, putBOM
);
220 StringStream
s(json_
);
221 while (s
.Peek() != '\0') {
222 bool success
= Transcoder
<UTF8
<>, AutoUTF
<unsigned> >::Transcode(s
, eos
);
223 EXPECT_TRUE(success
);
226 EXPECT_TRUE(CompareBufferFile(mb
.GetBuffer(), mb
.GetSize(), expectedFilename
));
230 bool CompareFile(const char* filename
, const char* expectedFilename
) {
231 size_t actualLength
, expectedLength
;
232 char* actualBuffer
= ReadFile(filename
, false, &actualLength
);
233 char* expectedBuffer
= ReadFile(expectedFilename
, true, &expectedLength
);
234 bool ret
= (expectedLength
== actualLength
) && memcmp(expectedBuffer
, actualBuffer
, actualLength
) == 0;
236 free(expectedBuffer
);
240 bool CompareBufferFile(const char* actualBuffer
, size_t actualLength
, const char* expectedFilename
) {
241 size_t expectedLength
;
242 char* expectedBuffer
= ReadFile(expectedFilename
, true, &expectedLength
);
243 bool ret
= (expectedLength
== actualLength
) && memcmp(expectedBuffer
, actualBuffer
, actualLength
) == 0;
244 free(expectedBuffer
);
252 EncodedStreamTest::~EncodedStreamTest() {}
254 TEST_F(EncodedStreamTest
, EncodedInputStream
) {
255 TestEncodedInputStream
<UTF8
<>, UTF8
<> >("utf8.json");
256 TestEncodedInputStream
<UTF8
<>, UTF8
<> >("utf8bom.json");
257 TestEncodedInputStream
<UTF16LE
<>, UTF16
<> >("utf16le.json");
258 TestEncodedInputStream
<UTF16LE
<>, UTF16
<> >("utf16lebom.json");
259 TestEncodedInputStream
<UTF16BE
<>, UTF16
<> >("utf16be.json");
260 TestEncodedInputStream
<UTF16BE
<>, UTF16
<> >("utf16bebom.json");
261 TestEncodedInputStream
<UTF32LE
<>, UTF32
<> >("utf32le.json");
262 TestEncodedInputStream
<UTF32LE
<>, UTF32
<> >("utf32lebom.json");
263 TestEncodedInputStream
<UTF32BE
<>, UTF32
<> >("utf32be.json");
264 TestEncodedInputStream
<UTF32BE
<>, UTF32
<> >("utf32bebom.json");
267 TEST_F(EncodedStreamTest
, AutoUTFInputStream
) {
268 TestAutoUTFInputStream("utf8.json", false);
269 TestAutoUTFInputStream("utf8bom.json", true);
270 TestAutoUTFInputStream("utf16le.json", false);
271 TestAutoUTFInputStream("utf16lebom.json",true);
272 TestAutoUTFInputStream("utf16be.json", false);
273 TestAutoUTFInputStream("utf16bebom.json",true);
274 TestAutoUTFInputStream("utf32le.json", false);
275 TestAutoUTFInputStream("utf32lebom.json",true);
276 TestAutoUTFInputStream("utf32be.json", false);
277 TestAutoUTFInputStream("utf32bebom.json", true);
280 // Auto detection fail, use user defined UTF type
281 const char json
[] = "{ }";
282 MemoryStream
ms(json
, sizeof(json
));
283 AutoUTFInputStream
<unsigned, MemoryStream
> eis(ms
, kUTF8
);
284 EXPECT_FALSE(eis
.HasBOM());
285 EXPECT_EQ(kUTF8
, eis
.GetType());
289 TEST_F(EncodedStreamTest
, EncodedOutputStream
) {
290 TestEncodedOutputStream
<UTF8
<>, UTF8
<> >("utf8.json", false);
291 TestEncodedOutputStream
<UTF8
<>, UTF8
<> >("utf8bom.json", true);
292 TestEncodedOutputStream
<UTF16LE
<>, UTF16
<> >("utf16le.json", false);
293 TestEncodedOutputStream
<UTF16LE
<>, UTF16
<> >("utf16lebom.json",true);
294 TestEncodedOutputStream
<UTF16BE
<>, UTF16
<> >("utf16be.json", false);
295 TestEncodedOutputStream
<UTF16BE
<>, UTF16
<> >("utf16bebom.json",true);
296 TestEncodedOutputStream
<UTF32LE
<>, UTF32
<> >("utf32le.json", false);
297 TestEncodedOutputStream
<UTF32LE
<>, UTF32
<> >("utf32lebom.json",true);
298 TestEncodedOutputStream
<UTF32BE
<>, UTF32
<> >("utf32be.json", false);
299 TestEncodedOutputStream
<UTF32BE
<>, UTF32
<> >("utf32bebom.json",true);
302 TEST_F(EncodedStreamTest
, AutoUTFOutputStream
) {
303 TestAutoUTFOutputStream(kUTF8
, false, "utf8.json");
304 TestAutoUTFOutputStream(kUTF8
, true, "utf8bom.json");
305 TestAutoUTFOutputStream(kUTF16LE
, false, "utf16le.json");
306 TestAutoUTFOutputStream(kUTF16LE
, true, "utf16lebom.json");
307 TestAutoUTFOutputStream(kUTF16BE
, false, "utf16be.json");
308 TestAutoUTFOutputStream(kUTF16BE
, true, "utf16bebom.json");
309 TestAutoUTFOutputStream(kUTF32LE
, false, "utf32le.json");
310 TestAutoUTFOutputStream(kUTF32LE
, true, "utf32lebom.json");
311 TestAutoUTFOutputStream(kUTF32BE
, false, "utf32be.json");
312 TestAutoUTFOutputStream(kUTF32BE
, true, "utf32bebom.json");