Add translations for various sub-directories
[binutils-gdb.git] / gdb / unittests / parallel-for-selftests.c
blob0261948361ae17e512f656ae3f35af6b3b802e26
1 /* Self tests for parallel_for_each
3 Copyright (C) 2021-2024 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
22 - FOR_EACH-defined.
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. */
25 #ifndef FOR_EACH
27 #include "gdbsupport/selftest.h"
28 #include "gdbsupport/parallel-for.h"
30 #if CXX_STD_THREAD
32 #include "gdbsupport/thread-pool.h"
34 namespace selftests {
35 namespace parallel_for {
37 struct save_restore_n_threads
39 save_restore_n_threads ()
40 : n_threads (gdb::thread_pool::g_thread_pool->thread_count ())
44 ~save_restore_n_threads ()
46 gdb::thread_pool::g_thread_pool->set_thread_count (n_threads);
49 int n_threads;
52 /* Define test_par using TEST in the FOR_EACH-defined part. */
53 #define TEST test_par
54 #define FOR_EACH gdb::parallel_for_each
55 #include "parallel-for-selftests.c"
56 #undef FOR_EACH
57 #undef TEST
59 /* Define test_seq using TEST in the FOR_EACH-defined part. */
60 #define TEST test_seq
61 #define FOR_EACH gdb::sequential_for_each
62 #include "parallel-for-selftests.c"
63 #undef FOR_EACH
64 #undef TEST
66 static void
67 test (int n_threads)
69 test_par (n_threads);
70 test_seq (n_threads);
73 static void
74 test_n_threads ()
76 test (0);
77 test (1);
78 test (3);
84 #endif /* CXX_STD_THREAD */
86 void _initialize_parallel_for_selftests ();
87 void
88 _initialize_parallel_for_selftests ()
90 #ifdef CXX_STD_THREAD
91 selftests::register_test ("parallel_for",
92 selftests::parallel_for::test_n_threads);
93 #endif /* CXX_STD_THREAD */
96 #else /* FOR_EACH */
98 static void
99 TEST (int n_threads)
101 save_restore_n_threads saver;
102 gdb::thread_pool::g_thread_pool->set_thread_count (n_threads);
104 #define NUMBER 10000
106 std::atomic<int> counter (0);
107 FOR_EACH (1, 0, NUMBER,
108 [&] (int start, int end)
110 counter += end - start;
112 SELF_CHECK (counter == NUMBER);
114 counter = 0;
115 FOR_EACH (1, 0, 0,
116 [&] (int start, int end)
118 counter += end - start;
120 SELF_CHECK (counter == 0);
122 #undef NUMBER
124 /* Check that if there are fewer tasks than threads, then we won't
125 end up with a null result. */
126 std::vector<std::unique_ptr<int>> intresults;
127 std::atomic<bool> any_empty_tasks (false);
129 FOR_EACH (1, 0, 1,
130 [&] (int start, int end)
132 if (start == end)
133 any_empty_tasks = true;
134 return std::make_unique<int> (end - start);
136 SELF_CHECK (!any_empty_tasks);
137 SELF_CHECK (std::all_of (intresults.begin (),
138 intresults.end (),
139 [] (const std::unique_ptr<int> &entry)
141 return entry != nullptr;
142 }));
145 #endif /* FOR_EACH */