diff --git a/BallanceTasEditor/Utils/TasStorage.cs b/BallanceTasEditor/Utils/TasStorage.cs
index be1e7b3..4b07531 100644
--- a/BallanceTasEditor/Utils/TasStorage.cs
+++ b/BallanceTasEditor/Utils/TasStorage.cs
@@ -16,7 +16,7 @@ namespace BallanceTasEditor.Utils {
///
/// 要访问的单元的索引。
/// 被访问的单元。
- /// 给定的索引超出范围。
+ /// 给定的索引超出范围。
T Visit(int index);
///
/// 在给定的索引之前插入给定的项目。
@@ -29,14 +29,14 @@ namespace BallanceTasEditor.Utils {
///
/// 要在前方插入数据的元素的索引。
/// 要插入的元素的迭代器。
- /// 给定的索引超出范围。
+ /// 给定的索引超出范围。
void Insert(int index, IEnumerable items);
///
/// 从给定单元开始,移除给定个数的元素。
///
/// 要开始移除的单元的索引。
/// 要移除的元素的个数。
- /// 给定的索引超出范围。
+ /// 给定的索引超出范围。
void Remove(int index, int count);
///
@@ -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) {
- MoveToIndex(index);
- return m_Cursor.Value;
+ 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 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,14 +265,20 @@ namespace BallanceTasEditor.Utils {
}
// 然后设置Cursor和Index
- // 如果全部删完了,就清除这两个的设置。
- // 否则就以prevNode为当前Cursor,Index--为对应Index。
if (IsEmpty()) {
+ // 如果全部删完了,就清除这两个的设置。
m_Cursor = null;
m_CursorIndex = null;
} else {
- m_Cursor = prevNode;
- --m_CursorIndex;
+ if (prevNode is null) {
+ // 如果是按头部删除的,则直接获取头部及其Index。
+ m_Cursor = m_Container.First;
+ m_CursorIndex = 0;
+ } else {
+ // 否则就以prevNode为当前Cursor,Index--为对应Index。
+ m_Cursor = prevNode;
+ --m_CursorIndex;
+ }
}
}
diff --git a/BallanceTasEditorTests/AssertExtension.cs b/BallanceTasEditorTests/AssertExtension.cs
new file mode 100644
index 0000000..ca2e247
--- /dev/null
+++ b/BallanceTasEditorTests/AssertExtension.cs
@@ -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(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.");
+ }
+ }
+}
diff --git a/BallanceTasEditorTests/BallanceTasEditorTests.csproj b/BallanceTasEditorTests/BallanceTasEditorTests.csproj
index 5edd915..97453ba 100644
--- a/BallanceTasEditorTests/BallanceTasEditorTests.csproj
+++ b/BallanceTasEditorTests/BallanceTasEditorTests.csproj
@@ -49,6 +49,7 @@
+
diff --git a/BallanceTasEditorTests/Utils/TasStorageTests.cs b/BallanceTasEditorTests/Utils/TasStorageTests.cs
index 09995e5..2aab09d 100644
--- a/BallanceTasEditorTests/Utils/TasStorageTests.cs
+++ b/BallanceTasEditorTests/Utils/TasStorageTests.cs
@@ -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