KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_priority_thread_pool_task.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <boost/test/unit_test.hpp>
21
22#include <atomic>
23#include <stdexcept>
24#include <vector>
25
27
28
29namespace
30{
31
36class COUNTING_TASK : public PRIORITY_THREAD_POOL_TASK<std::vector<int>>
37{
38public:
39 std::atomic<int> m_processed{ 0 };
40
41private:
42 int computePriorityKey( const int& aItem ) const override { return aItem; }
43
44 size_t task( int& aItem ) override
45 {
46 if( aItem < 0 )
47 throw std::runtime_error( "negative item" );
48
49 ++m_processed;
50 return 1;
51 }
52};
53
54} // namespace
55
56
57BOOST_AUTO_TEST_SUITE( PriorityThreadPoolTask )
58
59
60BOOST_AUTO_TEST_CASE( AllItemsProcessed )
61{
62 COUNTING_TASK task;
63 std::vector<int> items = { 5, 3, 8, 1, 9 };
64
65 BOOST_CHECK_NO_THROW( task.Execute( items ) );
66
67 // All items should have been processed by now
68 BOOST_CHECK_EQUAL( task.m_processed.load(), (int) items.size() );
69}
70
71
72BOOST_AUTO_TEST_CASE( ExceptionPropagates )
73{
74 COUNTING_TASK task;
75 std::vector<int> items = { 5, 3, -1, 1, 9 };
76
77 BOOST_CHECK_THROW( task.Execute( items ), std::runtime_error );
78}
79
80
A helper class to execute tasks on a thread pool in priority order, with progress reporting.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(AllItemsProcessed)
BOOST_CHECK_EQUAL(result, "25.4")