Version 24.8.3.2, tag libreoffice-24.8.3.2
[LibreOffice.git] / comphelper / qa / unit / parallelsorttest.cxx
bloba3618244ab8d8a13e819708eea9fef1589496e1d
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 <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>
18 #include <cstdlib>
19 #include <vector>
20 #include <algorithm>
21 #include <random>
23 class ParallelSortTest : public CppUnit::TestFixture
25 public:
26 void testSortTiny();
27 void testSortMedium();
28 void testSortBig();
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();
39 private:
40 void sortTest(size_t nLen);
41 void fillRandomUptoN(std::vector<size_t>& rVector, size_t N);
43 comphelper::ThreadPool* pThreadPool;
44 size_t mnThreads;
47 void ParallelSortTest::setUp()
49 pThreadPool = &comphelper::ThreadPool::getSharedOptimalPool();
50 mnThreads = pThreadPool->getWorkerCount();
53 void ParallelSortTest::tearDown()
55 if (pThreadPool)
56 pThreadPool->joinThreadsIfIdle();
59 void ParallelSortTest::fillRandomUptoN(std::vector<size_t>& rVector, size_t N)
61 rVector.resize(N);
62 for (size_t nIdx = 0; nIdx < N; ++nIdx)
63 rVector[nIdx] = 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()
81 sortTest(5);
82 sortTest(15);
83 sortTest(16);
84 sortTest(17);
87 void ParallelSortTest::testSortMedium()
89 sortTest(1025);
90 sortTest(1029);
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: */