-
Notifications
You must be signed in to change notification settings - Fork 0
/
indeling-util.test.js
242 lines (210 loc) · 11.7 KB
/
indeling-util.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* eslint-disable no-magic-numbers */
/* eslint-disable no-var */
/* eslint-disable vars-on-top */
/* eslint-disable no-redeclare */
const Markt = require('./algo/markt.ts').default;
const Indeling = require('./algo/indeling.ts').default;
const Ondernemer = require('./algo/ondernemer.ts').default;
const { calcToewijzingen } = require('./index.ts');
const { marktScenario } = require('./indeling-scenario.ts');
describe('calcToewijzingen', () => {
it('requires a valid marktDate', () => {
const markt = marktScenario({ marktDate: null });
const markt2 = marktScenario({ marktDate: 'invalid' });
const markt3 = marktScenario({ marktDate: '2019-01-01' });
expect(calcToewijzingen.bind(null, markt)).toThrow();
expect(calcToewijzingen.bind(null, markt2)).toThrow();
expect(calcToewijzingen.bind(null, markt3)).not.toThrow();
});
});
describe('Indeling.init', () => {
it('corrigeert branche.maximumPlaatsen', () => {
const markt = marktScenario({
marktplaatsen: [
{},
{ branches: ['a'] }, { branches: ['a'] },
{ branches: ['b'] }, { branches: ['b'] },
{ branches: ['c'] }
],
branches: [
{ brancheId: 'a', verplicht: true },
{ brancheId: 'b', verplicht: true, maximumPlaatsen: 1 },
{ brancheId: 'c', maximumPlaatsen: 1 },
{ brancheId: 'd', maximumPlaatsen: 2 }
]
});
const indeling = Indeling.init(markt);
expect(indeling.branches[0].maximumPlaatsen).toEqual(2);
expect(indeling.branches[1].maximumPlaatsen).toEqual(2);
expect(indeling.branches[2].maximumPlaatsen).toEqual(1);
expect(indeling.branches[3].maximumPlaatsen).toEqual(2);
});
});
describe('Markt.getAdjacentPlaatsen', () => {
const getAdjacent = (rows, placeIds, depth=1, obstakels, filter) => {
const markt = marktScenario({ rows, obstakels });
return Markt.getAdjacentPlaatsen(markt, placeIds, depth, filter)
.map(({ plaatsId }) => plaatsId)
.sort();
};
describe('Get adjacent for single place', () => {
it('returns nothing for single place rows', () => {
expect(getAdjacent([['1']], ['1'])).toStrictEqual([]);
expect(getAdjacent([['1']], ['1'], 2)).toStrictEqual([]);
});
it('returns places from one side when needle is at the beginning/end', () => {
// Needle at beginning...
expect(getAdjacent([['1', '2', '3', '4', '5', '6']], ['1'])).toStrictEqual(['2']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6']], ['1'], 2)).toStrictEqual(['2', '3']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6']], ['1'], 2, [], ({ plaatsId }) => plaatsId !== '3')).toStrictEqual(['2']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6']], ['1'], 2, [], ({ plaatsId }) => plaatsId !== '2')).toStrictEqual([]);
// ...and end.
expect(getAdjacent([['1', '2', '3', '4', '5', '6']], ['6'])).toStrictEqual(['5']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6']], ['6'], 2)).toStrictEqual(['4', '5']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6']], ['6'], 2, [], ({ plaatsId }) => plaatsId !== '4')).toStrictEqual(['5']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6']], ['6'], 2, [], ({ plaatsId }) => plaatsId !== '5')).toStrictEqual([]);
});
it('returns places from both sides when needle is not at beginning/end', () => {
expect(getAdjacent([['1', '2', '3', '4', '5', '6']], ['2'])).toStrictEqual(['1', '3']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6']], ['2'], 2)).toStrictEqual(['1', '3', '4']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6']], ['3'], 2)).toStrictEqual(['1', '2', '4', '5']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6']], ['3'], 2, [], ({ plaatsId }) => plaatsId !== '2')).toStrictEqual(['4', '5']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6']], ['3'], 2, [], ({ plaatsId }) => plaatsId !== '4')).toStrictEqual(['1', '2']);
});
it('does not return places from adjacent rows', () => {
expect(getAdjacent([['3', '4', '5', '6'], ['23', '22']], ['6'])).toStrictEqual(['5']);
expect(getAdjacent([['3', '4', '5', '6'], ['23', '22']], ['6'], 2)).toStrictEqual(['4', '5']);
expect(getAdjacent([['3', '4', '5', '6'], ['23', '22']], ['6'], 5)).toStrictEqual(['3', '4', '5']);
});
});
describe('Get adjacent for multiple places', () => {
it('returns nothing when needle matches row', () => {
expect(getAdjacent([['1', '2']], ['1', '2'])).toStrictEqual([]);
expect(getAdjacent([['1', '2']], ['1', '2'], 2)).toStrictEqual([]);
expect(getAdjacent([['1', '2']], ['1', '2'], 3)).toStrictEqual([]);
});
it('returns places from one side when needle is at the beginning/end', () => {
expect(getAdjacent([['1', '2', '3', '4', '5', '6']], ['1', '2'])).toStrictEqual(['3']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6']], ['1', '2'], 2)).toStrictEqual(['3', '4']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6']], ['1', '2'], 3)).toStrictEqual(['3', '4', '5']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6']], ['5', '6'])).toStrictEqual(['4']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6']], ['5', '6'], 2)).toStrictEqual(['3', '4']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6']], ['5', '6'], 3)).toStrictEqual(['2', '3', '4']);
});
it('returns places from both sides when needle is not at beginning/end', () => {
expect(getAdjacent([['1', '2', '3', '4', '5', '6']], ['2', '3'])).toStrictEqual(['1', '4']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6']], ['2', '3'], 2)).toStrictEqual(['1', '4', '5']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6']], ['3', '4'], 2)).toStrictEqual(['1', '2', '5', '6']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6', '7', '8']], ['3', '4'], 3)).toStrictEqual(['1', '2', '5', '6', '7']);
});
});
describe('Get adjacent for circular rows', () => {
it('returns nothing when needle covers all places in a row', () => {
expect(getAdjacent([['1', '2', '1']], ['1', '2'])).toStrictEqual([]);
expect(getAdjacent([['1', '2', '3', '4', '5', '1']], ['1', '2', '3', '4', '5'])).toStrictEqual([]);
});
it('goes around when single-place needle is at beginning/end', () => {
expect(getAdjacent([['1', '2', '3', '4', '5', '6', '1']], ['1'])).toStrictEqual(['2', '6']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6', '1']], ['1'], 2)).toStrictEqual(['2', '3', '5', '6']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6', '1']], ['2'], 2)).toStrictEqual(['1', '3', '4', '6']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6', '1']], ['1'], 3)).toStrictEqual(['2', '3', '4', '5', '6']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6', '1']], ['6'])).toStrictEqual(['1', '5']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6', '1']], ['6'], 2)).toStrictEqual(['1', '2', '4', '5']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6', '1']], ['6'], 3)).toStrictEqual(['1', '2', '3', '4', '5']);
});
it('goes around when multi-place needle is at beginning/end', () => {
expect(getAdjacent([['1', '2', '3', '4', '5', '6', '1']], ['1', '2'])).toStrictEqual(['3', '6']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6', '1']], ['1', '2'], 2)).toStrictEqual(['3', '4', '5', '6']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6', '1']], ['1', '2'], 3)).toStrictEqual(['3', '4', '5', '6']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6', '1']], ['5', '6'])).toStrictEqual(['1', '4']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6', '1']], ['5', '6'], 2)).toStrictEqual(['1', '2', '3', '4']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6', '1']], ['5', '6'], 3)).toStrictEqual(['1', '2', '3', '4']);
});
it('stops looking when depth is infinite', () => {
expect(getAdjacent([['1', '2', '3', '4', '5', '6', '1']], ['6'], Infinity)).toStrictEqual(['1', '2', '3', '4', '5']);
});
});
describe('Get adjacent with obstacles', () => {
const obstacles = [{
kraamA : '2',
kraamB : '3',
obstakel : ['bergketen']
}, {
kraamA : '6',
kraamB : '1',
obstakel : ['waterzuivering']
}];
it('stops expanding at an obstacle in a linear row', () => {
expect(getAdjacent([['1', '2', '3', '4', '5', '6']], ['2'], 1, obstacles)).toStrictEqual(['1']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6']], ['2'], 2, obstacles)).toStrictEqual(['1']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6']], ['3'], 1, obstacles)).toStrictEqual(['4']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6']], ['3'], 2, obstacles)).toStrictEqual(['4', '5']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6']], ['4'], 2, obstacles)).toStrictEqual(['3', '5', '6']);
});
it('stops expanding at an obstacle in a circular row', () => {
expect(getAdjacent([['1', '2', '3', '4', '5', '6', '1']], ['2'], 1, obstacles)).toStrictEqual(['1']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6', '1']], ['2'], 2, obstacles)).toStrictEqual(['1']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6', '1']], ['1'], 2, obstacles)).toStrictEqual(['2']);
expect(getAdjacent([['1', '2', '3', '4', '5', '6', '1']], ['4'], 3, obstacles)).toStrictEqual(['3', '5', '6']);
});
});
});
describe('Markt.groupByAdjacent', () => {
const groupByAdjacent = (rows, placeIds, obstakels) => {
const markt = marktScenario({ rows, obstakels });
const plaatsen = placeIds.map(plaatsId => ({ plaatsId }));
return Markt.groupByAdjacent(markt, plaatsen)
.map(group => group.map(({ plaatsId }) => plaatsId));
};
it('works', () => {
expect(groupByAdjacent(
[
['1', '2', '3', '4', '5', '6'],
['7', '8', '9', '10'],
['11', '12', '13', '14', '15', '16', '17'],
['18', '19', '20', '18']
],
['1', '2', '7', '12', '13', '14', '16', '17', '20', '18'],
[{
kraamA : '16',
kraamB : '17',
obstakel : ['raketlanceerinstallatie']
}]
)).toStrictEqual(
[['1', '2'], ['7'], ['12', '13', '14'], ['16'], ['17'], ['20', '18']]
);
});
});
describe('Markt.trimRow', () => {
function trimRow( row, plaatsIds ) {
row = row.map(id => ({ plaatsId: id }));
return Markt.trimRow(row, plaatsIds);
}
it('works', () => {
expect(
trimRow(['1', '2', '3', '4', '5', '6'], ['1'])
).toStrictEqual(
['1']
);
expect(
trimRow(['1', '2', '3', '4', '5', '6'], ['3'])
).toStrictEqual(
['3']
);
expect(
trimRow(['1', '2', '3', '4', '5', '6'], ['2', '4', '5'])
).toStrictEqual(
['2', '3', '4', '5']
);
expect(
trimRow(['1', '2', '3', '4', '5', '6'], ['1', '6'])
).toStrictEqual(
['1', '2', '3', '4', '5', '6']
);
expect(
trimRow(['1', '2', '3', '4', '5', '6'], ['6', '1'])
).toStrictEqual(
['1', '2', '3', '4', '5', '6']
);
});
});