Files
xod/packages/xod-project/test/gmath.spec.js
Evgeny Kochetkov e6ab08e297 chore(infra): format code with prettier
Just `yarn lint  --fix`
2018-03-05 17:59:03 +03:00

59 lines
1.8 KiB
JavaScript

import { expect } from 'chai';
import { sortGraph } from '../src/gmath';
import { expectEitherRight, expectEitherError } from './helpers';
import { ERROR } from '../src/constants';
describe('Graph math', () => {
describe('Topological sorting', () => {
it('should return [] for empty graph', () => {
expectEitherRight(sorted => {
expect(sorted).to.be.eql([]);
}, sortGraph([], []));
});
it('should return single vertex for single-vertex graph', () => {
expectEitherRight(sorted => {
expect(sorted).to.be.eql([42]);
}, sortGraph([42], []));
});
it('should return vertexes as is if there are no edges', () => {
expectEitherRight(sorted => {
expect(sorted).to.be.eql([42, 43, 44]);
}, sortGraph([42, 43, 44], []));
});
it('should return vertexes as is if already sorted', () => {
expectEitherRight(sorted => {
expect(sorted).to.be.eql([42, 43, 44]);
}, sortGraph([42, 43, 44], [[42, 43], [43, 44]]));
});
it('should return sorted vertexes if given vertexes are inversed', () => {
expectEitherRight(sorted => {
expect(sorted).to.be.eql([42, 43, 44]);
}, sortGraph([44, 43, 42], [[42, 43], [43, 44]]));
});
it('should throw error for cycled graph', () => {
expectEitherError(
ERROR.LOOPS_DETECTED,
sortGraph([42, 43, 44], [[42, 43], [43, 42]])
);
});
it('should sort diamond graph', () => {
expectEitherRight(sorted => {
expect(sorted).to.be.eql([42, 44, 43, 45]);
}, sortGraph([44, 43, 42, 45], [[42, 43], [42, 44], [43, 45], [44, 45]]));
});
it('should sort clusters', () => {
expectEitherRight(sorted => {
expect(sorted).to.be.eql([44, 42, 45, 43]);
}, sortGraph([44, 43, 42, 45], [[42, 43], [44, 45]]));
});
});
});