From 0a3158ce741989c1e182529fe46d17194e2d82c6 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Mon, 21 Mar 2016 11:15:29 -0700 Subject: [PATCH] Fix marking deletions when chaining move_last_over() --- src/collection_notifications.cpp | 14 ++++++++++++-- tests/collection_change_indices.cpp | 9 +++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/collection_notifications.cpp b/src/collection_notifications.cpp index 3fd03df7..5e95e6be 100644 --- a/src/collection_notifications.cpp +++ b/src/collection_notifications.cpp @@ -292,8 +292,17 @@ void CollectionChangeBuilder::move_over(size_t row_ndx, size_t last_row) move.to = row_ndx; updated_existing_move = true; - insertions.erase_at(last_row); - insertions.insert_at(row_ndx); + if (!insertions.empty()) { + REALM_ASSERT(std::prev(insertions.end())->second - 1 <= last_row); + insertions.remove(last_row); + } + + // Don't mark the moved-over row as deleted if it was a new insertion + if (!insertions.contains(row_ndx)) { + deletions.add_shifted(insertions.unshift(row_ndx)); + insertions.add(row_ndx); + } + // Because this is a move, the unshifted source row has already been marked as deleted } @@ -312,6 +321,7 @@ void CollectionChangeBuilder::move_over(size_t row_ndx, size_t last_row) deletions.add_shifted(insertions.unshift(row_ndx)); insertions.add(row_ndx); } + verify(); } void CollectionChangeBuilder::verify() diff --git a/tests/collection_change_indices.cpp b/tests/collection_change_indices.cpp index 8526fd07..fcf15e75 100644 --- a/tests/collection_change_indices.cpp +++ b/tests/collection_change_indices.cpp @@ -185,6 +185,15 @@ TEST_CASE("[collection_change] move_over()") { REQUIRE_INDICES(c.insertions, 5, 6); REQUIRE_MOVES(c, {10, 5}, {9, 6}); } + + SECTION("marks the moved-over row as deleted when chaining moves") { + c.move_over(5, 10); + c.move_over(0, 5); + + REQUIRE_INDICES(c.deletions, 0, 5, 10); + REQUIRE_INDICES(c.insertions, 0); + REQUIRE_MOVES(c, {10, 0}); + } } TEST_CASE("[collection_change] clear()") {