-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathTarjanSCC.test.js
92 lines (74 loc) · 2.21 KB
/
TarjanSCC.test.js
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
import { TarjanSCC } from '../TarjanSCC.js'
test('Test Case 1 - Simple graph with two SCCs', () => {
const graph = {
0: [1],
1: [2],
2: [0, 3],
3: [4],
4: []
}
const result = TarjanSCC(graph)
// Sort the components before comparison since order doesn't matter
const expected = [[4], [3], [0, 2, 1]].map((comp) => comp.sort())
const actual = result.map((comp) => comp.sort())
expect(actual).toEqual(expect.arrayContaining(expected))
})
test('Test Case 2 - All nodes in one SCC', () => {
const graph = {
A: ['B'],
B: ['C'],
C: ['A']
}
const result = TarjanSCC(graph)
// Sort the components before comparison since order doesn't matter
const expected = [['A', 'B', 'C']].map((comp) => comp.sort())
const actual = result.map((comp) => comp.sort())
expect(actual).toEqual(expect.arrayContaining(expected))
})
test('Test Case 3 - Disconnected nodes', () => {
const graph = {
1: [],
2: [],
3: []
}
const result = TarjanSCC(graph)
// Sort the components before comparison since order doesn't matter
const expected = [[1], [2], [3]].map((comp) => comp.sort())
const actual = result.map((comp) => comp.sort())
expect(actual).toEqual(expect.arrayContaining(expected))
})
test('Test Case 4 - Complex Graph', () => {
const graph = {
0: [1],
1: [2, 3],
2: [0],
3: [4],
4: [5],
5: [3]
}
const result = TarjanSCC(graph)
// Sort the components before comparison since order doesn't matter
const expected = [
[0, 2, 1],
[3, 5, 4]
].map((comp) => comp.sort())
const actual = result.map((comp) => comp.sort())
expect(actual).toEqual(expect.arrayContaining(expected))
})
test('Edge Case - Null input should throw error', () => {
expect(() => TarjanSCC(null)).toThrow(
'Graph must be a non-null object representing an adjacency list'
)
})
test('Edge Case - Node with non-array neighbors should throw error', () => {
const graph = {
A: 'not-an-array'
}
expect(() => TarjanSCC(graph)).toThrow('Neighbors of node A must be an array')
})
test('Edge Case - Neighbor not in graph should throw error', () => {
const graph = {
A: ['B']
}
expect(() => TarjanSCC(graph)).toThrow('Node B not found in graph')
})