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 <comphelper/parallelsort.hxx>
11 #include <comphelper/threadpool.hxx>
12 #include <rtl/string.hxx>
13 #include <cppunit/TestAssert.h>
14 #include <cppunit/TestFixture.h>
15 #include <cppunit/extensions/HelperMacros.h>
16 #include <cppunit/plugin/TestPlugIn.h>
23 class ParallelSortTest
: public CppUnit::TestFixture
27 void testSortMedium();
30 virtual void setUp() override
;
31 virtual void tearDown() override
;
33 CPPUNIT_TEST_SUITE(ParallelSortTest
);
34 CPPUNIT_TEST(testSortTiny
);
35 CPPUNIT_TEST(testSortMedium
);
36 CPPUNIT_TEST(testSortBig
);
37 CPPUNIT_TEST_SUITE_END();
40 void sortTest(size_t nLen
);
41 void fillRandomUptoN(std::vector
<size_t>& rVector
, size_t N
);
43 comphelper::ThreadPool
* pThreadPool
;
47 void ParallelSortTest::setUp()
49 pThreadPool
= &comphelper::ThreadPool::getSharedOptimalPool();
50 mnThreads
= pThreadPool
->getWorkerCount();
53 void ParallelSortTest::tearDown()
56 pThreadPool
->joinThreadsIfIdle();
59 void ParallelSortTest::fillRandomUptoN(std::vector
<size_t>& rVector
, size_t N
)
62 for (size_t nIdx
= 0; nIdx
< N
; ++nIdx
)
64 std::shuffle(rVector
.begin(), rVector
.end(), std::default_random_engine(42));
67 void ParallelSortTest::sortTest(size_t nLen
)
69 std::vector
<size_t> aVector(nLen
);
70 fillRandomUptoN(aVector
, nLen
);
71 comphelper::parallelSort(aVector
.begin(), aVector
.end());
72 for (size_t nIdx
= 0; nIdx
< nLen
; ++nIdx
)
74 OString aMsg
= "Wrong aVector[" + OString::number(nIdx
) + "]";
75 CPPUNIT_ASSERT_EQUAL_MESSAGE(aMsg
.getStr(), nIdx
, aVector
[nIdx
]);
79 void ParallelSortTest::testSortTiny()
87 void ParallelSortTest::testSortMedium()
91 sortTest(1024 * 2 + 1);
92 sortTest(1024 * 2 + 9);
95 void ParallelSortTest::testSortBig() { sortTest(1024 * 16 + 3); }
97 CPPUNIT_TEST_SUITE_REGISTRATION(ParallelSortTest
);
99 CPPUNIT_PLUGIN_IMPLEMENT();
101 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */