1 /* Self tests for parallel_for_each
3 Copyright (C) 2021-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* This file is divided in two parts:
21 - FOR_EACH-undefined, and
23 The former includes the latter, more than once, with different values for
24 FOR_EACH. The FOR_EACH-defined part reads like a regular function. */
28 #include "gdbsupport/selftest.h"
29 #include "gdbsupport/parallel-for.h"
33 #include "gdbsupport/thread-pool.h"
36 namespace parallel_for {
38 struct save_restore_n_threads
40 save_restore_n_threads ()
41 : n_threads (gdb::thread_pool::g_thread_pool->thread_count ())
45 ~save_restore_n_threads ()
47 gdb::thread_pool::g_thread_pool->set_thread_count (n_threads);
53 /* Define test_par using TEST in the FOR_EACH-defined part. */
55 #define FOR_EACH gdb::parallel_for_each
56 #include "parallel-for-selftests.c"
60 /* Define test_seq using TEST in the FOR_EACH-defined part. */
62 #define FOR_EACH gdb::sequential_for_each
63 #include "parallel-for-selftests.c"
85 #endif /* CXX_STD_THREAD */
87 void _initialize_parallel_for_selftests ();
89 _initialize_parallel_for_selftests ()
92 selftests::register_test ("parallel_for",
93 selftests::parallel_for::test_n_threads);
94 #endif /* CXX_STD_THREAD */
102 save_restore_n_threads saver;
103 gdb::thread_pool::g_thread_pool->set_thread_count (n_threads);
107 std::atomic<int> counter (0);
108 FOR_EACH (1, 0, NUMBER,
109 [&] (int start, int end)
111 counter += end - start;
113 SELF_CHECK (counter == NUMBER);
117 [&] (int start, int end)
119 counter += end - start;
121 SELF_CHECK (counter == 0);
123 auto task_size_max_ = [] (int iter)
125 return (size_t)SIZE_MAX;
127 auto task_size_max = gdb::make_function_view (task_size_max_);
130 FOR_EACH (1, 0, NUMBER,
131 [&] (int start, int end)
133 counter += end - start;
135 SELF_CHECK (counter == NUMBER);
137 auto task_size_one_ = [] (int iter)
141 auto task_size_one = gdb::make_function_view (task_size_one_);
144 FOR_EACH (1, 0, NUMBER,
145 [&] (int start, int end)
147 counter += end - start;
149 SELF_CHECK (counter == NUMBER);
154 #endif /* FOR_EACH */