LineBufferTest.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (C) 2015 The Android Open Source Project
  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 <gtest/gtest.h>
  17. #include <string>
  18. #include <android-base/file.h>
  19. #include "LineBuffer.h"
  20. class LineBufferTest : public ::testing::Test {
  21. protected:
  22. void SetUp() override {
  23. tmp_file_ = new TemporaryFile();
  24. ASSERT_TRUE(tmp_file_->fd != -1);
  25. }
  26. void TearDown() override {
  27. delete tmp_file_;
  28. }
  29. TemporaryFile* tmp_file_ = nullptr;
  30. };
  31. TEST_F(LineBufferTest, single_line) {
  32. std::string line_data;
  33. line_data += "Single line with newline.\n";
  34. ASSERT_TRUE(TEMP_FAILURE_RETRY(
  35. write(tmp_file_->fd, line_data.c_str(), line_data.size())) != -1);
  36. ASSERT_TRUE(lseek(tmp_file_->fd, 0, SEEK_SET) != off_t(-1));
  37. char buffer[100];
  38. LineBuffer line_buf(tmp_file_->fd, buffer, sizeof(buffer));
  39. char* line;
  40. size_t line_len;
  41. ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
  42. ASSERT_STREQ("Single line with newline.", line);
  43. ASSERT_EQ(sizeof("Single line with newline.") - 1, line_len);
  44. ASSERT_FALSE(line_buf.GetLine(&line, &line_len));
  45. }
  46. TEST_F(LineBufferTest, single_line_no_newline) {
  47. std::string line_data;
  48. line_data += "Single line with no newline.";
  49. ASSERT_TRUE(TEMP_FAILURE_RETRY(
  50. write(tmp_file_->fd, line_data.c_str(), line_data.size())) != -1);
  51. ASSERT_TRUE(lseek(tmp_file_->fd, 0, SEEK_SET) != off_t(-1));
  52. char buffer[100];
  53. LineBuffer line_buf(tmp_file_->fd, buffer, sizeof(buffer));
  54. char* line;
  55. size_t line_len;
  56. ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
  57. ASSERT_STREQ("Single line with no newline.", line);
  58. ASSERT_EQ(sizeof("Single line with no newline.") - 1, line_len);
  59. ASSERT_FALSE(line_buf.GetLine(&line, &line_len));
  60. }
  61. TEST_F(LineBufferTest, single_read) {
  62. std::string line_data;
  63. line_data += "The first line.\n";
  64. line_data += "Second line here.\n";
  65. line_data += "Third line is last.\n";
  66. ASSERT_TRUE(TEMP_FAILURE_RETRY(
  67. write(tmp_file_->fd, line_data.c_str(), line_data.size())) != -1);
  68. ASSERT_TRUE(lseek(tmp_file_->fd, 0, SEEK_SET) != off_t(-1));
  69. char buffer[100];
  70. LineBuffer line_buf(tmp_file_->fd, buffer, sizeof(buffer));
  71. char* line;
  72. size_t line_len;
  73. ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
  74. ASSERT_STREQ("The first line.", line);
  75. ASSERT_EQ(sizeof("The first line.") - 1, line_len);
  76. ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
  77. ASSERT_STREQ("Second line here.", line);
  78. ASSERT_EQ(sizeof("Second line here.") - 1, line_len);
  79. ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
  80. ASSERT_STREQ("Third line is last.", line);
  81. ASSERT_EQ(sizeof("Third line is last.") - 1, line_len);
  82. ASSERT_FALSE(line_buf.GetLine(&line, &line_len));
  83. }
  84. TEST_F(LineBufferTest, single_read_no_end_newline) {
  85. std::string line_data;
  86. line_data += "The first line.\n";
  87. line_data += "Second line here.\n";
  88. line_data += "Third line is last no newline.";
  89. ASSERT_TRUE(TEMP_FAILURE_RETRY(
  90. write(tmp_file_->fd, line_data.c_str(), line_data.size())) != -1);
  91. ASSERT_TRUE(lseek(tmp_file_->fd, 0, SEEK_SET) != off_t(-1));
  92. char buffer[100];
  93. LineBuffer line_buf(tmp_file_->fd, buffer, sizeof(buffer));
  94. char* line;
  95. size_t line_len;
  96. ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
  97. ASSERT_STREQ("The first line.", line);
  98. ASSERT_EQ(sizeof("The first line.") - 1, line_len);
  99. ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
  100. ASSERT_STREQ("Second line here.", line);
  101. ASSERT_EQ(sizeof("Second line here.") - 1, line_len);
  102. ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
  103. ASSERT_STREQ("Third line is last no newline.", line);
  104. ASSERT_EQ(sizeof("Third line is last no newline.") - 1, line_len);
  105. ASSERT_FALSE(line_buf.GetLine(&line, &line_len));
  106. }
  107. TEST_F(LineBufferTest, one_line_per_read) {
  108. std::string line_data;
  109. line_data += "The first line.\n";
  110. line_data += "Second line here.\n";
  111. line_data += "Third line is last.\n";
  112. line_data += "The fourth line.\n";
  113. ASSERT_TRUE(TEMP_FAILURE_RETRY(
  114. write(tmp_file_->fd, line_data.c_str(), line_data.size())) != -1);
  115. ASSERT_TRUE(lseek(tmp_file_->fd, 0, SEEK_SET) != off_t(-1));
  116. char buffer[24];
  117. LineBuffer line_buf(tmp_file_->fd, buffer, sizeof(buffer));
  118. char* line;
  119. size_t line_len;
  120. ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
  121. ASSERT_STREQ("The first line.", line);
  122. ASSERT_EQ(sizeof("The first line.") - 1, line_len);
  123. ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
  124. ASSERT_STREQ("Second line here.", line);
  125. ASSERT_EQ(sizeof("Second line here.") - 1, line_len);
  126. ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
  127. ASSERT_STREQ("Third line is last.", line);
  128. ASSERT_EQ(sizeof("Third line is last.") - 1, line_len);
  129. line_data += "The fourth line.\n";
  130. ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
  131. ASSERT_STREQ("The fourth line.", line);
  132. ASSERT_EQ(sizeof("The fourth line.") - 1, line_len);
  133. ASSERT_FALSE(line_buf.GetLine(&line, &line_len));
  134. }
  135. TEST_F(LineBufferTest, multiple_line_per_read_multiple_reads) {
  136. std::string line_data;
  137. line_data += "The first line.\n";
  138. line_data += "Second line here.\n";
  139. line_data += "Third line is last.\n";
  140. line_data += "The fourth line.\n";
  141. ASSERT_TRUE(TEMP_FAILURE_RETRY(
  142. write(tmp_file_->fd, line_data.c_str(), line_data.size())) != -1);
  143. ASSERT_TRUE(lseek(tmp_file_->fd, 0, SEEK_SET) != off_t(-1));
  144. char buffer[60];
  145. LineBuffer line_buf(tmp_file_->fd, buffer, sizeof(buffer));
  146. char* line;
  147. size_t line_len;
  148. ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
  149. ASSERT_STREQ("The first line.", line);
  150. ASSERT_EQ(sizeof("The first line.") - 1, line_len);
  151. ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
  152. ASSERT_STREQ("Second line here.", line);
  153. ASSERT_EQ(sizeof("Second line here.") - 1, line_len);
  154. ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
  155. ASSERT_STREQ("Third line is last.", line);
  156. ASSERT_EQ(sizeof("Third line is last.") - 1, line_len);
  157. line_data += "The fourth line.\n";
  158. ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
  159. ASSERT_STREQ("The fourth line.", line);
  160. ASSERT_EQ(sizeof("The fourth line.") - 1, line_len);
  161. ASSERT_FALSE(line_buf.GetLine(&line, &line_len));
  162. }
  163. TEST_F(LineBufferTest, line_larger_than_buffer) {
  164. std::string line_data;
  165. line_data += "The first line.\n";
  166. line_data += "Second line here.\n";
  167. line_data += "This is a really, really, really, kind of long.\n";
  168. line_data += "The fourth line.\n";
  169. ASSERT_TRUE(TEMP_FAILURE_RETRY(
  170. write(tmp_file_->fd, line_data.c_str(), line_data.size())) != -1);
  171. ASSERT_TRUE(lseek(tmp_file_->fd, 0, SEEK_SET) != off_t(-1));
  172. char buffer[25];
  173. LineBuffer line_buf(tmp_file_->fd, buffer, sizeof(buffer));
  174. char* line;
  175. size_t line_len;
  176. ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
  177. ASSERT_STREQ("The first line.", line);
  178. ASSERT_EQ(sizeof("The first line.") - 1, line_len);
  179. ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
  180. ASSERT_STREQ("Second line here.", line);
  181. ASSERT_EQ(sizeof("Second line here.") - 1, line_len);
  182. ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
  183. ASSERT_STREQ("This is a really, really", line);
  184. ASSERT_EQ(sizeof(buffer) - 1, line_len);
  185. ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
  186. ASSERT_STREQ(", really, kind of long.", line);
  187. ASSERT_EQ(sizeof(", really, kind of long.") - 1, line_len);
  188. line_data += "The fourth line.\n";
  189. ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
  190. ASSERT_STREQ("The fourth line.", line);
  191. ASSERT_EQ(sizeof("The fourth line.") - 1, line_len);
  192. ASSERT_FALSE(line_buf.GetLine(&line, &line_len));
  193. }