deno.land / std@0.224.0 / collections / sliding_windows_test.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals, assertThrows } from "../assert/mod.ts";import { slidingWindows } from "./sliding_windows.ts";
function slidingWindowsTest<T>( input: [ collection: T[], size: number, config?: { step?: number; partial?: boolean }, ], expected: T[][], message?: string,) { const actual = slidingWindows(...input); assertEquals(actual, expected, message);}
function slidingWindowsThrowsTest<T>( input: [ collection: T[], size: number, config?: { step?: number; partial?: boolean }, ], ErrorClass: ErrorConstructor, msgIncludes?: string, msg?: string | undefined,) { assertThrows( () => { slidingWindows(...input); }, ErrorClass, msgIncludes, msg, );}
Deno.test({ name: "slidingWindows() handles no mutation", fn() { const numbers = [1, 2, 3, 4, 5]; slidingWindows(numbers, 3); assertEquals(numbers, [1, 2, 3, 4, 5]); },});
Deno.test({ name: "slidingWindows() handles empty input", fn() { slidingWindowsTest([[], 3], []); slidingWindowsTest([[], 3, {}], []); slidingWindowsTest([[], 3, { step: 2 }], []); slidingWindowsTest([[], 3, { partial: true }], []); slidingWindowsTest([[], 3, { step: 2, partial: true }], []); },});
Deno.test({ name: "slidingWindows() handles default option", fn() { slidingWindowsTest([[1, 2, 3, 4, 5], 5], [ [1, 2, 3, 4, 5], ]); slidingWindowsTest([[1, 2, 3, 4, 5], 3], [ [1, 2, 3], [2, 3, 4], [3, 4, 5], ]); slidingWindowsTest([[1, 2, 3, 4, 5], 1], [ [1], [2], [3], [4], [5], ]); },});
Deno.test({ name: "slidingWindows() handles step option", fn() { slidingWindowsTest([[1, 2, 3, 4, 5], 5, { step: 2 }], [ [1, 2, 3, 4, 5], ]); slidingWindowsTest([[1, 2, 3, 4, 5], 3, { step: 2 }], [ [1, 2, 3], [3, 4, 5], ]); slidingWindowsTest([[1, 2, 3, 4, 5], 1, { step: 2 }], [ [1], [3], [5], ]); },});
Deno.test({ name: "slidingWindows() handles partial option", fn() { slidingWindowsTest([[1, 2, 3, 4, 5], 5, { partial: true }], [ [1, 2, 3, 4, 5], [2, 3, 4, 5], [3, 4, 5], [4, 5], [5], ]); slidingWindowsTest([[1, 2, 3, 4, 5], 3, { partial: true }], [ [1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5], [5], ]); slidingWindowsTest([[1, 2, 3, 4, 5], 1, { partial: true }], [ [1], [2], [3], [4], [5], ]); },});
Deno.test({ name: "slidingWindows() handles step and partial option", fn() { slidingWindowsTest([[1, 2, 3, 4, 5], 5, { step: 2, partial: true }], [ [1, 2, 3, 4, 5], [3, 4, 5], [5], ]); slidingWindowsTest([[1, 2, 3, 4, 5], 3, { step: 2, partial: true }], [ [1, 2, 3], [3, 4, 5], [5], ]); slidingWindowsTest([[1, 2, 3, 4, 5], 1, { step: 2, partial: true }], [ [1], [3], [5], ]); },});
Deno.test({ name: "slidingWindows() handles invalid size or step: other than number", fn() { slidingWindowsThrowsTest( [[1, 2, 3, 4, 5], NaN], RangeError, "Both size and step must be positive integer.", ); slidingWindowsThrowsTest( [[1, 2, 3, 4, 5], 3, { step: NaN }], RangeError, "Both size and step must be positive integer.", ); slidingWindowsThrowsTest( // @ts-ignore: for test [[1, 2, 3, 4, 5], "invalid"], RangeError, "Both size and step must be positive integer.", ); slidingWindowsThrowsTest( // @ts-ignore: for test [[1, 2, 3, 4, 5], 3, { step: "invalid" }], RangeError, "Both size and step must be positive integer.", ); },});
Deno.test({ name: "slidingWindows() handles invalid size or step: not integer number", fn() { slidingWindowsThrowsTest( [[1, 2, 3, 4, 5], 0.5], RangeError, "Both size and step must be positive integer.", ); slidingWindowsThrowsTest( [[1, 2, 3, 4, 5], 3, { step: 0.5 }], RangeError, "Both size and step must be positive integer.", ); slidingWindowsThrowsTest( [[1, 2, 3, 4, 5], 1.5], RangeError, "Both size and step must be positive integer.", ); slidingWindowsThrowsTest( [[1, 2, 3, 4, 5], 3, { step: 1.5 }], RangeError, "Both size and step must be positive integer.", ); },});
Deno.test({ name: "slidingWindows() handles invalid size or step: not positive number", fn() { slidingWindowsThrowsTest( [[1, 2, 3, 4, 5], 0], RangeError, "Both size and step must be positive integer.", ); slidingWindowsThrowsTest( [[1, 2, 3, 4, 5], 3, { step: 0 }], RangeError, "Both size and step must be positive integer.", ); slidingWindowsThrowsTest( [[1, 2, 3, 4, 5], -1], RangeError, "Both size and step must be positive integer.", ); slidingWindowsThrowsTest( [[1, 2, 3, 4, 5], 3, { step: -1 }], RangeError, "Both size and step must be positive integer.", ); },});
Deno.test({ name: "slidingWindows() handles invalid size or step: infinity", fn() { slidingWindowsThrowsTest( [[1, 2, 3, 4, 5], Number.NEGATIVE_INFINITY], RangeError, "Both size and step must be positive integer.", ); slidingWindowsThrowsTest( [[1, 2, 3, 4, 5], 3, { step: Number.NEGATIVE_INFINITY }], RangeError, "Both size and step must be positive integer.", ); slidingWindowsThrowsTest( [[1, 2, 3, 4, 5], Number.POSITIVE_INFINITY], RangeError, "Both size and step must be positive integer.", ); slidingWindowsThrowsTest( [[1, 2, 3, 4, 5], 3, { step: Number.POSITIVE_INFINITY }], RangeError, "Both size and step must be positive integer.", ); },});
Deno.test({ name: "slidingWindows() handles large size", fn() { slidingWindowsTest([[1, 2, 3, 4, 5], 100], []); slidingWindowsTest([[1, 2, 3, 4, 5], 100, { step: 2 }], []); slidingWindowsTest([[1, 2, 3, 4, 5], 100, { step: 2, partial: true }], [ [1, 2, 3, 4, 5], [3, 4, 5], [5], ]); },});
Deno.test({ name: "slidingWindows() handles large step", fn() { slidingWindowsTest([[1, 2, 3, 4, 5], 3, { step: 100 }], [ [1, 2, 3], ]); slidingWindowsTest([[1, 2, 3, 4, 5], 3, { step: 100, partial: true }], [ [1, 2, 3], ]); },});
Deno.test({ name: "slidingWindows() handles empty Array", fn() { slidingWindowsTest([Array(5), 5], [ Array(5), ]); slidingWindowsTest([Array(5), 3], [ Array(3), Array(3), Array(3), ]); slidingWindowsTest([Array(5), 1], [ Array(1), Array(1), Array(1), Array(1), Array(1), ]); },});
Version Info