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 io_test
30 #define BOOST_TEST_DYN_LINK
31 #include <boost/test/unit_test.hpp>
32 
33 #include <pagmo/io.hpp>
34 
35 #include <initializer_list>
36 #include <iomanip>
37 #include <map>
38 #include <sstream>
39 #include <stdexcept>
40 #include <string>
41 #include <utility>
42 #include <vector>
43 
44 #include <pagmo/threading.hpp>
45 
46 using namespace pagmo;
47 
BOOST_AUTO_TEST_CASE(stream_print_test_00)48 BOOST_AUTO_TEST_CASE(stream_print_test_00)
49 {
50     // A few simple tests.
51     std::ostringstream ss1, ss2;
52     stream(ss1, 1, 2, 3);
53     ss2 << 1 << 2 << 3;
54     BOOST_CHECK_EQUAL(ss1.str(), ss2.str());
55     ss1.str("");
56     ss2.str("");
57     stream(ss1, "Hello ", std::string(" world"));
58     ss2 << "Hello " << std::string(" world");
59     BOOST_CHECK_EQUAL(ss1.str(), ss2.str());
60     ss1.str("");
61     ss2.str("");
62     // Try with floating-point too.
63     stream(ss1, 1.234);
64     ss2 << 1.234;
65     BOOST_CHECK_EQUAL(ss1.str(), ss2.str());
66     ss1.str("");
67     ss2.str("");
68     // Custom precision.
69     ss1 << std::setprecision(10);
70     ss2 << std::setprecision(10);
71     stream(ss1, 1.234);
72     ss2 << 1.234;
73     BOOST_CHECK_EQUAL(ss1.str(), ss2.str());
74     ss1.str("");
75     ss2.str("");
76     // Special handling of bool.
77     stream(ss1, true, ' ', false);
78     BOOST_CHECK_EQUAL(ss1.str(), "true false");
79     ss1.str("");
80     // Vectors.
81     // Empty vector.
82     stream(ss1, std::vector<int>{});
83     BOOST_CHECK_EQUAL(ss1.str(), "[]");
84     ss1.str("");
85     // Single element.
86     stream(ss1, std::vector<int>{1});
87     ss2 << "[" << 1 << "]";
88     BOOST_CHECK_EQUAL(ss1.str(), ss2.str());
89     ss1.str("");
90     ss2.str("");
91     // Multiple elements.
92     stream(ss1, std::vector<int>{1, 2, 3});
93     ss2 << "[" << 1 << ", " << 2 << ", " << 3 << "]";
94     BOOST_CHECK_EQUAL(ss1.str(), ss2.str());
95     ss1.str("");
96     ss2.str("");
97     // Vector equal to the print limit.
98     stream(ss1, std::vector<int>{1, 2, 3, 4, 5});
99     ss2 << "[" << 1 << ", " << 2 << ", " << 3 << ", " << 4 << ", " << 5 << "]";
100     BOOST_CHECK_EQUAL(ss1.str(), ss2.str());
101     ss1.str("");
102     ss2.str("");
103     // Vector larger than the print limit.
104     stream(ss1, std::vector<int>{1, 2, 3, 4, 5, 6});
105     ss2 << "[" << 1 << ", " << 2 << ", " << 3 << ", " << 4 << ", " << 5 << ", ... ]";
106     BOOST_CHECK_EQUAL(ss1.str(), ss2.str());
107     // Go for the print as well, yay.
108     print(std::vector<int>{1, 2, 3, 4, 5, 6});
109     // Thread safety levels.
110     ss1.str("");
111     stream(ss1, thread_safety::none);
112     BOOST_CHECK_EQUAL(ss1.str(), "none");
113     ss1.str("");
114     stream(ss1, thread_safety::basic);
115     BOOST_CHECK_EQUAL(ss1.str(), "basic");
116 }
117 
BOOST_AUTO_TEST_CASE(stream_print_test_01)118 BOOST_AUTO_TEST_CASE(stream_print_test_01)
119 {
120     // Map.
121     using map_t = std::map<int, int>;
122     std::stringstream ss;
123     stream(ss, map_t{{0, 0}, {1, 1}, {2, 2}});
124     print(map_t{{0, 0}, {1, 1}, {2, 2}});
125     ss.str("");
126     stream(ss, map_t{{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}});
127     print(map_t{{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}});
128     // Pair.
129     using pair_t = std::pair<int, int>;
130     ss.str("");
131     stream(ss, pair_t{1, 2});
132     print(pair_t{1, 2});
133 }
134 
BOOST_AUTO_TEST_CASE(stream_table_test)135 BOOST_AUTO_TEST_CASE(stream_table_test)
136 {
137     detail::table t({"a", "b", "c"});
138     BOOST_CHECK_THROW(t.add_row(), std::invalid_argument);
139 }