| 1 | #include "test.h" |
| 2 | |
| 3 | #include <base/fs.h> |
| 4 | #include <base/io.h> |
| 5 | #include <base/str.h> |
| 6 | |
| 7 | #include <engine/shared/linereader.h> |
| 8 | |
| 9 | #include <gtest/gtest.h> |
| 10 | |
| 11 | void TestFileLineReaderRaw(const char *pWritten, unsigned WrittenLength, std::initializer_list<const char *> pReads, bool ExpectSuccess, bool WriteBom) |
| 12 | { |
| 13 | CTestInfo Info; |
| 14 | IOHANDLE File = io_open(filename: Info.m_aFilename, flags: IOFLAG_WRITE); |
| 15 | ASSERT_TRUE(File); |
| 16 | if(WriteBom) |
| 17 | { |
| 18 | constexpr const unsigned char UTF8_BOM[] = {0xEF, 0xBB, 0xBF}; |
| 19 | EXPECT_EQ(io_write(File, UTF8_BOM, sizeof(UTF8_BOM)), sizeof(UTF8_BOM)); |
| 20 | } |
| 21 | EXPECT_EQ(io_write(File, pWritten, WrittenLength), WrittenLength); |
| 22 | EXPECT_FALSE(io_close(File)); |
| 23 | |
| 24 | CLineReader LineReader; |
| 25 | const bool ActualSuccess = LineReader.OpenFile(File: io_open(filename: Info.m_aFilename, flags: IOFLAG_READ)); |
| 26 | ASSERT_EQ(ActualSuccess, ExpectSuccess); |
| 27 | if(ActualSuccess) |
| 28 | { |
| 29 | for(const char *pRead : pReads) |
| 30 | { |
| 31 | const char *pReadLine = LineReader.Get(); |
| 32 | ASSERT_TRUE(pReadLine) << "Line reader returned less lines than expected" ; |
| 33 | EXPECT_STREQ(pReadLine, pRead) << "Line reader returned unexpected line" ; |
| 34 | } |
| 35 | EXPECT_FALSE(LineReader.Get()) << "Line reader returned more lines than expected" ; |
| 36 | } |
| 37 | |
| 38 | fs_remove(filename: Info.m_aFilename); |
| 39 | } |
| 40 | |
| 41 | void TestFileLineReaderRaw(const char *pWritten, unsigned WrittenLength, std::initializer_list<const char *> pReads, bool ExpectSuccess) |
| 42 | { |
| 43 | TestFileLineReaderRaw(pWritten, WrittenLength, pReads, ExpectSuccess, WriteBom: false); |
| 44 | TestFileLineReaderRaw(pWritten, WrittenLength, pReads, ExpectSuccess, WriteBom: true); |
| 45 | } |
| 46 | |
| 47 | void TestFileLineReader(const char *pWritten, std::initializer_list<const char *> pReads) |
| 48 | { |
| 49 | TestFileLineReaderRaw(pWritten, WrittenLength: str_length(str: pWritten), pReads, ExpectSuccess: true); |
| 50 | } |
| 51 | |
| 52 | TEST(LineReader, NormalNewline) |
| 53 | { |
| 54 | TestFileLineReader(pWritten: "foo\nbar\nbaz" , pReads: {"foo" , "bar" , "baz" }); |
| 55 | TestFileLineReader(pWritten: "foo\nbar\nbaz\n" , pReads: {"foo" , "bar" , "baz" }); |
| 56 | } |
| 57 | |
| 58 | TEST(LineReader, CRLFNewline) |
| 59 | { |
| 60 | TestFileLineReader(pWritten: "foo\r\nbar\r\nbaz" , pReads: {"foo" , "bar" , "baz" }); |
| 61 | TestFileLineReader(pWritten: "foo\r\nbar\r\nbaz\r\n" , pReads: {"foo" , "bar" , "baz" }); |
| 62 | } |
| 63 | |
| 64 | TEST(LineReader, MixedNewline) |
| 65 | { |
| 66 | TestFileLineReader(pWritten: "1\n2\r\n3\n4\n5\r\n6" , pReads: {"1" , "2" , "3" , "4" , "5" , "6" }); |
| 67 | TestFileLineReader(pWritten: "1\n2\r\n3\n4\n5\r\n6\n" , pReads: {"1" , "2" , "3" , "4" , "5" , "6" }); |
| 68 | TestFileLineReader(pWritten: "1\n2\r\n3\n4\n5\r\n6\r\n" , pReads: {"1" , "2" , "3" , "4" , "5" , "6" }); |
| 69 | TestFileLineReader(pWritten: "1\n2\r\n3\n4\n5\r\n6\r" , pReads: {"1" , "2" , "3" , "4" , "5" , "6\r" }); |
| 70 | } |
| 71 | |
| 72 | TEST(LineReader, EmptyLines) |
| 73 | { |
| 74 | TestFileLineReader(pWritten: "\n\r\n\n\n\r\n" , pReads: {"" , "" , "" , "" , "" }); |
| 75 | TestFileLineReader(pWritten: "\n\r\n\n\n\r\n\n" , pReads: {"" , "" , "" , "" , "" , "" }); |
| 76 | TestFileLineReader(pWritten: "\n\r\n\n\n\r\n\r\n" , pReads: {"" , "" , "" , "" , "" , "" }); |
| 77 | TestFileLineReader(pWritten: "\n\r\n\n\n\r\n\r" , pReads: {"" , "" , "" , "" , "" , "\r" }); |
| 78 | } |
| 79 | |
| 80 | TEST(LineReader, Invalid) |
| 81 | { |
| 82 | // Lines containing invalid UTF-8 are skipped |
| 83 | TestFileLineReader(pWritten: "foo\xff\nbar\xff\nbaz\xff" , pReads: {}); |
| 84 | TestFileLineReader(pWritten: "foo\xff\nbar\nbaz" , pReads: {"bar" , "baz" }); |
| 85 | TestFileLineReader(pWritten: "foo\nbar\xff\nbaz" , pReads: {"foo" , "baz" }); |
| 86 | TestFileLineReader(pWritten: "foo\nbar\nbaz\xff" , pReads: {"foo" , "bar" }); |
| 87 | TestFileLineReader(pWritten: "foo\nbar1\xff\nbar2\xff\nfoobar\nbar3\xff\nbaz" , pReads: {"foo" , "foobar" , "baz" }); |
| 88 | } |
| 89 | |
| 90 | TEST(LineReader, NullBytes) |
| 91 | { |
| 92 | // Line reader does not read any lines if the file contains null bytes |
| 93 | TestFileLineReaderRaw(pWritten: "foo\0\nbar\nbaz" , WrittenLength: 12, pReads: {}, ExpectSuccess: false); |
| 94 | TestFileLineReaderRaw(pWritten: "foo\nbar\0\nbaz" , WrittenLength: 12, pReads: {}, ExpectSuccess: false); |
| 95 | TestFileLineReaderRaw(pWritten: "foo\nbar\nbaz\0" , WrittenLength: 12, pReads: {}, ExpectSuccess: false); |
| 96 | } |
| 97 | |