1 /* Copyright 2017-2021 PaGMO development team
2 
3 This file is part of the PaGMO library.
4 
5 The PaGMO library is free software; you can redistribute it and/or modify
6 it under the terms of either:
7 
8   * the GNU Lesser General Public License as published by the Free
9     Software Foundation; either version 3 of the License, or (at your
10     option) any later version.
11 
12 or
13 
14   * the GNU General Public License as published by the Free Software
15     Foundation; either version 3 of the License, or (at your option) any
16     later version.
17 
18 or both in parallel, as here.
19 
20 The PaGMO library is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
23 for more details.
24 
25 You should have received copies of the GNU General Public License and the
26 GNU Lesser General Public License along with the PaGMO library.  If not,
27 see https://www.gnu.org/licenses/. */
28 
29 #define BOOST_TEST_MODULE thread_island_test
30 #define BOOST_TEST_DYN_LINK
31 #include <boost/test/unit_test.hpp>
32 
33 #include <initializer_list>
34 #include <stdexcept>
35 #include <utility>
36 
37 #include <boost/algorithm/string/predicate.hpp>
38 
39 #include <pagmo/island.hpp>
40 #include <pagmo/islands/thread_island.hpp>
41 #include <pagmo/population.hpp>
42 #include <pagmo/problem.hpp>
43 #include <pagmo/threading.hpp>
44 #include <pagmo/types.hpp>
45 
46 using namespace pagmo;
47 
48 // Thread-unsafe UDA.
49 struct tu_uda {
evolvetu_uda50     population evolve(const population &pop) const
51     {
52         return pop;
53     }
get_thread_safetytu_uda54     thread_safety get_thread_safety() const
55     {
56         return thread_safety::none;
57     }
58 };
59 
60 // Thread unsafe UDP.
61 struct tu_udp {
fitnesstu_udp62     vector_double fitness(const vector_double &) const
63     {
64         return {1};
65     }
get_boundstu_udp66     std::pair<vector_double, vector_double> get_bounds() const
67     {
68         return {{0}, {1}};
69     }
get_thread_safetytu_udp70     thread_safety get_thread_safety() const
71     {
72         return thread_safety::none;
73     }
74 };
75 
BOOST_AUTO_TEST_CASE(thread_island_test)76 BOOST_AUTO_TEST_CASE(thread_island_test)
77 {
78     {
79         island isl(thread_island{}, tu_uda{}, problem(), 20u);
80         isl.evolve();
81         BOOST_CHECK_EXCEPTION(isl.wait_check(), std::invalid_argument, [](const std::invalid_argument &ia) {
82             return boost::contains(ia.what(),
83                                    "the 'thread_island' UDI requires an algorithm providing at least the 'basic' "
84                                    "thread safety guarantee");
85         });
86     }
87     {
88         island isl(thread_island{}, algorithm(), tu_udp{}, 20u);
89         isl.evolve();
90         BOOST_CHECK_EXCEPTION(isl.wait_check(), std::invalid_argument, [](const std::invalid_argument &ia) {
91             return boost::contains(ia.what(),
92                                    "the 'thread_island' UDI requires a problem providing at least the 'basic' "
93                                    "thread safety guarantee");
94         });
95     }
96 }
97