|
|
@ -5,7 +5,7 @@
|
|
|
|
using NUnit.Framework;
|
|
|
|
using NUnit.Framework;
|
|
|
|
using Array = hello_algo.chapter_array_and_linkedlist.Array;
|
|
|
|
using Array = hello_algo.chapter_array_and_linkedlist.Array;
|
|
|
|
|
|
|
|
|
|
|
|
namespace hello_algo.Test.chapter_array_and_linkedlist
|
|
|
|
namespace hello_algo.test.chapter_array_and_linkedlist
|
|
|
|
{
|
|
|
|
{
|
|
|
|
[TestFixture]
|
|
|
|
[TestFixture]
|
|
|
|
internal class ArrayTest
|
|
|
|
internal class ArrayTest
|
|
|
@ -15,14 +15,14 @@ namespace hello_algo.Test.chapter_array_and_linkedlist
|
|
|
|
[SetUp]
|
|
|
|
[SetUp]
|
|
|
|
public void setup()
|
|
|
|
public void setup()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
//初始化数组
|
|
|
|
// 初始化数组
|
|
|
|
nums = new int[] { 1, 3, 2, 5, 4 };
|
|
|
|
nums = new int[] { 1, 3, 2, 5, 4 };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
[Test]
|
|
|
|
public void TestRandomAccess()
|
|
|
|
public void TestRandomAccess()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
//随机访问
|
|
|
|
// 随机访问
|
|
|
|
int randomNum = Array.RandomAccess(nums);
|
|
|
|
int randomNum = Array.RandomAccess(nums);
|
|
|
|
Console.WriteLine($"在 nums 中获取随机元素 {randomNum}");
|
|
|
|
Console.WriteLine($"在 nums 中获取随机元素 {randomNum}");
|
|
|
|
Assert.Contains(randomNum, nums);
|
|
|
|
Assert.Contains(randomNum, nums);
|
|
|
@ -31,7 +31,7 @@ namespace hello_algo.Test.chapter_array_and_linkedlist
|
|
|
|
[Test]
|
|
|
|
[Test]
|
|
|
|
public void TestExtend()
|
|
|
|
public void TestExtend()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
//长度扩展
|
|
|
|
// 长度扩展
|
|
|
|
int[] target = { 1, 3, 2, 5, 4, 0, 0, 0 };
|
|
|
|
int[] target = { 1, 3, 2, 5, 4, 0, 0, 0 };
|
|
|
|
nums = Array.Extend(nums, 3);
|
|
|
|
nums = Array.Extend(nums, 3);
|
|
|
|
Console.WriteLine($"将数组长度扩展至 8 ,得到 nums = {Array.ToString(nums)}");
|
|
|
|
Console.WriteLine($"将数组长度扩展至 8 ,得到 nums = {Array.ToString(nums)}");
|
|
|
@ -41,7 +41,7 @@ namespace hello_algo.Test.chapter_array_and_linkedlist
|
|
|
|
[Test]
|
|
|
|
[Test]
|
|
|
|
public void TestInsert()
|
|
|
|
public void TestInsert()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
//插入元素
|
|
|
|
// 插入元素
|
|
|
|
int[] target = { 1, 3, 2, 6, 5 };
|
|
|
|
int[] target = { 1, 3, 2, 6, 5 };
|
|
|
|
Array.Insert(nums, 6, 3);
|
|
|
|
Array.Insert(nums, 6, 3);
|
|
|
|
Console.WriteLine($"在索引 3 处插入数字 6 ,得到 nums = {Array.ToString(nums)}");
|
|
|
|
Console.WriteLine($"在索引 3 处插入数字 6 ,得到 nums = {Array.ToString(nums)}");
|
|
|
@ -51,7 +51,7 @@ namespace hello_algo.Test.chapter_array_and_linkedlist
|
|
|
|
[Test]
|
|
|
|
[Test]
|
|
|
|
public void TestRemove()
|
|
|
|
public void TestRemove()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
//删除元素
|
|
|
|
// 删除元素
|
|
|
|
int[] target = { 1, 3, 5, 4, 4 };
|
|
|
|
int[] target = { 1, 3, 5, 4, 4 };
|
|
|
|
Array.Remove(nums, 2);
|
|
|
|
Array.Remove(nums, 2);
|
|
|
|
Console.WriteLine($"删除索引 2 处的元素,得到 nums = {Array.ToString(nums)}");
|
|
|
|
Console.WriteLine($"删除索引 2 处的元素,得到 nums = {Array.ToString(nums)}");
|
|
|
@ -61,7 +61,7 @@ namespace hello_algo.Test.chapter_array_and_linkedlist
|
|
|
|
[Test]
|
|
|
|
[Test]
|
|
|
|
public void TestFind()
|
|
|
|
public void TestFind()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
//查找元素
|
|
|
|
// 查找元素
|
|
|
|
int index = Array.Find(nums, 3);
|
|
|
|
int index = Array.Find(nums, 3);
|
|
|
|
Console.WriteLine("在 nums 中查找元素 3 , 得到索引 = " + index);
|
|
|
|
Console.WriteLine("在 nums 中查找元素 3 , 得到索引 = " + index);
|
|
|
|
Assert.AreEqual(1, index);
|
|
|
|
Assert.AreEqual(1, index);
|
|
|
|