filestreamtest.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #include "unittest.h"
  15. #include "rapidjson/filereadstream.h"
  16. #include "rapidjson/filewritestream.h"
  17. #include "rapidjson/encodedstream.h"
  18. using namespace rapidjson;
  19. class FileStreamTest : public ::testing::Test {
  20. public:
  21. FileStreamTest() : filename_(), json_(), length_(), abcde_() {}
  22. virtual ~FileStreamTest();
  23. virtual void SetUp() {
  24. const char *paths[] = {
  25. "data/sample.json",
  26. "bin/data/sample.json",
  27. "../bin/data/sample.json",
  28. "../../bin/data/sample.json",
  29. "../../../bin/data/sample.json"
  30. };
  31. FILE* fp = 0;
  32. for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) {
  33. fp = fopen(paths[i], "rb");
  34. if (fp) {
  35. filename_ = paths[i];
  36. break;
  37. }
  38. }
  39. ASSERT_TRUE(fp != 0);
  40. fseek(fp, 0, SEEK_END);
  41. length_ = static_cast<size_t>(ftell(fp));
  42. fseek(fp, 0, SEEK_SET);
  43. json_ = static_cast<char*>(malloc(length_ + 1));
  44. size_t readLength = fread(json_, 1, length_, fp);
  45. json_[readLength] = '\0';
  46. fclose(fp);
  47. const char *abcde_paths[] = {
  48. "data/abcde.txt",
  49. "bin/data/abcde.txt",
  50. "../bin/data/abcde.txt",
  51. "../../bin/data/abcde.txt",
  52. "../../../bin/data/abcde.txt"
  53. };
  54. fp = 0;
  55. for (size_t i = 0; i < sizeof(abcde_paths) / sizeof(abcde_paths[0]); i++) {
  56. fp = fopen(abcde_paths[i], "rb");
  57. if (fp) {
  58. abcde_ = abcde_paths[i];
  59. break;
  60. }
  61. }
  62. ASSERT_TRUE(fp != 0);
  63. fclose(fp);
  64. }
  65. virtual void TearDown() {
  66. free(json_);
  67. json_ = 0;
  68. }
  69. private:
  70. FileStreamTest(const FileStreamTest&);
  71. FileStreamTest& operator=(const FileStreamTest&);
  72. protected:
  73. const char* filename_;
  74. char *json_;
  75. size_t length_;
  76. const char* abcde_;
  77. };
  78. FileStreamTest::~FileStreamTest() {}
  79. TEST_F(FileStreamTest, FileReadStream) {
  80. FILE *fp = fopen(filename_, "rb");
  81. ASSERT_TRUE(fp != 0);
  82. char buffer[65536];
  83. FileReadStream s(fp, buffer, sizeof(buffer));
  84. for (size_t i = 0; i < length_; i++) {
  85. EXPECT_EQ(json_[i], s.Peek());
  86. EXPECT_EQ(json_[i], s.Peek()); // 2nd time should be the same
  87. EXPECT_EQ(json_[i], s.Take());
  88. }
  89. EXPECT_EQ(length_, s.Tell());
  90. EXPECT_EQ('\0', s.Peek());
  91. fclose(fp);
  92. }
  93. TEST_F(FileStreamTest, FileReadStream_Peek4) {
  94. FILE *fp = fopen(abcde_, "rb");
  95. ASSERT_TRUE(fp != 0);
  96. char buffer[4];
  97. FileReadStream s(fp, buffer, sizeof(buffer));
  98. const char* c = s.Peek4();
  99. for (int i = 0; i < 4; i++)
  100. EXPECT_EQ('a' + i, c[i]);
  101. EXPECT_EQ(0u, s.Tell());
  102. for (int i = 0; i < 5; i++) {
  103. EXPECT_EQ(static_cast<size_t>(i), s.Tell());
  104. EXPECT_EQ('a' + i, s.Peek());
  105. EXPECT_EQ('a' + i, s.Peek());
  106. EXPECT_EQ('a' + i, s.Take());
  107. }
  108. EXPECT_EQ(5u, s.Tell());
  109. EXPECT_EQ(0, s.Peek());
  110. EXPECT_EQ(0, s.Take());
  111. fclose(fp);
  112. }
  113. TEST_F(FileStreamTest, FileWriteStream) {
  114. char filename[L_tmpnam];
  115. FILE* fp = TempFile(filename);
  116. char buffer[65536];
  117. FileWriteStream os(fp, buffer, sizeof(buffer));
  118. for (size_t i = 0; i < length_; i++)
  119. os.Put(json_[i]);
  120. os.Flush();
  121. fclose(fp);
  122. // Read it back to verify
  123. fp = fopen(filename, "rb");
  124. FileReadStream is(fp, buffer, sizeof(buffer));
  125. for (size_t i = 0; i < length_; i++)
  126. EXPECT_EQ(json_[i], is.Take());
  127. EXPECT_EQ(length_, is.Tell());
  128. fclose(fp);
  129. //std::cout << filename << std::endl;
  130. remove(filename);
  131. }