summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKohei Yoshida <kohei.yoshida@collabora.com>2014-04-03 09:54:16 -0400
committerKohei Yoshida <kohei.yoshida@collabora.com>2014-04-07 14:43:06 -0400
commit137c288978fb8f4aee259fabfdcb9252b1b011d3 (patch)
tree58281922202d0b517937f96b2cd2c01fb48b83f0
parentf01792dda15fe4509a2925929f4d58daef9d15c0 (diff)
fdo#75741: Write test for this.
Change-Id: Ibd3a2efe0a195b7c402572b0efb5c03183df9cf8
-rw-r--r--sc/qa/unit/ucalc.cxx15
-rw-r--r--sc/qa/unit/ucalc.hxx5
-rw-r--r--sc/qa/unit/ucalc_sharedformula.cxx83
3 files changed, 95 insertions, 8 deletions
diff --git a/sc/qa/unit/ucalc.cxx b/sc/qa/unit/ucalc.cxx
index 68d1b2e82ae3..6ee5a4eac0ff 100644
--- a/sc/qa/unit/ucalc.cxx
+++ b/sc/qa/unit/ucalc.cxx
@@ -1773,8 +1773,10 @@ void Test::testNamedRange()
m_pDoc->SetValue (0, 0, 0, 101);
- bool bSuccess = insertRangeNames(m_pDoc, aNames, aNames + SAL_N_ELEMENTS(aNames));
+ ScRangeName* pNames = new ScRangeName;
+ bool bSuccess = insertRangeNames(m_pDoc, pNames, aNames, aNames + SAL_N_ELEMENTS(aNames));
CPPUNIT_ASSERT_MESSAGE("Failed to insert range names.", bSuccess);
+ m_pDoc->SetRangeName(pNames);
ScRangeName* pNewRanges = m_pDoc->GetRangeName();
CPPUNIT_ASSERT(pNewRanges);
@@ -1822,8 +1824,10 @@ void Test::testInsertNameList()
{ "MyRange3", "$Test.$C$1:$C$100", 3 }
};
- bool bSuccess = insertRangeNames(m_pDoc, aNames, aNames + SAL_N_ELEMENTS(aNames));
+ ScRangeName* pNames = new ScRangeName;
+ bool bSuccess = insertRangeNames(m_pDoc, pNames, aNames, aNames + SAL_N_ELEMENTS(aNames));
CPPUNIT_ASSERT_MESSAGE("Failed to insert range names.", bSuccess);
+ m_pDoc->SetRangeName(pNames);
ScDocFunc& rDocFunc = getDocShell().GetDocFunc();
ScAddress aPos(1,1,0);
@@ -5696,10 +5700,10 @@ ScDocShell* Test::findLoadedDocShellByName(const OUString& rName)
return NULL;
}
-bool Test::insertRangeNames(ScDocument* pDoc, const RangeNameDef* p, const RangeNameDef* pEnd)
+bool Test::insertRangeNames(
+ ScDocument* pDoc, ScRangeName* pNames, const RangeNameDef* p, const RangeNameDef* pEnd)
{
ScAddress aA1(0, 0, 0);
- ScRangeName* pNewRanges = new ScRangeName();
for (; p != pEnd; ++p)
{
ScRangeData* pNew = new ScRangeData(
@@ -5708,7 +5712,7 @@ bool Test::insertRangeNames(ScDocument* pDoc, const RangeNameDef* p, const Range
OUString::createFromAscii(p->mpExpr),
aA1, 0, formula::FormulaGrammar::GRAM_ENGLISH);
pNew->SetIndex(p->mnIndex);
- bool bSuccess = pNewRanges->insert(pNew);
+ bool bSuccess = pNames->insert(pNew);
if (!bSuccess)
{
cerr << "Insertion failed." << endl;
@@ -5716,7 +5720,6 @@ bool Test::insertRangeNames(ScDocument* pDoc, const RangeNameDef* p, const Range
}
}
- pDoc->SetRangeName(pNewRanges);
return true;
}
diff --git a/sc/qa/unit/ucalc.hxx b/sc/qa/unit/ucalc.hxx
index f97d0e928a5d..faf01dee58b3 100644
--- a/sc/qa/unit/ucalc.hxx
+++ b/sc/qa/unit/ucalc.hxx
@@ -39,7 +39,7 @@ public:
};
static ScDocShell* findLoadedDocShellByName(const OUString& rName);
- static bool insertRangeNames(ScDocument* pDoc, const RangeNameDef* p, const RangeNameDef* pEnd);
+ static bool insertRangeNames(ScDocument* pDoc, ScRangeName* pNames, const RangeNameDef* p, const RangeNameDef* pEnd);
static void printRange(ScDocument* pDoc, const ScRange& rRange, const char* pCaption);
static void clearRange(ScDocument* pDoc, const ScRange& rRange);
static void clearSheet(ScDocument* pDoc, SCTAB nTab);
@@ -277,6 +277,7 @@ public:
void testSharedFormulasCopyPaste();
void testSharedFormulaInsertColumn();
void testSharedFormulaMoveBlock();
+ void testSharedFormulaUpdateOnNamedRangeChange();
void testFormulaPosition();
void testMixData();
@@ -457,7 +458,7 @@ public:
CPPUNIT_TEST(testSharedFormulasRefUpdateDeleteSheets);
CPPUNIT_TEST(testSharedFormulasCopyPaste);
CPPUNIT_TEST(testSharedFormulaInsertColumn);
- CPPUNIT_TEST(testSharedFormulaMoveBlock);
+ CPPUNIT_TEST(testSharedFormulaUpdateOnNamedRangeChange);
CPPUNIT_TEST(testFormulaPosition);
CPPUNIT_TEST(testMixData);
CPPUNIT_TEST(testJumpToPrecedentsDependents);
diff --git a/sc/qa/unit/ucalc_sharedformula.cxx b/sc/qa/unit/ucalc_sharedformula.cxx
index b95a673d0c92..acb249a83d1b 100644
--- a/sc/qa/unit/ucalc_sharedformula.cxx
+++ b/sc/qa/unit/ucalc_sharedformula.cxx
@@ -18,6 +18,8 @@
#include <docfunc.hxx>
#include <tokenarray.hxx>
#include <tokenstringcontext.hxx>
+#include <globalnames.hxx>
+
#include "svl/sharedstring.hxx"
#include "formula/grammar.hxx"
@@ -1189,4 +1191,85 @@ void Test::testSharedFormulaMoveBlock()
m_pDoc->DeleteTab(0);
}
+void Test::testSharedFormulaUpdateOnNamedRangeChange()
+{
+ sc::AutoCalcSwitch aACSwitch(*m_pDoc, true); // turn on auto calc.
+
+ m_pDoc->InsertTab(0, "Test");
+
+ const char* pName = "MyRange";
+ const char* pExpr1 = "$Test.$A$1:$A$3";
+ const char* pExpr2 = "$Test.$A$1:$A$4";
+
+ RangeNameDef aName;
+ aName.mpName = pName;
+ aName.mpExpr = pExpr1;
+ aName.mnIndex = 1;
+ ScRangeName* pNames = new ScRangeName;
+ bool bSuccess = insertRangeNames(m_pDoc, pNames, &aName, &aName + 1);
+ CPPUNIT_ASSERT(bSuccess);
+ CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), pNames->size());
+ m_pDoc->SetRangeName(pNames);
+
+ // Set values to A1:A4.
+ m_pDoc->SetValue(ScAddress(0,0,0), 1.0);
+ m_pDoc->SetValue(ScAddress(0,1,0), 2.0);
+ m_pDoc->SetValue(ScAddress(0,2,0), 3.0);
+ m_pDoc->SetValue(ScAddress(0,3,0), 4.0);
+
+ // Set formula to B1:B3.
+ m_pDoc->SetString(ScAddress(1,0,0), "=SUM(MyRange)");
+ m_pDoc->SetString(ScAddress(1,1,0), "=SUM(MyRange)");
+ m_pDoc->SetString(ScAddress(1,2,0), "=SUM(MyRange)");
+
+ // B1:B3 should be grouped.
+ ScFormulaCell* pFC = m_pDoc->GetFormulaCell(ScAddress(1,0,0));
+ CPPUNIT_ASSERT(pFC);
+ CPPUNIT_ASSERT_EQUAL(static_cast<SCROW>(0), pFC->GetSharedTopRow());
+ CPPUNIT_ASSERT_EQUAL(static_cast<SCROW>(3), pFC->GetSharedLength());
+
+ CPPUNIT_ASSERT_EQUAL(6.0, m_pDoc->GetValue(ScAddress(1,0,0)));
+ CPPUNIT_ASSERT_EQUAL(6.0, m_pDoc->GetValue(ScAddress(1,1,0)));
+ CPPUNIT_ASSERT_EQUAL(6.0, m_pDoc->GetValue(ScAddress(1,2,0)));
+
+ // Set a single formula to C1.
+ m_pDoc->SetString(ScAddress(2,0,0), "=AVERAGE(MyRange)");
+ pFC = m_pDoc->GetFormulaCell(ScAddress(2,0,0));
+ CPPUNIT_ASSERT(pFC);
+ CPPUNIT_ASSERT_MESSAGE("C1 should not be shared.", !pFC->IsShared());
+ CPPUNIT_ASSERT_EQUAL(2.0, m_pDoc->GetValue(ScAddress(2,0,0)));
+
+ // Update the range of MyRange.
+ pNames = new ScRangeName;
+ aName.mpExpr = pExpr2;
+ bSuccess = insertRangeNames(m_pDoc, pNames, &aName, &aName + 1);
+ CPPUNIT_ASSERT(bSuccess);
+ CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), pNames->size());
+ ScDocFunc& rFunc = getDocShell().GetDocFunc();
+
+ typedef boost::ptr_map<OUString, ScRangeName> NameMapType;
+ NameMapType aNewNames;
+ OUString aScope(STR_GLOBAL_RANGE_NAME);
+ aNewNames.insert(aScope, pNames);
+ rFunc.ModifyAllRangeNames(aNewNames);
+
+ // Check to make sure all displayed formulas are still good.
+ if (!checkFormula(*m_pDoc, ScAddress(1,0,0), "SUM(MyRange)"))
+ CPPUNIT_FAIL("Wrong formula!");
+ if (!checkFormula(*m_pDoc, ScAddress(1,1,0), "SUM(MyRange)"))
+ CPPUNIT_FAIL("Wrong formula!");
+ if (!checkFormula(*m_pDoc, ScAddress(1,2,0), "SUM(MyRange)"))
+ CPPUNIT_FAIL("Wrong formula!");
+ if (!checkFormula(*m_pDoc, ScAddress(2,0,0), "AVERAGE(MyRange)"))
+ CPPUNIT_FAIL("Wrong formula!");
+
+ // Check the calculation results as well.
+ CPPUNIT_ASSERT_EQUAL(10.0, m_pDoc->GetValue(ScAddress(1,0,0)));
+ CPPUNIT_ASSERT_EQUAL(10.0, m_pDoc->GetValue(ScAddress(1,1,0)));
+ CPPUNIT_ASSERT_EQUAL(10.0, m_pDoc->GetValue(ScAddress(1,2,0)));
+ CPPUNIT_ASSERT_EQUAL(2.5, m_pDoc->GetValue(ScAddress(2,0,0)));
+
+ m_pDoc->DeleteTab(0);
+}
+
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */