Skip to content

Commit aa868cf

Browse files
committed
Update csharp array to unify code style,and create its unit test.
1 parent 5c99993 commit aa868cf

File tree

2 files changed

+82
-47
lines changed

2 files changed

+82
-47
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
/*
2-
* File: Array.cs
3-
* Created Time: 2022-12-14
4-
* Author: mingXta ([email protected])
5-
*/
6-
1+
// File: Array.cs
2+
// Created Time: 2022-12-14
3+
// Author: mingXta ([email protected])
4+
75
namespace hello_algo.chapter_array_and_linkedlist
86
{
97
public class Array
108
{
11-
/* 随机返回一个数组元素 */
9+
//随机返回一个数组元素
1210
public static int RandomAccess(int[] nums)
1311
{
1412
Random random = new();
@@ -17,7 +15,7 @@ public static int RandomAccess(int[] nums)
1715
return randomNum;
1816
}
1917

20-
/* 扩展数组长度 */
18+
//扩展数组长度
2119
public static int[] Extend(int[] nums, int enlarge)
2220
{
2321
// 初始化一个扩展长度后的数组
@@ -31,7 +29,7 @@ public static int[] Extend(int[] nums, int enlarge)
3129
return res;
3230
}
3331

34-
/* 在数组的索引 index 处插入元素 num */
32+
//在数组的索引 index 处插入元素 num
3533
public static void Insert(int[] nums, int num, int index)
3634
{
3735
// 把索引 index 以及之后的所有元素向后移动一位
@@ -43,7 +41,7 @@ public static void Insert(int[] nums, int num, int index)
4341
nums[index] = num;
4442
}
4543

46-
/* 删除索引 index 处元素 */
44+
//删除索引 index 处元素
4745
public static void Remove(int[] nums, int index)
4846
{
4947
// 把索引 index 之后的所有元素向前移动一位
@@ -53,7 +51,7 @@ public static void Remove(int[] nums, int index)
5351
}
5452
}
5553

56-
/* 遍历数组 */
54+
//遍历数组
5755
public static void Traverse(int[] nums)
5856
{
5957
int count = 0;
@@ -69,7 +67,7 @@ public static void Traverse(int[] nums)
6967
}
7068
}
7169

72-
/* 在数组中查找指定元素 */
70+
//在数组中查找指定元素
7371
public static int Find(int[] nums, int target)
7472
{
7573
for (int i = 0; i < nums.Length; i++)
@@ -80,43 +78,10 @@ public static int Find(int[] nums, int target)
8078
return -1;
8179
}
8280

83-
/*辅助函数,数组转字符串 */
81+
//辅助函数,数组转字符串
8482
public static string ToString(int[] nums)
8583
{
8684
return string.Join(",", nums);
8785
}
88-
89-
/* Driver Code */
90-
public static void Main()
91-
{
92-
/* 初始化数组 */
93-
int[] arr = new int[5];
94-
Console.WriteLine("数组 arr = " + ToString(arr));
95-
int[] nums = { 1, 3, 2, 5, 4 };
96-
Console.WriteLine("数组 nums = " + ToString(nums));
97-
98-
/* 随机访问 */
99-
int randomNum = RandomAccess(nums);
100-
Console.WriteLine("在 nums 中获取随机元素 " + randomNum);
101-
102-
/* 长度扩展 */
103-
nums = Extend(nums, 3);
104-
Console.WriteLine("将数组长度扩展至 8 ,得到 nums = " + ToString(nums));
105-
106-
/* 插入元素 */
107-
Insert(nums, 6, 3);
108-
Console.WriteLine("在索引 3 处插入数字 6 ,得到 nums = " + ToString(nums));
109-
110-
/* 删除元素 */
111-
Remove(nums, 2);
112-
Console.WriteLine("删除索引 2 处的元素,得到 nums = " + ToString(nums));
113-
114-
/* 遍历数组 */
115-
Traverse(nums);
116-
117-
/* 查找元素 */
118-
int index = Find(nums, 3);
119-
Console.WriteLine("在 nums 中查找元素 3 ,得到索引 = " + index);
120-
}
12186
}
122-
}
87+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// File: ArrayTest.cs
2+
// Created Time: 2022-12-16
3+
// Author: mingXta ([email protected])
4+
5+
using NUnit.Framework;
6+
using Array = hello_algo.chapter_array_and_linkedlist.Array;
7+
8+
namespace hello_algo.Test.chapter_array_and_linkedlist
9+
{
10+
[TestFixture]
11+
internal class ArrayTest
12+
{
13+
private int[] nums;
14+
15+
[SetUp]
16+
public void setup()
17+
{
18+
//初始化数组
19+
nums = new int[] { 1, 3, 2, 5, 4 };
20+
}
21+
22+
[Test]
23+
public void TestRandomAccess()
24+
{
25+
//随机访问
26+
int randomNum = Array.RandomAccess(nums);
27+
Console.WriteLine($"在 nums 中获取随机元素 {randomNum}");
28+
Assert.Contains(randomNum, nums);
29+
}
30+
31+
[Test]
32+
public void TestExtend()
33+
{
34+
//长度扩展
35+
int[] target = { 1, 3, 2, 5, 4, 0, 0, 0 };
36+
nums = Array.Extend(nums, 3);
37+
Console.WriteLine($"将数组长度扩展至 8 ,得到 nums = {Array.ToString(nums)}");
38+
Assert.AreEqual(target, nums);
39+
}
40+
41+
[Test]
42+
public void TestInsert()
43+
{
44+
//插入元素
45+
int[] target = { 1, 3, 2, 6, 5 };
46+
Array.Insert(nums, 6, 3);
47+
Console.WriteLine($"在索引 3 处插入数字 6 ,得到 nums = {Array.ToString(nums)}");
48+
Assert.AreEqual(target, nums);
49+
}
50+
51+
[Test]
52+
public void TestRemove()
53+
{
54+
//删除元素
55+
int[] target = { 1, 3, 5, 4, 4 };
56+
Array.Remove(nums, 2);
57+
Console.WriteLine($"删除索引 2 处的元素,得到 nums = {Array.ToString(nums)}");
58+
Assert.AreEqual(target, nums);
59+
}
60+
61+
[Test]
62+
public void TestFind()
63+
{
64+
//查找元素
65+
int index = Array.Find(nums, 3);
66+
Console.WriteLine("在 nums 中查找元素 3 , 得到索引 = " + index);
67+
Assert.AreEqual(1, index);
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)