Add tests to Graph

This commit is contained in:
mindfulminun committed 2022-10-23 21:52:40 -05:00
1 parent f0203d4c60
commit a344784c6a
3 files changed
+74 -11

No files matched your search

+56 -1
View File
@@ -1,3 +1,58 @@
import { g1, g2 } 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/graph.ts";
// TODO: Add real tests!
// 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']
)
})
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(`Paths are different! Expected: [${expectedV.join(' -> ')}]; Actual: [${path.vertices.map(v => v.id).join(' -> ')}]`)
}
}
+9 -9
View File
@@ -41,11 +41,11 @@ export function g1() {
*
*
* ```plaintext
* 0
* A
* / \
* 1 2
* B C
* / \ / \
* 3 4 5 6
* D E F G
* ```
*/
export function g2() {
@@ -61,12 +61,12 @@ export function g2() {
G.createVertex(null, 'G')
]
G.createEdge(vertices[0], vertices[1], null)
G.createEdge(vertices[0], vertices[2], null)
G.createEdge(vertices[1], vertices[3], null)
G.createEdge(vertices[1], vertices[4], null)
G.createEdge(vertices[2], vertices[5], null)
G.createEdge(vertices[2], vertices[6], null)
G.createEdge(vertices[0], vertices[1], null, { id: 'AB' })
G.createEdge(vertices[0], vertices[2], null, { id: 'AC' })
G.createEdge(vertices[1], vertices[3], null, { id: 'BD' })
G.createEdge(vertices[1], vertices[4], null, { id: 'BE' })
G.createEdge(vertices[2], vertices[5], null, { id: 'CF' })
G.createEdge(vertices[2], vertices[6], null, { id: 'CG' })
return G
}
+9 -1
View File
@@ -225,20 +225,26 @@ export class Path<vData, eData> {
graph: Graph<vData, eData>
start: Vertex<vData, eData> | null
edges: Edge<vData, eData>[]
/** An internal cache of the vertices. */
#vertices: Vertex<vData, eData>[] | null
constructor(graph: Graph<vData, eData>, start?: Vertex<vData, eData>) {
this.graph = graph
this.edges = []
this.start = start ?? null
this.#vertices = null
}
addEdge(e: Edge<vData, eData>) {
if (!this.start) this.start = e.u
if (e.graph !== this.graph) throw Error("The edges in a path must all belong to the same graph.")
this.edges.push(e)
this.#vertices = null
}
get vertices() {
if (this.#vertices) return this.#vertices
const vertices: Vertex<vData, eData>[] = []
if (!this.start) return vertices
@@ -250,12 +256,14 @@ export class Path<vData, eData> {
vertices.push(current)
}
return vertices
this.#vertices = vertices
return this.#vertices
}
copy() {
const path = new Path<vData, eData>(this.graph, this.start ?? undefined)
path.edges = this.edges.slice()
path.#vertices = this.#vertices?.slice() ?? null
return path
}
}