feat: solution for 'Повторный экзамен #2: Граф с рефлексией на код'

This commit is contained in:
2026-07-01 11:22:21 +03:00
parent 7832ec4f07
commit 9412dbf851
7 changed files with 240 additions and 101 deletions
+55
View File
@@ -0,0 +1,55 @@
const Graph = require('../src/index');
describe('Graph with reflexivity', () => {
let g;
beforeEach(() => {
g = new Graph();
});
test('adding a node creates reflexive edge', () => {
g.addNode('A');
expect(g.nodes()).toContain('A');
expect(g.hasEdge('A', 'A')).toBe(true);
});
test('adding an edge between existing nodes', () => {
g.addNode('A');
g.addNode('B');
g.addEdge('A', 'B');
expect(g.hasEdge('A', 'B')).toBe(true);
expect(g.hasEdge('B', 'A')).toBe(false);
});
test('adding an edge automatically adds missing nodes', () => {
g.addEdge('X', 'Y');
expect(g.nodes()).toEqual(expect.arrayContaining(['X', 'Y']));
expect(g.hasEdge('X', 'Y')).toBe(true);
// reflexive edges for both nodes
expect(g.hasEdge('X', 'X')).toBe(true);
expect(g.hasEdge('Y', 'Y')).toBe(true);
});
test('getNeighbors returns correct neighbors', () => {
g.addNode('1');
g.addNode('2');
g.addEdge('1', '2');
expect(g.getNeighbors('1')).toEqual(expect.arrayContaining(['1', '2']));
expect(g.getNeighbors('2')).toEqual(['2']);
});
test('edges method returns all edges', () => {
g.addNode('A');
g.addNode('B');
g.addEdge('A', 'B');
const edges = g.edges();
expect(edges).toEqual(
expect.arrayContaining([
['A', 'A'],
['B', 'B'],
['A', 'B']
])
);
expect(edges.length).toBe(3);
});
});