|
| 1 | +/* eslint-env jest */ |
| 2 | +// @ts-ignore ambiguous import |
| 3 | +import slidingWindow from "./slidingWindow.ts"; |
| 4 | + |
| 5 | +describe("slidingWindow", () => { |
| 6 | + it("should return an array of pairs of consecutive elements for sliding window length of 2", () => { |
| 7 | + expect(slidingWindow(2)([1, 2, 3, 4, 5, 6])).toEqual([ |
| 8 | + [1, 2], |
| 9 | + [2, 3], |
| 10 | + [3, 4], |
| 11 | + [4, 5], |
| 12 | + [5, 6] |
| 13 | + ]); |
| 14 | + }); |
| 15 | + |
| 16 | + it("should return an array composed of triples of consecutive elements for sliding window length of 3", () => { |
| 17 | + expect(slidingWindow(3)([1, 2, 3, 4, 5, 6])).toEqual([ |
| 18 | + [1, 2, 3], |
| 19 | + [2, 3, 4], |
| 20 | + [3, 4, 5], |
| 21 | + [4, 5, 6] |
| 22 | + ]); |
| 23 | + }); |
| 24 | + |
| 25 | + it("should return an array composed of quadruples of consecutive elements for sliding window length of 4", () => { |
| 26 | + expect(slidingWindow(4)([1, 2, 3, 4, 5, 6])).toEqual([ |
| 27 | + [1, 2, 3, 4], |
| 28 | + [2, 3, 4, 5], |
| 29 | + [3, 4, 5, 6] |
| 30 | + ]); |
| 31 | + }); |
| 32 | + |
| 33 | + it("should return a wrapped array when sliding window size is equal to length of the given array", () => { |
| 34 | + expect(slidingWindow(6)([1, 2, 3, 4, 5, 6])).toEqual([[1, 2, 3, 4, 5, 6]]); |
| 35 | + }); |
| 36 | + |
| 37 | + it("should return an array of wrapped values in arrays when sliding window length is 1", () => { |
| 38 | + expect(slidingWindow(1)([1, 2, 3, 4, 5, 6])).toEqual([ |
| 39 | + [1], |
| 40 | + [2], |
| 41 | + [3], |
| 42 | + [4], |
| 43 | + [5], |
| 44 | + [6] |
| 45 | + ]); |
| 46 | + }); |
| 47 | + |
| 48 | + it("should return an empty array when sliding window size is greater than length of the given array", () => { |
| 49 | + expect(slidingWindow(3)([])).toEqual([]); |
| 50 | + }); |
| 51 | + |
| 52 | + it("should return an empty array when sliding window size is less than or equal to zero", () => { |
| 53 | + expect(slidingWindow(0)([1, 2, 3])).toEqual([]); |
| 54 | + expect(slidingWindow(-1)([1, 2, 3])).toEqual([]); |
| 55 | + }); |
| 56 | +}); |
0 commit comments