|
| 1 | +#include "LineReader.h" |
| 2 | + |
| 3 | +#include "TestHelpers.h" |
| 4 | + |
| 5 | +#include <algorithm> |
| 6 | +#include <string> |
| 7 | + |
| 8 | +#include "gtest/gtest.h" |
| 9 | + |
| 10 | + |
| 11 | +namespace |
| 12 | +{ |
| 13 | +# define CLineReader CLockFreeLineReader |
| 14 | + |
| 15 | + const size_t MaxLogLineLength = 1024; // copy-pasted value from LineReader.cpp |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | +TEST(CLineReader, Open) |
| 20 | +{ |
| 21 | + TempFile file(""); |
| 22 | + CLineReader reader; |
| 23 | + EXPECT_TRUE(reader.Open(file.GetFilename().c_str())); |
| 24 | + reader.Close(); |
| 25 | +} |
| 26 | + |
| 27 | +TEST(CLineReader, MissedOpen) |
| 28 | +{ |
| 29 | + CLineReader reader; |
| 30 | + const auto line = reader.GetNextLine(); |
| 31 | + EXPECT_FALSE(line); |
| 32 | +} |
| 33 | + |
| 34 | +TEST(CLineReader, EmptyFile) |
| 35 | +{ |
| 36 | + TempFile file(""); |
| 37 | + CLineReader reader; |
| 38 | + EXPECT_TRUE(reader.Open(file.GetFilename().c_str())); |
| 39 | + const auto line = reader.GetNextLine(); |
| 40 | + EXPECT_FALSE(line); |
| 41 | +} |
| 42 | + |
| 43 | +TEST(CLineReader, OneLineNoLF) |
| 44 | +{ |
| 45 | + TempFile file("ABCD"); |
| 46 | + CLineReader reader; |
| 47 | + EXPECT_TRUE(reader.Open(file.GetFilename().c_str())); |
| 48 | + auto line = reader.GetNextLine(); |
| 49 | + ASSERT_TRUE(line); |
| 50 | + EXPECT_EQ(std::string(*line), "ABCD"); |
| 51 | + line = reader.GetNextLine(); |
| 52 | + EXPECT_FALSE(line); |
| 53 | +} |
| 54 | + |
| 55 | +TEST(CLineReader, OneLineCRLF) |
| 56 | +{ |
| 57 | + TempFile file("ABCD\r\n"); |
| 58 | + CLineReader reader; |
| 59 | + EXPECT_TRUE(reader.Open(file.GetFilename().c_str())); |
| 60 | + auto line = reader.GetNextLine(); |
| 61 | + ASSERT_TRUE(line); |
| 62 | + EXPECT_EQ(std::string(*line), "ABCD\r\n"); |
| 63 | + line = reader.GetNextLine(); |
| 64 | + EXPECT_FALSE(line); |
| 65 | +} |
| 66 | + |
| 67 | +TEST(CLineReader, OneLineLF) |
| 68 | +{ |
| 69 | + TempFile file("ABCD\n"); |
| 70 | + CLineReader reader; |
| 71 | + EXPECT_TRUE(reader.Open(file.GetFilename().c_str())); |
| 72 | + auto line = reader.GetNextLine(); |
| 73 | + ASSERT_TRUE(line); |
| 74 | + EXPECT_EQ(std::string(*line), "ABCD\n"); |
| 75 | + line = reader.GetNextLine(); |
| 76 | + EXPECT_FALSE(line); |
| 77 | +} |
| 78 | + |
| 79 | +TEST(CLineReader, TwoLinesLF_NoLF) |
| 80 | +{ |
| 81 | + TempFile file("abc\nDEFG"); |
| 82 | + CLineReader reader; |
| 83 | + EXPECT_TRUE(reader.Open(file.GetFilename().c_str())); |
| 84 | + auto line = reader.GetNextLine(); |
| 85 | + ASSERT_TRUE(line); |
| 86 | + EXPECT_EQ(std::string(*line), "abc\n"); |
| 87 | + line = reader.GetNextLine(); |
| 88 | + ASSERT_TRUE(line); |
| 89 | + EXPECT_EQ(std::string(*line), "DEFG"); |
| 90 | + line = reader.GetNextLine(); |
| 91 | + EXPECT_FALSE(line); |
| 92 | +} |
| 93 | + |
| 94 | +TEST(CLineReader, TwoLinesLF_LF) |
| 95 | +{ |
| 96 | + TempFile file("abc\nDEFG\n"); |
| 97 | + CLineReader reader; |
| 98 | + EXPECT_TRUE(reader.Open(file.GetFilename().c_str())); |
| 99 | + auto line = reader.GetNextLine(); |
| 100 | + ASSERT_TRUE(line); |
| 101 | + EXPECT_EQ(std::string(*line), "abc\n"); |
| 102 | + line = reader.GetNextLine(); |
| 103 | + ASSERT_TRUE(line); |
| 104 | + EXPECT_EQ(std::string(*line), "DEFG\n"); |
| 105 | + line = reader.GetNextLine(); |
| 106 | + EXPECT_FALSE(line); |
| 107 | +} |
| 108 | + |
| 109 | +TEST(CLineReader, EmptyLines) |
| 110 | +{ |
| 111 | + TempFile file("\n\n"); |
| 112 | + CLineReader reader; |
| 113 | + EXPECT_TRUE(reader.Open(file.GetFilename().c_str())); |
| 114 | + auto line = reader.GetNextLine(); |
| 115 | + ASSERT_TRUE(line); |
| 116 | + EXPECT_EQ(std::string(*line), "\n"); |
| 117 | + line = reader.GetNextLine(); |
| 118 | + ASSERT_TRUE(line); |
| 119 | + EXPECT_EQ(std::string(*line), "\n"); |
| 120 | + line = reader.GetNextLine(); |
| 121 | + EXPECT_FALSE(line); |
| 122 | +} |
| 123 | + |
| 124 | +TEST(CLineReader, ThreeLines) |
| 125 | +{ |
| 126 | + TempFile file("Abcdef\n\n3rd Line"); |
| 127 | + CLineReader reader; |
| 128 | + EXPECT_TRUE(reader.Open(file.GetFilename().c_str())); |
| 129 | + auto line = reader.GetNextLine(); |
| 130 | + ASSERT_TRUE(line); |
| 131 | + EXPECT_EQ(std::string(*line), "Abcdef\n"); |
| 132 | + line = reader.GetNextLine(); |
| 133 | + ASSERT_TRUE(line); |
| 134 | + EXPECT_EQ(std::string(*line), "\n"); |
| 135 | + line = reader.GetNextLine(); |
| 136 | + ASSERT_TRUE(line); |
| 137 | + EXPECT_EQ(std::string(*line), "3rd Line"); |
| 138 | + line = reader.GetNextLine(); |
| 139 | + EXPECT_FALSE(line); |
| 140 | +} |
| 141 | + |
| 142 | +TEST(CLineReader, LineMaxLength_1) |
| 143 | +{ |
| 144 | + const std::string str = std::string(MaxLogLineLength, 'x'); |
| 145 | + TempFile file(str); |
| 146 | + CLineReader reader; |
| 147 | + EXPECT_TRUE(reader.Open(file.GetFilename().c_str())); |
| 148 | + auto line = reader.GetNextLine(); |
| 149 | + ASSERT_TRUE(line); |
| 150 | + EXPECT_EQ(*line, str); |
| 151 | + line = reader.GetNextLine(); |
| 152 | + EXPECT_FALSE(line); |
| 153 | +} |
| 154 | + |
| 155 | +TEST(CLineReader, LineMaxLength_2) |
| 156 | +{ |
| 157 | + const std::string str = std::string(MaxLogLineLength - 1, 'x'); |
| 158 | + TempFile file(str + "\n"); |
| 159 | + CLineReader reader; |
| 160 | + EXPECT_TRUE(reader.Open(file.GetFilename().c_str())); |
| 161 | + auto line = reader.GetNextLine(); |
| 162 | + ASSERT_TRUE(line); |
| 163 | + EXPECT_EQ(*line, str + "\n"); |
| 164 | + line = reader.GetNextLine(); |
| 165 | + EXPECT_FALSE(line); |
| 166 | +} |
| 167 | + |
| 168 | +TEST(CLineReader, LineTooLong_1) |
| 169 | +{ |
| 170 | + const std::string str = std::string(MaxLogLineLength + 1, 'x'); |
| 171 | + TempFile file(str); |
| 172 | + CLineReader reader; |
| 173 | + EXPECT_TRUE(reader.Open(file.GetFilename().c_str())); |
| 174 | + auto line = reader.GetNextLine(); |
| 175 | + EXPECT_FALSE(line); |
| 176 | +} |
| 177 | + |
| 178 | +TEST(CLineReader, LineTooLong_2) |
| 179 | +{ |
| 180 | + const std::string str = std::string(MaxLogLineLength, 'x'); |
| 181 | + TempFile file(str + "\n"); |
| 182 | + CLineReader reader; |
| 183 | + EXPECT_TRUE(reader.Open(file.GetFilename().c_str())); |
| 184 | + auto line = reader.GetNextLine(); |
| 185 | + EXPECT_FALSE(line); |
| 186 | +} |
0 commit comments