245 lines
6.1 KiB
C#
245 lines
6.1 KiB
C#
// LeetCode 3025 單元測試
|
||
// 使用 xUnit 框架
|
||
|
||
using Xunit;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
|
||
public class SolutionTests
|
||
{
|
||
private readonly Solution _solution;
|
||
|
||
public SolutionTests()
|
||
{
|
||
_solution = new Solution();
|
||
}
|
||
|
||
[Fact]
|
||
public void TestCase1_ExampleFromProblem()
|
||
{
|
||
// Arrange
|
||
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
|
||
int result = _solution.NumberOfPairs(pointsArray);
|
||
|
||
// Assert
|
||
Assert.True(result >= 0); // 至少結果應該是非負數
|
||
Assert.True(result <= 50 * 49); // 最多不會超過所有可能的點對
|
||
}
|
||
}
|