Electroneum
Toggle main menu visibility
Loading...
Searching...
No Matches
filestreamtest.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
#include "
rapidjson/filereadstream.h
"
17
#include "
rapidjson/filewritestream.h
"
18
#include "
rapidjson/encodedstream.h
"
19
20
using namespace
rapidjson
;
21
22
class
FileStreamTest
:
public
::testing::Test
{
23
public
:
24
FileStreamTest
() :
filename_
(),
json_
(),
length_
() {}
25
virtual
~FileStreamTest
();
26
27
virtual
void
SetUp
() {
28
const
char
*paths[] = {
29
"data/sample.json"
,
30
"bin/data/sample.json"
,
31
"../bin/data/sample.json"
,
32
"../../bin/data/sample.json"
,
33
"../../../bin/data/sample.json"
34
};
35
FILE* fp = 0;
36
for
(
size_t
i = 0; i <
sizeof
(paths) /
sizeof
(paths[0]); i++) {
37
fp = fopen(paths[i],
"rb"
);
38
if
(fp) {
39
filename_
= paths[i];
40
break
;
41
}
42
}
43
ASSERT_TRUE
(fp != 0);
44
45
fseek(fp, 0, SEEK_END);
46
length_
=
static_cast<
size_t
>
(ftell(fp));
47
fseek(fp, 0, SEEK_SET);
48
json_
=
static_cast<
char
*
>
(malloc(
length_
+ 1));
49
size_t
readLength = fread(
json_
, 1,
length_
, fp);
50
json_
[readLength] =
'\0'
;
51
fclose(fp);
52
}
53
54
virtual
void
TearDown
() {
55
free(
json_
);
56
json_
= 0;
57
}
58
59
private
:
60
FileStreamTest
(
const
FileStreamTest
&);
61
FileStreamTest
&
operator=
(
const
FileStreamTest
&);
62
63
protected
:
64
const
char
*
filename_
;
65
char
*
json_
;
66
size_t
length_
;
67
};
68
69
FileStreamTest::~FileStreamTest
() {}
70
71
TEST_F
(
FileStreamTest
,
FileReadStream
) {
72
FILE *fp = fopen(filename_,
"rb"
);
73
ASSERT_TRUE
(fp != 0);
74
char
buffer[65536];
75
FileReadStream
s(fp, buffer,
sizeof
(buffer));
76
77
for
(
size_t
i = 0; i < length_; i++) {
78
EXPECT_EQ
(json_[i], s.
Peek
());
79
EXPECT_EQ
(json_[i], s.
Peek
());
// 2nd time should be the same
80
EXPECT_EQ
(json_[i], s.
Take
());
81
}
82
83
EXPECT_EQ
(length_, s.
Tell
());
84
EXPECT_EQ
(
'\0'
, s.
Peek
());
85
86
fclose(fp);
87
}
88
89
TEST_F
(
FileStreamTest
,
FileWriteStream
) {
90
char
filename[L_tmpnam];
91
FILE* fp =
TempFile
(filename);
92
93
char
buffer[65536];
94
FileWriteStream
os(fp, buffer,
sizeof
(buffer));
95
for
(
size_t
i = 0; i < length_; i++)
96
os.
Put
(json_[i]);
97
os.
Flush
();
98
fclose(fp);
99
100
// Read it back to verify
101
fp = fopen(filename,
"rb"
);
102
FileReadStream
is(fp, buffer,
sizeof
(buffer));
103
104
for
(
size_t
i = 0; i < length_; i++)
105
EXPECT_EQ
(json_[i], is.
Take
());
106
107
EXPECT_EQ
(length_, is.
Tell
());
108
fclose(fp);
109
110
//std::cout << filename << std::endl;
111
remove(filename);
112
}
operator=
connection< TProtocol > & operator=(const connection< TProtocol > &obj)
Definition
abstract_tcp_server_cp.h:196
FileReadStream
File byte stream for input using fread().
Definition
filereadstream.h:34
FileReadStream::Peek
Ch Peek() const
Definition
filereadstream.h:50
FileReadStream::Take
Ch Take()
Definition
filereadstream.h:51
FileReadStream::Tell
size_t Tell() const
Definition
filereadstream.h:52
FileStreamTest
Definition
filestreamtest.cpp:22
FileStreamTest::~FileStreamTest
virtual ~FileStreamTest()
Definition
filestreamtest.cpp:69
FileStreamTest::SetUp
virtual void SetUp()
Definition
filestreamtest.cpp:27
FileStreamTest::json_
char * json_
Definition
filestreamtest.cpp:65
FileStreamTest::TearDown
virtual void TearDown()
Definition
filestreamtest.cpp:54
FileStreamTest::filename_
const char * filename_
Definition
filestreamtest.cpp:64
FileStreamTest::FileStreamTest
FileStreamTest()
Definition
filestreamtest.cpp:24
FileStreamTest::length_
size_t length_
Definition
filestreamtest.cpp:66
FileWriteStream
Wrapper of C file stream for output using fwrite().
Definition
filewritestream.h:32
FileWriteStream::Flush
void Flush()
Definition
filewritestream.h:63
FileWriteStream::Put
void Put(char c)
Definition
filewritestream.h:40
testing::Test
Definition
gtest.h:371
encodedstream.h
TEST_F
#define TEST_F(test_fixture, test_name)
Definition
gtest.h:2216
EXPECT_EQ
#define EXPECT_EQ(val1, val2)
Definition
gtest.h:1922
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition
gtest.h:1865
filereadstream.h
filewritestream.h
rapidjson
main RapidJSON namespace
unittest.h
TempFile
FILE * TempFile(char *filename)
Definition
unittest.h:80
external
rapidjson
test
unittest
filestreamtest.cpp
Generated on
for Electroneum by
1.17.0