summaryrefslogtreecommitdiff
path: root/src/scripts/tests/util.spec.ts
blob: 74d62dfa1a25dbedfb0e3f8f0266c8ac7c137915 (plain)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
///<reference path="../util.ts" />
///<reference path="../../../typings/globals/jasmine/index.d.ts" />
import {Util} from "../util";


class TestUtils {
    /**
     * Checks whether the two (three-dimensional) wall lists are equivalent in content.
     *
     * This is a set-compare method, meaning that the order of the elements does not matter, but that they are present
     * in both arrays.
     *
     * Example of such a list: [[[1, 1], [2, 1]], [[3, 1], [0, 0]]]
     *
     * @param list1 The first list
     * @param list2 The second list
     */
    public static wallListEquals(list1: IRoomWall[], list2: IRoomWall[]): void {
        let current, found, counter;

        counter = 0;
        while (list1.length > 0) {
            current = list1.pop();
            found = false;
            list2.forEach((e: IRoomWall) => {
                if (current.startPos[0] === e.startPos[0] && current.startPos[1] === e.startPos[1] &&
                    current.horizontal === e.horizontal && current.length === e.length) {
                    counter++;
                    found = true;
                }
            });
            if (!found) {
                fail();
            }
        }
        expect(list2.length).toEqual(counter);
    }

    /**
     * Does the same as wallList3DEquals, only for two lists of tiles.
     *
     * @param expected
     * @param actual
     */
    public static positionListEquals(expected: IGridPosition[], actual: IGridPosition[]): void {
        let current, found;
        let counter = 0;

        while (expected.length > 0) {
            current = expected.pop();
            found = false;
            actual.forEach((e) => {
                if (current.x === e.x && current.y === e.y) {
                    counter++;
                    found = true;
                }
            });
            if (!found) {
                fail();
            }
        }

        expect(actual.length).toEqual(counter);
    }

    public static boundsEquals(actual: IBounds, expected: IBounds): void {
        expect(actual.min[0]).toBe(expected.min[0]);
        expect(actual.min[1]).toBe(expected.min[1]);
        expect(actual.center[0]).toBe(expected.center[0]);
        expect(actual.center[1]).toBe(expected.center[1]);
        expect(actual.max[0]).toBe(expected.max[0]);
        expect(actual.max[1]).toBe(expected.max[1]);
    }
}

describe("Deriving wall locations", () => {
    it("should generate walls around a single tile", () => {
            let room = {
                id: -1,
                datacenterId: -1,
                name: "testroom",
                roomType: "none",
                tiles: [{
                    id: -1,
                    roomId: -1,
                    position: {x: 1, y: 1}
                }]
            };

        let result = Util.deriveWallLocations([
                room
            ]);
        let expected: IRoomWall[] = [
            {
                startPos: [1, 1],
                horizontal: false,
                length: 1
            },
            {
                startPos: [2, 1],
                horizontal: false,
                length: 1
            },
            {
                startPos: [1, 1],
                horizontal: true,
                length: 1
            },
            {
                startPos: [1, 2],
                horizontal: true,
                length: 1
            }
            ];

        TestUtils.wallListEquals(expected, result);
        }
    );

    it("should generate walls around two tiles connected by an edge", () => {
            let room = {
                id: -1,
                datacenterId: -1,
                name: "testroom",
                roomType: "none",
                tiles: [
                    {
                        id: -1,
                        roomId: -1,
                        position: {x: 1, y: 1}
                    }, {
                        id: -1,
                        roomId: -1,
                        position: {x: 1, y: 2}
                    }
                ]
            };

        let result = Util.deriveWallLocations([
                room
            ]);
            let expected: IRoomWall[] = [
                {
                    startPos: [1, 1],
                    horizontal: false,
                    length: 2
                },
                {
                    startPos: [2, 1],
                    horizontal: false,
                    length: 2
                },
                {
                    startPos: [1, 1],
                    horizontal: true,
                    length: 1
                },
                {
                    startPos: [1, 3],
                    horizontal: true,
                    length: 1
                }
            ];

            TestUtils.wallListEquals(expected, result);
        }
    );

    it("should generate walls around two independent rooms with one tile each", () => {
            let room1 = {
                id: -1,
                datacenterId: -1,
                name: "testroom",
                roomType: "none",
                tiles: [
                    {
                        id: -1,
                        roomId: -1,
                        position: {x: 1, y: 1}
                    }
                ]
            };

            let room2 = {
                id: -1,
                datacenterId: -1,
                name: "testroom",
                roomType: "none",
                tiles: [{
                    id: -1,
                    roomId: -1,
                    position: {x: 1, y: 3}
                }
                ]
            };

        let result = Util.deriveWallLocations([
                room1, room2
            ]);
            let expected: IRoomWall[] = [
                {
                    startPos: [1, 1],
                    horizontal: false,
                    length: 1
                },
                {
                    startPos: [1, 3],
                    horizontal: false,
                    length: 1
                },
                {
                    startPos: [2, 1],
                    horizontal: false,
                    length: 1
                },
                {
                    startPos: [2, 3],
                    horizontal: false,
                    length: 1
                },
                {
                    startPos: [1, 1],
                    horizontal: true,
                    length: 1
                },
                {
                    startPos: [1, 2],
                    horizontal: true,
                    length: 1
                },
                {
                    startPos: [1, 3],
                    horizontal: true,
                    length: 1
                },
                {
                    startPos: [1, 4],
                    horizontal: true,
                    length: 1
                }
            ];

            TestUtils.wallListEquals(expected, result);
        }
    );
});

describe("Deriving valid next tile positions", () => {
    it("should derive correctly 4 valid tile positions around 1 selected tile with no other rooms", () => {
        let result = Util.deriveValidNextTilePositions([], [{
            id: -1,
            roomId: -1,
            position: {x: 1, y: 1}
        }]);
        let expected = [
            {x: 1, y: 0}, {x: 2, y: 1}, {x: 1, y: 2}, {x: 0, y: 1}
        ];

        TestUtils.positionListEquals(expected, result);
    });

    it("should derive correctly 6 valid tile positions around 2 selected tiles with no other rooms", () => {
        let result = Util.deriveValidNextTilePositions([], [{
            id: -1,
            roomId: -1,
            position: {x: 1, y: 1}
        }, {
            id: -1,
            roomId: -1,
            position: {x: 2, y: 1}
        }]);
        let expected = [
            {x: 1, y: 0}, {x: 2, y: 0}, {x: 3, y: 1}, {x: 1, y: 2}, {x: 2, y: 2}, {x: 0, y: 1}
        ];

        TestUtils.positionListEquals(expected, result);
    });

    it("should derive correctly 3 valid tile positions around 1 selected tiles with 1 adjacent room", () => {
        let room = {
            id: -1,
            datacenterId: -1,
            name: "testroom",
            roomType: "none",
            tiles: [{
                id: -1,
                roomId: -1,
                position: {x: 0, y: 1}
            }]
        };
        let result = Util.deriveValidNextTilePositions([room], [{
            id: -1,
            roomId: -1,
            position: {x: 1, y: 1}
        }]);
        let expected = [
            {x: 1, y: 0}, {x: 2, y: 1}, {x: 1, y: 2}
        ];

        TestUtils.positionListEquals(expected, result);
    });
});

describe("Calculating the bounds and average point of a list of rooms", () => {
    it("should calculate correctly the bounds of a 1-tile room", () => {
        let room = {
            id: -1,
            datacenterId: -1,
            name: "testroom",
            roomType: "none",
            tiles: [{
                id: -1,
                roomId: -1,
                position: {x: 1, y: 1}
            }]
        };
        let result = Util.calculateRoomListBounds([room]);
        let expected = {
            min: [1, 1],
            center: [1.5, 1.5],
            max: [2, 2]
        };

        TestUtils.boundsEquals(result, expected);
    });
});