refactor(utils): improve TasStorage implementation and tests

- Change exception type from ArgumentOutOfRangeException to ArgumentException
- Fix node seeking logic by correcting candidate order
- Update Visit, Insert, and Remove methods with proper range checks
- Enhance cursor management after removal operations
- Add comprehensive test cases for edge scenarios
- Introduce AssertExtension for better exception testing
- Handle empty collection states more robustly
This commit is contained in:
2025-11-15 12:20:46 +08:00
parent 630365a6a6
commit df4a7252c1
4 changed files with 98 additions and 21 deletions

View File

@ -16,7 +16,7 @@ namespace BallanceTasEditor.Utils {
/// </summary>
/// <param name="index">要访问的单元的索引。</param>
/// <returns>被访问的单元。</returns>
/// <exception cref="ArgumentOutOfRangeException">给定的索引超出范围。</exception>
/// <exception cref="ArgumentException">给定的索引超出范围。</exception>
T Visit(int index);
/// <summary>
/// 在给定的索引<b>之前</b>插入给定的项目。
@ -29,14 +29,14 @@ namespace BallanceTasEditor.Utils {
/// </remarks>
/// <param name="index">要在前方插入数据的元素的索引。</param>
/// <param name="items">要插入的元素的迭代器。</param>
/// <exception cref="ArgumentOutOfRangeException">给定的索引超出范围。</exception>
/// <exception cref="ArgumentException">给定的索引超出范围。</exception>
void Insert(int index, IEnumerable<T> items);
/// <summary>
/// 从给定单元开始,移除给定个数的元素。
/// </summary>
/// <param name="index">要开始移除的单元的索引。</param>
/// <param name="count">要移除的元素的个数。</param>
/// <exception cref="ArgumentOutOfRangeException">给定的索引超出范围。</exception>
/// <exception cref="ArgumentException">给定的索引超出范围。</exception>
void Remove(int index, int count);
/// <summary>
@ -154,7 +154,7 @@ namespace BallanceTasEditor.Utils {
public int Offset;
public int CompareTo(NodeSeekInfo other) {
return this.Offset.CompareTo(other.Offset);
return Math.Abs(this.Offset).CompareTo(Math.Abs(other.Offset));
}
}
@ -173,8 +173,8 @@ namespace BallanceTasEditor.Utils {
// 创建三个候选方案。
var candidates = new NodeSeekInfo[3] {
new NodeSeekInfo() { Origin = NodeSeekOrigin.Head, Offset = desiredIndex },
new NodeSeekInfo() { Origin = NodeSeekOrigin.Cursor, Offset = desiredIndex - (GetCount() - 1) },
new NodeSeekInfo() { Origin = NodeSeekOrigin.Tail, Offset = desiredIndex - m_CursorIndex.Value },
new NodeSeekInfo() { Origin = NodeSeekOrigin.Tail, Offset = desiredIndex - (GetCount() - 1) },
new NodeSeekInfo() { Origin = NodeSeekOrigin.Cursor, Offset = desiredIndex - m_CursorIndex.Value },
};
// 确定哪个候选方案最短。
var bestCandidate = candidates.Min();
@ -211,15 +211,25 @@ namespace BallanceTasEditor.Utils {
}
public T Visit(int index) {
if (index < 0 || index >= GetCount()) {
throw new ArgumentOutOfRangeException("Index out of range.");
} else {
MoveToIndex(index);
return m_Cursor.Value;
}
}
public void Insert(int index, IEnumerable<T> items) {
if (index == GetCount()) {
if (index < 0 || index > GetCount()) {
throw new ArgumentOutOfRangeException("Index out of range.");
} else if (index == GetCount()) {
foreach (T item in items) {
m_Container.AddLast(item);
}
m_Cursor = m_Container.First;
if (m_Cursor is null) m_CursorIndex = null;
else m_CursorIndex = 0;
} else {
MoveToIndex(index);
@ -233,7 +243,9 @@ namespace BallanceTasEditor.Utils {
}
public void Remove(int index, int count) {
if (index + count >= GetCount())
if (count == 0)
return;
if (index + count > GetCount())
throw new ArgumentOutOfRangeException("Expected removed items out of range.");
MoveToIndex(index);
@ -253,16 +265,22 @@ namespace BallanceTasEditor.Utils {
}
// 然后设置Cursor和Index
// 如果全部删完了,就清除这两个的设置。
// 否则就以prevNode为当前CursorIndex--为对应Index。
if (IsEmpty()) {
// 如果全部删完了,就清除这两个的设置。
m_Cursor = null;
m_CursorIndex = null;
} else {
if (prevNode is null) {
// 如果是按头部删除的则直接获取头部及其Index。
m_Cursor = m_Container.First;
m_CursorIndex = 0;
} else {
// 否则就以prevNode为当前CursorIndex--为对应Index。
m_Cursor = prevNode;
--m_CursorIndex;
}
}
}
public void Clear() {
m_Container.Clear();

View File

@ -0,0 +1,28 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BallanceTasEditorTests {
public static class AssertExtension {
public static T ThrowsDerivedException<T>(Action action) where T : Exception {
try {
action();
} catch (T ex) {
return ex;
} catch (Exception ex) {
if (ex is T derivedEx)
return derivedEx;
throw new AssertFailedException(
$"Expected exception of type {typeof(T)} or derived type, but got {ex.GetType()}. " +
$"Message: {ex.Message}");
}
throw new AssertFailedException(
$"Expected exception of type {typeof(T)} or derived type, but no exception was thrown.");
}
}
}

View File

@ -49,6 +49,7 @@
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="AssertExtension.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utils\TasStorageTests.cs" />
</ItemGroup>

View File

@ -10,6 +10,7 @@ namespace BallanceTasEditorTests.Utils {
[TestClass]
public class TasStorageTests {
private static readonly int[] BLANK = { };
private static readonly int[] PROBE = { 10, 20, 30, 40, 50 };
private static IEnumerable<object[]> TasStorageInstanceProvider {
@ -28,18 +29,18 @@ namespace BallanceTasEditorTests.Utils {
[DynamicData(nameof(TasStorageInstanceProvider))]
public void VisitTest(ITasStorage<int> storage) {
// 空时访问
Assert.ThrowsException<ArgumentOutOfRangeException>(() => storage.Visit(-1));
Assert.ThrowsException<ArgumentOutOfRangeException>(() => storage.Visit(0));
Assert.ThrowsException<ArgumentOutOfRangeException>(() => storage.Visit(1));
AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Visit(-1));
AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Visit(0));
AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Visit(1));
// 设置数据
storage.Insert(0, PROBE);
// 访问数据
Assert.ThrowsException<ArgumentOutOfRangeException>(() => storage.Visit(-1));
AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Visit(-1));
for (int i = 0; i < PROBE.Length; i++) {
Assert.AreEqual(storage.Visit(i), PROBE[i]);
}
Assert.ThrowsException<ArgumentOutOfRangeException>(() => storage.Visit(PROBE.Length));
AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Visit(PROBE.Length));
}
/// <summary>
@ -52,8 +53,8 @@ namespace BallanceTasEditorTests.Utils {
// 和在非空时的头,中,尾分别插入的结果。
// 先检测空插入
Assert.ThrowsException<ArgumentOutOfRangeException>(() => storage.Insert(-1, PROBE));
Assert.ThrowsException<ArgumentOutOfRangeException>(() => storage.Insert(1, PROBE));
AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Insert(-1, PROBE));
AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Insert(1, PROBE));
storage.Insert(0, PROBE);
for (int i = 0; i < PROBE.Length; i++) {
Assert.AreEqual(storage.Visit(i), PROBE[i]);
@ -78,6 +79,7 @@ namespace BallanceTasEditorTests.Utils {
Assert.AreEqual(storage.Visit(i), expected[i]);
}
}
}
/// <summary>
@ -86,7 +88,28 @@ namespace BallanceTasEditorTests.Utils {
[TestMethod]
[DynamicData(nameof(TasStorageInstanceProvider))]
public void RemoveTest(ITasStorage<int> storage) {
// 在空的时候删除0项
storage.Remove(0, 0);
// 插入项目后尝试在头中尾分别删除
var indices = new int[] { 0, PROBE.Length / 2, PROBE.Length - 1 };
foreach (var index in indices) {
// 清空,插入,删除
storage.Clear();
storage.Insert(0, PROBE);
storage.Remove(index, 1);
// 用List做正确模拟
var expected = new List<int>();
expected.AddRange(PROBE);
expected.RemoveRange(index, 1);
// 检查结果
Assert.AreEqual(storage.GetCount(), expected.Count);
for (int i = 0; i < expected.Count; i++) {
Assert.AreEqual(storage.Visit(i), expected[i]);
}
}
}
/// <summary>
@ -159,6 +182,13 @@ namespace BallanceTasEditorTests.Utils {
// 再次检查数据
Assert.IsTrue(storage.IsEmpty());
Assert.AreEqual(storage.GetCount(), 0);
// 清空后插入0项然后确认
storage.Clear();
storage.Insert(0, BLANK);
AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Visit(-1));
AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Visit(0));
AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Visit(1));
}
}
}