2023-08-01 23:43:43 +03:00
|
|
|
import { Graph } from './Graph';
|
|
|
|
|
|
|
|
describe('Testing Graph constuction', () => {
|
|
|
|
test('empty Graph should be empty', () => {
|
|
|
|
const graph = new Graph();
|
|
|
|
expect(graph.nodes.size).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('adding edges should create nodes', () => {
|
|
|
|
const graph = new Graph();
|
2023-08-22 20:29:07 +03:00
|
|
|
graph.addEdge('13', '37');
|
|
|
|
expect([... graph.nodes.keys()]).toStrictEqual(['13', '37']);
|
2023-08-01 23:43:43 +03:00
|
|
|
|
2023-08-22 20:29:07 +03:00
|
|
|
graph.addEdge('13', '38');
|
|
|
|
expect([... graph.nodes.keys()]).toStrictEqual(['13', '37', '38']);
|
2023-08-01 23:43:43 +03:00
|
|
|
});
|
2023-08-02 18:24:17 +03:00
|
|
|
|
|
|
|
test('creating from array', () => {
|
2023-08-22 20:29:07 +03:00
|
|
|
const graph = new Graph([['1', '2'], ['3'], ['4', '1']]);
|
|
|
|
expect([... graph.nodes.keys()]).toStrictEqual(['1', '2', '3', '4']);
|
|
|
|
expect([... graph.nodes.get('1')!.outputs]).toStrictEqual(['2']);
|
2023-08-02 18:24:17 +03:00
|
|
|
});
|
2023-08-03 16:42:49 +03:00
|
|
|
|
|
|
|
test('cloning', () => {
|
2023-08-22 20:29:07 +03:00
|
|
|
const graph = new Graph([['1', '2'], ['3'], ['4', '1']]);
|
2023-08-03 16:42:49 +03:00
|
|
|
const clone = graph.clone();
|
|
|
|
expect([... graph.nodes.keys()]).toStrictEqual([... clone.nodes.keys()]);
|
|
|
|
expect([... graph.nodes.values()]).toStrictEqual([... clone.nodes.values()]);
|
|
|
|
|
2023-08-22 20:29:07 +03:00
|
|
|
clone.removeNode('3');
|
|
|
|
expect(clone.nodes.get('3')).toBeUndefined();
|
|
|
|
expect(graph.nodes.get('3')).not.toBeUndefined();
|
2023-08-03 16:42:49 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Testing Graph editing', () => {
|
|
|
|
test('removing edges should not remove nodes', () => {
|
2023-08-22 20:29:07 +03:00
|
|
|
const graph = new Graph([['1', '2'], ['3'], ['4', '1']]);
|
|
|
|
expect(graph.hasEdge('4', '1')).toBeTruthy();
|
2023-08-03 16:42:49 +03:00
|
|
|
|
2023-08-22 20:29:07 +03:00
|
|
|
graph.removeEdge('5', '0');
|
|
|
|
graph.removeEdge('4', '1');
|
2023-08-03 16:42:49 +03:00
|
|
|
|
2023-08-22 20:29:07 +03:00
|
|
|
expect([... graph.nodes.keys()]).toStrictEqual(['1', '2', '3', '4']);
|
|
|
|
expect(graph.hasEdge('4', '1')).toBeFalsy();
|
2023-08-03 16:42:49 +03:00
|
|
|
});
|
|
|
|
|
2023-08-16 00:39:16 +03:00
|
|
|
test('folding node redirectes edges', () => {
|
2023-08-22 20:29:07 +03:00
|
|
|
const graph = new Graph([['1', '3'], ['2', '3'], ['3', '4'], ['3', '5'], ['3', '3']]);
|
|
|
|
graph.foldNode('3');
|
|
|
|
expect(graph.hasNode('3')).toBeFalsy();
|
|
|
|
expect(graph.hasEdge('1', '4')).toBeTruthy();
|
|
|
|
expect(graph.hasEdge('1', '5')).toBeTruthy();
|
|
|
|
expect(graph.hasEdge('2', '4')).toBeTruthy();
|
|
|
|
expect(graph.hasEdge('2', '5')).toBeTruthy();
|
2023-08-16 00:39:16 +03:00
|
|
|
});
|
|
|
|
|
2023-08-03 16:42:49 +03:00
|
|
|
test('removing isolated nodes', () => {
|
2023-08-22 20:29:07 +03:00
|
|
|
const graph = new Graph([['9', '1'], ['9', '2'], ['2', '1'], ['4', '3'], ['5', '9'], ['7'], ['8']]);
|
2023-08-03 16:42:49 +03:00
|
|
|
graph.removeIsolated()
|
2023-08-22 20:29:07 +03:00
|
|
|
expect([... graph.nodes.keys()]).toStrictEqual(['9', '1', '2', '4', '3', '5']);
|
2023-08-03 16:42:49 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
test('transitive reduction', () => {
|
2023-08-22 20:29:07 +03:00
|
|
|
const graph = new Graph([['1', '3'], ['1', '2'], ['2', '3']]);
|
2023-08-16 00:39:16 +03:00
|
|
|
graph.transitiveReduction();
|
2023-08-22 20:29:07 +03:00
|
|
|
expect(graph.hasEdge('1', '2')).toBeTruthy();
|
|
|
|
expect(graph.hasEdge('2', '3')).toBeTruthy();
|
|
|
|
expect(graph.hasEdge('1', '3')).toBeFalsy();
|
2023-08-03 16:42:49 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Testing Graph sort', () => {
|
|
|
|
test('topological order', () => {
|
2023-08-22 20:29:07 +03:00
|
|
|
const graph = new Graph([['9', '1'], ['9', '2'], ['2', '1'], ['4', '3'], ['5', '9']]);
|
|
|
|
expect(graph.tolopogicalOrder()).toStrictEqual(['5', '4', '3', '9', '2', '1']);
|
2023-08-03 16:42:49 +03:00
|
|
|
});
|
2023-08-02 18:24:17 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Testing Graph queries', () => {
|
|
|
|
test('expand outputs', () => {
|
2023-08-22 20:29:07 +03:00
|
|
|
const graph = new Graph([['1', '2'], ['2', '3'], ['2', '5'], ['5', '6'], ['6', '1'], ['7']]);
|
2023-08-02 18:24:17 +03:00
|
|
|
expect(graph.expandOutputs([])).toStrictEqual([]);
|
2023-08-22 20:29:07 +03:00
|
|
|
expect(graph.expandOutputs(['3'])).toStrictEqual([]);
|
|
|
|
expect(graph.expandOutputs(['7'])).toStrictEqual([]);
|
|
|
|
expect(graph.expandOutputs(['2', '5'])).toStrictEqual(['3', '6', '1']);
|
2023-08-02 18:24:17 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
test('expand into unique array', () => {
|
2023-08-22 20:29:07 +03:00
|
|
|
const graph = new Graph([['1', '2'], ['1', '3'], ['2', '5'], ['3', '5']]);
|
|
|
|
expect(graph.expandOutputs(['1'])).toStrictEqual(['2', '3' , '5']);
|
2023-08-02 18:24:17 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
test('expand inputs', () => {
|
2023-08-22 20:29:07 +03:00
|
|
|
const graph = new Graph([['1', '2'], ['2', '3'], ['2', '5'], ['5', '6'], ['6', '1'], ['7']]);
|
2023-08-02 18:24:17 +03:00
|
|
|
expect(graph.expandInputs([])).toStrictEqual([]);
|
2023-08-22 20:29:07 +03:00
|
|
|
expect(graph.expandInputs(['7'])).toStrictEqual([]);
|
|
|
|
expect(graph.expandInputs(['6'])).toStrictEqual(['5', '2', '1']);
|
2023-08-02 18:24:17 +03:00
|
|
|
});
|
|
|
|
|
2023-08-01 23:43:43 +03:00
|
|
|
});
|