|
| 1 | +package com.thealgorithms.others; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.BeforeEach; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +class MaximumSlidingWindowTest { |
| 9 | + |
| 10 | + MaximumSlidingWindow msw; |
| 11 | + int[] nums; |
| 12 | + int k; |
| 13 | + |
| 14 | + @BeforeEach |
| 15 | + void setUp() { |
| 16 | + msw = new MaximumSlidingWindow(); // Initialize the MaximumSlidingWindow object |
| 17 | + } |
| 18 | + |
| 19 | + // Test for a simple sliding window case |
| 20 | + @Test |
| 21 | + void testMaxSlidingWindow_SimpleCase() { |
| 22 | + nums = new int[] {1, 3, -1, -3, 5, 3, 6, 7}; |
| 23 | + k = 3; |
| 24 | + int[] expected = {3, 3, 5, 5, 6, 7}; |
| 25 | + assertArrayEquals(expected, msw.maxSlidingWindow(nums, k)); |
| 26 | + } |
| 27 | + |
| 28 | + // Test when window size is 1 (output should be the array itself) |
| 29 | + @Test |
| 30 | + void testMaxSlidingWindow_WindowSizeOne() { |
| 31 | + nums = new int[] {4, 2, 12, 11, -5}; |
| 32 | + k = 1; |
| 33 | + int[] expected = {4, 2, 12, 11, -5}; |
| 34 | + assertArrayEquals(expected, msw.maxSlidingWindow(nums, k)); |
| 35 | + } |
| 36 | + |
| 37 | + // Test when the window size is equal to the array length (output should be a single max element) |
| 38 | + @Test |
| 39 | + void testMaxSlidingWindow_WindowSizeEqualsArrayLength() { |
| 40 | + nums = new int[] {4, 2, 12, 11, -5}; |
| 41 | + k = nums.length; |
| 42 | + int[] expected = {12}; // Maximum of the entire array |
| 43 | + assertArrayEquals(expected, msw.maxSlidingWindow(nums, k)); |
| 44 | + } |
| 45 | + |
| 46 | + // Test when the input array is empty |
| 47 | + @Test |
| 48 | + void testMaxSlidingWindow_EmptyArray() { |
| 49 | + nums = new int[] {}; |
| 50 | + k = 3; |
| 51 | + int[] expected = {}; |
| 52 | + assertArrayEquals(expected, msw.maxSlidingWindow(nums, k)); |
| 53 | + } |
| 54 | + |
| 55 | + // Test when the window size is larger than the array (should return empty) |
| 56 | + @Test |
| 57 | + void testMaxSlidingWindow_WindowSizeLargerThanArray() { |
| 58 | + nums = new int[] {1, 2, 3}; |
| 59 | + k = 5; |
| 60 | + int[] expected = {}; // Window size is too large, so no result |
| 61 | + assertArrayEquals(expected, msw.maxSlidingWindow(nums, k)); |
| 62 | + } |
| 63 | +} |
0 commit comments