1+ package com .thealgorithms .dynamicprogramming ;
2+
3+ import static org .junit .jupiter .api .Assertions .assertEquals ;
4+
5+ import org .junit .jupiter .api .Test ;
6+
7+ class MaximumProductSubarrayTest {
8+
9+ /**
10+ * Test case for an array with all positive numbers.
11+ * The expected maximum product is the product of all elements.
12+ */
13+ @ Test
14+ void testAllPositiveNumbers () {
15+ int [] nums = {2 , 3 , 4 };
16+ int expected = 24 ;
17+ int actual = MaximumProductSubarray .maxProduct (nums );
18+ assertEquals (expected , actual , "The maximum product should be 24." );
19+ }
20+
21+ /**
22+ * Test case for an array with positive and negative numbers.
23+ * The expected maximum product is 24 (subarray [2, -3, -4]).
24+ */
25+ @ Test
26+ void testMixedPositiveAndNegative () {
27+ int [] nums = {2 , -3 , -4 , 1 };
28+ int expected = 24 ;
29+ int actual = MaximumProductSubarray .maxProduct (nums );
30+ assertEquals (expected , actual , "The maximum product should be 24." );
31+ }
32+
33+ /**
34+ * Test case for an array containing zeros.
35+ * The expected maximum product is 24 (subarray [2, 3, 4]).
36+ */
37+ @ Test
38+ void testArrayWithZeros () {
39+ int [] nums = {2 , 3 , 0 , 4 , 6 };
40+ int expected = 24 ;
41+ int actual = MaximumProductSubarray .maxProduct (nums );
42+ assertEquals (expected , actual , "The maximum product should be 24." );
43+ }
44+
45+ /**
46+ * Test case for an array with a single element.
47+ * The expected maximum product is the element itself.
48+ */
49+ @ Test
50+ void testSingleElement () {
51+ int [] nums = {5 };
52+ int expected = 5 ;
53+ int actual = MaximumProductSubarray .maxProduct (nums );
54+ assertEquals (expected , actual , "The maximum product should be 5." );
55+ }
56+
57+ /**
58+ * Test case for an array with all negative numbers.
59+ * The expected maximum product is the largest single negative number.
60+ */
61+ @ Test
62+ void testAllNegativeNumbers () {
63+ int [] nums = {-2 , -3 , -4 };
64+ int expected = 12 ;
65+ int actual = MaximumProductSubarray .maxProduct (nums );
66+ assertEquals (expected , actual , "The maximum product should be 12." );
67+ }
68+
69+ /**
70+ * Test case for an array with negative numbers where odd count of negatives
71+ * breaks the chain. The expected maximum product is 60 (subarray [-2, -3, 10]).
72+ */
73+ @ Test
74+ void testOddNegativeNumbers () {
75+ int [] nums = {-2 , -3 , 10 , -1 };
76+ int expected = 60 ;
77+ int actual = MaximumProductSubarray .maxProduct (nums );
78+ assertEquals (expected , actual , "The maximum product should be 60." );
79+ }
80+
81+ /**
82+ * Test case for an empty array.
83+ * The expected maximum product is 0.
84+ */
85+ @ Test
86+ void testEmptyArray () {
87+ int [] nums = {};
88+ int expected = 0 ;
89+ int actual = MaximumProductSubarray .maxProduct (nums );
90+ assertEquals (expected , actual , "The maximum product should be 0 for an empty array." );
91+ }
92+
93+ /**
94+ * Test case for an array with alternating positive and negative numbers.
95+ * The expected maximum product is 6 (subarray [2, 3]).
96+ */
97+ @ Test
98+ void testAlternatingNumbers () {
99+ int [] nums = {2 , 3 , -2 , 4 };
100+ int expected = 6 ;
101+ int actual = MaximumProductSubarray .maxProduct (nums );
102+ assertEquals (expected , actual , "The maximum product should be 6." );
103+ }
104+
105+ /**
106+ * Test case for the memoized version with all positive numbers.
107+ * The expected maximum product is 24.
108+ */
109+ @ Test
110+ void testMemoizedAllPositiveNumbers () {
111+ int [] nums = {2 , 3 , 4 };
112+ int expected = 24 ;
113+ int actual = MaximumProductSubarray .maxProductMemoized (nums );
114+ assertEquals (expected , actual , "The maximum product should be 24." );
115+ }
116+
117+ /**
118+ * Test case for the memoized version with mixed positive and negative numbers.
119+ * The expected maximum product is 24.
120+ */
121+ @ Test
122+ void testMemoizedMixedNumbers () {
123+ int [] nums = {2 , -3 , -4 , 1 };
124+ int expected = 24 ;
125+ int actual = MaximumProductSubarray .maxProductMemoized (nums );
126+ assertEquals (expected , actual , "The maximum product should be 24." );
127+ }
128+
129+ /**
130+ * Test case for an array with large positive and negative numbers.
131+ * The expected maximum product is 720 (subarray [6, -3, -20]).
132+ */
133+ @ Test
134+ void testLargeNumbers () {
135+ int [] nums = {6 , -3 , -20 , 0 , 5 };
136+ int expected = 360 ;
137+ int actual = MaximumProductSubarray .maxProduct (nums );
138+ assertEquals (expected , actual , "The maximum product should be 360." );
139+ }
140+ }
0 commit comments