Avoid potential negative array index access to cached text.
[LibreOffice.git] / sc / qa / unit / datacache.cxx
blobe3ee2058f63bb1b85bf36141c6e8fe98a88e6f8b
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 <unotools/configmgr.hxx>
12 #include "helper/qahelper.hxx"
14 #include <global.hxx>
15 #include <document.hxx>
16 #include <scdll.hxx>
18 #include <tools/stream.hxx>
20 class ScCacheTest : public CppUnit::TestFixture
22 public:
23 void testCacheSimple();
24 void testCacheString();
25 void testCacheFormula();
27 CPPUNIT_TEST_SUITE(ScCacheTest);
28 CPPUNIT_TEST(testCacheSimple);
29 CPPUNIT_TEST(testCacheString);
30 CPPUNIT_TEST(testCacheFormula);
31 CPPUNIT_TEST_SUITE_END();
33 public:
34 ScCacheTest()
36 utl::ConfigManager::EnableFuzzing();
37 ScDLL::Init();
38 ScGlobal::Init();
40 ~ScCacheTest() { ScGlobal::Clear(); }
43 void ScCacheTest::testCacheSimple()
45 ScDocument aDoc;
46 aDoc.InsertTab(0, "test");
47 for (SCROW nRow = 0; nRow < 10; ++nRow)
48 aDoc.SetValue(0, nRow, 0, nRow);
50 aDoc.SetValue(0, 100000, 0, -10);
52 SvMemoryStream aStrm;
53 aDoc.StoreTabToCache(0, aStrm);
55 aStrm.Seek(0);
57 ScDocument aNewDoc;
58 aNewDoc.InsertTab(0, "test");
59 aNewDoc.RestoreTabFromCache(0, aStrm);
61 for (SCROW nRow = 0; nRow < 10; ++nRow)
62 ASSERT_DOUBLES_EQUAL(nRow, aNewDoc.GetValue(0, nRow, 0));
65 void ScCacheTest::testCacheString()
67 ScDocument aDoc;
68 aDoc.InsertTab(0, "test");
70 aDoc.SetString(0, 0, 0, "TestString");
71 aDoc.SetString(0, 1, 0, "asjdaonfdssda");
72 aDoc.SetString(0, 2, 0, "da");
74 SvMemoryStream aStrm;
75 aDoc.StoreTabToCache(0, aStrm);
77 aStrm.Seek(0);
79 ScDocument aNewDoc;
80 aNewDoc.InsertTab(0, "test");
81 aNewDoc.RestoreTabFromCache(0, aStrm);
83 CPPUNIT_ASSERT_EQUAL(OUString("TestString"), aNewDoc.GetString(0, 0, 0));
84 CPPUNIT_ASSERT_EQUAL(OUString("asjdaonfdssda"), aNewDoc.GetString(0, 1, 0));
85 CPPUNIT_ASSERT_EQUAL(OUString("da"), aNewDoc.GetString(0, 2, 0));
88 void ScCacheTest::testCacheFormula()
90 ScDocument aDoc;
91 aDoc.InsertTab(0, "test");
93 aDoc.SetString(0, 0, 0, "=B1");
94 aDoc.SetString(0, 1, 0, "=B2");
95 aDoc.SetString(0, 2, 0, "=B3");
96 aDoc.SetString(0, 3, 0, "=B4");
97 aDoc.SetString(0, 4, 0, "=B5");
98 aDoc.SetString(0, 5, 0, "=B1");
100 SvMemoryStream aStrm;
101 aDoc.StoreTabToCache(0, aStrm);
103 aStrm.Seek(0);
105 ScDocument aNewDoc;
106 aNewDoc.InsertTab(0, "test");
107 aNewDoc.RestoreTabFromCache(0, aStrm);
109 std::vector<OUString> aFormulas = { "=B1", "=B2", "=B3", "=B4", "=B5", "=B1" };
110 for (SCROW nRow = 0; nRow <= 5; ++nRow)
112 OUString aFormula = aNewDoc.GetFormula(0, nRow, 0);
113 CPPUNIT_ASSERT_EQUAL(aFormulas[nRow], aFormula);
117 CPPUNIT_TEST_SUITE_REGISTRATION(ScCacheTest);
119 CPPUNIT_PLUGIN_IMPLEMENT();
121 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */