LineReaderTest.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright 2013-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <folly/experimental/symbolizer/LineReader.h>
  17. #include <glog/logging.h>
  18. #include <folly/FileUtil.h>
  19. #include <folly/experimental/TestUtil.h>
  20. #include <folly/portability/GTest.h>
  21. namespace folly {
  22. namespace symbolizer {
  23. namespace test {
  24. using folly::test::TemporaryFile;
  25. void writeAll(int fd, const char* str) {
  26. ssize_t n = strlen(str);
  27. CHECK_EQ(n, writeFull(fd, str, n));
  28. }
  29. void expect(LineReader& lr, const char* expected) {
  30. StringPiece line;
  31. size_t expectedLen = strlen(expected);
  32. EXPECT_EQ(
  33. expectedLen != 0 ? LineReader::kReading : LineReader::kEof,
  34. lr.readLine(line));
  35. EXPECT_EQ(expectedLen, line.size());
  36. EXPECT_EQ(std::string(expected, expectedLen), line.str());
  37. }
  38. TEST(LineReader, Simple) {
  39. TemporaryFile file;
  40. int fd = file.fd();
  41. writeAll(
  42. fd,
  43. "Meow\n"
  44. "Hello world\n"
  45. "This is a long line. It is longer than the other lines.\n"
  46. "\n"
  47. "Incomplete last line");
  48. {
  49. CHECK_ERR(lseek(fd, 0, SEEK_SET));
  50. char buf[10];
  51. LineReader lr(fd, buf, sizeof(buf));
  52. expect(lr, "Meow\n");
  53. expect(lr, "Hello worl");
  54. expect(lr, "d\n");
  55. expect(lr, "This is a ");
  56. expect(lr, "long line.");
  57. expect(lr, " It is lon");
  58. expect(lr, "ger than t");
  59. expect(lr, "he other l");
  60. expect(lr, "ines.\n");
  61. expect(lr, "\n");
  62. expect(lr, "Incomplete");
  63. expect(lr, " last line");
  64. expect(lr, "");
  65. }
  66. {
  67. CHECK_ERR(lseek(fd, 0, SEEK_SET));
  68. char buf[80];
  69. LineReader lr(fd, buf, sizeof(buf));
  70. expect(lr, "Meow\n");
  71. expect(lr, "Hello world\n");
  72. expect(lr, "This is a long line. It is longer than the other lines.\n");
  73. expect(lr, "\n");
  74. expect(lr, "Incomplete last line");
  75. expect(lr, "");
  76. }
  77. }
  78. } // namespace test
  79. } // namespace symbolizer
  80. } // namespace folly