dist-javascript-algorithms-and-data-structures
Version:
Algorithms and data-structures implemented on JavaScript
139 lines (133 loc) • 5.99 kB
JavaScript
"use strict";
var _GraphVertex = _interopRequireDefault(require("../GraphVertex"));
var _GraphEdge = _interopRequireDefault(require("../GraphEdge"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
describe('GraphVertex', () => {
it('should throw an error when trying to create vertex without value', () => {
let vertex = null;
function createEmptyVertex() {
vertex = new _GraphVertex.default();
}
expect(vertex).toBeNull();
expect(createEmptyVertex).toThrow();
});
it('should create graph vertex', () => {
const vertex = new _GraphVertex.default('A');
expect(vertex).toBeDefined();
expect(vertex.value).toBe('A');
expect(vertex.toString()).toBe('A');
expect(vertex.getKey()).toBe('A');
expect(vertex.edges.toString()).toBe('');
expect(vertex.getEdges()).toEqual([]);
});
it('should add edges to vertex and check if it exists', () => {
const vertexA = new _GraphVertex.default('A');
const vertexB = new _GraphVertex.default('B');
const edgeAB = new _GraphEdge.default(vertexA, vertexB);
vertexA.addEdge(edgeAB);
expect(vertexA.hasEdge(edgeAB)).toBe(true);
expect(vertexB.hasEdge(edgeAB)).toBe(false);
expect(vertexA.getEdges().length).toBe(1);
expect(vertexA.getEdges()[0].toString()).toBe('A_B');
});
it('should delete edges from vertex', () => {
const vertexA = new _GraphVertex.default('A');
const vertexB = new _GraphVertex.default('B');
const vertexC = new _GraphVertex.default('C');
const edgeAB = new _GraphEdge.default(vertexA, vertexB);
const edgeAC = new _GraphEdge.default(vertexA, vertexC);
vertexA.addEdge(edgeAB).addEdge(edgeAC);
expect(vertexA.hasEdge(edgeAB)).toBe(true);
expect(vertexB.hasEdge(edgeAB)).toBe(false);
expect(vertexA.hasEdge(edgeAC)).toBe(true);
expect(vertexC.hasEdge(edgeAC)).toBe(false);
expect(vertexA.getEdges().length).toBe(2);
expect(vertexA.getEdges()[0].toString()).toBe('A_B');
expect(vertexA.getEdges()[1].toString()).toBe('A_C');
vertexA.deleteEdge(edgeAB);
expect(vertexA.hasEdge(edgeAB)).toBe(false);
expect(vertexA.hasEdge(edgeAC)).toBe(true);
expect(vertexA.getEdges()[0].toString()).toBe('A_C');
vertexA.deleteEdge(edgeAC);
expect(vertexA.hasEdge(edgeAB)).toBe(false);
expect(vertexA.hasEdge(edgeAC)).toBe(false);
expect(vertexA.getEdges().length).toBe(0);
});
it('should delete all edges from vertex', () => {
const vertexA = new _GraphVertex.default('A');
const vertexB = new _GraphVertex.default('B');
const vertexC = new _GraphVertex.default('C');
const edgeAB = new _GraphEdge.default(vertexA, vertexB);
const edgeAC = new _GraphEdge.default(vertexA, vertexC);
vertexA.addEdge(edgeAB).addEdge(edgeAC);
expect(vertexA.hasEdge(edgeAB)).toBe(true);
expect(vertexB.hasEdge(edgeAB)).toBe(false);
expect(vertexA.hasEdge(edgeAC)).toBe(true);
expect(vertexC.hasEdge(edgeAC)).toBe(false);
expect(vertexA.getEdges().length).toBe(2);
vertexA.deleteAllEdges();
expect(vertexA.hasEdge(edgeAB)).toBe(false);
expect(vertexB.hasEdge(edgeAB)).toBe(false);
expect(vertexA.hasEdge(edgeAC)).toBe(false);
expect(vertexC.hasEdge(edgeAC)).toBe(false);
expect(vertexA.getEdges().length).toBe(0);
});
it('should return vertex neighbors in case if current node is start one', () => {
const vertexA = new _GraphVertex.default('A');
const vertexB = new _GraphVertex.default('B');
const vertexC = new _GraphVertex.default('C');
const edgeAB = new _GraphEdge.default(vertexA, vertexB);
const edgeAC = new _GraphEdge.default(vertexA, vertexC);
vertexA.addEdge(edgeAB).addEdge(edgeAC);
expect(vertexB.getNeighbors()).toEqual([]);
const neighbors = vertexA.getNeighbors();
expect(neighbors.length).toBe(2);
expect(neighbors[0]).toEqual(vertexB);
expect(neighbors[1]).toEqual(vertexC);
});
it('should return vertex neighbors in case if current node is end one', () => {
const vertexA = new _GraphVertex.default('A');
const vertexB = new _GraphVertex.default('B');
const vertexC = new _GraphVertex.default('C');
const edgeBA = new _GraphEdge.default(vertexB, vertexA);
const edgeCA = new _GraphEdge.default(vertexC, vertexA);
vertexA.addEdge(edgeBA).addEdge(edgeCA);
expect(vertexB.getNeighbors()).toEqual([]);
const neighbors = vertexA.getNeighbors();
expect(neighbors.length).toBe(2);
expect(neighbors[0]).toEqual(vertexB);
expect(neighbors[1]).toEqual(vertexC);
});
it('should check if vertex has specific neighbor', () => {
const vertexA = new _GraphVertex.default('A');
const vertexB = new _GraphVertex.default('B');
const vertexC = new _GraphVertex.default('C');
const edgeAB = new _GraphEdge.default(vertexA, vertexB);
vertexA.addEdge(edgeAB);
expect(vertexA.hasNeighbor(vertexB)).toBe(true);
expect(vertexA.hasNeighbor(vertexC)).toBe(false);
});
it('should edge by vertex', () => {
const vertexA = new _GraphVertex.default('A');
const vertexB = new _GraphVertex.default('B');
const vertexC = new _GraphVertex.default('C');
const edgeAB = new _GraphEdge.default(vertexA, vertexB);
vertexA.addEdge(edgeAB);
expect(vertexA.findEdge(vertexB)).toEqual(edgeAB);
expect(vertexA.findEdge(vertexC)).toBeNull();
});
it('should calculate vertex degree', () => {
const vertexA = new _GraphVertex.default('A');
const vertexB = new _GraphVertex.default('B');
expect(vertexA.getDegree()).toBe(0);
const edgeAB = new _GraphEdge.default(vertexA, vertexB);
vertexA.addEdge(edgeAB);
expect(vertexA.getDegree()).toBe(1);
const edgeBA = new _GraphEdge.default(vertexB, vertexA);
vertexA.addEdge(edgeBA);
expect(vertexA.getDegree()).toBe(2);
vertexA.addEdge(edgeAB);
expect(vertexA.getDegree()).toBe(3);
expect(vertexA.getEdges().length).toEqual(3);
});
});