Skip to content

Commit 79f3ead

Browse files
Add unit test for DFS
1 parent 982a2fa commit 79f3ead

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed

test/gtest/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ set(unittest_SOURCES
1212
dataflow.cpp
1313
dfa_minimization.cpp
1414
disjoint_sets.cpp
15+
graph.cpp
1516
leaves.cpp
1617
glbs.cpp
1718
interpreter.cpp

test/gtest/graph.cpp

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* Copyright 2026 WebAssembly Community Group participants
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <vector>
18+
19+
#include "support/graph_traversal.h"
20+
#include "gtest/gtest.h"
21+
22+
using namespace wasm;
23+
24+
TEST(GraphTest, Linear) {
25+
// 0 -> 1 -> 2
26+
std::vector<int> roots = {0};
27+
std::vector<int> order;
28+
auto successors = [&](const auto& push, int n) {
29+
order.push_back(n);
30+
if (n < 2) {
31+
push(n + 1);
32+
}
33+
};
34+
35+
Graph g(roots.begin(), roots.end(), successors);
36+
auto visited = g.traverseDepthFirst();
37+
38+
std::vector<int> expectedOrder = {0, 1, 2};
39+
EXPECT_EQ(order, expectedOrder);
40+
41+
std::unordered_set<int> expectedVisited = {0, 1, 2};
42+
EXPECT_EQ(visited, expectedVisited);
43+
}
44+
45+
TEST(GraphTest, Cycle) {
46+
// 0 -> 1 -> 0
47+
std::vector<int> roots = {0};
48+
std::vector<int> order;
49+
auto successors = [&](const auto& push, int n) {
50+
order.push_back(n);
51+
if (n == 0) {
52+
push(1);
53+
} else if (n == 1) {
54+
push(0);
55+
}
56+
};
57+
58+
Graph g(roots.begin(), roots.end(), successors);
59+
auto visited = g.traverseDepthFirst();
60+
61+
std::vector<int> expectedOrder = {0, 1};
62+
EXPECT_EQ(order, expectedOrder);
63+
64+
std::unordered_set<int> expectedVisited = {0, 1};
65+
EXPECT_EQ(visited, expectedVisited);
66+
}
67+
68+
TEST(GraphTest, Diamond) {
69+
// 0 -> 1, 2
70+
// 1 -> 3
71+
// 2 -> 3
72+
73+
std::vector<int> roots = {0};
74+
std::vector<int> order;
75+
auto successors = [&](const auto& push, int n) {
76+
order.push_back(n);
77+
if (n == 0) {
78+
push(2);
79+
push(1);
80+
} else if (n == 1 || n == 2) {
81+
push(3);
82+
}
83+
};
84+
85+
Graph g(roots.begin(), roots.end(), successors);
86+
auto visited = g.traverseDepthFirst();
87+
88+
std::vector<int> expectedOrder = {0, 1, 3, 2};
89+
EXPECT_EQ(order, expectedOrder);
90+
91+
std::unordered_set<int> expectedVisited = {0, 1, 2, 3};
92+
EXPECT_EQ(visited, expectedVisited);
93+
}
94+
95+
TEST(GraphTest, DuplicateRoots) {
96+
// 0 -> 1, 2
97+
// 1 -> 0
98+
// 2 -> 0
99+
// 0 is added as a root 3 times
100+
101+
std::vector<int> roots = {0, 0, 0};
102+
std::vector<int> order;
103+
auto successors = [&](const auto& push, int n) {
104+
order.push_back(n);
105+
if (n == 0) {
106+
push(2);
107+
push(1);
108+
} else if (n == 1 || n == 2) {
109+
push(0);
110+
}
111+
};
112+
113+
Graph g(roots.begin(), roots.end(), successors);
114+
auto visited = g.traverseDepthFirst();
115+
116+
std::vector<int> expectedOrder = {0, 1, 2, 0, 0};
117+
EXPECT_EQ(order, expectedOrder);
118+
119+
std::unordered_set<int> expectedVisited = {0, 1, 2};
120+
EXPECT_EQ(visited, expectedVisited);
121+
}
122+
123+
TEST(GraphTest, Disjoint) {
124+
// 0 -> 1
125+
// 2 -> 3
126+
127+
std::vector<int> roots = {2, 0};
128+
std::vector<int> order;
129+
auto successors = [&](const auto& push, int n) {
130+
order.push_back(n);
131+
if (n == 0) {
132+
push(1);
133+
} else if (n == 2) {
134+
push(3);
135+
}
136+
};
137+
138+
Graph g(roots.begin(), roots.end(), successors);
139+
auto visited = g.traverseDepthFirst();
140+
141+
std::vector<int> expectedOrder = {0, 1, 2, 3};
142+
EXPECT_EQ(order, expectedOrder);
143+
144+
std::unordered_set<int> expectedVisited = {0, 1, 2, 3};
145+
EXPECT_EQ(visited, expectedVisited);
146+
}

0 commit comments

Comments
 (0)