This commit is contained in:
mindfulminun committed 2024-07-20 15:30:48 -05:00
1 parent 9cf0bf415b
commit 71323fc7fd
25 files changed
+186 -94

No files matched your search

+88
View File
@@ -0,0 +1,88 @@
import { g1, g2, g3 } from '../../tools/graph/_graph-samples.ts'
import { GraphSolver } from '../../tools/graph/graph-solver.ts'
import { assertEquals } from "https://deno.land/std/testing/asserts.ts"
import { Path } from "../../tools/graph/mod.ts"
import { dedent } from "../../core/string.ts"
// TODO: Add more tests!
Deno.test('GraphSolver::BFS w/ g1', () => {
const G = g1()
const solver = new GraphSolver(G)
const path = solver.BFS(G.vertices.get('A')!, v => v.id === 'F')
const expectedV = ['A', 'C', 'F']
const expectedE = ['AC', 'CF']
assertPathsEqual(path, expectedV, expectedE)
})
Deno.test('GraphSolver::BFS w/ g2', () => {
const G = g2()
const solver = new GraphSolver(G)
const path = solver.BFS(G.vertices.get('A')!, v => v.id === 'G')
const expectedV = ['A', 'C', 'G']
const expectedE = ['AC', 'CG']
assertPathsEqual(path, expectedV, expectedE)
})
Deno.test('GraphSolver::DFS w/ g1', () => {
const G = g1()
const solver = new GraphSolver(G)
const path = solver.DFS(G.vertices.get('A')!, v => v.id === 'E')
assertPathsEqual(
path,
['A', 'C', 'F', 'E'],
['AC', 'CF', 'EF']
)
})
Deno.test('GraphSolver::bidi Path', () => {
const G = g3()
const solver = new GraphSolver(G)
const path = solver.bidi({
source: G.vertices.get('A')!,
sink: G.vertices.get('E')!
})
assertPathsEqual(
path,
'ABCDE'.split(''),
'AB-BC-CD-DE'.split('-')
)
})
Deno.test('GraphSolver::kruskalPresorted', () => {
const G = g3()
const solver = new GraphSolver(G)
const set = solver.kruskalPresorted()
// const expected = new Set<string>(['GH', 'FG', 'CI', 'AB', 'CF', 'GI', 'CD', 'AH'])
// ;[...set].map(e => e.id).forEach(e => expected.delete(e))
// if (expected.size > 0) throw Error(`Missing edges: ${[...expected].join(', ')}`)H
throw Error(set.size + '')
})
function assertPathsEqual<vData, eData>(
path: Path<vData, eData> | null | undefined,
expectedV: string[],
expectedE: string[]
) {
if (!path) throw Error("Path is null or undefined")
try {
assertEquals(path?.edges.length, expectedE.length)
assertEquals(path?.vertices.length, expectedV.length)
expectedV.forEach((v, i) => assertEquals(path?.vertices[i].id, v))
expectedE.forEach((e, i) => assertEquals(path?.edges[i].id, e))
} catch (_e) {
throw Error(dedent`
Paths are different!
Expected: [${expectedV.join(' -> ')}]
Actual: [${path.vertices.map(v => v.id).join(' -> ')}]
`)
}
}
+49
View File
@@ -0,0 +1,49 @@
import { range } from "../../core/iterable.ts";
import { Queue } from "../../tools/structures/Queue.ts"
import { Stack } from "../../tools/structures/Stack.ts"
import { BinaryHeap } from "../../tools/structures/Heap.ts"
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts"
Deno.test("Data structures - Stack", () => {
const stack = new Stack([1, 2, 3])
stack.push(4)
stack.push(5)
for (const i of range(5, 0)) {
assertEquals(stack.pop(), i)
}
assertEquals(stack.length, 0)
})
Deno.test("Data structures - Queue", () => {
const queue = new Queue([0, 1, 2, 3])
queue.push(4)
queue.push(5)
for (const i of range(6)) {
assertEquals(queue.pop(), i)
}
assertEquals(queue.length, 0)
})
Deno.test("Data structures - Binary Heap: Bubble up", () => {
const heap = new BinaryHeap<number>((a, b) => a - b)
heap.push(5)
heap.push(3)
assertEquals(heap.peek(), 3)
})
Deno.test("Data structures - Binary Heap", () => {
const heap = new BinaryHeap(
(a, b) => a - b,
[5, 3, 8, 1, 2, 6, 9, 4, 7]
)
let i = 1
for (const el of heap) {
assertEquals(el, i++)
}
assertEquals(i, 10)
})