Skip to content

Commit 41081d8

Browse files
committed
multidigraph tests
1 parent fcd187b commit 41081d8

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

tests/test_multidigraph.cpp

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#include "multidigraph.h"
2+
#include <gtest/gtest.h>
3+
4+
TEST(MultiDiGraphTest, AddMultipleEdges) {
5+
MultiDiGraph g;
6+
g.add_edge(1, 2, {{"weight", "1.0"}});
7+
g.add_edge(1, 2, {{"weight", "2.0"}});
8+
9+
EXPECT_TRUE(g.has_edge(1, 2));
10+
EXPECT_EQ(static_cast<int>(g.size()), 2);
11+
}
12+
13+
TEST(MultiDiGraphTest, RemoveEdgeWithKey) {
14+
MultiDiGraph g;
15+
g.add_edge(1, 2, {{"label", "a"}});
16+
g.add_edge(1, 2, {{"label", "b"}});
17+
18+
auto edges = g.edges();
19+
ASSERT_EQ(edges.size(), 2);
20+
int key = std::get<2>(edges[0]);
21+
22+
g.remove_edge(1, 2, key);
23+
EXPECT_EQ(static_cast<int>(g.size()), 1);
24+
EXPECT_TRUE(g.has_edge(1, 2));
25+
}
26+
27+
TEST(MultiDiGraphTest, HasEdgeWithKey) {
28+
MultiDiGraph g;
29+
g.add_edge(1, 2);
30+
g.add_edge(1, 2);
31+
32+
auto edges = g.edges();
33+
ASSERT_EQ(edges.size(), 2);
34+
int key0 = std::get<2>(edges[0]);
35+
int key1 = std::get<2>(edges[1]);
36+
37+
EXPECT_TRUE(g.has_edge(1, 2, key0));
38+
EXPECT_TRUE(g.has_edge(1, 2, key1));
39+
EXPECT_FALSE(g.has_edge(2, 1, key0)); // directed
40+
}
41+
42+
TEST(MultiDiGraphTest, EdgeAttributes) {
43+
MultiDiGraph g;
44+
g.add_edge(1, 2, {{"label", "x"}});
45+
g.add_edge(1, 2, {{"label", "y"}});
46+
47+
std::set<std::string> labels;
48+
for (const auto& [u, v, k] : g.edges()) {
49+
labels.insert(g._multi_adj[u][v][k].at("label"));
50+
}
51+
52+
EXPECT_EQ(labels.size(), 2);
53+
EXPECT_TRUE(labels.count("x"));
54+
EXPECT_TRUE(labels.count("y"));
55+
}
56+
57+
TEST(MultiDiGraphTest, DegreeCountsAllEdges) {
58+
MultiDiGraph g;
59+
g.add_edge(1, 2);
60+
g.add_edge(1, 2);
61+
g.add_edge(2, 3);
62+
63+
auto deg = g.degree();
64+
EXPECT_EQ(deg[1], 2);
65+
EXPECT_EQ(deg[2], 1); // only out-edges counted
66+
}
67+
68+
TEST(MultiDiGraphTest, NeighborsCorrect) {
69+
MultiDiGraph g;
70+
g.add_edge(1, 2);
71+
g.add_edge(1, 3);
72+
g.add_edge(1, 2);
73+
74+
auto nbrs = g.neighbors(1);
75+
std::set<int> nbr_set(nbrs.begin(), nbrs.end());
76+
77+
EXPECT_EQ(nbr_set.size(), 2);
78+
EXPECT_TRUE(nbr_set.count(2));
79+
EXPECT_TRUE(nbr_set.count(3));
80+
}
81+
82+
TEST(MultiDiGraphTest, CopyPreservesGraph) {
83+
MultiDiGraph g;
84+
g.add_edge(1, 2);
85+
g.add_edge(1, 2);
86+
g.add_edge(2, 3);
87+
88+
MultiDiGraph g2 = g.copy();
89+
EXPECT_EQ(static_cast<int>(g2.size()), 3);
90+
EXPECT_TRUE(g2.has_edge(1, 2));
91+
EXPECT_TRUE(g2.has_edge(2, 3));
92+
}
93+
94+
TEST(MultiDiGraphTest, ClearEdgesPreservesNodes) {
95+
MultiDiGraph g;
96+
g.add_node(1, {{"color", "red"}});
97+
g.add_node(2);
98+
g.add_edge(1, 2);
99+
g.add_edge(1, 2);
100+
101+
g.clear_edges();
102+
103+
EXPECT_EQ(static_cast<int>(g.size()), 0);
104+
EXPECT_TRUE(g.has_node(1));
105+
EXPECT_TRUE(g.has_node(2));
106+
EXPECT_EQ(g._node.at(1).at("color"), "red");
107+
}
108+
109+
TEST(MultiDiGraphTest, IsMultigraphAndDirected) {
110+
MultiDiGraph g;
111+
EXPECT_TRUE(g.is_multigraph());
112+
EXPECT_TRUE(g.is_directed());
113+
}
114+
115+
TEST(MultiDiGraphTest, SizeWithWeights) {
116+
MultiDiGraph g;
117+
g.add_edge(1, 2, {{"w", "1.5"}});
118+
g.add_edge(1, 2, {{"w", "2.5"}});
119+
g.add_edge(2, 3, {{"w", "3.0"}});
120+
121+
EXPECT_DOUBLE_EQ(g.size("w"), 7.0);
122+
}
123+
124+
TEST(MultiDiGraphTest, ToUndirectedCreatesMultiGraph) {
125+
MultiDiGraph g;
126+
g.add_edge(1, 2, {{"x", "a"}});
127+
g.add_edge(1, 2, {{"x", "b"}});
128+
129+
MultiGraph undirected = g.to_undirected();
130+
EXPECT_TRUE(undirected.is_multigraph());
131+
EXPECT_FALSE(undirected.is_directed());
132+
133+
std::map<std::string, int> label_count;
134+
for (const auto& [u, v, k] : undirected.edges()) {
135+
label_count[undirected._multi_adj[u][v][k]["x"]]++;
136+
label_count[undirected._multi_adj[v][u][k]["x"]]++;
137+
}
138+
139+
EXPECT_EQ(label_count["a"], 2); // both directions
140+
EXPECT_EQ(label_count["b"], 2);
141+
}

0 commit comments

Comments
 (0)