more unit tests
This commit is contained in:
+10
-20
@@ -168,46 +168,36 @@ module.exports = (options) => {
|
|||||||
|
|
||||||
//check overlapping
|
//check overlapping
|
||||||
const list = Object.values(nodes).filter(n => n.x !== undefined);
|
const list = Object.values(nodes).filter(n => n.x !== undefined);
|
||||||
for (let n1 = 0; n1 < list.length - 1; n1++) {
|
for (let n1 = 0; n1 < list.length - 1; n1++)
|
||||||
for (let n2 = n1 + 1; n2 < list.length; n2++) {
|
for (let n2 = n1 + 1; n2 < list.length; n2++)
|
||||||
if (list[n1].x === list[n2].x && list[n1].y === list[n2].y) {
|
if (list[n1].x === list[n2].x && list[n1].y === list[n2].y)
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (let li = 0; li < links.length; li++) {
|
for (let li = 0; li < links.length; li++) {
|
||||||
link = links[li];
|
link = links[li];
|
||||||
src = nodes[link.from];
|
src = nodes[link.from];
|
||||||
dst = nodes[link.to];
|
dst = nodes[link.to];
|
||||||
if (src.x !== undefined && dst.x !== undefined) {
|
if (src.x !== undefined && dst.x !== undefined) {
|
||||||
if (self.nodeBetween(nodes, link.from, link.to)) {
|
if (self.nodeBetween(nodes, link.from, link.to))
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
switch (link.direction) {
|
switch (link.direction) {
|
||||||
case 'up':
|
case 'up':
|
||||||
case 'top':
|
case 'top':
|
||||||
if (dst.y - src.y >= 0) {
|
if (dst.y - src.y >= 0) return false;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 'down':
|
case 'down':
|
||||||
case 'bottom':
|
case 'bottom':
|
||||||
if (dst.y - src.y <= 0) {
|
if (dst.y - src.y <= 0) return false;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 'left':
|
case 'left':
|
||||||
if (dst.x - src.x >= 0) {
|
if (dst.x - src.x >= 0) return false;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 'right':
|
case 'right':
|
||||||
if (dst.x - src.x <= 0) {
|
if (dst.x - src.x <= 0) return false;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if (!options['diagonals'] && src.x !== dst.x && src.y !== dst.y)
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
+38
-13
@@ -253,11 +253,11 @@ describe('getPosition', () => {
|
|||||||
|
|
||||||
describe('isValid', () => {
|
describe('isValid', () => {
|
||||||
test('no nodes', () => {
|
test('no nodes', () => {
|
||||||
const res = placing({debug: true}).isValid({}, []);
|
const res = placing({debug: true, diagonals: true}).isValid({}, []);
|
||||||
expect(res).toBe(true);
|
expect(res).toBe(true);
|
||||||
});
|
});
|
||||||
test('no placed nodes', () => {
|
test('no placed nodes', () => {
|
||||||
const res = placing({debug: true}).isValid({
|
const res = placing({debug: true, diagonals: true}).isValid({
|
||||||
'a': {}, 'b': {}
|
'a': {}, 'b': {}
|
||||||
}, [
|
}, [
|
||||||
{from: 'a', to: 'b'}
|
{from: 'a', to: 'b'}
|
||||||
@@ -265,7 +265,7 @@ describe('isValid', () => {
|
|||||||
expect(res).toBe(true);
|
expect(res).toBe(true);
|
||||||
});
|
});
|
||||||
test('one nodes', () => {
|
test('one nodes', () => {
|
||||||
const res = placing({debug: true}).isValid({
|
const res = placing({debug: true, diagonals: true}).isValid({
|
||||||
'a': {name: 'a', x: 0, y: 0}, 'b': {}
|
'a': {name: 'a', x: 0, y: 0}, 'b': {}
|
||||||
}, [
|
}, [
|
||||||
{from: 'a', to: 'b'}
|
{from: 'a', to: 'b'}
|
||||||
@@ -273,13 +273,13 @@ describe('isValid', () => {
|
|||||||
expect(res).toBe(true);
|
expect(res).toBe(true);
|
||||||
});
|
});
|
||||||
test('overlapping nodes', () => {
|
test('overlapping nodes', () => {
|
||||||
const res = placing({debug: true}).isValid({
|
const res = placing({debug: true, diagonals: true}).isValid({
|
||||||
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 0, y: 0}
|
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 0, y: 0}
|
||||||
}, []);
|
}, []);
|
||||||
expect(res).toBe(false);
|
expect(res).toBe(false);
|
||||||
});
|
});
|
||||||
test('in between node', () => {
|
test('in between node', () => {
|
||||||
const res = placing({debug: true}).isValid({
|
const res = placing({debug: true, diagonals: true}).isValid({
|
||||||
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: 0}, 'c': {name: 'c', x: 1, y: 0}
|
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: 0}, 'c': {name: 'c', x: 1, y: 0}
|
||||||
}, [
|
}, [
|
||||||
{from: 'a', to: 'b'}
|
{from: 'a', to: 'b'}
|
||||||
@@ -287,7 +287,7 @@ describe('isValid', () => {
|
|||||||
expect(res).toBe(false);
|
expect(res).toBe(false);
|
||||||
});
|
});
|
||||||
test('invalid right link', () => {
|
test('invalid right link', () => {
|
||||||
const res = placing({debug: true}).isValid({
|
const res = placing({debug: true, diagonals: true}).isValid({
|
||||||
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: -2, y: 2}
|
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: -2, y: 2}
|
||||||
}, [
|
}, [
|
||||||
{from: 'a', to: 'b', direction: 'right'}
|
{from: 'a', to: 'b', direction: 'right'}
|
||||||
@@ -295,7 +295,7 @@ describe('isValid', () => {
|
|||||||
expect(res).toBe(false);
|
expect(res).toBe(false);
|
||||||
});
|
});
|
||||||
test('invalid left link', () => {
|
test('invalid left link', () => {
|
||||||
const res = placing({debug: true}).isValid({
|
const res = placing({debug: true, diagonals: true}).isValid({
|
||||||
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: 2}
|
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: 2}
|
||||||
}, [
|
}, [
|
||||||
{from: 'a', to: 'b', direction: 'left'}
|
{from: 'a', to: 'b', direction: 'left'}
|
||||||
@@ -303,7 +303,7 @@ describe('isValid', () => {
|
|||||||
expect(res).toBe(false);
|
expect(res).toBe(false);
|
||||||
});
|
});
|
||||||
test('invalid up link', () => {
|
test('invalid up link', () => {
|
||||||
const res = placing({debug: true}).isValid({
|
const res = placing({debug: true, diagonals: true}).isValid({
|
||||||
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: 2}
|
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: 2}
|
||||||
}, [
|
}, [
|
||||||
{from: 'a', to: 'b', direction: 'up'}
|
{from: 'a', to: 'b', direction: 'up'}
|
||||||
@@ -311,7 +311,7 @@ describe('isValid', () => {
|
|||||||
expect(res).toBe(false);
|
expect(res).toBe(false);
|
||||||
});
|
});
|
||||||
test('invalid down link', () => {
|
test('invalid down link', () => {
|
||||||
const res = placing({debug: true}).isValid({
|
const res = placing({debug: true, diagonals: true}).isValid({
|
||||||
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: -2}
|
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: -2}
|
||||||
}, [
|
}, [
|
||||||
{from: 'a', to: 'b', direction: 'down'}
|
{from: 'a', to: 'b', direction: 'down'}
|
||||||
@@ -319,7 +319,7 @@ describe('isValid', () => {
|
|||||||
expect(res).toBe(false);
|
expect(res).toBe(false);
|
||||||
});
|
});
|
||||||
test('valid right link', () => {
|
test('valid right link', () => {
|
||||||
const res = placing({debug: true}).isValid({
|
const res = placing({debug: true, diagonals: true}).isValid({
|
||||||
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: 2}
|
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: 2}
|
||||||
}, [
|
}, [
|
||||||
{from: 'a', to: 'b', direction: 'right'}
|
{from: 'a', to: 'b', direction: 'right'}
|
||||||
@@ -327,7 +327,7 @@ describe('isValid', () => {
|
|||||||
expect(res).toBe(true);
|
expect(res).toBe(true);
|
||||||
});
|
});
|
||||||
test('valid left link', () => {
|
test('valid left link', () => {
|
||||||
const res = placing({debug: true}).isValid({
|
const res = placing({debug: true, diagonals: true}).isValid({
|
||||||
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: -2, y: 2}
|
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: -2, y: 2}
|
||||||
}, [
|
}, [
|
||||||
{from: 'a', to: 'b', direction: 'left'}
|
{from: 'a', to: 'b', direction: 'left'}
|
||||||
@@ -335,7 +335,7 @@ describe('isValid', () => {
|
|||||||
expect(res).toBe(true);
|
expect(res).toBe(true);
|
||||||
});
|
});
|
||||||
test('valid up link', () => {
|
test('valid up link', () => {
|
||||||
const res = placing({debug: true}).isValid({
|
const res = placing({debug: true, diagonals: true}).isValid({
|
||||||
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: -2}
|
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: -2}
|
||||||
}, [
|
}, [
|
||||||
{from: 'a', to: 'b', direction: 'up'}
|
{from: 'a', to: 'b', direction: 'up'}
|
||||||
@@ -343,13 +343,21 @@ describe('isValid', () => {
|
|||||||
expect(res).toBe(true);
|
expect(res).toBe(true);
|
||||||
});
|
});
|
||||||
test('valid down link', () => {
|
test('valid down link', () => {
|
||||||
const res = placing({debug: true}).isValid({
|
const res = placing({debug: true, diagonals: true}).isValid({
|
||||||
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: 2}
|
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: 2}
|
||||||
}, [
|
}, [
|
||||||
{from: 'a', to: 'b', direction: 'down'}
|
{from: 'a', to: 'b', direction: 'down'}
|
||||||
]);
|
]);
|
||||||
expect(res).toBe(true);
|
expect(res).toBe(true);
|
||||||
});
|
});
|
||||||
|
test('invalid diagonal link', () => {
|
||||||
|
const res = placing({debug: true, diagonals: false}).isValid({
|
||||||
|
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: 2}
|
||||||
|
}, [
|
||||||
|
{from: 'a', to: 'b'}
|
||||||
|
]);
|
||||||
|
expect(res).toBe(false);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('correctPlacing', () => {
|
describe('correctPlacing', () => {
|
||||||
@@ -443,6 +451,23 @@ describe('compute', () => {
|
|||||||
'6': {name: '6', x: 0, y: 1}
|
'6': {name: '6', x: 0, y: 1}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
test('6 nodes 6 links no directions no diagonals', () => {
|
||||||
|
const nodes = placing({'max-link-length': 2, diagonals: false}).compute(createNodes(6), [
|
||||||
|
{from: '1', to: '2'},
|
||||||
|
{from: '1', to: '3'},
|
||||||
|
{from: '3', to: '4'},
|
||||||
|
{from: '4', to: '5'},
|
||||||
|
{from: '3', to: '6'}
|
||||||
|
]);
|
||||||
|
expect(nodes).toEqual({
|
||||||
|
'1': {name: '1', x: 0, y: 0},
|
||||||
|
'2': {name: '2', x: 0, y: 1},
|
||||||
|
'3': {name: '3', x: 1, y: 0},
|
||||||
|
'4': {name: '4', x: 1, y: 1},
|
||||||
|
'5': {name: '5', x: 2, y: 1},
|
||||||
|
'6': {name: '6', x: 2, y: 0}
|
||||||
|
});
|
||||||
|
});
|
||||||
test('3 nodes impossible', () => {
|
test('3 nodes impossible', () => {
|
||||||
const nodes = placing({'max-link-length': 2, diagonals: false}).compute(createNodes(3), [
|
const nodes = placing({'max-link-length': 2, diagonals: false}).compute(createNodes(3), [
|
||||||
{from: '1', to: '2', direction: 'left'},
|
{from: '1', to: '2', direction: 'left'},
|
||||||
|
|||||||
Reference in New Issue
Block a user