diff --git a/OpenMcdf.Tests/StorageTests.cs b/OpenMcdf.Tests/StorageTests.cs index 653db80..8ce6c6c 100644 --- a/OpenMcdf.Tests/StorageTests.cs +++ b/OpenMcdf.Tests/StorageTests.cs @@ -142,106 +142,73 @@ public void CreateDuplicateStorageThrowsException(Version version) Assert.ThrowsException(() => rootStorage.CreateStorage("Test")); } + private static void CreateDeleteTest(Version version, MemoryStream memoryStream) + { + using var rootStorage = RootStorage.Create(memoryStream, version, StorageModeFlags.LeaveOpen); + rootStorage.CreateStorage("Test2"); + rootStorage.CreateStorage("Test1"); + rootStorage.CreateStorage("Test3"); + } + [TestMethod] [DataRow(Version.V3)] [DataRow(Version.V4)] - public void DeleteSingleStorage(Version version) + public void DeleteStorageLeft(Version version) { using MemoryStream memoryStream = new(); - using (var rootStorage = RootStorage.Create(memoryStream, version, StorageModeFlags.LeaveOpen)) - { - rootStorage.CreateStorage("Test"); - Assert.AreEqual(1, rootStorage.EnumerateEntries().Count()); - } - using (var rootStorage = RootStorage.Open(memoryStream, StorageModeFlags.LeaveOpen)) - { - rootStorage.Delete("Test"); - Assert.AreEqual(0, rootStorage.EnumerateEntries().Count()); + CreateDeleteTest(version, memoryStream); - rootStorage.Delete("NonExistentEntry"); - } + using var rootStorage = RootStorage.Open(memoryStream); + rootStorage.Delete("NonExistentEntry"); + Assert.AreEqual(3, rootStorage.EnumerateEntries().Count()); - using (var rootStorage = RootStorage.Open(memoryStream)) - { - Assert.AreEqual(0, rootStorage.EnumerateEntries().Count()); - } + rootStorage.Delete("Test1"); + Assert.AreEqual(2, rootStorage.EnumerateEntries().Count()); } [TestMethod] [DataRow(Version.V3)] [DataRow(Version.V4)] - public void DeleteRedBlackTreeChildLeaf(Version version) + public void DeleteStorageRoot(Version version) { using MemoryStream memoryStream = new(); - using (var rootStorage = RootStorage.Create(memoryStream, version, StorageModeFlags.LeaveOpen)) - { - rootStorage.CreateStorage("Test1"); - rootStorage.CreateStorage("Test2"); - Assert.AreEqual(2, rootStorage.EnumerateEntries().Count()); - } - using (var rootStorage = RootStorage.Open(memoryStream, StorageModeFlags.LeaveOpen)) - { - rootStorage.Delete("Test1"); - Assert.AreEqual(1, rootStorage.EnumerateEntries().Count()); - } + CreateDeleteTest(version, memoryStream); - using (var rootStorage = RootStorage.Open(memoryStream)) - { - Assert.AreEqual(1, rootStorage.EnumerateEntries().Count()); - } + using var rootStorage = RootStorage.Open(memoryStream); + rootStorage.Delete("Test2"); + Assert.AreEqual(2, rootStorage.EnumerateEntries().Count()); } [TestMethod] [DataRow(Version.V3)] [DataRow(Version.V4)] - public void DeleteRedBlackTreeSiblingLeaf(Version version) + public void DeleteStorageRight(Version version) { using MemoryStream memoryStream = new(); - using (var rootStorage = RootStorage.Create(memoryStream, version, StorageModeFlags.LeaveOpen)) - { - rootStorage.CreateStorage("Test1"); - rootStorage.CreateStorage("Test2"); - Assert.AreEqual(2, rootStorage.EnumerateEntries().Count()); - } - using (var rootStorage = RootStorage.Open(memoryStream, StorageModeFlags.LeaveOpen)) - { - rootStorage.Delete("Test2"); - Assert.AreEqual(1, rootStorage.EnumerateEntries().Count()); - } + CreateDeleteTest(version, memoryStream); - using (var rootStorage = RootStorage.Open(memoryStream)) - { - Assert.AreEqual(1, rootStorage.EnumerateEntries().Count()); - } + using var rootStorage = RootStorage.Open(memoryStream); + rootStorage.Delete("Test3"); + Assert.AreEqual(2, rootStorage.EnumerateEntries().Count()); } [TestMethod] [DataRow(Version.V3)] [DataRow(Version.V4)] - public void DeleteRedBlackTreeSibling(Version version) + public void DeleteStorageAll(Version version) { using MemoryStream memoryStream = new(); - using (var rootStorage = RootStorage.Create(memoryStream, version, StorageModeFlags.LeaveOpen)) - { - rootStorage.CreateStorage("Test1"); - rootStorage.CreateStorage("Test2"); - rootStorage.CreateStorage("Test3"); - Assert.AreEqual(3, rootStorage.EnumerateEntries().Count()); - } - using (var rootStorage = RootStorage.Open(memoryStream, StorageModeFlags.LeaveOpen)) - { - rootStorage.Delete("Test2"); - Assert.AreEqual(2, rootStorage.EnumerateEntries().Count()); - } + CreateDeleteTest(version, memoryStream); - using (var rootStorage = RootStorage.Open(memoryStream)) - { - Assert.AreEqual(2, rootStorage.EnumerateEntries().Count()); - } + using var rootStorage = RootStorage.Open(memoryStream); + rootStorage.Delete("Test1"); + rootStorage.Delete("Test2"); + rootStorage.Delete("Test3"); + Assert.AreEqual(0, rootStorage.EnumerateEntries().Count()); } [TestMethod] diff --git a/OpenMcdf/DirectoryTree.cs b/OpenMcdf/DirectoryTree.cs index 9ab9f3c..e4126e3 100644 --- a/OpenMcdf/DirectoryTree.cs +++ b/OpenMcdf/DirectoryTree.cs @@ -205,30 +205,22 @@ internal void WriteTrace(TextWriter writer) return; } - Stack<(DirectoryEntry node, int indentLevel)> stack = new Stack<(DirectoryEntry, int)>(); - directories.TryGetDictionaryEntry(root.ChildId, out DirectoryEntry? current); - int currentIndentLevel = 0; + DirectoryEntry current = directories.GetDictionaryEntry(root.ChildId); + WriteTrace(writer, current, 0); + } - while (stack.Count > 0 || current is not null) - { - if (current != null) - { - stack.Push((current, currentIndentLevel)); - directories.TryGetDictionaryEntry(current.RightSiblingId, out current); - currentIndentLevel++; - } - else - { - (DirectoryEntry node, int indentLevel) = stack.Pop(); - currentIndentLevel = indentLevel; + void WriteTrace(TextWriter writer, DirectoryEntry entry, int indent) + { + directories.TryGetDictionaryEntry(entry.RightSiblingId, out DirectoryEntry? rightSibling); + if (rightSibling is not null) + WriteTrace(writer, rightSibling, indent + 1); - for (int i = 0; i < indentLevel; i++) - writer.Write(" "); - writer.WriteLine(node.Color == NodeColor.Black ? $" {node} " : $"<{node}>"); + for (int i = 0; i < indent; i++) + writer.Write(" "); + writer.WriteLine(entry); - directories.TryGetDictionaryEntry(node.LeftSiblingId, out current); - currentIndentLevel++; - } - } + directories.TryGetDictionaryEntry(entry.LeftSiblingId, out DirectoryEntry? leftSibling); + if (leftSibling is not null) + WriteTrace(writer, leftSibling, indent + 1); } }