]>
Commit | Line | Data |
---|---|---|
31f18b77 FG |
1 | // Tencent is pleased to support the open source community by making RapidJSON available. |
2 | // | |
1e59de90 | 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. |
31f18b77 FG |
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: | |
1e59de90 | 24 | FileStreamTest() : filename_(), json_(), length_(), abcde_() {} |
31f18b77 FG |
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); | |
1e59de90 TL |
52 | |
53 | const char *abcde_paths[] = { | |
54 | "data/abcde.txt", | |
55 | "bin/data/abcde.txt", | |
56 | "../bin/data/abcde.txt", | |
57 | "../../bin/data/abcde.txt", | |
58 | "../../../bin/data/abcde.txt" | |
59 | }; | |
60 | fp = 0; | |
61 | for (size_t i = 0; i < sizeof(abcde_paths) / sizeof(abcde_paths[0]); i++) { | |
62 | fp = fopen(abcde_paths[i], "rb"); | |
63 | if (fp) { | |
64 | abcde_ = abcde_paths[i]; | |
65 | break; | |
66 | } | |
67 | } | |
68 | ASSERT_TRUE(fp != 0); | |
69 | fclose(fp); | |
31f18b77 FG |
70 | } |
71 | ||
72 | virtual void TearDown() { | |
73 | free(json_); | |
74 | json_ = 0; | |
75 | } | |
76 | ||
77 | private: | |
78 | FileStreamTest(const FileStreamTest&); | |
79 | FileStreamTest& operator=(const FileStreamTest&); | |
80 | ||
81 | protected: | |
82 | const char* filename_; | |
83 | char *json_; | |
84 | size_t length_; | |
1e59de90 | 85 | const char* abcde_; |
31f18b77 FG |
86 | }; |
87 | ||
88 | FileStreamTest::~FileStreamTest() {} | |
89 | ||
90 | TEST_F(FileStreamTest, FileReadStream) { | |
91 | FILE *fp = fopen(filename_, "rb"); | |
92 | ASSERT_TRUE(fp != 0); | |
93 | char buffer[65536]; | |
94 | FileReadStream s(fp, buffer, sizeof(buffer)); | |
95 | ||
96 | for (size_t i = 0; i < length_; i++) { | |
97 | EXPECT_EQ(json_[i], s.Peek()); | |
98 | EXPECT_EQ(json_[i], s.Peek()); // 2nd time should be the same | |
99 | EXPECT_EQ(json_[i], s.Take()); | |
100 | } | |
101 | ||
102 | EXPECT_EQ(length_, s.Tell()); | |
103 | EXPECT_EQ('\0', s.Peek()); | |
104 | ||
105 | fclose(fp); | |
106 | } | |
107 | ||
1e59de90 TL |
108 | TEST_F(FileStreamTest, FileReadStream_Peek4) { |
109 | FILE *fp = fopen(abcde_, "rb"); | |
110 | ASSERT_TRUE(fp != 0); | |
111 | char buffer[4]; | |
112 | FileReadStream s(fp, buffer, sizeof(buffer)); | |
113 | ||
114 | const char* c = s.Peek4(); | |
115 | for (int i = 0; i < 4; i++) | |
116 | EXPECT_EQ('a' + i, c[i]); | |
117 | EXPECT_EQ(0u, s.Tell()); | |
118 | ||
119 | for (int i = 0; i < 5; i++) { | |
120 | EXPECT_EQ(static_cast<size_t>(i), s.Tell()); | |
121 | EXPECT_EQ('a' + i, s.Peek()); | |
122 | EXPECT_EQ('a' + i, s.Peek()); | |
123 | EXPECT_EQ('a' + i, s.Take()); | |
124 | } | |
125 | EXPECT_EQ(5u, s.Tell()); | |
126 | EXPECT_EQ(0, s.Peek()); | |
127 | EXPECT_EQ(0, s.Take()); | |
128 | ||
129 | fclose(fp); | |
130 | } | |
131 | ||
31f18b77 FG |
132 | TEST_F(FileStreamTest, FileWriteStream) { |
133 | char filename[L_tmpnam]; | |
134 | FILE* fp = TempFile(filename); | |
135 | ||
136 | char buffer[65536]; | |
137 | FileWriteStream os(fp, buffer, sizeof(buffer)); | |
138 | for (size_t i = 0; i < length_; i++) | |
139 | os.Put(json_[i]); | |
140 | os.Flush(); | |
141 | fclose(fp); | |
142 | ||
143 | // Read it back to verify | |
144 | fp = fopen(filename, "rb"); | |
145 | FileReadStream is(fp, buffer, sizeof(buffer)); | |
146 | ||
147 | for (size_t i = 0; i < length_; i++) | |
148 | EXPECT_EQ(json_[i], is.Take()); | |
149 | ||
150 | EXPECT_EQ(length_, is.Tell()); | |
151 | fclose(fp); | |
152 | ||
153 | //std::cout << filename << std::endl; | |
154 | remove(filename); | |
155 | } |