|
| 1 | +package com.thealgorithms.slidingwindow; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +public class CountNiceSubarraysTest { |
| 8 | + @Test |
| 9 | + void testExampleCase() { |
| 10 | + int[] nums = {1, 1, 2, 1, 1}; |
| 11 | + assertEquals(2, CountNiceSubarrays.countNiceSubarrays(nums, 3)); |
| 12 | + } |
| 13 | + |
| 14 | + @Test |
| 15 | + void testAllEvenNumbers() { |
| 16 | + int[] nums = {2, 4, 6, 8}; |
| 17 | + assertEquals(0, CountNiceSubarrays.countNiceSubarrays(nums, 1)); |
| 18 | + } |
| 19 | + |
| 20 | + @Test |
| 21 | + void testSingleOdd() { |
| 22 | + int[] nums = {1}; |
| 23 | + assertEquals(1, CountNiceSubarrays.countNiceSubarrays(nums, 1)); |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + void testMultipleChoices() { |
| 28 | + int[] nums = {2, 2, 1, 2, 2, 1, 2}; |
| 29 | + assertEquals(6, CountNiceSubarrays.countNiceSubarrays(nums, 2)); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + void testTrailingEvenNumbers() { |
| 34 | + int[] nums = {1, 2, 2, 2}; |
| 35 | + assertEquals(4, CountNiceSubarrays.countNiceSubarrays(nums, 1)); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void testMultipleWindowShrinks() { |
| 40 | + int[] nums = {1, 1, 1, 1}; |
| 41 | + assertEquals(3, CountNiceSubarrays.countNiceSubarrays(nums, 2)); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void testEvensBetweenOdds() { |
| 46 | + int[] nums = {2, 1, 2, 1, 2}; |
| 47 | + assertEquals(4, CountNiceSubarrays.countNiceSubarrays(nums, 2)); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void testShrinkWithTrailingEvens() { |
| 52 | + int[] nums = {2, 2, 1, 2, 2, 1, 2, 2}; |
| 53 | + assertEquals(9, CountNiceSubarrays.countNiceSubarrays(nums, 2)); |
| 54 | + } |
| 55 | +} |
0 commit comments