filestreamtest.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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_() {}
  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. }
  48. virtual void TearDown() {
  49. free(json_);
  50. json_ = 0;
  51. }
  52. private:
  53. FileStreamTest(const FileStreamTest&);
  54. FileStreamTest& operator=(const FileStreamTest&);
  55. protected:
  56. const char* filename_;
  57. char *json_;
  58. size_t length_;
  59. };
  60. FileStreamTest::~FileStreamTest() {}
  61. TEST_F(FileStreamTest, FileReadStream) {
  62. FILE *fp = fopen(filename_, "rb");
  63. ASSERT_TRUE(fp != 0);
  64. char buffer[65536];
  65. FileReadStream s(fp, buffer, sizeof(buffer));
  66. for (size_t i = 0; i < length_; i++) {
  67. EXPECT_EQ(json_[i], s.Peek());
  68. EXPECT_EQ(json_[i], s.Peek()); // 2nd time should be the same
  69. EXPECT_EQ(json_[i], s.Take());
  70. }
  71. EXPECT_EQ(length_, s.Tell());
  72. EXPECT_EQ('\0', s.Peek());
  73. fclose(fp);
  74. }
  75. TEST_F(FileStreamTest, FileWriteStream) {
  76. char filename[L_tmpnam];
  77. FILE* fp = TempFile(filename);
  78. char buffer[65536];
  79. FileWriteStream os(fp, buffer, sizeof(buffer));
  80. for (size_t i = 0; i < length_; i++)
  81. os.Put(json_[i]);
  82. os.Flush();
  83. fclose(fp);
  84. // Read it back to verify
  85. fp = fopen(filename, "rb");
  86. FileReadStream is(fp, buffer, sizeof(buffer));
  87. for (size_t i = 0; i < length_; i++)
  88. EXPECT_EQ(json_[i], is.Take());
  89. EXPECT_EQ(length_, is.Tell());
  90. fclose(fp);
  91. //std::cout << filename << std::endl;
  92. remove(filename);
  93. }