diff --git a/problems/1792-maximum-average-pass-ratio/test/SolutionTests.cs b/problems/1792-maximum-average-pass-ratio/test/SolutionTests.cs new file mode 100644 index 0000000..3a89a51 --- /dev/null +++ b/problems/1792-maximum-average-pass-ratio/test/SolutionTests.cs @@ -0,0 +1,176 @@ +// LeetCode 1792 單元測試 +// 使用 xUnit 框架 + +using Xunit; +using System; +using System.Collections.Generic; + +public class SolutionTests +{ + private readonly Solution _solution; + private const double TOLERANCE = 0.00001; + + public SolutionTests() + { + _solution = new Solution(); + } + + #region Basic Test + [Fact] + public void TestCase1() + { + // Arrange + int[][] classes = new int[][] + { + new int[] {1, 2}, + new int[] {3, 5}, + new int[] {2, 2} + }; + int extraStudents = 2; + double expected = 0.78333; + + // Act + var result = _solution.MaxAverageRatio(classes, extraStudents); + + // Assert + Assert.True(Math.Abs(result - expected) < TOLERANCE, + $"Expected: {expected}, Actual: {result}"); + } + + [Fact] + public void TestCase2() + { + + // Arrange + int[][] classes = new int[][] + { + new int[] {2, 4}, // 通過率: 2/4 = 0.5 + new int[] {3, 9}, // 通過率: 3/9 = 0.333... + new int[] {4, 5}, // 通過率: 4/5 = 0.8 + new int[] {2, 10} // 通過率: 2/10 = 0.2 + }; + int extraStudents = 4; + double expected = 0.53485; + + // Act + var result = _solution.MaxAverageRatio(classes, extraStudents); + + // Assert + Assert.True(Math.Abs(result - expected) < TOLERANCE, + $"Expected: {expected}, Actual: {result}"); + } + + [Fact] + public void TestBruteForceVsPriorityQueue() + { + // Arrange + int[][] classes = new int[][] + { + new int[] {1, 3}, + new int[] {2, 4}, + new int[] {1, 5} + }; + int extraStudents = 3; + + // Act + var resultBruteForce = _solution.MaxAverageRatioByBruteForce(classes, extraStudents); + var resultPriorityQueue = _solution.MaxAverageRatio(classes, extraStudents); + + // Assert + Assert.True(Math.Abs(resultBruteForce - resultPriorityQueue) < TOLERANCE, + $"Brute Force: {resultBruteForce}, Priority Queue: {resultPriorityQueue}"); + } + #endregion + + #region Edge Test + [Fact] + public void TestEdgeCases_MinClassesLength_OneClass() + { + // Arrange + int[][] classes = new int[][] + { + new int[] {50, 100} + }; + int extraStudents = 10; + double expected = 60.0 / 110.0; + + // Act + var result = _solution.MaxAverageRatio(classes, extraStudents); + + // Assert + Assert.True(result >= 0 && result <= 1, + $"Result should be between 0 and 1, got: {result}"); + Assert.True(Math.Abs(result - expected) < TOLERANCE, + $"Expected: {expected}, Actual: {result}"); + } + + [Fact] + public void TestEdgeCases_LargeClassesLength() + { + // Arrange + const int numClasses = 1000; + var classes = new int[numClasses][]; + var random = new Random(42); + + for (int i = 0; i < numClasses; i++) + { + int total = random.Next(10, 100); // total 在 10-100 之間 + int pass = random.Next(1, total + 1); // pass 在 1-total 之間 + classes[i] = new int[] { pass, total }; + } + int extraStudents = 100; + + // Act + var result = _solution.MaxAverageRatio(classes, extraStudents); + + // Assert + Assert.True(result >= 0 && result <= 1, + $"Result should be between 0 and 1, got: {result}"); + } + + [Fact] + public void TestEdgeCases_MinExtraStudents_One() + { + // Arrange + int[][] classes = new int[][] + { + new int[] {1, 3}, + new int[] {2, 4} + }; + int extraStudents = 1; + + // Act + var result = _solution.MaxAverageRatio(classes, extraStudents); + + // Assert + double originalAvg = (1.0 / 3 + 2.0 / 4) / 2; + Assert.True(result > originalAvg, + $"Result {result} should be greater than original average {originalAvg}"); + } + + [Fact] + public void TestEdgeCases_LargeExtraStudents() + { + // Arrange - 大量額外學生 + int[][] classes = new int[][] + { + new int[] {10, 100}, // 10% 通過率 + new int[] {20, 100}, // 20% 通過率 + new int[] {30, 100} // 30% 通過率 + }; + int extraStudents = 10000; + + // Act + var result = _solution.MaxAverageRatio(classes, extraStudents); + + // Assert + Assert.True(result >= 0 && result <= 1, + $"Result should be between 0 and 1, got: {result}"); + + // 大量學生加入後,通過率應該顯著提升 + double originalAvg = (0.1 + 0.2 + 0.3) / 3; // = 0.2 + Assert.True(result > originalAvg + 0.1, + $"Result {result} should be significantly higher than original average {originalAvg}"); + } + #endregion +} \ No newline at end of file diff --git a/problems/1792-maximum-average-pass-ratio/test/TestProject.csproj b/problems/1792-maximum-average-pass-ratio/test/TestProject.csproj new file mode 100644 index 0000000..2f44c14 --- /dev/null +++ b/problems/1792-maximum-average-pass-ratio/test/TestProject.csproj @@ -0,0 +1,22 @@ + + + + net8.0 + enable + false + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + +