summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2023-01-29 15:17:50 +0100
committerRoland Reichwein <mail@reichwein.it>2023-01-29 15:17:50 +0100
commitb1fb4f3aa1ec29781d2e25b1a05fb70e143fd24d (patch)
treedde76c41fa7aaf8b8b7e036b72c9983590ae873d
parentfa0cfcee2c7494e5025f6fb51152352282cbc032 (diff)
Diff bugfix
-rw-r--r--diff.cpp10
-rw-r--r--tests/test-diff.cpp27
2 files changed, 37 insertions, 0 deletions
diff --git a/diff.cpp b/diff.cpp
index b56fd51..ea9b04e 100644
--- a/diff.cpp
+++ b/diff.cpp
@@ -81,6 +81,16 @@ void Diff::create(const std::string& old_version, const std::string& new_version
return;
}
+ // re-adjust if search crossed
+ if (old_pos0 > old_pos1) {
+ old_pos0 = old_pos1;
+ new_pos0 = old_pos1;
+ }
+ if (new_pos0 > new_pos1) {
+ old_pos0 = new_pos1;
+ new_pos0 = new_pos1;
+ }
+
// insert in the middle
if (old_pos0 == old_pos1) {
m_pos0 = old_pos0;
diff --git a/tests/test-diff.cpp b/tests/test-diff.cpp
index 1704cfd..1625716 100644
--- a/tests/test-diff.cpp
+++ b/tests/test-diff.cpp
@@ -6,11 +6,14 @@
#include <stdlib.h>
+#include <boost/property_tree/ptree.hpp>
+
#include "libreichwein/file.h"
#include "diff.h"
namespace fs = std::filesystem;
+namespace pt = boost::property_tree;
using namespace Reichwein;
class DiffTest: public ::testing::Test
@@ -58,6 +61,18 @@ TEST_F(DiffTest, constructor)
Diff d{"<diff><start>5</start><end>50</end><data>abc</data></diff>"};
EXPECT_EQ(d.get_xml(), "<diff><start>5</start><end>50</end><data>abc</data></diff>");
}
+
+ // constructor via ptree
+ {
+ pt::ptree ptree;
+ EXPECT_THROW(Diff d{ptree}, std::exception);
+
+ ptree.put("diff.start", 0);
+ ptree.put("diff.end", 0);
+ ptree.put("diff.data", "abc");
+ Diff d{ptree};
+ EXPECT_EQ(d.get_xml(), "<diff><start>0</start><end>0</end><data>abc</data></diff>");
+ }
// constructor via versions
{
@@ -120,6 +135,18 @@ TEST_F(DiffTest, constructor)
Diff d{"abc", "c"};
EXPECT_EQ(d.get_xml(), "<diff><start>0</start><end>2</end><data/></diff>");
}
+ {
+ Diff d{"aaaa", "aa"};
+ EXPECT_EQ(d.get_xml(), "<diff><start>2</start><end>4</end><data/></diff>");
+ }
+ {
+ Diff d{"baaaa", "baa"};
+ EXPECT_EQ(d.get_xml(), "<diff><start>3</start><end>5</end><data/></diff>");
+ }
+ {
+ Diff d{"baaaab", "baab"};
+ EXPECT_EQ(d.get_xml(), "<diff><start>1</start><end>3</end><data/></diff>");
+ }
}
TEST_F(DiffTest, diff_create)