-
Notifications
You must be signed in to change notification settings - Fork 21
/
adjacency-matrix-weighted-directed_test.ts
71 lines (60 loc) · 2.62 KB
/
adjacency-matrix-weighted-directed_test.ts
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
import { AdjacencyMatrixWeightedDirectedMixin } from "../adjacency-matrix-weighted-directed.ts";
import { assertEquals } from "./test_deps.ts";
const { test } = Deno;
const DirectedMatrix = AdjacencyMatrixWeightedDirectedMixin(Int32Array);
const exampleGraph = DirectedMatrix.create(6);
exampleGraph.addEdge(0, 1, 3);
exampleGraph.addEdge(0, 2, 2);
exampleGraph.addEdge(0, 3, 1);
exampleGraph.addEdge(2, 4, 8);
exampleGraph.addEdge(2, 5, 6);
test("[AdjacencyMatrixWeightedDirected.constructor] creates a graph from an existing graph", () => {
const graph = new DirectedMatrix(exampleGraph);
assertEquals(graph.vertices, 6);
assertEquals(graph.edges, 36);
assertEquals(graph.isFull(), false);
assertEquals(
(graph as unknown as Int32Array).buffer !== exampleGraph.buffer,
true,
);
});
test("[AdjacencyMatrixWeightedDirected.creates] creates a graph of given amount of vertices", () => {
const graph = DirectedMatrix.create(10);
assertEquals(graph.vertices, 10);
});
test("[AdjacencyMatrixWeightedDirected.getLength] returns the length of underlying TypedArray required to hold the graph", () => {
assertEquals(DirectedMatrix.getLength(60), 3600);
});
test("[AdjacencyMatrixWeightedDirected#addEdge] adds an edge to a graph", () => {
const graph = new DirectedMatrix(exampleGraph);
assertEquals(graph.hasEdge(0, 5), false);
graph.addEdge(0, 5, 1);
assertEquals(graph.hasEdge(0, 5), true);
});
test("[AdjacencyMatrixWeightedDirected#removeEdge] removes an edge from a graph", () => {
const graph = new DirectedMatrix(exampleGraph);
assertEquals(graph.hasEdge(0, 1), true);
graph.removeEdge(0, 1);
assertEquals(graph.hasEdge(0, 1), false);
graph.removeEdge(0, 1);
graph.removeEdge(1, 5);
});
test("[AdjacencyMatrixWeightedDirected#outEdges] iterates over outgoing edges of a vertex", () => {
const graph = new DirectedMatrix(exampleGraph);
assertEquals([...graph.outEdges(0)], [1, 2, 3]);
});
test("[AdjacencyMatrixWeightedDirected#inEdges] iterates over incoming edges of a vertex", () => {
const graph = new DirectedMatrix(exampleGraph);
assertEquals([...graph.inEdges(1)], [0]);
assertEquals([...graph.inEdges(4)], [2]);
assertEquals([...graph.inEdges(0)], []);
});
test("[AdjacencyMatrixWeightedDirected#hasEdges] checks whether there is an edge between two given vertices", () => {
const graph = new DirectedMatrix(exampleGraph);
assertEquals(graph.hasEdge(0, 1), true);
assertEquals(graph.hasEdge(0, 5), false);
assertEquals(graph.hasEdge(2, 5), true);
});
test("[DirectedMatrix.$species] returns underlying TypedArray when sliced", () => {
assertEquals(DirectedMatrix[Symbol.species], Int32Array);
});