File: ring.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 (209 lines) | stat: -rw-r--r-- 5,797 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* 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 ring
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

#include <algorithm>
#include <cstddef>
#include <iostream>
#include <sstream>
#include <stdexcept>

#include <boost/algorithm/string/predicate.hpp>
#include <boost/graph/adjacency_list.hpp>

#include <pagmo/s11n.hpp>
#include <pagmo/topologies/ring.hpp>
#include <pagmo/topology.hpp>

using namespace pagmo;

void verify_ring_topology(const ring &r)
{
    const auto s = r.num_vertices();

    if (s < 2u) {
        return;
    }

    const auto w = r.get_weight();

    for (std::size_t i = 0; i < s; ++i) {
        const auto conn = r.get_connections(i);
        if (s > 2u) {
            BOOST_CHECK(conn.first.size() == 2u);
            BOOST_CHECK(conn.second.size() == 2u);
        } else {
            BOOST_CHECK(conn.first.size() == 1u);
            BOOST_CHECK(conn.second.size() == 1u);
        }
        BOOST_CHECK(std::all_of(conn.second.begin(), conn.second.end(), [w](double x) { return x == w; }));

        const auto next = (i + 1u) % s;
        const auto prev = (i == 0u) ? (s - 1u) : (i - 1u);
        BOOST_CHECK(std::find(conn.first.begin(), conn.first.end(), next) != conn.first.end());
        BOOST_CHECK(std::find(conn.first.begin(), conn.first.end(), prev) != conn.first.end());
    }
}

BOOST_AUTO_TEST_CASE(basic_test)
{
    ring r0;
    BOOST_CHECK(r0.get_weight() == 1);
    BOOST_CHECK(r0.num_vertices() == 0u);

    r0.push_back();
    BOOST_CHECK(r0.num_vertices() == 1u);

    r0.push_back();
    BOOST_CHECK(r0.num_vertices() == 2u);
    verify_ring_topology(r0);

    r0.push_back();
    BOOST_CHECK(r0.num_vertices() == 3u);
    verify_ring_topology(r0);

    r0.push_back();
    BOOST_CHECK(r0.num_vertices() == 4u);
    verify_ring_topology(r0);

    r0.push_back();
    BOOST_CHECK(r0.num_vertices() == 5u);
    verify_ring_topology(r0);

    r0.push_back();
    BOOST_CHECK(r0.num_vertices() == 6u);
    verify_ring_topology(r0);

    r0 = ring(.5);
    BOOST_CHECK(r0.get_weight() == .5);
    BOOST_CHECK(r0.num_vertices() == 0u);

    r0.push_back();
    BOOST_CHECK(r0.num_vertices() == 1u);

    r0.push_back();
    BOOST_CHECK(r0.num_vertices() == 2u);
    verify_ring_topology(r0);

    r0.push_back();
    BOOST_CHECK(r0.num_vertices() == 3u);
    verify_ring_topology(r0);

    r0.push_back();
    BOOST_CHECK(r0.num_vertices() == 4u);
    verify_ring_topology(r0);

    r0.push_back();
    BOOST_CHECK(r0.num_vertices() == 5u);
    verify_ring_topology(r0);

    r0.push_back();
    BOOST_CHECK(r0.num_vertices() == 6u);
    verify_ring_topology(r0);

    BOOST_CHECK(r0.get_name() == "Ring");

    // Minimal serialization test.
    {
        topology t0(r0);
        std::stringstream ss;
        {
            boost::archive::binary_oarchive oarchive(ss);
            oarchive << t0;
        }
        topology t1;
        BOOST_CHECK(!t1.is<ring>());
        {
            boost::archive::binary_iarchive iarchive(ss);
            iarchive >> t1;
        }
        BOOST_CHECK(t1.is<ring>());
        BOOST_CHECK(t1.extract<ring>()->num_vertices() == 6u);
        BOOST_CHECK(t1.extract<ring>()->get_weight() == .5);
        verify_ring_topology(*t1.extract<ring>());
    }

    // Ctor from edge weight.
    BOOST_CHECK_EXCEPTION(r0 = ring(-2), std::invalid_argument, [](const std::invalid_argument &ia) {
        return boost::contains(ia.what(), " is not in the [0., 1.] range");
    });

    // Ctor from number of vertices and edge weight.
    BOOST_CHECK_EXCEPTION(r0 = ring(0, -2), std::invalid_argument, [](const std::invalid_argument &ia) {
        return boost::contains(ia.what(), " is not in the [0., 1.] range");
    });

    r0 = ring(0, 0);

    BOOST_CHECK(r0.get_weight() == 0.);
    BOOST_CHECK(r0.num_vertices() == 0u);
    verify_ring_topology(r0);

    r0 = ring(1, .2);
    BOOST_CHECK(r0.get_weight() == .2);
    BOOST_CHECK(r0.num_vertices() == 1u);
    verify_ring_topology(r0);

    r0 = ring(2, .3);
    BOOST_CHECK(r0.get_weight() == .3);
    BOOST_CHECK(r0.num_vertices() == 2u);
    verify_ring_topology(r0);

    r0 = ring(3, .4);
    BOOST_CHECK(r0.get_weight() == .4);
    BOOST_CHECK(r0.num_vertices() == 3u);
    verify_ring_topology(r0);

    r0 = ring(4, .5);
    BOOST_CHECK(r0.get_weight() == .5);
    BOOST_CHECK(r0.num_vertices() == 4u);
    verify_ring_topology(r0);

    // Example of cout printing for ring.
    r0.push_back();
    r0.push_back();
    r0.push_back();
    r0.push_back();

    r0.set_weight(0, 1, .1);
    r0.set_weight(4, 5, .7);

    std::cout << r0.get_extra_info() << '\n';
}

BOOST_AUTO_TEST_CASE(to_bgl_test)
{
    BOOST_CHECK(has_to_bgl<ring>::value);

    auto r0 = ring(4, .5);
    BOOST_CHECK(boost::num_vertices(r0.to_bgl()) == 4);
    BOOST_CHECK(boost::num_edges(r0.to_bgl()) == 8);
}