File: io.cpp

package info (click to toggle)
pagmo 2.19.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 85,228 kB
  • sloc: cpp: 1,753,592; makefile: 223; sh: 121; python: 46
file content (139 lines) | stat: -rw-r--r-- 4,173 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* Copyright 2017-2021 PaGMO development team

This file is part of the PaGMO library.

The PaGMO library is free software; you can redistribute it and/or modify
it under the terms of either:

  * the GNU Lesser General Public License as published by the Free
    Software Foundation; either version 3 of the License, or (at your
    option) any later version.

or

  * the GNU General Public License as published by the Free Software
    Foundation; either version 3 of the License, or (at your option) any
    later version.

or both in parallel, as here.

The PaGMO library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received copies of the GNU General Public License and the
GNU Lesser General Public License along with the PaGMO library.  If not,
see https://www.gnu.org/licenses/. */

#define BOOST_TEST_MODULE io_test
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

#include <pagmo/io.hpp>

#include <initializer_list>
#include <iomanip>
#include <map>
#include <sstream>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>

#include <pagmo/threading.hpp>

using namespace pagmo;

BOOST_AUTO_TEST_CASE(stream_print_test_00)
{
    // A few simple tests.
    std::ostringstream ss1, ss2;
    stream(ss1, 1, 2, 3);
    ss2 << 1 << 2 << 3;
    BOOST_CHECK_EQUAL(ss1.str(), ss2.str());
    ss1.str("");
    ss2.str("");
    stream(ss1, "Hello ", std::string(" world"));
    ss2 << "Hello " << std::string(" world");
    BOOST_CHECK_EQUAL(ss1.str(), ss2.str());
    ss1.str("");
    ss2.str("");
    // Try with floating-point too.
    stream(ss1, 1.234);
    ss2 << 1.234;
    BOOST_CHECK_EQUAL(ss1.str(), ss2.str());
    ss1.str("");
    ss2.str("");
    // Custom precision.
    ss1 << std::setprecision(10);
    ss2 << std::setprecision(10);
    stream(ss1, 1.234);
    ss2 << 1.234;
    BOOST_CHECK_EQUAL(ss1.str(), ss2.str());
    ss1.str("");
    ss2.str("");
    // Special handling of bool.
    stream(ss1, true, ' ', false);
    BOOST_CHECK_EQUAL(ss1.str(), "true false");
    ss1.str("");
    // Vectors.
    // Empty vector.
    stream(ss1, std::vector<int>{});
    BOOST_CHECK_EQUAL(ss1.str(), "[]");
    ss1.str("");
    // Single element.
    stream(ss1, std::vector<int>{1});
    ss2 << "[" << 1 << "]";
    BOOST_CHECK_EQUAL(ss1.str(), ss2.str());
    ss1.str("");
    ss2.str("");
    // Multiple elements.
    stream(ss1, std::vector<int>{1, 2, 3});
    ss2 << "[" << 1 << ", " << 2 << ", " << 3 << "]";
    BOOST_CHECK_EQUAL(ss1.str(), ss2.str());
    ss1.str("");
    ss2.str("");
    // Vector equal to the print limit.
    stream(ss1, std::vector<int>{1, 2, 3, 4, 5});
    ss2 << "[" << 1 << ", " << 2 << ", " << 3 << ", " << 4 << ", " << 5 << "]";
    BOOST_CHECK_EQUAL(ss1.str(), ss2.str());
    ss1.str("");
    ss2.str("");
    // Vector larger than the print limit.
    stream(ss1, std::vector<int>{1, 2, 3, 4, 5, 6});
    ss2 << "[" << 1 << ", " << 2 << ", " << 3 << ", " << 4 << ", " << 5 << ", ... ]";
    BOOST_CHECK_EQUAL(ss1.str(), ss2.str());
    // Go for the print as well, yay.
    print(std::vector<int>{1, 2, 3, 4, 5, 6});
    // Thread safety levels.
    ss1.str("");
    stream(ss1, thread_safety::none);
    BOOST_CHECK_EQUAL(ss1.str(), "none");
    ss1.str("");
    stream(ss1, thread_safety::basic);
    BOOST_CHECK_EQUAL(ss1.str(), "basic");
}

BOOST_AUTO_TEST_CASE(stream_print_test_01)
{
    // Map.
    using map_t = std::map<int, int>;
    std::stringstream ss;
    stream(ss, map_t{{0, 0}, {1, 1}, {2, 2}});
    print(map_t{{0, 0}, {1, 1}, {2, 2}});
    ss.str("");
    stream(ss, map_t{{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}});
    print(map_t{{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}});
    // Pair.
    using pair_t = std::pair<int, int>;
    ss.str("");
    stream(ss, pair_t{1, 2});
    print(pair_t{1, 2});
}

BOOST_AUTO_TEST_CASE(stream_table_test)
{
    detail::table t({"a", "b", "c"});
    BOOST_CHECK_THROW(t.add_row(), std::invalid_argument);
}