Handle also the case where a range extends an existing one with overlap

Check for a new range being joined Extending an existing one in any of
four directions (up, right, down, left), within a tab (sheet), that
is. And add unit test for this.

Change-Id: I4bd0525c2837f8b4b9d5a8967e0d5d661c6a5e2f
Reviewed-on: https://gerrit.libreoffice.org/42304
Reviewed-by: Eike Rathke <erack@redhat.com>
Tested-by: Eike Rathke <erack@redhat.com>
diff --git a/sc/qa/unit/rangelst_test.cxx b/sc/qa/unit/rangelst_test.cxx
index 0f61bca..79c5736 100644
--- a/sc/qa/unit/rangelst_test.cxx
+++ b/sc/qa/unit/rangelst_test.cxx
@@ -45,6 +45,7 @@ public:
    void testJoin_Case1();
    void testJoin_Case2();
    void testJoin_Case3();
    void testJoin_Case4();
    void testGetIntersectedRange();

    void testUpdateReference_DeleteRow();
@@ -74,6 +75,7 @@ public:
    CPPUNIT_TEST(testJoin_Case1);
    CPPUNIT_TEST(testJoin_Case2);
    CPPUNIT_TEST(testJoin_Case3);
    CPPUNIT_TEST(testJoin_Case4);
    CPPUNIT_TEST(testUpdateReference_DeleteRow);
    CPPUNIT_TEST(testUpdateReference_DeleteLastRow);
    CPPUNIT_TEST(testUpdateReference_DeleteCol);
@@ -447,6 +449,38 @@ void Test::testJoin_Case3()
    CPPUNIT_ASSERT_EQUAL(ScRange(8,8,0,9,9,0), *aList[1]);
}

void Test::testJoin_Case4()
{
    ScRangeList aList;
    aList.Join(ScRange(1,1,0,2,6,0));
    // Join a range that overlaps it and extends it vertically
    aList.Join(ScRange(1,4,0,2,8,0));

    // The one range in the list should have been extended
    CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList.size());
    CPPUNIT_ASSERT_EQUAL(ScRange(1,1,0,2,8,0), *aList[0]);

    // Join a range that overlaps it and extends it horizontally
    aList.Join(ScRange(2,1,0,4,8,0));

    // Again, should have just been extended
    CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList.size());
    CPPUNIT_ASSERT_EQUAL(ScRange(1,1,0,4,8,0), *aList[0]);

    // And then the same but on top / to the left of existing range
    ScRangeList aList2;
    aList2.Join(ScRange(4,4,0,8,8,0));
    aList2.Join(ScRange(4,1,0,8,6,0));

    CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList2.size());
    CPPUNIT_ASSERT_EQUAL(ScRange(4,1,0,8,8,0), *aList2[0]);

    aList2.Join(ScRange(1,1,0,6,8,0));

    CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList2.size());
    CPPUNIT_ASSERT_EQUAL(ScRange(1,1,0,8,8,0), *aList2[0]);
}

void Test::testUpdateReference_DeleteRow()
{
    ScRangeList aList(ScRange(1,1,0,4,4,0));
diff --git a/sc/source/core/tool/rangelst.cxx b/sc/source/core/tool/rangelst.cxx
index b45a12c3c..5daf4e5 100644
--- a/sc/source/core/tool/rangelst.cxx
+++ b/sc/source/core/tool/rangelst.cxx
@@ -286,12 +286,14 @@ void ScRangeList::Join( const ScRange& r, bool bIsInList )
        {   // 2D
            if ( p->aStart.Col() == nCol1 && p->aEnd.Col() == nCol2 )
            {
                if ( p->aStart.Row() == nRow2+1 )
                if ( p->aStart.Row() <= nRow2+1 &&
                     p->aStart.Row() >= nRow1 )
                {   // top
                    p->aStart.SetRow( nRow1 );
                    bJoined = true;
                }
                else if ( p->aEnd.Row() == nRow1-1 )
                else if ( p->aEnd.Row() >= nRow1-1 &&
                          p->aEnd.Row() <= nRow2 )
                {   // bottom
                    p->aEnd.SetRow( nRow2 );
                    bJoined = true;
@@ -299,12 +301,14 @@ void ScRangeList::Join( const ScRange& r, bool bIsInList )
            }
            else if ( p->aStart.Row() == nRow1 && p->aEnd.Row() == nRow2 )
            {
                if ( p->aStart.Col() == nCol2+1 )
                if ( p->aStart.Col() <= nCol2+1 &&
                     p->aStart.Col() >= nCol1 )
                {   // left
                    p->aStart.SetCol( nCol1 );
                    bJoined = true;
                }
                else if ( p->aEnd.Col() == nCol1-1 )
                else if ( p->aEnd.Col() >= nCol1-1 &&
                          p->aEnd.Col() <= nCol2 )
                {   // right
                    p->aEnd.SetCol( nCol2 );
                    bJoined = true;