Skip to content

Commit

Permalink
feat: add test for graph
Browse files Browse the repository at this point in the history
  • Loading branch information
kazushisan committed Sep 22, 2024
1 parent e97b74c commit 09ec27c
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions lib/util/Graph.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { Graph } from './Graph.js';

describe('Graph', () => {
it('should add edges correctly', () => {
const graph = new Graph();
graph.addEdge('A', 'B');
graph.addEdge('A', 'C');

assert.equal(graph.vertexes.size, 3);
assert.deepEqual(graph.vertexes.get('A')?.to, new Set(['B', 'C']));
assert.deepEqual(graph.vertexes.get('B')?.from, new Set(['A']));
assert.deepEqual(graph.vertexes.get('C')?.from, new Set(['A']));
});

it('should delete vertex correctly', () => {
const graph = new Graph();
graph.addEdge('A', 'B');
graph.addEdge('A', 'C');
graph.addEdge('B', 'C');

graph.deleteVertex('A');

assert.equal(graph.vertexes.size, 2);
assert.equal(graph.vertexes.has('A'), false);
assert.deepEqual(graph.vertexes.get('B')?.to, new Set(['C']));
assert.deepEqual(graph.vertexes.get('C')?.from, new Set(['B']));
});

it('should remove vertexes without any edges', () => {
const graph = new Graph();
graph.addEdge('A', 'B');
graph.addEdge('A', 'C');
graph.addEdge('B', 'C');
graph.deleteVertex('B');

assert.equal(graph.vertexes.size, 2);
assert.equal(graph.vertexes.has('B'), false);
assert.equal(graph.vertexes.has('A'), true);
assert.equal(graph.vertexes.has('C'), true);
assert.deepEqual(graph.vertexes.get('A')?.to, new Set(['C']));
assert.deepEqual(graph.vertexes.get('A')?.from.size, 0);
assert.equal(graph.vertexes.get('C')?.to.size, 0);
assert.deepEqual(graph.vertexes.get('C')?.from, new Set(['A']));
});
});

0 comments on commit 09ec27c

Please sign in to comment.