1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <sal/config.h>
11 #include "helper/qahelper.hxx"
12 #include <unotools/configmgr.hxx>
13 #include <document.hxx>
18 #include <address.hxx>
19 #include <rangeutl.hxx>
20 #include <refupdatecontext.hxx>
22 class ScRangeTest
: public ScUcalcTestBase
26 CPPUNIT_TEST_FIXTURE(ScRangeTest
, testOverlap
)
28 ScRange
aRange1( ScAddress( 0, 0, 0 ), ScAddress( 1, 1, 1 ));
29 CPPUNIT_ASSERT(aRange1
.Contains( ScAddress( 0, 0, 0 )));
30 CPPUNIT_ASSERT(aRange1
.Contains( ScAddress( 1, 1, 1 )));
31 CPPUNIT_ASSERT(!aRange1
.Contains( ScAddress( 2, 1, 1 )));
32 CPPUNIT_ASSERT(!aRange1
.Contains( ScAddress( 1, 2, 1 )));
33 CPPUNIT_ASSERT(!aRange1
.Contains( ScAddress( 1, 1, 2 )));
35 ScRange
aRange2( ScAddress( 0, 0, 0 ), ScAddress( 10, 10, 10 ));
36 ScRange
aRange3( ScAddress( 5, 5, 5 ), ScAddress( 15, 15, 15 ));
37 CPPUNIT_ASSERT(!aRange2
.Contains( aRange3
));
38 CPPUNIT_ASSERT(!aRange3
.Contains( aRange2
));
39 CPPUNIT_ASSERT(aRange2
.Intersects( aRange3
));
40 CPPUNIT_ASSERT(aRange3
.Intersects( aRange2
));
41 CPPUNIT_ASSERT(!aRange3
.Intersects( aRange1
));
42 CPPUNIT_ASSERT(!aRange1
.Intersects( aRange3
));
45 CPPUNIT_TEST_FIXTURE(ScRangeTest
, testRangeParsing
)
48 ScRefFlags nRes
= aRange
.Parse(":1", *m_pDoc
, formula::FormulaGrammar::CONV_OOO
);
49 CPPUNIT_ASSERT_MESSAGE("Should fail to parse.", !(nRes
& ScRefFlags::VALID
));
52 CPPUNIT_TEST_FIXTURE(ScRangeTest
, testAddressParsing
)
55 ScRefFlags nRes
= aAddr
.Parse("1", *m_pDoc
, formula::FormulaGrammar::CONV_OOO
);
56 CPPUNIT_ASSERT_MESSAGE("Should fail to parse.", !(nRes
& ScRefFlags::VALID
));
59 CPPUNIT_TEST_FIXTURE(ScRangeTest
, testTdf147451
)
62 // "Sheet1" is technically a valid address like "XF1", but it should overflow.
63 ScRefFlags nRes
= aAddr
.Parse("Sheet1", *m_pDoc
, formula::FormulaGrammar::CONV_OOO
);
64 CPPUNIT_ASSERT_MESSAGE("Should fail to parse.", !(nRes
& ScRefFlags::VALID
));
67 class ScRangeUpdaterTest
: public CppUnit::TestFixture
71 virtual void setUp() override
73 utl::ConfigManager::EnableFuzzing();
79 CPPUNIT_TEST_FIXTURE(ScRangeUpdaterTest
, testUpdateInsertTabBeforePos
)
82 ScAddress
aAddr(1, 1, 1);
83 sc::RefUpdateInsertTabContext
aContext(aDoc
, 0, 1);
84 ScRangeUpdater::UpdateInsertTab(aAddr
, aContext
);
85 CPPUNIT_ASSERT_EQUAL(ScAddress(1, 1, 2), aAddr
);
88 CPPUNIT_TEST_FIXTURE(ScRangeUpdaterTest
, testUpdateInsertTabAtPos
)
91 ScAddress
aAddr(1, 1, 1);
92 sc::RefUpdateInsertTabContext
aContext(aDoc
, 1, 1);
93 ScRangeUpdater::UpdateInsertTab(aAddr
, aContext
);
94 CPPUNIT_ASSERT_EQUAL(ScAddress(1, 1, 2), aAddr
);
97 CPPUNIT_TEST_FIXTURE(ScRangeUpdaterTest
, testUpdateInsertTabAfterPos
)
100 ScAddress
aAddr(1, 1, 1);
101 sc::RefUpdateInsertTabContext
aContext(aDoc
, 2, 1);
102 ScRangeUpdater::UpdateInsertTab(aAddr
, aContext
);
103 CPPUNIT_ASSERT_EQUAL(ScAddress(1, 1, 1), aAddr
);
106 CPPUNIT_TEST_FIXTURE(ScRangeUpdaterTest
, testUpdateDeleteTabBeforePos
)
109 ScAddress
aAddr(1, 1, 1);
110 sc::RefUpdateDeleteTabContext
aContext(aDoc
, 0, 1);
111 ScRangeUpdater::UpdateDeleteTab(aAddr
, aContext
);
112 CPPUNIT_ASSERT_EQUAL(ScAddress(1, 1, 0), aAddr
);
115 CPPUNIT_TEST_FIXTURE(ScRangeUpdaterTest
, testUpdateDeleteTabAtPos
)
119 // Position within deleted range is moved to the front.
121 ScAddress
aAddr(1, 1, 1);
122 sc::RefUpdateDeleteTabContext
aContext(aDoc
, 1, 1);
123 ScRangeUpdater::UpdateDeleteTab(aAddr
, aContext
);
124 CPPUNIT_ASSERT_EQUAL(ScAddress(1, 1, 0), aAddr
);
127 ScAddress
aAddr(1, 1, 2);
128 sc::RefUpdateDeleteTabContext
aContext(aDoc
, 1, 2);
129 ScRangeUpdater::UpdateDeleteTab(aAddr
, aContext
);
130 CPPUNIT_ASSERT_EQUAL(ScAddress(1, 1, 0), aAddr
);
133 // Would-be negative results are clamped to 0.
135 ScAddress
aAddr(1, 1, 0);
136 sc::RefUpdateDeleteTabContext
aContext(aDoc
, 0, 1);
137 ScRangeUpdater::UpdateDeleteTab(aAddr
, aContext
);
138 CPPUNIT_ASSERT_EQUAL(ScAddress(1, 1, 0), aAddr
);
141 ScAddress
aAddr(1, 1, 1);
142 sc::RefUpdateDeleteTabContext
aContext(aDoc
, 0, 2);
143 ScRangeUpdater::UpdateDeleteTab(aAddr
, aContext
);
144 CPPUNIT_ASSERT_EQUAL(ScAddress(1, 1, 0), aAddr
);
148 CPPUNIT_TEST_FIXTURE(ScRangeUpdaterTest
, testUpdateDeleteTabAfterPos
)
151 ScAddress
aAddr(1, 1, 1);
152 sc::RefUpdateDeleteTabContext
aContext(aDoc
, 2, 1);
153 ScRangeUpdater::UpdateDeleteTab(aAddr
, aContext
);
154 CPPUNIT_ASSERT_EQUAL(ScAddress(1, 1, 1), aAddr
);
157 CPPUNIT_PLUGIN_IMPLEMENT();
159 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */