diff --git a/BallanceTasEditor.sln b/BallanceTasEditor.sln index 4625436..e32fb19 100644 --- a/BallanceTasEditor.sln +++ b/BallanceTasEditor.sln @@ -5,7 +5,7 @@ VisualStudioVersion = 17.14.36414.22 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BallanceTasEditor", "BallanceTasEditor\BallanceTasEditor.csproj", "{DD898514-03ED-4257-AFD1-290EEDF68113}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BallanceTasEditorTest", "BallanceTasEditorTest\BallanceTasEditorTest.csproj", "{1DC1A8CD-E963-4FF8-91D9-059522419961}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BallanceTasEditorTests", "BallanceTasEditorTests\BallanceTasEditorTests.csproj", "{D2E825CE-691B-48D7-8D87-D2CED1B25FF9}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -17,10 +17,10 @@ Global {DD898514-03ED-4257-AFD1-290EEDF68113}.Debug|Any CPU.Build.0 = Debug|Any CPU {DD898514-03ED-4257-AFD1-290EEDF68113}.Release|Any CPU.ActiveCfg = Release|Any CPU {DD898514-03ED-4257-AFD1-290EEDF68113}.Release|Any CPU.Build.0 = Release|Any CPU - {1DC1A8CD-E963-4FF8-91D9-059522419961}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {1DC1A8CD-E963-4FF8-91D9-059522419961}.Debug|Any CPU.Build.0 = Debug|Any CPU - {1DC1A8CD-E963-4FF8-91D9-059522419961}.Release|Any CPU.ActiveCfg = Release|Any CPU - {1DC1A8CD-E963-4FF8-91D9-059522419961}.Release|Any CPU.Build.0 = Release|Any CPU + {D2E825CE-691B-48D7-8D87-D2CED1B25FF9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D2E825CE-691B-48D7-8D87-D2CED1B25FF9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D2E825CE-691B-48D7-8D87-D2CED1B25FF9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D2E825CE-691B-48D7-8D87-D2CED1B25FF9}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/BallanceTasEditor/BallanceTasEditor.csproj b/BallanceTasEditor/BallanceTasEditor.csproj index 9b868b0..e8605ce 100644 --- a/BallanceTasEditor/BallanceTasEditor.csproj +++ b/BallanceTasEditor/BallanceTasEditor.csproj @@ -99,7 +99,7 @@ - + MainWindow.xaml Code diff --git a/BallanceTasEditor/Utils/TasMemory.cs b/BallanceTasEditor/Utils/TasStorage.cs similarity index 95% rename from BallanceTasEditor/Utils/TasMemory.cs rename to BallanceTasEditor/Utils/TasStorage.cs index 16da0e2..be1e7b3 100644 --- a/BallanceTasEditor/Utils/TasMemory.cs +++ b/BallanceTasEditor/Utils/TasStorage.cs @@ -10,7 +10,7 @@ namespace BallanceTasEditor.Utils { /// /// 所有用于在内存中存储TAS帧的结构都必须实现此interface。 /// - public interface ITasMemory where T : class { + public interface ITasStorage { /// /// 访问给定索引的值。 /// @@ -61,8 +61,8 @@ namespace BallanceTasEditor.Utils { /// /// 其实就是把List的InsertRange的复杂度从O(n*m)修正为O(n)。 /// - public class GapBufferTasMemory : ITasMemory where T : class { - public GapBufferTasMemory() { + public class GapBufferTasStorage : ITasStorage { + public GapBufferTasStorage() { } @@ -97,8 +97,8 @@ namespace BallanceTasEditor.Utils { /// /// 由于List的InsertRange的复杂度是O(n*m),可能不符合要求。 /// - public class ListTasMemory : ITasMemory where T : class { - public ListTasMemory() { + public class ListTasStorage : ITasStorage { + public ListTasStorage() { m_Container = new List(); } @@ -132,8 +132,8 @@ namespace BallanceTasEditor.Utils { /// /// 传统的基于LinkedList的TAS存储器。 /// - public class LegacyTasMemory : ITasMemory where T : class { - public LegacyTasMemory() { + public class LegacyTasStorage : ITasStorage { + public LegacyTasStorage() { m_Container = new LinkedList(); m_Cursor = null; m_CursorIndex = null; @@ -165,7 +165,7 @@ namespace BallanceTasEditor.Utils { /// private void MoveToIndex(int desiredIndex) { // 检查基本环境 - if (desiredIndex >= GetCount()) + if (desiredIndex < 0 || desiredIndex >= GetCount()) throw new ArgumentOutOfRangeException("Index out of range"); if (m_Cursor is null || !m_CursorIndex.HasValue || IsEmpty()) throw new InvalidOperationException("Can not move cursor when container is empty."); diff --git a/BallanceTasEditorTest/UnitTest1.cs b/BallanceTasEditorTest/UnitTest1.cs deleted file mode 100644 index 59ed8e9..0000000 --- a/BallanceTasEditorTest/UnitTest1.cs +++ /dev/null @@ -1,11 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System; - -namespace BallanceTasEditorTest { - [TestClass] - public class UnitTest1 { - [TestMethod] - public void TestMethod1() { - } - } -} diff --git a/BallanceTasEditorTest/BallanceTasEditorTest.csproj b/BallanceTasEditorTests/BallanceTasEditorTests.csproj similarity index 95% rename from BallanceTasEditorTest/BallanceTasEditorTest.csproj rename to BallanceTasEditorTests/BallanceTasEditorTests.csproj index 2f059d8..5edd915 100644 --- a/BallanceTasEditorTest/BallanceTasEditorTest.csproj +++ b/BallanceTasEditorTests/BallanceTasEditorTests.csproj @@ -5,11 +5,11 @@ Debug AnyCPU - {1DC1A8CD-E963-4FF8-91D9-059522419961} + {D2E825CE-691B-48D7-8D87-D2CED1B25FF9} Library Properties - BallanceTasEditorTest - BallanceTasEditorTest + BallanceTasEditorTests + BallanceTasEditorTests v4.8 512 {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} @@ -49,8 +49,8 @@ - + diff --git a/BallanceTasEditorTest/Properties/AssemblyInfo.cs b/BallanceTasEditorTests/Properties/AssemblyInfo.cs similarity index 74% rename from BallanceTasEditorTest/Properties/AssemblyInfo.cs rename to BallanceTasEditorTests/Properties/AssemblyInfo.cs index 4363650..84551d8 100644 --- a/BallanceTasEditorTest/Properties/AssemblyInfo.cs +++ b/BallanceTasEditorTests/Properties/AssemblyInfo.cs @@ -2,18 +2,18 @@ using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -[assembly: AssemblyTitle("BallanceTasEditorTest")] +[assembly: AssemblyTitle("BallanceTasEditorTests")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("BallanceTasEditorTest")] +[assembly: AssemblyProduct("BallanceTasEditorTests")] [assembly: AssemblyCopyright("Copyright © 2025")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] [assembly: ComVisible(false)] -[assembly: Guid("1dc1a8cd-e963-4ff8-91d9-059522419961")] +[assembly: Guid("d2e825ce-691b-48d7-8d87-d2ced1b25ff9")] // [assembly: AssemblyVersion("1.0.*")] [assembly: AssemblyVersion("1.0.0.0")] diff --git a/BallanceTasEditorTests/Utils/TasStorageTests.cs b/BallanceTasEditorTests/Utils/TasStorageTests.cs new file mode 100644 index 0000000..09995e5 --- /dev/null +++ b/BallanceTasEditorTests/Utils/TasStorageTests.cs @@ -0,0 +1,164 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using BallanceTasEditor.Utils; + +namespace BallanceTasEditorTests.Utils { + [TestClass] + public class TasStorageTests { + + private static readonly int[] PROBE = { 10, 20, 30, 40, 50 }; + + private static IEnumerable TasStorageInstanceProvider { + get { + yield return new object[] { new ListTasStorage() }; + yield return new object[] { new LegacyTasStorage() }; + // TODO: Add GapBufferTasStorage once we finish it. + //yield return new object[] { new GapBufferTasStorage() }; + } + } + + /// + /// Visit函数独立测试。 + /// + [TestMethod] + [DynamicData(nameof(TasStorageInstanceProvider))] + public void VisitTest(ITasStorage storage) { + // 空时访问 + Assert.ThrowsException(() => storage.Visit(-1)); + Assert.ThrowsException(() => storage.Visit(0)); + Assert.ThrowsException(() => storage.Visit(1)); + + // 设置数据 + storage.Insert(0, PROBE); + // 访问数据 + Assert.ThrowsException(() => storage.Visit(-1)); + for (int i = 0; i < PROBE.Length; i++) { + Assert.AreEqual(storage.Visit(i), PROBE[i]); + } + Assert.ThrowsException(() => storage.Visit(PROBE.Length)); + } + + /// + /// Insert函数独立测试。 + /// + [TestMethod] + [DynamicData(nameof(TasStorageInstanceProvider))] + public void InsertTest(ITasStorage storage) { + // 需要在不同的存储器上,分别检测在空的时候插入, + // 和在非空时的头,中,尾分别插入的结果。 + + // 先检测空插入 + Assert.ThrowsException(() => storage.Insert(-1, PROBE)); + Assert.ThrowsException(() => storage.Insert(1, PROBE)); + storage.Insert(0, PROBE); + for (int i = 0; i < PROBE.Length; i++) { + Assert.AreEqual(storage.Visit(i), PROBE[i]); + } + + // 再检测有数据的插入,分别在头尾和中间进行 + var indices = new int[] { 0, PROBE.Length / 2, PROBE.Length - 1, PROBE.Length }; + foreach (var index in indices) { + // 清空,一次插入,然后二次插入 + storage.Clear(); + storage.Insert(0, PROBE); + storage.Insert(index, PROBE); + + // 用List做正确模拟 + var expected = new List(); + expected.AddRange(PROBE); + expected.InsertRange(index, PROBE); + + // 检查结果 + Assert.AreEqual(storage.GetCount(), expected.Count); + for (int i = 0; i < expected.Count; i++) { + Assert.AreEqual(storage.Visit(i), expected[i]); + } + } + } + + /// + /// Remove函数独立测试。 + /// + [TestMethod] + [DynamicData(nameof(TasStorageInstanceProvider))] + public void RemoveTest(ITasStorage storage) { + + } + + /// + /// Clear函数独立测试。 + /// + [TestMethod] + [DynamicData(nameof(TasStorageInstanceProvider))] + public void ClearTest(ITasStorage storage) { + // 设置数据后清空 + storage.Insert(0, PROBE); + storage.Clear(); + + // 检查是否为空 + Assert.IsTrue(storage.IsEmpty()); + } + + /// + /// IsEmpty函数独立测试。 + /// + [TestMethod] + [DynamicData(nameof(TasStorageInstanceProvider))] + public void IsEmptyTest(ITasStorage storage) { + // 检查是否为空 + Assert.IsTrue(storage.IsEmpty()); + + // 插入数据后再检查 + storage.Insert(0, PROBE); + Assert.IsFalse(storage.IsEmpty()); + } + + /// + /// GetCount函数独立测试。 + /// + [TestMethod] + [DynamicData(nameof(TasStorageInstanceProvider))] + public void GetCountTest(ITasStorage storage) { + // 检查长度为0 + Assert.AreEqual(storage.GetCount(), 0); + + // 插入数据后再检查 + storage.Insert(0, PROBE); + Assert.AreEqual(storage.GetCount(), PROBE.Length); + } + + /// + /// 混合检查Visit,Clear,GetCount,IsEmpty。 + /// + /// + [TestMethod] + [DynamicData(nameof(TasStorageInstanceProvider))] + public void HybridTest(ITasStorage storage) { + // 检查空和大小 + Assert.IsTrue(storage.IsEmpty()); + Assert.AreEqual(storage.GetCount(), 0); + + // 设置内容 + storage.Insert(0, PROBE); + // 并再次检查大小 + Assert.IsFalse(storage.IsEmpty()); + Assert.AreEqual(storage.GetCount(), PROBE.Length); + + // 访问数据 + var len = PROBE.Length; + for (int i = 0; i < len; ++i) { + Assert.AreEqual(storage.Visit(i), PROBE[i]); + } + + // 清空数据 + storage.Clear(); + // 再次检查数据 + Assert.IsTrue(storage.IsEmpty()); + Assert.AreEqual(storage.GetCount(), 0); + } + } +} diff --git a/BallanceTasEditorTest/packages.config b/BallanceTasEditorTests/packages.config similarity index 100% rename from BallanceTasEditorTest/packages.config rename to BallanceTasEditorTests/packages.config