-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathtarjan.test.ts
More file actions
61 lines (53 loc) · 1.65 KB
/
Copy pathtarjan.test.ts
File metadata and controls
61 lines (53 loc) · 1.65 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
import { describe, expect, it } from 'vitest';
import { tarjan } from '../../../src/graph/algorithms/tarjan.js';
import { CodeGraph } from '../../../src/graph/model.js';
function sortCycles(cycles) {
return cycles.map((c) => [...c].sort()).sort((a, b) => a[0].localeCompare(b[0]));
}
describe('tarjan', () => {
it('returns empty for acyclic graph', () => {
const g = new CodeGraph();
g.addEdge('a', 'b');
g.addEdge('b', 'c');
expect(tarjan(g)).toHaveLength(0);
});
it('detects 2-node cycle', () => {
const g = new CodeGraph();
g.addEdge('a', 'b');
g.addEdge('b', 'a');
const sccs = tarjan(g);
expect(sccs).toHaveLength(1);
expect(sccs[0].sort()).toEqual(['a', 'b']);
});
it('detects 3-node cycle', () => {
const g = new CodeGraph();
g.addEdge('a', 'b');
g.addEdge('b', 'c');
g.addEdge('c', 'a');
const sccs = tarjan(g);
expect(sccs).toHaveLength(1);
expect(sccs[0].sort()).toEqual(['a', 'b', 'c']);
});
it('detects multiple independent cycles', () => {
const g = new CodeGraph();
g.addEdge('a', 'b');
g.addEdge('b', 'a');
g.addEdge('x', 'y');
g.addEdge('y', 'z');
g.addEdge('z', 'x');
g.addEdge('p', 'q'); // non-cyclic
const sccs = sortCycles(tarjan(g));
expect(sccs).toHaveLength(2);
expect(sccs[0]).toEqual(['a', 'b']);
expect(sccs[1]).toEqual(['x', 'y', 'z']);
});
it('handles empty graph', () => {
const g = new CodeGraph();
expect(tarjan(g)).toHaveLength(0);
});
it('ignores self-loops (single-node SCCs are filtered)', () => {
const g = new CodeGraph();
g.addNode('a');
expect(tarjan(g)).toHaveLength(0);
});
});