-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdaw_graph_algorithm_test.cpp
More file actions
186 lines (167 loc) · 4.88 KB
/
daw_graph_algorithm_test.cpp
File metadata and controls
186 lines (167 loc) · 4.88 KB
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
// Copyright (c) Darrell Wright
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/beached/header_libraries
//
#include "daw/daw_graph.h"
#include "daw/daw_graph_algorithm.h"
#include "daw/daw_algorithm.h"
#include "daw/daw_benchmark.h"
#include <iostream>
#include <iterator>
#include <string>
#include <unordered_map>
#include <vector>
void test_topoligical_walk_001( daw::graph_t<char> &graph ) {
std::string result{ };
daw::topological_sorted_walk(
graph,
[&result]( auto const &node ) {
result.push_back( node.value( ) );
},
[]( auto const &lhs, auto const &rhs ) {
return lhs.value( ) < rhs.value( );
} );
daw::expecting( "CABDFE", result );
}
void test_topoligical_walk_002( daw::graph_t<char> const &graph ) {
std::string result{ };
daw::topological_sorted_walk(
graph,
[&result]( auto const &node ) {
result.push_back( node.value( ) );
},
[]( auto const &lhs, auto const &rhs ) {
return lhs.value( ) < rhs.value( );
} );
daw::expecting( "CABDFE", result );
}
void test_topoligical_walk_003( ) {
daw::graph_t<char> graph{ };
auto n0 = graph.add_node( '0' );
auto n1 = graph.add_node( '1' );
auto n2 = graph.add_node( '2' );
auto n3 = graph.add_node( '3' );
auto n4 = graph.add_node( '4' );
auto n5 = graph.add_node( '5' );
graph.add_directed_edge( n2, n3 );
graph.add_directed_edge( n3, n1 );
graph.add_directed_edge( n4, n0 );
graph.add_directed_edge( n4, n1 );
graph.add_directed_edge( n5, n0 );
graph.add_directed_edge( n5, n2 );
std::string result{ };
daw::topological_sorted_walk(
graph,
[&result]( auto const &node ) {
result.push_back( node.value( ) );
},
[]( auto const &lhs, auto const &rhs ) {
return lhs.value( ) < rhs.value( );
} );
daw::expecting( "542310", result );
}
void test_topoligical_range_001( ) {
daw::graph_t<char> graph{ };
auto n0 = graph.add_node( '0' );
auto n1 = graph.add_node( '1' );
auto n2 = graph.add_node( '2' );
auto n3 = graph.add_node( '3' );
auto n4 = graph.add_node( '4' );
auto n5 = graph.add_node( '5' );
graph.add_directed_edge( n2, n3 );
graph.add_directed_edge( n3, n1 );
graph.add_directed_edge( n4, n0 );
graph.add_directed_edge( n4, n1 );
graph.add_directed_edge( n5, n0 );
graph.add_directed_edge( n5, n2 );
std::string result{ };
auto rng = daw::make_topological_sorted_range(
graph, []( auto const &lhs, auto const &rhs ) {
return lhs.value( ) < rhs.value( );
} );
result.reserve( rng.size( ) );
daw::algorithm::transform(
rng.begin( ), rng.end( ), std::back_inserter( result ), []( auto &&node ) {
return node.value( );
} );
daw::expecting( "542310", result );
}
void test_bfs_walk_001( daw::graph_t<char> const &graph,
daw::node_id_t root_id ) {
std::string result{ };
daw::bfs_walk(
graph,
root_id,
[&result]( auto &&node ) {
result.push_back( node.value( ) );
},
std::less<void>{ } );
daw::expecting( "CAFBDEE", result );
}
void test_dfs_walk_001( daw::graph_t<char> graph, daw::node_id_t root_id ) {
std::string result{ };
daw::dfs_walk(
graph,
root_id,
[&result]( auto &&node ) {
result.push_back( node.value( ) );
},
std::less<void>{ } );
daw::expecting( "CABEDF", result );
}
void test_dfs_walk_002( daw::graph_t<char> const &graph,
daw::node_id_t root_id ) {
std::string result{ };
daw::dfs_walk(
graph,
root_id,
[&result]( auto &&node ) {
result.push_back( node.value( ) );
},
std::less<void>{ } );
daw::expecting( "CABEDF", result );
}
void test_mst_001( daw::graph_t<char> graph, daw::node_id_t ) {
std::string result{ };
daw::mst( graph );
daw::topological_sorted_walk(
graph,
[&result]( auto const &node ) {
result.push_back( node.value( ) );
},
[]( auto const &lhs, auto const &rhs ) {
return lhs.value( ) < rhs.value( );
} );
daw::expecting( "CABDEF", result );
}
int main( ) {
daw::graph_t<char> graph{ };
auto nA = graph.add_node( 'A' );
auto nB = graph.add_node( 'B' );
auto nC = graph.add_node( 'C' );
auto nD = graph.add_node( 'D' );
auto nE = graph.add_node( 'E' );
auto nF = graph.add_node( 'F' );
graph.add_directed_edge( nC, nA );
graph.add_directed_edge( nC, nF );
graph.add_directed_edge( nA, nB );
graph.add_directed_edge( nA, nD );
graph.add_directed_edge( nB, nE );
graph.add_directed_edge( nF, nE );
std::cout << "Starting test_topoligical_range_00\n";
test_topoligical_range_001( );
std::cout << "Starting test_topoligical_walk_003\n";
test_topoligical_walk_003( );
// TODO: failing
// test_topoligical_walk_001( graph );
// test_topoligical_walk_002( graph );
std::cout << "Starting test_bfs_walk_001\n";
test_bfs_walk_001( graph, nC );
// TODO: failing
test_dfs_walk_001( graph, nC );
test_dfs_walk_002( graph, nC );
// test_mst_001( graph, nC );
}