From 97d0d9c7089864709af4fd0c33fa6e52527dd7b5 Mon Sep 17 00:00:00 2001 From: sdottaka Date: Tue, 17 Dec 2024 22:54:08 +0900 Subject: [PATCH] WIP --- Src/CompareEngines/ByteCompare.cpp | 2 +- .../ByteCompare/ByteCompare_test.cpp | 344 ++++++++++++++++++ 2 files changed, 345 insertions(+), 1 deletion(-) diff --git a/Src/CompareEngines/ByteCompare.cpp b/Src/CompareEngines/ByteCompare.cpp index bfbae24660b..0484828d683 100644 --- a/Src/CompareEngines/ByteCompare.cpp +++ b/Src/CompareEngines/ByteCompare.cpp @@ -129,7 +129,7 @@ int ByteCompare::CompareFiles(DiffFileData* diffData) bfend[i] += rtn; if (m_pOptions->m_bIgnoreEofNewlinePresence) { - for (int j = (std::max)(bfstart[i], bfend[i] - 4); j < bfend[i]; ++j) + for (int64_t j = (std::max)(bfstart[i], bfend[i] - 4); j < bfend[i]; ++j) { const char c = buff[i][j]; if (c == '\r' || c == '\n') diff --git a/Testing/GoogleTest/ByteCompare/ByteCompare_test.cpp b/Testing/GoogleTest/ByteCompare/ByteCompare_test.cpp index 5de67084948..6b0e7cecd23 100644 --- a/Testing/GoogleTest/ByteCompare/ByteCompare_test.cpp +++ b/Testing/GoogleTest/ByteCompare/ByteCompare_test.cpp @@ -1184,4 +1184,348 @@ namespace } } + TEST_F(ByteCompareTest, IgnoreEofNewlinePresenceAndIgnoreBlankLines) + { + CompareEngines::ByteCompare bc; + QuickCompareOptions option; + std::string filename_left = "_tmp_.txt"; + std::string filename_right = "_tmp_2.txt"; + + option.m_bIgnoreEofNewlinePresence = true; + option.m_bIgnoreBlankLines = true; + bc.SetCompareOptions(option); + + for (int i = 0; i < 3; i++) + { + {// same left: LF - right: no EOL + std::vector buf_left(WMCMPBUFF * 2 - 1 + i); + std::vector buf_right(WMCMPBUFF * 2 - 1 + i); + + memset(buf_left.data(), 'A', buf_left.size()); + memset(buf_right.data(), 'A', buf_right.size()); + + buf_left[10] = '\n'; + buf_right[10] = '\n'; + buf_right[11] = '\n'; + + buf_left[buf_left.size() - 1] = '\n'; + + TempFile file_left(filename_left, buf_left.data(), buf_left.size()); + TempFile file_right(filename_right, buf_right.data(), buf_right.size()); + + FilePair pair(filename_left, filename_right); + + EXPECT_EQ(DIFFCODE::TEXT | DIFFCODE::SAME, bc.CompareFiles(&pair.diffData)); + } + + {// same left: no EOL - right: LF + std::vector buf_left(WMCMPBUFF * 2 - 1 + i); + std::vector buf_right(WMCMPBUFF * 2 - 1 + i); + + memset(buf_left.data(), 'A', buf_left.size()); + memset(buf_right.data(), 'A', buf_right.size()); + + buf_right[10] = '\n'; + buf_left[10] = '\n'; + buf_left[11] = '\n'; + + buf_right[buf_right.size() - 1] = '\n'; + + TempFile file_left(filename_left, buf_left.data(), buf_left.size()); + TempFile file_right(filename_right, buf_right.data(), buf_right.size()); + + FilePair pair(filename_left, filename_right); + + EXPECT_EQ(DIFFCODE::TEXT | DIFFCODE::SAME, bc.CompareFiles(&pair.diffData)); + } + } + + for (int i = 0; i < 3; i++) + { + {// same left: CRLF - right: no EOL + std::vector buf_left(WMCMPBUFF * 2 - 2 + i); + std::vector buf_right(WMCMPBUFF * 2 - 2 + i); + + memset(buf_left.data(), 'A', buf_left.size()); + memset(buf_right.data(), 'A', buf_right.size()); + + buf_left[10] = '\n'; + buf_right[10] = '\n'; + buf_right[11] = '\n'; + + buf_left[19] = '\n'; + buf_right[20] = '\n'; + buf_right[21] = '\n'; + + buf_left[buf_left.size() - 2] = '\r'; + buf_left[buf_left.size() - 1] = '\n'; + + TempFile file_left(filename_left, buf_left.data(), buf_left.size()); + TempFile file_right(filename_right, buf_right.data(), buf_right.size()); + + FilePair pair(filename_left, filename_right); + + EXPECT_EQ(DIFFCODE::TEXT | DIFFCODE::SAME, bc.CompareFiles(&pair.diffData)); + } + + {// same left: no EOL - right: CRLF + std::vector buf_left(WMCMPBUFF * 2 - 2 + i); + std::vector buf_right(WMCMPBUFF * 2 - 2 + i); + + memset(buf_left.data(), 'A', buf_left.size()); + memset(buf_right.data(), 'A', buf_right.size()); + + buf_right[10] = '\n'; + buf_left[10] = '\n'; + buf_left[11] = '\n'; + + buf_right[19] = '\n'; + buf_left[20] = '\n'; + buf_left[21] = '\n'; + + buf_right[buf_right.size() - 2] = '\r'; + buf_right[buf_right.size() - 1] = '\n'; + + TempFile file_left(filename_left, buf_left.data(), buf_left.size()); + TempFile file_right(filename_right, buf_right.data(), buf_right.size()); + + FilePair pair(filename_left, filename_right); + + EXPECT_EQ(DIFFCODE::TEXT | DIFFCODE::SAME, bc.CompareFiles(&pair.diffData)); + } + } + + for (int i = 0; i < 3; i++) + { + {// diff left: CR+A - right: no EOL + std::vector buf_left(WMCMPBUFF * 2 - 2 + i); + std::vector buf_right(WMCMPBUFF * 2 - 2 + i); + + memset(buf_left.data(), 'A', buf_left.size()); + memset(buf_right.data(), 'A', buf_right.size()); + + buf_left[10] = '\n'; + buf_right[10] = '\n'; + buf_right[11] = '\n'; + + buf_left[19] = '\n'; + buf_right[20] = '\n'; + buf_right[21] = '\n'; + + buf_left[buf_left.size() - 2] = '\r'; + buf_left[buf_left.size() - 1] = 'A'; + + TempFile file_left(filename_left, buf_left.data(), buf_left.size()); + TempFile file_right(filename_right, buf_right.data(), buf_right.size()); + + FilePair pair(filename_left, filename_right); + + EXPECT_EQ(DIFFCODE::TEXT | DIFFCODE::DIFF, bc.CompareFiles(&pair.diffData)); + } + + {// diff left: no EOL - right: CR+A + std::vector buf_left(WMCMPBUFF * 2 - 2 + i); + std::vector buf_right(WMCMPBUFF * 2 - 2 + i); + + memset(buf_left.data(), 'A', buf_left.size()); + memset(buf_right.data(), 'A', buf_right.size()); + + buf_right[10] = '\n'; + buf_left[10] = '\n'; + buf_left[11] = '\n'; + + buf_right[19] = '\n'; + buf_left[20] = '\n'; + buf_left[21] = '\n'; + + buf_right[buf_right.size() - 2] = '\r'; + buf_right[buf_right.size() - 1] = 'A'; + + TempFile file_left(filename_left, buf_left.data(), buf_left.size()); + TempFile file_right(filename_right, buf_right.data(), buf_right.size()); + + FilePair pair(filename_left, filename_right); + + EXPECT_EQ(DIFFCODE::TEXT | DIFFCODE::DIFF, bc.CompareFiles(&pair.diffData)); + } + } + + for (int i = 0; i < 3; i++) + { + {// diff left: LF+LF - right: no EOL + std::vector buf_left(WMCMPBUFF * 2 - 2 + i); + std::vector buf_right(WMCMPBUFF * 2 - 2 + i); + + memset(buf_left.data(), 'A', buf_left.size()); + memset(buf_right.data(), 'A', buf_right.size()); + + buf_left[10] = '\n'; + buf_right[10] = '\n'; + buf_right[11] = '\n'; + + buf_left[19] = '\n'; + buf_right[20] = '\n'; + buf_right[21] = '\n'; + + buf_left[buf_left.size() - 2] = '\n'; + buf_left[buf_left.size() - 1] = '\n'; + + TempFile file_left(filename_left, buf_left.data(), buf_left.size()); + TempFile file_right(filename_right, buf_right.data(), buf_right.size()); + + FilePair pair(filename_left, filename_right); + + EXPECT_EQ(DIFFCODE::TEXT | DIFFCODE::DIFF, bc.CompareFiles(&pair.diffData)); + } + + {// diff left: no EOL - right: LF+LF + std::vector buf_left(WMCMPBUFF * 2 - 2 + i); + std::vector buf_right(WMCMPBUFF * 2 - 2 + i); + + memset(buf_left.data(), 'A', buf_left.size()); + memset(buf_right.data(), 'A', buf_right.size()); + + buf_right[10] = '\n'; + buf_left[10] = '\n'; + buf_left[11] = '\n'; + + buf_right[19] = '\n'; + buf_left[20] = '\n'; + buf_left[21] = '\n'; + + buf_right[buf_right.size() - 2] = '\n'; + buf_right[buf_right.size() - 1] = '\n'; + + TempFile file_left(filename_left, buf_left.data(), buf_left.size()); + TempFile file_right(filename_right, buf_right.data(), buf_right.size()); + + FilePair pair(filename_left, filename_right); + + EXPECT_EQ(DIFFCODE::TEXT | DIFFCODE::DIFF, bc.CompareFiles(&pair.diffData)); + } + } + + for (int i = 0; i < 3; i++) + { + {// diff left: CR+CR - right: no EOL + std::vector buf_left(WMCMPBUFF * 2 - 2 + i); + std::vector buf_right(WMCMPBUFF * 2 - 2 + i); + + memset(buf_left.data(), 'A', buf_left.size()); + memset(buf_right.data(), 'A', buf_right.size()); + + buf_left[10] = '\n'; + buf_right[10] = '\n'; + buf_right[11] = '\n'; + + buf_left[19] = '\n'; + buf_right[20] = '\n'; + buf_right[21] = '\n'; + + buf_left[buf_left.size() - 2] = '\r'; + buf_left[buf_left.size() - 1] = '\r'; + + TempFile file_left(filename_left, buf_left.data(), buf_left.size()); + TempFile file_right(filename_right, buf_right.data(), buf_right.size()); + + FilePair pair(filename_left, filename_right); + + EXPECT_EQ(DIFFCODE::TEXT | DIFFCODE::DIFF, bc.CompareFiles(&pair.diffData)); + } + + {// diff left: no EOL - right: CR+CR + std::vector buf_left(WMCMPBUFF * 2 - 2 + i); + std::vector buf_right(WMCMPBUFF * 2 - 2 + i); + + memset(buf_left.data(), 'A', buf_left.size()); + memset(buf_right.data(), 'A', buf_right.size()); + + buf_right[10] = '\n'; + buf_left[10] = '\n'; + buf_left[11] = '\n'; + + buf_right[19] = '\n'; + buf_left[20] = '\n'; + buf_left[21] = '\n'; + + buf_right[buf_right.size() - 2] = '\r'; + buf_right[buf_right.size() - 1] = '\r'; + + TempFile file_left(filename_left, buf_left.data(), buf_left.size()); + TempFile file_right(filename_right, buf_right.data(), buf_right.size()); + + FilePair pair(filename_left, filename_right); + + EXPECT_EQ(DIFFCODE::TEXT | DIFFCODE::DIFF, bc.CompareFiles(&pair.diffData)); + } + } + + for (int i = 0; i < 4; i++) + { + {// diff left: CRLF+CRLF - right: no EOL + std::vector buf_left(WMCMPBUFF * 2 - 4 + i); + std::vector buf_right(WMCMPBUFF * 2 - 4 + i); + + memset(buf_left.data(), 'A', buf_left.size()); + memset(buf_right.data(), 'A', buf_right.size()); + + buf_left[10] = '\n'; + buf_right[10] = '\n'; + buf_right[11] = '\n'; + + buf_left[19] = '\n'; + buf_right[20] = '\n'; + buf_right[21] = '\n'; + + buf_left[28] = '\n'; + buf_right[30] = '\n'; + buf_right[31] = '\n'; + + buf_left[buf_left.size() - 4] = '\r'; + buf_left[buf_left.size() - 3] = '\n'; + buf_left[buf_left.size() - 2] = '\r'; + buf_left[buf_left.size() - 1] = '\n'; + + TempFile file_left(filename_left, buf_left.data(), buf_left.size()); + TempFile file_right(filename_right, buf_right.data(), buf_right.size()); + + FilePair pair(filename_left, filename_right); + + EXPECT_EQ(DIFFCODE::TEXT | DIFFCODE::DIFF, bc.CompareFiles(&pair.diffData)); + } + + {// diff left: no EOL - right: CRLF+CRLF + std::vector buf_left(WMCMPBUFF * 2 - 4 + i); + std::vector buf_right(WMCMPBUFF * 2 - 4 + i); + + memset(buf_left.data(), 'A', buf_left.size()); + memset(buf_right.data(), 'A', buf_right.size()); + + buf_right[10] = '\n'; + buf_left[10] = '\n'; + buf_left[11] = '\n'; + + buf_right[19] = '\n'; + buf_left[20] = '\n'; + buf_left[21] = '\n'; + + buf_right[28] = '\n'; + buf_left[30] = '\n'; + buf_left[31] = '\n'; + + buf_right[buf_right.size() - 4] = '\r'; + buf_right[buf_right.size() - 3] = '\n'; + buf_right[buf_right.size() - 2] = '\r'; + buf_right[buf_right.size() - 1] = '\n'; + + TempFile file_left(filename_left, buf_left.data(), buf_left.size()); + TempFile file_right(filename_right, buf_right.data(), buf_right.size()); + + FilePair pair(filename_left, filename_right); + + EXPECT_EQ(DIFFCODE::TEXT | DIFFCODE::DIFF, bc.CompareFiles(&pair.diffData)); + } + } + } + } // namespace