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

This commit is contained in:
2026-07-01 11:19:02 +03:00
parent 045dba9aef
commit cfe5d77a10
10 changed files with 309 additions and 94 deletions
+51 -26
View File
@@ -1,39 +1,64 @@
import Graph from '../src/graph.js';
const Graph = require('../src/graph');
describe('Graph', () => {
test('should add nodes and edges correctly', () => {
test('should add reflection node and evaluate correctly', () => {
const g = new Graph();
g.addNode('x');
g.addNode('y');
g.addEdge('x', 'y');
expect(g.hasEdge('x', 'y')).toBe(true);
expect(g.hasEdge('y', 'x')).toBe(false);
g.addNode('A', 'reflection');
const outputs = g.evaluate('A', 42);
expect(outputs['A']).toBe(42);
});
test('reflexive should add self loops', () => {
test('should add rewrite node and evaluate correctly', () => {
const g = new Graph();
g.addNode('x');
g.addNode('y');
g.addEdge('x', 'y');
g.reflexive();
expect(g.hasEdge('x', 'x')).toBe(true);
expect(g.hasEdge('y', 'y')).toBe(true);
g.addNode('B', 'rewrite');
const outputs = g.evaluate('B', 'hello');
expect(outputs['B']).toBe('HELLO');
});
test('getAdjacencyList returns correct structure', () => {
test('should propagate through connected nodes', () => {
const g = new Graph();
g.addNode('x');
g.addNode('y');
g.addEdge('x', 'y');
g.addNode('A', 'reflection');
g.addNode('B', 'rewrite');
g.addEdge('A', 'B');
const outputs = g.evaluate('A', 'test');
expect(outputs['A']).toBe('test');
expect(outputs['B']).toBe('TEST');
});
g.reflexive();
test('should throw error on unknown node type', () => {
const g = new Graph();
expect(() => g.addNode('C', 'unknown')).toThrow();
});
const adj = g.getAdjacencyList();
expect(adj['x']).toContain('y');
expect(adj['x']).toContain('x');
expect(adj['y']).toContain('y');
test('should throw error on duplicate node name', () => {
const g = new Graph();
g.addNode('D', 'reflection');
expect(() => g.addNode('D', 'rewrite')).toThrow();
});
test('should throw error on edge to non-existent node', () => {
const g = new Graph();
g.addNode('E', 'reflection');
expect(() => g.addEdge('E', 'F')).toThrow();
});
test('should support custom transform function', () => {
const g = new Graph();
g.addNode('G', 'rewrite', { transform: (x) => x * 2 });
const outputs = g.evaluate('G', 5);
expect(outputs['G']).toBe(10);
});
test('should handle multiple outputs', () => {
const g = new Graph();
g.addNode('A', 'reflection');
g.addNode('B', 'rewrite');
g.addNode('C', 'rewrite');
g.addEdge('A', 'B');
g.addEdge('A', 'C');
const outputs = g.evaluate('A', 'multi');
expect(outputs['A']).toBe('multi');
expect(outputs['B']).toBe('MULTI');
expect(outputs['C']).toBe('MULTI');
});
});