Skip to content

Commit

Permalink
test: 图 单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
originalix committed Apr 6, 2021
1 parent bdf43af commit f158bb0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 25 deletions.
16 changes: 16 additions & 0 deletions __test__/graph/digraph.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { StdIn } from '@/utils'
import Digraph from '@/algs4/graph/digraph'

describe('有向图', () => {
let data: number[] | null
beforeEach(async () => {
const stream = await StdIn.readFile('tinyDG.txt')
data = stream.reduce((prev, line) => [...prev, ...line.split(' ')], []).map((val: string) => +val)
})

test('有向图数据结构', () => {
const graph = Digraph.createByReadIn(13, data)
expect(graph.countE()).toBe(15)
expect(graph.countV()).toBe(13)
})
})
16 changes: 16 additions & 0 deletions __test__/graph/graph.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Graph from '@/algs4/graph/graph'
import DepthFirstPaths from '@/algs4/graph/dfs'
import BreadthFirstPaths from '@/algs4/graph/bfs'
import ConnectComponents from '@/algs4/graph/connect-components'
import SymbolGraph from '@/algs4/graph/symbol-graph'

describe('无向图', () => {
let data: number[] | null
Expand Down Expand Up @@ -87,4 +88,19 @@ describe('无向图', () => {
[9, 10, 11, 12],
])
})

test('符号图', async () => {
const symbolData = await StdIn.readFile('routes.txt')
const sg = new SymbolGraph(symbolData, ' ')
const G = sg.getG()
const routeName = 'ORD'
const adj = G.getAdj(sg.index(routeName))
const res = []
while (adj.hasNext()) {
const w = adj.next()
res.push(sg.name(w))
}

expect(res.reverse()).toStrictEqual(['DEN', 'HOU', 'DFW', 'PHX', 'JFK', 'ATL'])
})
})
11 changes: 0 additions & 11 deletions src/algs4/graph/digraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,3 @@ export default class Digraph {
return R
}
}

async function main() {
const stream = await StdIn.readFile('tinyDG.txt')
const data = stream.reduce((prev, line) => [...prev, ...line.split(' ')], []).map((val: string) => +val)
console.log(data)

const G = Digraph.createByReadIn(13, data)
console.log(G)
}

__DEBUG__ && main()
14 changes: 0 additions & 14 deletions src/algs4/graph/symbol-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,3 @@ export default class SymbolGraph {
return this.G
}
}

async function main() {
const res = await StdIn.readFile('routes.txt')
const sg = new SymbolGraph(res, ' ')
const G = sg.getG()
const routeName = 'ORD'
const adj = G.getAdj(sg.index(routeName))
while (adj.hasNext()) {
const w = adj.next()
console.log(sg.name(w))
}
}

main()

0 comments on commit f158bb0

Please sign in to comment.