Skip to content

Commit

Permalink
Fix delete root directory entry child
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-visionaid committed Nov 18, 2024
1 parent 6e3fca4 commit 3041ce9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 87 deletions.
97 changes: 32 additions & 65 deletions OpenMcdf.Tests/StorageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,106 +142,73 @@ public void CreateDuplicateStorageThrowsException(Version version)
Assert.ThrowsException<IOException>(() => 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]
Expand Down
36 changes: 14 additions & 22 deletions OpenMcdf/DirectoryTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 3041ce9

Please sign in to comment.