Kylin/UnitTest/DataStructure/ListGraphTest.cpp
2023-12-27 10:29:16 +08:00

230 lines
6.4 KiB
C++

#include "ListGraph.h"
#include <boost/test/unit_test.hpp>
using namespace Kylin;
class ListGraphTest {
public:
void initComplexGraph() {
complexGraph.setEdge(0, 1, 10);
complexGraph.setEdge(1, 0, 10);
complexGraph.setEdge(0, 5, 11);
complexGraph.setEdge(5, 0, 11);
complexGraph.setEdge(1, 2, 18);
complexGraph.setEdge(2, 1, 18);
complexGraph.setEdge(1, 8, 12);
complexGraph.setEdge(8, 1, 12);
complexGraph.setEdge(1, 6, 16);
complexGraph.setEdge(6, 1, 16);
complexGraph.setEdge(2, 3, 22);
complexGraph.setEdge(3, 2, 22);
complexGraph.setEdge(2, 8, 8);
complexGraph.setEdge(8, 2, 8);
complexGraph.setEdge(3, 8, 21);
complexGraph.setEdge(8, 3, 21);
complexGraph.setEdge(3, 6, 24);
complexGraph.setEdge(6, 3, 24);
complexGraph.setEdge(3, 7, 16);
complexGraph.setEdge(7, 3, 16);
complexGraph.setEdge(3, 4, 20);
complexGraph.setEdge(4, 3, 20);
complexGraph.setEdge(4, 5, 26);
complexGraph.setEdge(5, 4, 26);
complexGraph.setEdge(4, 7, 7);
complexGraph.setEdge(7, 4, 7);
complexGraph.setEdge(5, 6, 17);
complexGraph.setEdge(6, 5, 17);
complexGraph.setEdge(6, 7, 19);
complexGraph.setEdge(7, 6, 19);
}
void initSearchGraph() {
auto VD = "ABEDCGFHI";
for (size_t i = 0; i < 9; i++) searchGraph.setVertex(i, VD[i]);
searchGraph.setEdge(0, 1, 0);
searchGraph.setEdge(1, 0, 0);
searchGraph.setEdge(0, 3, 0);
searchGraph.setEdge(3, 0, 0);
searchGraph.setEdge(0, 4, 0);
searchGraph.setEdge(4, 0, 0);
searchGraph.setEdge(1, 2, 0);
searchGraph.setEdge(2, 1, 0);
searchGraph.setEdge(1, 4, 0);
searchGraph.setEdge(4, 1, 0);
searchGraph.setEdge(2, 5, 0);
searchGraph.setEdge(5, 2, 0);
searchGraph.setEdge(3, 6, 0);
searchGraph.setEdge(6, 3, 0);
searchGraph.setEdge(4, 6, 0);
searchGraph.setEdge(6, 4, 0);
searchGraph.setEdge(6, 7, 0);
searchGraph.setEdge(7, 6, 0);
searchGraph.setEdge(7, 8, 0);
searchGraph.setEdge(8, 7, 0);
}
void initPathGraph() {
pathGraph.setEdge(0, 1, 10);
pathGraph.setEdge(0, 3, 30);
pathGraph.setEdge(0, 4, 100);
pathGraph.setEdge(1, 2, 50);
pathGraph.setEdge(2, 4, 10);
pathGraph.setEdge(3, 2, 20);
pathGraph.setEdge(3, 4, 60);
}
ListGraphTest() {
graph.addVertex('A');
graph.addVertex('B');
graph.addVertex('C');
graph.addVertex('D');
graph.setEdge(0, 1, 5);
graph.setEdge(0, 3, 6);
graph.setEdge(1, 2, 8);
graph.setEdge(2, 3, 2);
graph.setEdge(3, 1, 9);
initSearchGraph();
initComplexGraph();
initPathGraph();
}
ListGraph<char, int> graph{0};
ListGraph<size_t, size_t> complexGraph{9};
ListGraph<char, int> searchGraph{9};
ListGraph<size_t, size_t> pathGraph{5};
};
BOOST_FIXTURE_TEST_CASE(Vertex, ListGraphTest) { BOOST_CHECK_EQUAL(graph.vertex(2), 'C'); }
BOOST_FIXTURE_TEST_CASE(SetVertex, ListGraphTest) {
graph.setVertex(2, 'Z');
BOOST_CHECK_EQUAL(graph.vertex(2), 'Z');
}
BOOST_FIXTURE_TEST_CASE(Adjacent, ListGraphTest) {
auto adjacent = graph.adjacent(0);
BOOST_CHECK_EQUAL(adjacent.size(), 2);
BOOST_CHECK_EQUAL(adjacent[0], 3);
BOOST_CHECK_EQUAL(adjacent[1], 1);
}
BOOST_FIXTURE_TEST_CASE(Edge1, ListGraphTest) { BOOST_CHECK_EQUAL(*graph.edge(1, 2), 8); }
BOOST_FIXTURE_TEST_CASE(SetEdge, ListGraphTest) {
BOOST_CHECK_EQUAL(graph.setEdge(1, 3, 123), true);
BOOST_CHECK_EQUAL(*(graph.edge(1, 3)), 123);
}
BOOST_FIXTURE_TEST_CASE(RemoveEdge, ListGraphTest) {
graph.removeEdge(3, 1);
BOOST_CHECK_EQUAL(graph.edgeCount(), 4);
BOOST_CHECK_EQUAL(static_cast<bool>(graph.edge(3, 1)), false);
}
BOOST_FIXTURE_TEST_CASE(VertexCount, ListGraphTest) {
BOOST_CHECK_EQUAL(graph.vertexCount(), 4);
BOOST_CHECK_EQUAL(complexGraph.vertexCount(), 9);
}
BOOST_FIXTURE_TEST_CASE(EdgeCount, ListGraphTest) { BOOST_CHECK_EQUAL(graph.edgeCount(), 5); }
BOOST_FIXTURE_TEST_CASE(OutDegree, ListGraphTest) { BOOST_CHECK_EQUAL(graph.outDegree(1), 1); }
BOOST_FIXTURE_TEST_CASE(InDegree, ListGraphTest) { BOOST_CHECK_EQUAL(graph.inDegree(1), 2); }
BOOST_FIXTURE_TEST_CASE(Degree, ListGraphTest) { BOOST_CHECK_EQUAL(graph.degree(1), 3); }
BOOST_FIXTURE_TEST_CASE(RemoveVertex, ListGraphTest) {
graph.removeVertex();
BOOST_CHECK_EQUAL(graph.edgeCount(), 2);
}
BOOST_FIXTURE_TEST_CASE(BreadthFirstSearch, ListGraphTest) {
auto sa = searchGraph.breadthFirstSearch(0);
size_t searchIndex[9]{0, 4, 3, 1, 6, 2, 7, 5, 8};
for (size_t i = 0; i < sa.length(); i++) {
BOOST_CHECK_EQUAL(sa[i], searchIndex[i]);
}
}
BOOST_FIXTURE_TEST_CASE(DepthFirstSearch, ListGraphTest) {
auto sa = searchGraph.depthFirstSearch(0);
size_t searchIndex[9]{0, 1, 2, 5, 4, 6, 3, 7, 8};
for (size_t i = 0; i < sa.length(); i++) {
BOOST_CHECK_EQUAL(sa[i], searchIndex[i]);
}
}
BOOST_FIXTURE_TEST_CASE(Prim, ListGraphTest) {
auto edges = complexGraph.prim(10000);
size_t cost = 0;
for (size_t i = 0; i < edges.size(); i++) {
cost += edges[i].value;
}
BOOST_CHECK_EQUAL(cost, 99);
}
BOOST_FIXTURE_TEST_CASE(Kruskai, ListGraphTest) {
auto edges = complexGraph.kruskal();
size_t cost = 0;
for (size_t i = 0; i < edges.length(); i++) {
cost += edges[i].value;
}
BOOST_CHECK_EQUAL(cost, 99);
}
BOOST_FIXTURE_TEST_CASE(Dijsktra, ListGraphTest) {
size_t paths[]{0, 3, 2, 4};
auto path = pathGraph.dijkstra(0, 4, 10000);
BOOST_CHECK_EQUAL(path.size(), 4);
for (size_t i = 0; i < 4; i++) {
BOOST_CHECK_EQUAL(path[i], paths[i]);
}
}
BOOST_FIXTURE_TEST_CASE(Floy, ListGraphTest) {
ListGraph<int, int> graph(3);
graph.setEdge(0, 1, 4);
graph.setEdge(0, 2, 11);
graph.setEdge(1, 2, 2);
graph.setEdge(1, 0, 6);
graph.setEdge(2, 0, 3);
size_t paths[]{0, 1, 2};
auto path = graph.floyd(0, 2, 10000);
BOOST_CHECK_EQUAL(path.size(), 3);
for (size_t i = 0; i < 3; i++) {
BOOST_CHECK_EQUAL(path[i], paths[i]);
}
}