|
| 1 | +using Algorithms.Other; |
| 2 | +using NUnit.Framework; |
| 3 | +using System; |
| 4 | + |
| 5 | +namespace Algorithms.Tests.Other; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Comprehensive test suite for Kadane's Algorithm implementation. |
| 9 | +/// Tests cover various scenarios including: |
| 10 | +/// - Arrays with all positive numbers |
| 11 | +/// - Arrays with mixed positive and negative numbers |
| 12 | +/// - Arrays with all negative numbers |
| 13 | +/// - Edge cases (single element, empty array, null array) |
| 14 | +/// - Index tracking functionality |
| 15 | +/// - Long integer support for large numbers |
| 16 | +/// </summary> |
| 17 | +public static class KadanesAlgorithmTests |
| 18 | +{ |
| 19 | + [Test] |
| 20 | + public static void FindMaximumSubarraySum_WithPositiveNumbers_ReturnsCorrectSum() |
| 21 | + { |
| 22 | + // Arrange: When all numbers are positive, the entire array is the maximum subarray |
| 23 | + int[] array = { 1, 2, 3, 4, 5 }; |
| 24 | + |
| 25 | + // Act |
| 26 | + int result = KadanesAlgorithm.FindMaximumSubarraySum(array); |
| 27 | + |
| 28 | + // Assert: Sum of all elements = 1 + 2 + 3 + 4 + 5 = 15 |
| 29 | + Assert.That(result, Is.EqualTo(15)); |
| 30 | + } |
| 31 | + |
| 32 | + [Test] |
| 33 | + public static void FindMaximumSubarraySum_WithMixedNumbers_ReturnsCorrectSum() |
| 34 | + { |
| 35 | + // Arrange: Classic example with mixed positive and negative numbers |
| 36 | + // The maximum subarray is [4, -1, 2, 1] starting at index 3 |
| 37 | + int[] array = { -2, 1, -3, 4, -1, 2, 1, -5, 4 }; |
| 38 | + |
| 39 | + // Act |
| 40 | + int result = KadanesAlgorithm.FindMaximumSubarraySum(array); |
| 41 | + |
| 42 | + // Assert: Maximum sum is 4 + (-1) + 2 + 1 = 6 |
| 43 | + Assert.That(result, Is.EqualTo(6)); // Subarray [4, -1, 2, 1] |
| 44 | + } |
| 45 | + |
| 46 | + [Test] |
| 47 | + public static void FindMaximumSubarraySum_WithAllNegativeNumbers_ReturnsLargestNegative() |
| 48 | + { |
| 49 | + // Arrange: When all numbers are negative, the algorithm returns the least negative number |
| 50 | + // This represents a subarray of length 1 containing the largest (least negative) element |
| 51 | + int[] array = { -5, -2, -8, -1, -4 }; |
| 52 | + |
| 53 | + // Act |
| 54 | + int result = KadanesAlgorithm.FindMaximumSubarraySum(array); |
| 55 | + |
| 56 | + // Assert: -1 is the largest (least negative) number in the array |
| 57 | + Assert.That(result, Is.EqualTo(-1)); |
| 58 | + } |
| 59 | + |
| 60 | + [Test] |
| 61 | + public static void FindMaximumSubarraySum_WithSingleElement_ReturnsThatElement() |
| 62 | + { |
| 63 | + // Arrange: Edge case with only one element |
| 64 | + // The only possible subarray is the element itself |
| 65 | + int[] array = { 42 }; |
| 66 | + |
| 67 | + // Act |
| 68 | + int result = KadanesAlgorithm.FindMaximumSubarraySum(array); |
| 69 | + |
| 70 | + // Assert: The single element is both the subarray and its sum |
| 71 | + Assert.That(result, Is.EqualTo(42)); |
| 72 | + } |
| 73 | + |
| 74 | + [Test] |
| 75 | + public static void FindMaximumSubarraySum_WithNullArray_ThrowsArgumentException() |
| 76 | + { |
| 77 | + // Arrange: Test defensive programming - null input validation |
| 78 | + int[]? array = null; |
| 79 | + |
| 80 | + // Act & Assert: Should throw ArgumentException for null input |
| 81 | + Assert.Throws<ArgumentException>(() => KadanesAlgorithm.FindMaximumSubarraySum(array!)); |
| 82 | + } |
| 83 | + |
| 84 | + [Test] |
| 85 | + public static void FindMaximumSubarraySum_WithEmptyArray_ThrowsArgumentException() |
| 86 | + { |
| 87 | + // Arrange |
| 88 | + int[] array = Array.Empty<int>(); |
| 89 | + |
| 90 | + // Act & Assert |
| 91 | + Assert.Throws<ArgumentException>(() => KadanesAlgorithm.FindMaximumSubarraySum(array)); |
| 92 | + } |
| 93 | + |
| 94 | + [Test] |
| 95 | + public static void FindMaximumSubarraySum_WithAlternatingNumbers_ReturnsCorrectSum() |
| 96 | + { |
| 97 | + // Arrange: Alternating positive and negative numbers |
| 98 | + // Despite negative values, the entire array gives the maximum sum |
| 99 | + int[] array = { 5, -3, 5, -3, 5 }; |
| 100 | + |
| 101 | + // Act |
| 102 | + int result = KadanesAlgorithm.FindMaximumSubarraySum(array); |
| 103 | + |
| 104 | + // Assert: Sum of entire array = 5 - 3 + 5 - 3 + 5 = 9 |
| 105 | + Assert.That(result, Is.EqualTo(9)); // Entire array |
| 106 | + } |
| 107 | + |
| 108 | + [Test] |
| 109 | + public static void FindMaximumSubarrayWithIndices_ReturnsCorrectIndices() |
| 110 | + { |
| 111 | + // Arrange: Test the variant that returns indices of the maximum subarray |
| 112 | + // Array: [-2, 1, -3, 4, -1, 2, 1, -5, 4] |
| 113 | + // Index: 0 1 2 3 4 5 6 7 8 |
| 114 | + int[] array = { -2, 1, -3, 4, -1, 2, 1, -5, 4 }; |
| 115 | + |
| 116 | + // Act |
| 117 | + var (maxSum, startIndex, endIndex) = KadanesAlgorithm.FindMaximumSubarrayWithIndices(array); |
| 118 | + |
| 119 | + // Assert: Maximum subarray is [4, -1, 2, 1] from index 3 to 6 |
| 120 | + Assert.That(maxSum, Is.EqualTo(6)); |
| 121 | + Assert.That(startIndex, Is.EqualTo(3)); |
| 122 | + Assert.That(endIndex, Is.EqualTo(6)); |
| 123 | + } |
| 124 | + |
| 125 | + [Test] |
| 126 | + public static void FindMaximumSubarrayWithIndices_WithSingleElement_ReturnsZeroIndices() |
| 127 | + { |
| 128 | + // Arrange |
| 129 | + int[] array = { 10 }; |
| 130 | + |
| 131 | + // Act |
| 132 | + var (maxSum, startIndex, endIndex) = KadanesAlgorithm.FindMaximumSubarrayWithIndices(array); |
| 133 | + |
| 134 | + // Assert |
| 135 | + Assert.That(maxSum, Is.EqualTo(10)); |
| 136 | + Assert.That(startIndex, Is.EqualTo(0)); |
| 137 | + Assert.That(endIndex, Is.EqualTo(0)); |
| 138 | + } |
| 139 | + |
| 140 | + [Test] |
| 141 | + public static void FindMaximumSubarrayWithIndices_WithNullArray_ThrowsArgumentException() |
| 142 | + { |
| 143 | + // Arrange |
| 144 | + int[]? array = null; |
| 145 | + |
| 146 | + // Act & Assert |
| 147 | + Assert.Throws<ArgumentException>(() => KadanesAlgorithm.FindMaximumSubarrayWithIndices(array!)); |
| 148 | + } |
| 149 | + |
| 150 | + [Test] |
| 151 | + public static void FindMaximumSubarraySum_WithLongArray_ReturnsCorrectSum() |
| 152 | + { |
| 153 | + // Arrange: Test the long integer overload with same values as int test |
| 154 | + // Verifies that the algorithm works correctly with long data type |
| 155 | + long[] array = { -2L, 1L, -3L, 4L, -1L, 2L, 1L, -5L, 4L }; |
| 156 | + |
| 157 | + // Act |
| 158 | + long result = KadanesAlgorithm.FindMaximumSubarraySum(array); |
| 159 | + |
| 160 | + // Assert: Should produce same result as int version |
| 161 | + Assert.That(result, Is.EqualTo(6L)); |
| 162 | + } |
| 163 | + |
| 164 | + [Test] |
| 165 | + public static void FindMaximumSubarraySum_WithLargeLongNumbers_ReturnsCorrectSum() |
| 166 | + { |
| 167 | + // Arrange: Test with large numbers that would overflow int type |
| 168 | + // This demonstrates why the long overload is necessary |
| 169 | + // Sum would be 1,500,000,000 which fits in long but is near int.MaxValue |
| 170 | + long[] array = { 1000000000L, -500000000L, 1000000000L }; |
| 171 | + |
| 172 | + // Act |
| 173 | + long result = KadanesAlgorithm.FindMaximumSubarraySum(array); |
| 174 | + |
| 175 | + // Assert: Entire array sum = 1,000,000,000 - 500,000,000 + 1,000,000,000 = 1,500,000,000 |
| 176 | + Assert.That(result, Is.EqualTo(1500000000L)); |
| 177 | + } |
| 178 | + |
| 179 | + [Test] |
| 180 | + public static void FindMaximumSubarraySum_WithLongNullArray_ThrowsArgumentException() |
| 181 | + { |
| 182 | + // Arrange |
| 183 | + long[]? array = null; |
| 184 | + |
| 185 | + // Act & Assert |
| 186 | + Assert.Throws<ArgumentException>(() => KadanesAlgorithm.FindMaximumSubarraySum(array!)); |
| 187 | + } |
| 188 | + |
| 189 | + [Test] |
| 190 | + public static void FindMaximumSubarraySum_WithZeros_ReturnsZero() |
| 191 | + { |
| 192 | + // Arrange: Edge case with all zeros |
| 193 | + // Any subarray will have sum of 0 |
| 194 | + int[] array = { 0, 0, 0, 0 }; |
| 195 | + |
| 196 | + // Act |
| 197 | + int result = KadanesAlgorithm.FindMaximumSubarraySum(array); |
| 198 | + |
| 199 | + // Assert: Maximum sum is 0 |
| 200 | + Assert.That(result, Is.EqualTo(0)); |
| 201 | + } |
| 202 | + |
| 203 | + [Test] |
| 204 | + public static void FindMaximumSubarraySum_WithMixedZerosAndNegatives_ReturnsZero() |
| 205 | + { |
| 206 | + // Arrange: Mix of zeros and negative numbers |
| 207 | + // The best subarray is any single zero (or multiple zeros) |
| 208 | + int[] array = { -5, 0, -3, 0, -2 }; |
| 209 | + |
| 210 | + // Act |
| 211 | + int result = KadanesAlgorithm.FindMaximumSubarraySum(array); |
| 212 | + |
| 213 | + // Assert: Zero is better than any negative number |
| 214 | + Assert.That(result, Is.EqualTo(0)); |
| 215 | + } |
| 216 | +} |
0 commit comments