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:
FileStreamTest()24     FileStreamTest() : filename_(), json_(), length_() {}
25 
SetUp()26     virtual void SetUp() {
27         const char *paths[] = {
28             "data/sample.json",
29             "bin/data/sample.json",
30             "../bin/data/sample.json",
31             "../../bin/data/sample.json",
32             "../../../bin/data/sample.json"
33         };
34         FILE* fp = 0;
35         for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) {
36             fp = fopen(paths[i], "rb");
37             if (fp) {
38                 filename_ = paths[i];
39                 break;
40             }
41         }
42         ASSERT_TRUE(fp != 0);
43 
44         fseek(fp, 0, SEEK_END);
45         length_ = (size_t)ftell(fp);
46         fseek(fp, 0, SEEK_SET);
47         json_ = (char*)malloc(length_ + 1);
48         size_t readLength = fread(json_, 1, length_, fp);
49         json_[readLength] = '\0';
50         fclose(fp);
51     }
52 
TearDown()53     virtual void TearDown() {
54         free(json_);
55         json_ = 0;
56     }
57 
58 private:
59     FileStreamTest(const FileStreamTest&);
60     FileStreamTest& operator=(const FileStreamTest&);
61 
62 protected:
63     const char* filename_;
64     char *json_;
65     size_t length_;
66 };
67 
TEST_F(FileStreamTest,FileReadStream)68 TEST_F(FileStreamTest, FileReadStream) {
69     FILE *fp = fopen(filename_, "rb");
70     ASSERT_TRUE(fp != 0);
71     char buffer[65536];
72     FileReadStream s(fp, buffer, sizeof(buffer));
73 
74     for (size_t i = 0; i < length_; i++) {
75         EXPECT_EQ(json_[i], s.Peek());
76         EXPECT_EQ(json_[i], s.Peek());  // 2nd time should be the same
77         EXPECT_EQ(json_[i], s.Take());
78     }
79 
80     EXPECT_EQ(length_, s.Tell());
81     EXPECT_EQ('\0', s.Peek());
82 
83     fclose(fp);
84 }
85 
TEST_F(FileStreamTest,FileWriteStream)86 TEST_F(FileStreamTest, FileWriteStream) {
87     char filename[L_tmpnam];
88     FILE* fp = TempFile(filename);
89 
90     char buffer[65536];
91     FileWriteStream os(fp, buffer, sizeof(buffer));
92     for (size_t i = 0; i < length_; i++)
93         os.Put(json_[i]);
94     os.Flush();
95     fclose(fp);
96 
97     // Read it back to verify
98     fp = fopen(filename, "rb");
99     FileReadStream is(fp, buffer, sizeof(buffer));
100 
101     for (size_t i = 0; i < length_; i++)
102         EXPECT_EQ(json_[i], is.Take());
103 
104     EXPECT_EQ(length_, is.Tell());
105     fclose(fp);
106 
107     //std::cout << filename << std::endl;
108     remove(filename);
109 }
110