sc: add unit test for SortOrderReverser

Add the unit test to the basic types test suite.

The unit test checks the unosrt function when using different
sort orders. It also checks combining the sort orders with
addOrderIndices function.

Additionally move the existing ScAddressTest to the "types"
folder.

Change-Id: I298918b971e3386b8ea513c09879045e25010ad9
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/190911
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
This commit is contained in:
Tomaž Vajngerl
2025-12-17 07:16:49 +01:00
committed by Tomaž Vajngerl
parent ca8a3549ea
commit a8dcbc862b
3 changed files with 83 additions and 2 deletions
+3 -1
View File
@@ -13,6 +13,7 @@ $(eval $(call gb_CppunitTest_CppunitTest,sc_basic_types_test))
$(eval $(call gb_CppunitTest_add_exception_objects,sc_basic_types_test,\
sc/qa/unit/types/ScAddressTest \
sc/qa/unit/types/SortOrderReverserTest \
))
$(eval $(call gb_CppunitTest_use_external,sc_basic_types_test,boost_headers))
@@ -25,7 +26,8 @@ $(eval $(call gb_CppunitTest_set_include,sc_basic_types_test,\
))
$(eval $(call gb_CppunitTest_use_libraries,sc_basic_types_test,\
sal \
sc \
scui \
))
# Needed for include/test/cppunitasserthelper.hxx
+1 -1
View File
@@ -20,7 +20,7 @@ class ScTable;
namespace sc
{
/** Stores the sort order and can reverse the sorting of rows (unsorting). */
struct SortOrderReverser
struct SC_DLLPUBLIC SortOrderReverser
{
public:
SCROW mnFirstRow;
@@ -0,0 +1,79 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <cppunit/plugin/TestPlugIn.h>
#include <cppunit/TestAssert.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <SheetView.hxx>
class SortOrderReverserTest : public CppUnit::TestFixture
{
};
CPPUNIT_TEST_FIXTURE(SortOrderReverserTest, testUnsort)
{
// Unchanged order - output rows should be the same as input
{
sc::SortOrderReverser aReverser;
aReverser.addOrderIndices({ 1, 2, 3, 4 }, 5, 8);
CPPUNIT_ASSERT_EQUAL(SCROW(5), aReverser.unsort(5));
CPPUNIT_ASSERT_EQUAL(SCROW(6), aReverser.unsort(6));
CPPUNIT_ASSERT_EQUAL(SCROW(7), aReverser.unsort(7));
CPPUNIT_ASSERT_EQUAL(SCROW(8), aReverser.unsort(8));
}
// Reversed order - output rows should be in reverse order than input
{
sc::SortOrderReverser aReverser;
aReverser.addOrderIndices({ 4, 3, 2, 1 }, 5, 8);
CPPUNIT_ASSERT_EQUAL(SCROW(8), aReverser.unsort(5));
CPPUNIT_ASSERT_EQUAL(SCROW(7), aReverser.unsort(6));
CPPUNIT_ASSERT_EQUAL(SCROW(6), aReverser.unsort(7));
CPPUNIT_ASSERT_EQUAL(SCROW(5), aReverser.unsort(8));
}
// Some random order - output rows should follow the order
{
sc::SortOrderReverser aReverser;
aReverser.addOrderIndices({ 3, 1, 4, 2 }, 5, 8);
CPPUNIT_ASSERT_EQUAL(SCROW(7), aReverser.unsort(5));
CPPUNIT_ASSERT_EQUAL(SCROW(5), aReverser.unsort(6));
CPPUNIT_ASSERT_EQUAL(SCROW(8), aReverser.unsort(7));
CPPUNIT_ASSERT_EQUAL(SCROW(6), aReverser.unsort(8));
}
}
CPPUNIT_TEST_FIXTURE(SortOrderReverserTest, testCombiningSortOrder)
{
sc::SortOrderReverser aReverser;
// first sorting order
aReverser.addOrderIndices({ 3, 1, 4, 2 }, 5, 8);
// second sorting order - only reverses the order
aReverser.addOrderIndices({ 4, 3, 2, 1 }, 5, 8);
// check
CPPUNIT_ASSERT_EQUAL(SCROW(6), aReverser.unsort(5));
CPPUNIT_ASSERT_EQUAL(SCROW(8), aReverser.unsort(6));
CPPUNIT_ASSERT_EQUAL(SCROW(5), aReverser.unsort(7));
CPPUNIT_ASSERT_EQUAL(SCROW(7), aReverser.unsort(8));
// directly compare the order
std::vector<SCCOLROW> aExpectedOrder{ 2, 4, 1, 3 };
CPPUNIT_ASSERT(
std::equal(aExpectedOrder.begin(), aExpectedOrder.end(), aReverser.maOrder.begin()));
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */