Start writing unit test for broadcaster storage. Found & fixed one bug.

Change-Id: Ibc00f3fb4eb188b036b4f3ae70e45cb9c7385fe8
diff --git a/sc/inc/column.hxx b/sc/inc/column.hxx
index 3c79fa3..e594ba2 100644
--- a/sc/inc/column.hxx
+++ b/sc/inc/column.hxx
@@ -473,6 +473,7 @@ public:
    void SetNumberFormat( SCROW nRow, sal_uInt32 nNumberFormat );

    SvtBroadcaster* GetBroadcaster( SCROW nRow );
    const SvtBroadcaster* GetBroadcaster( SCROW nRow ) const;

private:
    void DeleteRange( SCSIZE nStartIndex, SCSIZE nEndIndex, sal_uInt16 nDelFlag );
diff --git a/sc/inc/document.hxx b/sc/inc/document.hxx
index 79a0626..adca7d5 100644
--- a/sc/inc/document.hxx
+++ b/sc/inc/document.hxx
@@ -1950,6 +1950,9 @@ public:
    const double* FetchDoubleArray(
        sc::FormulaGroupContext& rCxt, const ScAddress& rPos, SCROW nLength ) const;

    SvtBroadcaster* GetBroadcaster( const ScAddress& rPos );
    const SvtBroadcaster* GetBroadcaster( const ScAddress& rPos ) const;

private: // CLOOK-Impl-methods

    /**
@@ -1967,8 +1970,6 @@ private: // CLOOK-Impl-methods
        ScDocument* mpDoc;
    };

    SvtBroadcaster* GetBroadcaster( const ScAddress& rPos );

    bool TableExists( SCTAB nTab ) const;

    void    MergeNumberFormatter(ScDocument* pSrcDoc);
diff --git a/sc/inc/table.hxx b/sc/inc/table.hxx
index c7b3498..c581228 100644
--- a/sc/inc/table.hxx
+++ b/sc/inc/table.hxx
@@ -832,6 +832,7 @@ public:
    ScRefCellValue GetRefCellValue( SCCOL nCol, SCROW nRow );

    SvtBroadcaster* GetBroadcaster( SCCOL nCol, SCROW nRow );
    const SvtBroadcaster* GetBroadcaster( SCCOL nCol, SCROW nRow ) const;

    /** Replace behaves differently to the Search; adjust the rCol and rRow accordingly.

diff --git a/sc/qa/unit/ucalc.cxx b/sc/qa/unit/ucalc.cxx
index 3fcc260..27fa863 100644
--- a/sc/qa/unit/ucalc.cxx
+++ b/sc/qa/unit/ucalc.cxx
@@ -50,6 +50,7 @@
#include "types.hxx"
#include "conditio.hxx"
#include "globstr.hrc"
#include "tokenarray.hxx"

#include "formula/IFunctionDescription.hxx"

@@ -119,6 +120,13 @@ public:
     * Another test for formula dependency tracking, inspired by fdo#56278.
     */
    void testFormulaDepTracking2();

    /**
     * More direct test for cell broadcaster management, used to track formula
     * dependencies.
     */
    void testCellBroadcaster();

    void testFuncParam();
    void testNamedRange();
    void testCSV();
@@ -267,6 +275,7 @@ public:
    CPPUNIT_TEST(testVolatileFunc);
    CPPUNIT_TEST(testFormulaDepTracking);
    CPPUNIT_TEST(testFormulaDepTracking2);
    CPPUNIT_TEST(testCellBroadcaster);
    CPPUNIT_TEST(testFuncParam);
    CPPUNIT_TEST(testNamedRange);
    CPPUNIT_TEST(testCSV);
@@ -1518,6 +1527,49 @@ void Test::testFormulaDepTracking2()
    m_pDoc->DeleteTab(0);
}

void Test::testCellBroadcaster()
{
    CPPUNIT_ASSERT_MESSAGE ("failed to insert sheet", m_pDoc->InsertTab (0, "foo"));

    AutoCalcSwitch aACSwitch(m_pDoc, true); // turn on auto calculation.
    m_pDoc->SetString(ScAddress(1,0,0), "=A1"); // B1 depends on A1.
    double val = m_pDoc->GetValue(ScAddress(1,0,0)); // A1 is empty, so the result should be 0.
    CPPUNIT_ASSERT_EQUAL(0.0, val);

    const SvtBroadcaster* pBC = m_pDoc->GetBroadcaster(ScAddress(0,0,0));
    CPPUNIT_ASSERT_MESSAGE("Cell A1 should have a broadcaster.", pBC);

    // Change the value of A1 and make sure that B1 follows.
    m_pDoc->SetValue(ScAddress(0,0,0), 1.23);
    val = m_pDoc->GetValue(ScAddress(1,0,0));
    CPPUNIT_ASSERT_EQUAL(1.23, val);

    // Move column A down 5 cells. Make sure B1 now references A6, not A1.
    m_pDoc->InsertRow(0, 0, 0, 0, 0, 5);
    ScFormulaCell* pFC = m_pDoc->GetFormulaCell(ScAddress(1,0,0));
    CPPUNIT_ASSERT_MESSAGE("Expected a formula cell.", pFC);
    ScTokenArray* pTokens = pFC->GetCode();
    ScToken* pToken = static_cast<ScToken*>(pTokens->First());
    CPPUNIT_ASSERT_MESSAGE("Reference token not found.", pToken && pToken->GetType() == formula::svSingleRef);
    ScSingleRefData& rRef = pToken->GetSingleRef();
    CPPUNIT_ASSERT_EQUAL(static_cast<SCsCOL>(-1), rRef.nRelCol);
    CPPUNIT_ASSERT_EQUAL(static_cast<SCsROW>(5), rRef.nRelRow);

    // Make sure the broadcaster has also moved.
    pBC = m_pDoc->GetBroadcaster(ScAddress(0,0,0));
    CPPUNIT_ASSERT_MESSAGE("Broadcaster shouldn't exist at A1.", !pBC);
    pBC = m_pDoc->GetBroadcaster(ScAddress(0,5,0));
    CPPUNIT_ASSERT_MESSAGE("Broadcaster should exist at A6.", pBC);

    // Set new value to A6 and make sure B1 gets updated.
    m_pDoc->SetValue(ScAddress(0,5,0), 45.6);
    val = m_pDoc->GetValue(ScAddress(1,0,0));
    CPPUNIT_ASSERT_EQUAL(45.6, val);

    m_pDoc->DeleteTab(0);
    CPPUNIT_ASSERT_MESSAGE("good, all test passed.", false);
}

void Test::testFuncParam()
{
    OUString aTabName("foo");
diff --git a/sc/source/core/data/column.cxx b/sc/source/core/data/column.cxx
index 6f4aff8..55fe296 100644
--- a/sc/source/core/data/column.cxx
+++ b/sc/source/core/data/column.cxx
@@ -1225,6 +1225,8 @@ void ScColumn::InsertRow( SCROW nStartRow, SCSIZE nSize )
    maTextWidths.resize(MAXROWCOUNT);
    maScriptTypes.insert_empty(nStartRow, nSize);
    maScriptTypes.resize(MAXROWCOUNT);
    maBroadcasters.insert_empty(nStartRow, nSize);
    maBroadcasters.resize(MAXROWCOUNT);

    CellStorageModified();
}
diff --git a/sc/source/core/data/column2.cxx b/sc/source/core/data/column2.cxx
index 40fcf2c..9fcf5aa 100644
--- a/sc/source/core/data/column2.cxx
+++ b/sc/source/core/data/column2.cxx
@@ -1513,6 +1513,11 @@ SvtBroadcaster* ScColumn::GetBroadcaster(SCROW nRow)
    return maBroadcasters.get<SvtBroadcaster*>(nRow);
}

const SvtBroadcaster* ScColumn::GetBroadcaster(SCROW nRow) const
{
    return maBroadcasters.get<SvtBroadcaster*>(nRow);
}

unsigned short ScColumn::GetTextWidth(SCROW nRow) const
{
    return maTextWidths.get<unsigned short>(nRow);
diff --git a/sc/source/core/data/document.cxx b/sc/source/core/data/document.cxx
index 7a26719..8da6a51 100644
--- a/sc/source/core/data/document.cxx
+++ b/sc/source/core/data/document.cxx
@@ -2210,6 +2210,14 @@ SvtBroadcaster* ScDocument::GetBroadcaster( const ScAddress& rPos )
    return maTabs[rPos.Tab()]->GetBroadcaster(rPos.Col(), rPos.Row());
}

const SvtBroadcaster* ScDocument::GetBroadcaster( const ScAddress& rPos ) const
{
    if (!TableExists(rPos.Tab()))
        return NULL;

    return maTabs[rPos.Tab()]->GetBroadcaster(rPos.Col(), rPos.Row());
}

bool ScDocument::TableExists( SCTAB nTab ) const
{
    return ValidTab(nTab) && static_cast<size_t>(nTab) < maTabs.size() && maTabs[nTab];
diff --git a/sc/source/core/data/table1.cxx b/sc/source/core/data/table1.cxx
index 37278d6..57315b7 100644
--- a/sc/source/core/data/table1.cxx
+++ b/sc/source/core/data/table1.cxx
@@ -2168,6 +2168,14 @@ SvtBroadcaster* ScTable::GetBroadcaster( SCCOL nCol, SCROW nRow )
    return aCol[nCol].GetBroadcaster(nRow);
}

const SvtBroadcaster* ScTable::GetBroadcaster( SCCOL nCol, SCROW nRow ) const
{
    if (!ValidColRow(nCol, nRow))
        return NULL;

    return aCol[nCol].GetBroadcaster(nRow);
}

void ScTable::DeleteConditionalFormat( sal_uLong nIndex )
{
    mpCondFormatList->erase(nIndex);