Version 7.5.1.1, tag libreoffice-7.5.1.1
[LibreOffice.git] / sc / qa / unit / ucalc_range.cxx
blob5ea2d2ecfc7db0d42a10cef04f5374f95b200e84
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #include <sal/config.h>
11 #include "helper/qahelper.hxx"
12 #include <unotools/configmgr.hxx>
13 #include <document.hxx>
14 #include <docsh.hxx>
15 #include <global.hxx>
16 #include <scdll.hxx>
18 #include <address.hxx>
19 #include <rangeutl.hxx>
20 #include <refupdatecontext.hxx>
22 class ScRangeTest : public ScUcalcTestBase
24 public:
25 CPPUNIT_TEST_SUITE(ScRangeTest);
26 CPPUNIT_TEST(testOverlap);
27 CPPUNIT_TEST(testRangeParsing);
28 CPPUNIT_TEST(testAddressParsing);
29 CPPUNIT_TEST(testTdf147451);
30 CPPUNIT_TEST_SUITE_END();
32 void testOverlap();
33 void testRangeParsing();
34 void testAddressParsing();
35 void testTdf147451();
38 void ScRangeTest::testOverlap()
40 ScRange aRange1( ScAddress( 0, 0, 0 ), ScAddress( 1, 1, 1 ));
41 CPPUNIT_ASSERT(aRange1.Contains( ScAddress( 0, 0, 0 )));
42 CPPUNIT_ASSERT(aRange1.Contains( ScAddress( 1, 1, 1 )));
43 CPPUNIT_ASSERT(!aRange1.Contains( ScAddress( 2, 1, 1 )));
44 CPPUNIT_ASSERT(!aRange1.Contains( ScAddress( 1, 2, 1 )));
45 CPPUNIT_ASSERT(!aRange1.Contains( ScAddress( 1, 1, 2 )));
47 ScRange aRange2( ScAddress( 0, 0, 0 ), ScAddress( 10, 10, 10 ));
48 ScRange aRange3( ScAddress( 5, 5, 5 ), ScAddress( 15, 15, 15 ));
49 CPPUNIT_ASSERT(!aRange2.Contains( aRange3 ));
50 CPPUNIT_ASSERT(!aRange3.Contains( aRange2 ));
51 CPPUNIT_ASSERT(aRange2.Intersects( aRange3 ));
52 CPPUNIT_ASSERT(aRange3.Intersects( aRange2 ));
53 CPPUNIT_ASSERT(!aRange3.Intersects( aRange1 ));
54 CPPUNIT_ASSERT(!aRange1.Intersects( aRange3 ));
57 void ScRangeTest::testRangeParsing()
59 ScRange aRange;
60 ScRefFlags nRes = aRange.Parse(":1", *m_pDoc, formula::FormulaGrammar::CONV_OOO);
61 CPPUNIT_ASSERT_MESSAGE("Should fail to parse.", !(nRes & ScRefFlags::VALID));
64 void ScRangeTest::testAddressParsing()
66 ScAddress aAddr;
67 ScRefFlags nRes = aAddr.Parse("1", *m_pDoc, formula::FormulaGrammar::CONV_OOO);
68 CPPUNIT_ASSERT_MESSAGE("Should fail to parse.", !(nRes & ScRefFlags::VALID));
71 void ScRangeTest::testTdf147451()
73 ScAddress aAddr;
74 // "Sheet1" is technically a valid address like "XF1", but it should overflow.
75 ScRefFlags nRes = aAddr.Parse("Sheet1", *m_pDoc, formula::FormulaGrammar::CONV_OOO);
76 CPPUNIT_ASSERT_MESSAGE("Should fail to parse.", !(nRes & ScRefFlags::VALID));
79 class ScRangeUpdaterTest : public CppUnit::TestFixture
81 public:
83 virtual void setUp() override
85 utl::ConfigManager::EnableFuzzing();
86 ScDLL::Init();
87 ScGlobal::Init();
89 void testUpdateInsertTabBeforePos();
90 void testUpdateInsertTabAtPos();
91 void testUpdateInsertTabAfterPos();
92 void testUpdateDeleteTabBeforePos();
93 void testUpdateDeleteTabAtPos();
94 void testUpdateDeleteTabAfterPos();
96 CPPUNIT_TEST_SUITE(ScRangeUpdaterTest);
97 CPPUNIT_TEST(testUpdateInsertTabBeforePos);
98 CPPUNIT_TEST(testUpdateInsertTabAtPos);
99 CPPUNIT_TEST(testUpdateInsertTabAfterPos);
100 CPPUNIT_TEST(testUpdateDeleteTabBeforePos);
101 CPPUNIT_TEST(testUpdateDeleteTabAtPos);
102 CPPUNIT_TEST(testUpdateDeleteTabAfterPos);
103 CPPUNIT_TEST_SUITE_END();
106 void ScRangeUpdaterTest::testUpdateInsertTabBeforePos()
108 ScDocument aDoc;
109 ScAddress aAddr(1, 1, 1);
110 sc::RefUpdateInsertTabContext aContext(aDoc, 0, 1);
111 ScRangeUpdater::UpdateInsertTab(aAddr, aContext);
112 CPPUNIT_ASSERT_EQUAL(ScAddress(1, 1, 2), aAddr);
115 void ScRangeUpdaterTest::testUpdateInsertTabAtPos()
117 ScDocument aDoc;
118 ScAddress aAddr(1, 1, 1);
119 sc::RefUpdateInsertTabContext aContext(aDoc, 1, 1);
120 ScRangeUpdater::UpdateInsertTab(aAddr, aContext);
121 CPPUNIT_ASSERT_EQUAL(ScAddress(1, 1, 2), aAddr);
124 void ScRangeUpdaterTest::testUpdateInsertTabAfterPos()
126 ScDocument aDoc;
127 ScAddress aAddr(1, 1, 1);
128 sc::RefUpdateInsertTabContext aContext(aDoc, 2, 1);
129 ScRangeUpdater::UpdateInsertTab(aAddr, aContext);
130 CPPUNIT_ASSERT_EQUAL(ScAddress(1, 1, 1), aAddr);
133 void ScRangeUpdaterTest::testUpdateDeleteTabBeforePos()
135 ScDocument aDoc;
136 ScAddress aAddr(1, 1, 1);
137 sc::RefUpdateDeleteTabContext aContext(aDoc, 0, 1);
138 ScRangeUpdater::UpdateDeleteTab(aAddr, aContext);
139 CPPUNIT_ASSERT_EQUAL(ScAddress(1, 1, 0), aAddr);
142 void ScRangeUpdaterTest::testUpdateDeleteTabAtPos()
144 ScDocument aDoc;
146 // Position within deleted range is moved to the front.
148 ScAddress aAddr(1, 1, 1);
149 sc::RefUpdateDeleteTabContext aContext(aDoc, 1, 1);
150 ScRangeUpdater::UpdateDeleteTab(aAddr, aContext);
151 CPPUNIT_ASSERT_EQUAL(ScAddress(1, 1, 0), aAddr);
154 ScAddress aAddr(1, 1, 2);
155 sc::RefUpdateDeleteTabContext aContext(aDoc, 1, 2);
156 ScRangeUpdater::UpdateDeleteTab(aAddr, aContext);
157 CPPUNIT_ASSERT_EQUAL(ScAddress(1, 1, 0), aAddr);
160 // Would-be negative results are clamped to 0.
162 ScAddress aAddr(1, 1, 0);
163 sc::RefUpdateDeleteTabContext aContext(aDoc, 0, 1);
164 ScRangeUpdater::UpdateDeleteTab(aAddr, aContext);
165 CPPUNIT_ASSERT_EQUAL(ScAddress(1, 1, 0), aAddr);
168 ScAddress aAddr(1, 1, 1);
169 sc::RefUpdateDeleteTabContext aContext(aDoc, 0, 2);
170 ScRangeUpdater::UpdateDeleteTab(aAddr, aContext);
171 CPPUNIT_ASSERT_EQUAL(ScAddress(1, 1, 0), aAddr);
175 void ScRangeUpdaterTest::testUpdateDeleteTabAfterPos()
177 ScDocument aDoc;
178 ScAddress aAddr(1, 1, 1);
179 sc::RefUpdateDeleteTabContext aContext(aDoc, 2, 1);
180 ScRangeUpdater::UpdateDeleteTab(aAddr, aContext);
181 CPPUNIT_ASSERT_EQUAL(ScAddress(1, 1, 1), aAddr);
184 CPPUNIT_TEST_SUITE_REGISTRATION(ScRangeTest);
185 CPPUNIT_TEST_SUITE_REGISTRATION(ScRangeUpdaterTest);
187 CPPUNIT_PLUGIN_IMPLEMENT();
189 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */