Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
grid_test.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32#include <gtest/gtest.h>
33#include <grid.H>
34#include <tpl_graph.H>
35
36using namespace Aleph;
37
40 Build_Grid<decltype(g)> build;
41 build(g, 10, 10);
42
43 EXPECT_EQ(g.get_num_nodes(), 100);
44
45 // horizontal: 10 * 9 = 90
46 // vertical: 10 * 9 = 90
47 // upper-left diag: 9 * 9 = 81
48 // upper-right diag: 9 * 9 = 81
49 // Total arcs = 342
50 EXPECT_EQ(g.get_num_arcs(), 342);
51}
52
55 Build_Grid<decltype(g)> build;
56
57 EXPECT_THROW(build(g, 1, 10), std::length_error);
58 EXPECT_THROW(build(g, 10, 1), std::length_error);
59
60 EXPECT_NO_THROW(build(g, 2, 2));
62}
63
66
67 struct NodeOp {
68 void operator()(decltype(g)& /*g*/, decltype(g)::Node* n, size_t row, size_t col) {
69 n->get_info() = row * 100 + col;
70 }
71 };
72
73 struct ArcOp {
74 void operator()(decltype(g)& /*g*/, decltype(g)::Arc* a, size_t /*row*/, size_t /*col*/) {
75 a->get_info() = 1;
76 }
77 };
78
79 Build_Grid<decltype(g), NodeOp, ArcOp> build;
80 build(g, 3, 3);
81
83
84 size_t sum_info = 0;
85 for (auto it = g.get_node_it(); it.has_curr(); it.next()) {
86 sum_info += it.get_curr()->get_info();
87 }
88
89 // row 0: 0, 1, 2 = 3
90 // row 1: 100, 101, 102 = 303
91 // row 2: 200, 201, 202 = 603
92 // sum = 909
93 EXPECT_EQ(sum_info, 909);
94
95 size_t arc_sum = 0;
96 for (auto it = g.get_arc_it(); it.has_curr(); it.next()) {
97 arc_sum += it.get_curr()->get_info();
98 }
99
100 // 3x3 grid:
101 // horiz: 3 * 2 = 6
102 // vert: 3 * 2 = 6
103 // ul: 2 * 2 = 4
104 // ur: 2 * 2 = 4
105 // total = 20
106 EXPECT_EQ(arc_sum, 20);
107}
WeightedDigraph::Node Node
WeightedDigraph::Arc Arc
size_t row
Definition ca-c-api.h:115
size_t size_t col
Definition ca-c-api.h:116
Functor to build a 2D grid graph with 8-connectivity.
Definition grid.H:178
Graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:429
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Definition graph-dry.H:637
auto get_arc_it() const noexcept
Obtains an iterator to the arc of graph.
Definition graph-dry.H:2908
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:737
constexpr size_t get_num_arcs() const noexcept
Definition graph-dry.H:826
auto get_node_it() const noexcept
Obtains an iterator to the nodes of graph.
Definition graph-dry.H:2886
#define TEST(name)
2D grid graph generation with 8-connectivity.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:223
Generic graph and digraph implementations.