[ 測試撰寫 ] LeetCode 56. Merge Intervals #130
Answered
by
twy30
LPenny-github
asked this question in
Q&A
-
|
相關 Discussions: LeetCode: 再麻煩 @twy30 給予 測試撰寫的建議 orz 感激不盡 using Xunit;
public class UnitTestMergeIntervals
{
[Fact]
public void LeetCodeExample1()
{
int[][] input = new int[4][];
input[0] = new[] { 1, 3 };
input[1] = new[] { 2, 6 };
input[2] = new[] { 8, 10 };
input[3] = new[] { 15, 18 };
int[][] expected = new int[3][];
expected[0] = new[] { 1, 6 };
expected[1] = new[] { 8, 10 };
expected[2] = new[] { 15, 18 };
int[][] actual = new MergeIntervals().Merge(input);
Assert.Equal(expected.Length, actual.Length);
Assert.Equal(expected[0][0], actual[0][0]);
Assert.Equal(expected[0][1], actual[0][1]);
Assert.Equal(expected[1][0], actual[1][0]);
Assert.Equal(expected[1][1], actual[1][1]);
Assert.Equal(expected[2][0], actual[2][0]);
Assert.Equal(expected[2][1], actual[2][1]);
}
[Fact]
public void LeetCodeExample2()
{
int[][] input = new int[2][];
input[0] = new[] { 1, 4 };
input[1] = new[] { 4, 5 };
int[][] expected = new int[1][];
expected[0] = new[] { 1, 5 };
int[][] actual = new MergeIntervals().Merge(input);
Assert.Equal(expected.Length, actual.Length);
Assert.Equal(expected[0][0], actual[0][0]);
Assert.Equal(expected[0][1], actual[0][1]);
}
[Fact]
public void LeetCodeExample3()
{
// 答錯所獲得的範例
int[][] input = new int[3][];
input[0] = new[] { 1, 4 };
input[1] = new[] { 0, 2 };
input[2] = new[] { 3, 5 };
int[][] expected = new int[1][];
expected[0] = new[] { 0, 5 };
int[][] actual = new MergeIntervals().Merge(input);
Assert.Equal(expected.Length, actual.Length);
Assert.Equal(expected[0][0], actual[0][0]);
Assert.Equal(expected[0][1], actual[0][1]);
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
twy30
Feb 25, 2021
Replies: 1 comment 1 reply
-
|
你好 😊
public static void Equal<T>(IEnumerable<T> expected, IEnumerable<T> actual)及 https://github.com/xunit/assert.xunit/blob/2.4.1/CollectionAsserts.cs#L306 public static void Equal<T>(IEnumerable<T> expected, IEnumerable<T> actual, IEqualityComparer<T> comparer)以下程式碼我沒有實際測試過,然而,以 Assert.Equal(expected, actual, new IntervalComparer); |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
LPenny-github
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@LPenny-github
你好 😊
Xunit支援「比對IEnumerable<T>」 ( https://github.com/xunit/assert.xunit/blob/2.4.1/CollectionAsserts.cs#L293 )及 https://github.com/xunit/assert.xunit/blob/2.4.1/CollectionAsserts.cs#L306
以下程式碼我沒有實際測試過,然而,以
LeetCodeExample1()為例,應該可以改成