[#3025] test(numberOfPairs): write C# unit test

This commit is contained in:
2025-09-02 12:28:52 +08:00
parent 5018b4ab9f
commit 38f948f691
4 changed files with 243 additions and 55 deletions

View File

@@ -26,17 +26,12 @@ Return the count.
- 預期時間/空間複雜度?
### 解法概述
1. **暴力解法**:
**解法**:
- 思路:
- 遍歷所有點對 (A, B)
- 檢查每個pair形成的矩形, 裡面跟邊界會不會包含其他點
- 時間複雜度O(N^3)
- 空間複雜度O(?)
2. **優化解法**:
- 思路:
- 時間複雜度O(?)
- 空間複雜度O(?)
- 空間複雜度O(1)
## 測試案例

View File

@@ -9,7 +9,7 @@ using System.Linq;
public class Solution
{
public int NumberOfPairsWithBruteForce(int[][] points)
public int NumberOfPairs(int[][] points)
{
// 遍歷所有點對 (A, B)
// 檢查每個pair形成的矩形, 裡面跟邊界會不會包含其他點
@@ -76,11 +76,11 @@ public class Program {
new int[] {2, 6}
};
// Expected:
Console.WriteLine($"測試案例 1: [[6,2],[4,4],[2,6]]");
Console.WriteLine($"預期: 2");
Console.WriteLine($"Test Case: [[6,2],[4,4],[2,6]]");
Console.WriteLine($"Expected: 2");
// Actual:
int result1 = solution.NumberOfPairsWithBruteForce(points);
Console.WriteLine($"Brute Force: {result1}");
int result1 = solution.NumberOfPairs(points);
Console.WriteLine($"Result: {result1}");
}
}

View File

@@ -5,39 +5,240 @@ using Xunit;
using System;
using System.Collections.Generic;
public class SolutionTests {
public class SolutionTests
{
private readonly Solution _solution;
public SolutionTests() {
public SolutionTests()
{
_solution = new Solution();
}
[Fact]
public void TestCase1() {
public void TestCase1_ExampleFromProblem()
{
// Arrange
// TODO: 設定輸入資料
// var input = ;
// var expected = ;
int[][] points = new int[][] {
new int[] {6, 2},
new int[] {4, 4},
new int[] {2, 6}
};
int expected = 2;
// Act
var result = _solution.NumberOfPairs(points);
// Assert
Assert.Equal(expected, result);
}
[Fact]
public void TestCase2_TwoPoints()
{
// Arrange
int[][] points = new int[][] {
new int[] {1, 3},
new int[] {2, 1}
};
int expected = 1;
// Act
var result = _solution.NumberOfPairs(points);
// Assert
Assert.Equal(expected, result);
}
[Fact]
public void TestCase3_NoValidPairs()
{
// Arrange - 沒有有效點對的情況
int[][] points = new int[][] {
new int[] {1, 1},
new int[] {2, 2},
new int[] {3, 3}
};
int expected = 0;
// Act
var result = _solution.NumberOfPairs(points);
// Assert
Assert.Equal(expected, result);
}
[Fact]
public void TestCase4_SameXCoordinate()
{
// Arrange - x 座標相同的情況
int[][] points = new int[][] {
new int[] {3, 5},
new int[] {3, 2},
new int[] {3, 1}
};
int expected = 2;
// Act
var result = _solution.NumberOfPairs(points);
// Assert
Assert.Equal(expected, result);
}
[Fact]
public void TestCase5_SameYCoordinate()
{
// Arrange - y 座標相同的情況
int[][] points = new int[][] {
new int[] {1, 4},
new int[] {3, 4},
new int[] {5, 4}
};
int expected = 2;
// Act
var result = _solution.NumberOfPairs(points);
// Assert
Assert.Equal(expected, result);
}
[Fact]
public void TestCase6_PointsInsideRectangle()
{
// Arrange - 矩形內有其他點的情況
int[][] points = new int[][] {
new int[] {0, 4}, // A
new int[] {4, 0}, // B
new int[] {2, 2} // C (在 A-B 矩形內)
};
int expected = 2;
// Act
var result = _solution.NumberOfPairs(points);
// Assert
Assert.Equal(expected, result);
}
[Fact]
public void TestCase7_PointsOnBorder()
{
// Arrange - 點在矩形邊界上的情況
int[][] points = new int[][] {
new int[] {0, 3}, // A
new int[] {3, 0}, // B
new int[] {0, 0} // C (在矩形邊界上)
};
int expected = 2; // (A,C) 和 (C,B) 有效
// Act
var result = _solution.NumberOfPairs(points);
// Assert
Assert.Equal(expected, result);
}
[Fact]
public void TestEdgeCases_MinimumPoints()
{
// Arrange - 最少點數情況 (題目限制 n >= 2)
int[][] points = new int[][] {
new int[] {0, 1},
new int[] {1, 0}
};
int expected = 1; // (0,1) 在 (1,0) 的左上方
// Act
int result = _solution.NumberOfPairs(points);
// Assert
Assert.Equal(expected, result);
}
[Fact]
public void TestEdgeCases_IdenticalCoordinates()
{
// Arrange - 座標值相同的情況
int[][] points = new int[][] {
new int[] {2, 2},
new int[] {2, 2} // 題目保證所有點都不同,但測試邊界
};
// 這個測試案例實際上違反了題目條件 (All points[i] are distinct)
// 但可以測試程式的健壯性
int expected = 2; // 兩個相同點互相滿足左上方條件
// Act
int result = _solution.NumberOfPairs(points);
// Assert
Assert.Equal(expected, result);
}
[Fact]
public void TestEdgeCases_MaxCoordinateValues()
{
// Arrange - 最大座標值情況 (題目限制 0 <= coordinates <= 50)
int[][] points = new int[][] {
new int[] {0, 50},
new int[] {50, 0},
new int[] {25, 25}
};
int expected = 2; // (A,C) 和 (C,B) 有效
// Act
int result = _solution.NumberOfPairs(points);
// Assert
Assert.Equal(expected, result);
}
[Fact]
public void TestEdgeCases_EmptyArray()
{
// Arrange - 空陣列情況
int[][] points = new int[0][];
int expected = 0;
// Act
int result = _solution.NumberOfPairs(points);
// Assert
Assert.Equal(expected, result);
}
[Fact]
public void TestEdgeCases_MaxPoints_AllDistinct_RandomPattern() {
// Arrange - 50個不同的點更複雜的模式
var points = new List<int[]>();
// 創建一個螺旋模式的50個不同點確保都在 [0,50] 範圍內且互不相同
var usedPoints = new HashSet<string>();
var random = new Random(42); // 固定種子確保測試可重現
while (points.Count < 50) {
int x = random.Next(0, 51); // 0-50
int y = random.Next(0, 51); // 0-50
string pointKey = $"{x},{y}";
if (!usedPoints.Contains(pointKey)) {
usedPoints.Add(pointKey);
points.Add(new int[] { x, y });
}
}
int[][] pointsArray = points.ToArray();
// 由於是隨機分佈,很難預測確切結果,但應該是一個合理的正數
// 這個測試主要驗證程式在最大輸入時不會崩潰或超時
// Act
// var result = _solution.YourMethod(input);
int result = _solution.NumberOfPairs(pointsArray);
// Assert
// Assert.Equal(expected, result);
Assert.True(true); // 暫時通過,等待實作
}
[Fact]
public void TestCase2() {
// TODO: 第二個測試案例
Assert.True(true);
}
[Fact]
public void TestEdgeCases() {
// TODO: 邊界情況測試
// 空陣列、單一元素、最大值、最小值等
Assert.True(true);
Assert.True(result >= 0); // 至少結果應該是非負數
Assert.True(result <= 50 * 49); // 最多不會超過所有可能的點對
}
}

View File

@@ -1,21 +1,13 @@
# 邊界情況清單
## 需要測試的邊界情況
- [ ] 空輸入 (空陣列、空字串等)
- [ ] 單一元素
- [ ] 重複元素
- [ ] 最大值/最小值
- [ ] 負數情況
- [ ] 超大資料量
- [ ] 特殊字符 (如果是字串題目)
## 額外測試案例
### 案例 1:
**輸入**:
**輸出**:
**說明**:
### 案例 2:
**輸入**:
**輸出**:
**說明**:
- [x] 空輸入 (空陣列) - `TestEdgeCases_EmptyArray`
- [x] 最少元素 (n=2) - `TestEdgeCases_MinimumPoints`
- [x] 重複座標 (雖然違反題目條件) - `TestEdgeCases_IdenticalCoordinates`
- [x] 最大值/最小值 (座標範圍 [0,50]) - `TestEdgeCases_MaxCoordinateValues`
- [x] 最大資料量 (n=50) - `TestEdgeCases_MaxPoints_AllDistinct_RandomPattern`
- [x] 相同 x 座標 - `TestCase4_SameXCoordinate`
- [x] 相同 y 座標 - `TestCase5_SameYCoordinate`
- [x] 矩形內含其他點 - `TestCase6_PointsInsideRectangle`
- [x] 點在矩形邊界上 - `TestCase7_PointsOnBorder`
- [x] 無有效點對情況 - `TestCase3_NoValidPairs`