OSDN Git Service

Fix that WinMerge sometimes reported wrong results in Quick Contents Compare Mode(2)
authorsdottaka <none@none>
Sun, 3 Mar 2013 14:27:31 +0000 (23:27 +0900)
committersdottaka <none@none>
Sun, 3 Mar 2013 14:27:31 +0000 (23:27 +0900)
Src/CompareEngines/ByteComparator.cpp
Src/CompareEngines/ByteCompare.cpp
Testing/GoogleTest/ByteCompare/ByteCompare_test.cpp [new file with mode: 0644]

index 62f0c98..4bbe799 100644 (file)
@@ -345,7 +345,7 @@ ByteComparator::COMP_RESULT ByteComparator::CompareBuffers(
                                }
                                if (m_eol0 || m_eol1)
                                {
-                                       if (!m_eol0 || !m_eol1)
+                                       if ((!m_eol0 || !m_eol1) && (orig0 == end0 || orig1 == end1))
                                        {
                                                // one side had an end-of-line, but the other didn't
                                                return RESULT_DIFF;
@@ -445,7 +445,7 @@ need_more:
  * @param [in] end Pointer to the buffer end.
  * @param [in] eof Are we at end of the buffer?
  */
-void ByteComparator::HandleSide0Eol(char **ptr, const char *end, bool eof)
+inline void ByteComparator::HandleSide0Eol(char **ptr, const char *end, bool eof)
 {
        char * pbuf = *ptr;
        if (m_cr0)
@@ -497,7 +497,7 @@ void ByteComparator::HandleSide0Eol(char **ptr, const char *end, bool eof)
  * @param [in] end Pointer to the buffer end.
  * @param [in] eof Are we at end of the buffer?
  */
-void ByteComparator::HandleSide1Eol(char **ptr, const char *end, bool eof)
+inline void ByteComparator::HandleSide1Eol(char **ptr, const char *end, bool eof)
 {
        char * pbuf = *ptr;
 
index 83a9eb6..a60dbf4 100644 (file)
@@ -213,7 +213,7 @@ int ByteCompare::CompareFiles(FileLocation *location)
                        //move uncompared data to begin of buff0
                        memcpy(&buff[0][0], &buff[0][m], l);
                        bfstart[0] = 0;
-                       bfstart[1] = ptr1 - orig1;
+                       bfstart[1] += ptr1 - orig1;
                        bfend[0] = l;
                }
                else if (result == ByteComparator::NEED_MORE_1)
@@ -223,16 +223,16 @@ int ByteCompare::CompareFiles(FileLocation *location)
                        //move uncompared data to begin of buff1
                        memcpy(&buff[1][0], &buff[1][m], l);
                        bfstart[1] = 0;
-                       bfstart[0] = ptr0 - orig0;
+                       bfstart[0] += ptr0 - orig0;
                        bfend[1] = l;
                }
                else if (result == ByteComparator::NEED_MORE_BOTH)
                {
                        if ((end0 == ptr0) && (end1 == ptr1))
                        {
-                               bfstart[0] = ptr0 - orig0;
+                               bfstart[0] += ptr0 - orig0;
                                bfend[0] = 0;
-                               bfstart[1] = ptr1 - orig1;
+                               bfstart[1] += ptr1 - orig1;
                                bfend[1] = 0;
                        }
                        else
@@ -244,7 +244,7 @@ int ByteCompare::CompareFiles(FileLocation *location)
                                        //move uncompared data to begin of buff0
                                        memcpy(&buff[0][0], &buff[0][m], l);
                                        bfstart[0] = 0;
-                                       bfend[0] = l;
+                                       bfend[0] += l;
                                }
                                if (ptr1 < end1)
                                {
@@ -253,7 +253,7 @@ int ByteCompare::CompareFiles(FileLocation *location)
                                        //move uncompared data to begin of buff1
                                        memcpy(&buff[1][0], &buff[1][ m], l);
                                        bfstart[1] = 0;
-                                       bfend[1] = l;
+                                       bfend[1] += l;
                                }
                        }
                }
diff --git a/Testing/GoogleTest/ByteCompare/ByteCompare_test.cpp b/Testing/GoogleTest/ByteCompare/ByteCompare_test.cpp
new file mode 100644 (file)
index 0000000..2759955
--- /dev/null
@@ -0,0 +1,100 @@
+#include <gtest/gtest.h>
+#include "diff.h"
+#include "CompareEngines/ByteCompare.h"
+#include "CompareOptions.h"
+#include "FileLocation.h"
+#include "DiffItem.h"
+#include <io.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <fstream>
+
+namespace
+{
+       // The fixture for testing paths functions.
+       class ByteCompareTest : public testing::Test
+       {
+       protected:
+               // You can remove any or all of the following functions if its body
+               // is   empty.
+
+               ByteCompareTest()
+               {
+                       // You can do set-up work for each test here.
+               }
+
+               virtual ~ByteCompareTest()
+               {
+                       // You can do clean-up work     that doesn't throw exceptions here.
+               }
+
+               // If   the     constructor     and     destructor are not enough for setting up
+               // and cleaning up each test, you can define the following methods:
+
+               virtual void SetUp()
+               {
+                       // before each test).
+               }
+
+               virtual void TearDown()
+               {
+                       // Code here will be called     immediately     after each test (right
+                       // before the destructor).
+               }
+
+               // Objects declared here can be used by all tests in the test case for Foo.
+       };
+
+       TEST_F(ByteCompareTest, IgnoreEOLDifference)
+       {
+               CompareEngines::ByteCompare bc;
+               QuickCompareOptions option;
+               FileLocation location[2];
+               file_data filedata[2];
+               std::string filename_crlf = "_tmp_cflf.txt";
+               std::string filename_lf   = "_tmp_lf.txt";
+
+               option.m_bIgnoreEOLDifference = true;
+               bc.SetCompareOptions(option);
+
+               // same
+               {
+                       std::ofstream ostr_crlf(filename_crlf.c_str(), std::ios::out|std::ios::binary|std::ios::trunc);
+                       std::ofstream ostr_lf(filename_lf.c_str(), std::ios::out|std::ios::binary|std::ios::trunc);
+                       for (int i = 0; i < 10000; i++)
+                       {
+                               ostr_crlf << "testdata_" << i << "_01234567890123456789012345678901234567890123456789\r\n";
+                               ostr_lf   << "testdata_" << i << "_01234567890123456789012345678901234567890123456789\n";
+                       }
+               }               
+               filedata[0].desc = _open(filename_crlf.c_str(), O_RDONLY | O_BINARY, _S_IREAD);
+               filedata[1].desc = _open(filename_lf.c_str(), O_RDONLY | O_BINARY, _S_IREAD);
+               bc.SetFileData(2, filedata);
+               EXPECT_EQ(DIFFCODE::TEXT|DIFFCODE::SAME, bc.CompareFiles(location));
+               _close(filedata[0].desc);
+               _close(filedata[1].desc);
+
+               // diff
+               {
+                       std::ofstream ostr_crlf(filename_crlf.c_str(), std::ios::out|std::ios::binary|std::ios::trunc);
+                       std::ofstream ostr_lf(filename_lf.c_str(), std::ios::out|std::ios::binary|std::ios::trunc);
+                       for (int i = 0; i < 10000; i++)
+                       {
+                               ostr_crlf << "testdata_" << i << "_01234567890123456789012345678901234567890123456789\r\n";
+                               ostr_lf   << "testdata_" << i << "_01234567890123456789012345678901234567890123456789\n";
+                       }
+                       ostr_lf << "etc";
+               }               
+               filedata[0].desc = _open(filename_crlf.c_str(), O_RDONLY | O_BINARY, _S_IREAD);
+               filedata[1].desc = _open(filename_lf.c_str(), O_RDONLY | O_BINARY, _S_IREAD);
+               bc.SetFileData(2, filedata);
+               EXPECT_EQ(DIFFCODE::TEXT|DIFFCODE::DIFF, bc.CompareFiles(location));
+               _close(filedata[0].desc);
+               _close(filedata[1].desc);
+
+               remove(filename_crlf.c_str());
+               remove(filename_lf.c_str());
+       }
+
+
+}  // namespace