From 7d8c8125864a8e8667c245cb65c72f3a2b2fb6ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8B=E8=88=98=E6=9C=97?= Date: Wed, 18 Sep 2024 09:00:01 +0900 Subject: [PATCH] =?UTF-8?q?#192=20=E4=BB=A5=E4=B8=8B=E3=81=AE=E4=BA=8C?= =?UTF-8?q?=E3=81=A4=E3=81=AE=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0=E3=81=97=E3=81=9F=E3=80=82=20public=20IList=20AddRange(IEnumerable=20nodes,=20boo?= =?UTF-8?q?l=20allowOverwrite=20=3D=20false)=20public=20static=20VirtualDi?= =?UTF-8?q?rectory=20operator=20+(VirtualDirectory=20directory,=20IList=20nodes)=20=E3=83=86=E3=82=B9=E3=83=88=E6=B8=88?= =?UTF-8?q?=E3=81=BF=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../VirtualDirectoryTests.cs | 47 ++++++++++++ .../VirtualStorageTests.cs | 71 +++++++++++++++++++ .../VirtualStorageLibrary/VirtualDirectory.cs | 45 ++++++++++++ .../VirtualStorageLibrary.csproj | 6 +- 4 files changed, 166 insertions(+), 3 deletions(-) diff --git a/AkiraNetwork/VirtualStorageLibrary.Test/VirtualDirectoryTests.cs b/AkiraNetwork/VirtualStorageLibrary.Test/VirtualDirectoryTests.cs index 5bdfd10..3e8398b 100644 --- a/AkiraNetwork/VirtualStorageLibrary.Test/VirtualDirectoryTests.cs +++ b/AkiraNetwork/VirtualStorageLibrary.Test/VirtualDirectoryTests.cs @@ -338,6 +338,31 @@ public void Add_InvalidNodeNameWithRoot_ThrowsArgumentException() Debug.WriteLine(ex.Message); } + [TestMethod] + public void AddRange_AddNodes() + { + // Arrange + VirtualStorage vs = new(); + VirtualDirectory dir = vs.GetDirectory("/"); + + // Act + IList addedNodes = dir.AddRange( + [ + new VirtualItem("item1"), + new VirtualItem("item2"), + new VirtualItem("item3") + ]); + + // Assert + Assert.IsTrue(vs.ItemExists("/item1")); + Assert.IsTrue(vs.ItemExists("/item2")); + Assert.IsTrue(vs.ItemExists("/item3")); + Assert.AreEqual(3, addedNodes.Count); + Assert.AreEqual("item1", (string)addedNodes[0].Name); + Assert.AreEqual("item2", (string)addedNodes[1].Name); + Assert.AreEqual("item3", (string)addedNodes[2].Name); + } + [TestMethod] public void AddItem_AddsNewItemSuccessfully() { @@ -1700,6 +1725,28 @@ public void Operator_Plus_AddsExistingDirectoryToNewDirectory_And_VerifiesIsRefe Assert.IsFalse(addedTestLink.IsReferencedInStorage); } + [TestMethod] + public void Operator_Plus_AddsNodesToDirectory() + { + // Arrange + VirtualStorage vs = new(); + VirtualDirectory dir = vs.GetDirectory("/"); + + // Act + dir += + [ + new VirtualItem("item1"), + new VirtualItem("item2"), + new VirtualItem("item3") + ]; + + // Assert + Assert.AreEqual(3, dir.Count); + Assert.IsTrue(dir.ItemExists("item1")); + Assert.IsTrue(dir.ItemExists("item2")); + Assert.IsTrue(dir.ItemExists("item3")); + } + [TestMethod] public void AddDirectoryToAnotherDirectory_CreatesClone_And_VerifiesIsReferencedInStorage() { diff --git a/AkiraNetwork/VirtualStorageLibrary.Test/VirtualStorageTests.cs b/AkiraNetwork/VirtualStorageLibrary.Test/VirtualStorageTests.cs index 6a403df..d9e83af 100644 --- a/AkiraNetwork/VirtualStorageLibrary.Test/VirtualStorageTests.cs +++ b/AkiraNetwork/VirtualStorageLibrary.Test/VirtualStorageTests.cs @@ -11059,6 +11059,77 @@ public void Indexer_Test14() Debug.WriteLine($"/dir2/link1(ItemData) = {vs.Item["/dir2/link1"].ItemData}"); } + [TestMethod] + [TestCategory("Indexer")] + public void Indexer_Test15() + { + // Arrange + VirtualStorage vs = new(); + VirtualItem item1 = new("item1", "pen"); + VirtualItem item2 = new("item2", "pineapple"); + VirtualItem item3 = new("item3", "apple"); + vs["/"] += new VirtualDirectory("PPAP"); + + // Act + vs["/PPAP"] += [item1, item2, item3]; + + // Debug Out + Debug.WriteLine($"/PPAP/item1 = {vs.Item["/PPAP/item1"].ItemData}"); + Debug.WriteLine($"/PPAP/item2 = {vs.Item["/PPAP/item2"].ItemData}"); + Debug.WriteLine($"/PPAP/item3 = {vs.Item["/PPAP/item3"].ItemData}"); + + // Assert + Assert.AreEqual("pen", vs.Item["/PPAP/item1"].ItemData); + Assert.AreEqual("pineapple", vs.Item["/PPAP/item2"].ItemData); + Assert.AreEqual("apple", vs.Item["/PPAP/item3"].ItemData); + } + + [TestMethod] + [TestCategory("Indexer")] + public void Indexer_Test16() + { + // Arrange + VirtualStorage vs = new(); + VirtualItem item1 = new("item1", [1, 2, 3]); + VirtualItem item2 = new("item2", [4, 5, 6]); + VirtualItem item3 = new("item3", [7, 8, 9]); + vs["/"] += new VirtualDirectory("dir1"); + vs["/"] += new VirtualDirectory("dir2"); + + // Act + vs["/dir1"] += [item1, item2, item3]; + + // Debug Out + Debug.WriteLine($"\n/dir1/item1 = {vs.Item["/dir1/item1"].ItemData}"); + Debug.WriteLine($"/dir1/item2 = {vs.Item["/dir1/item2"].ItemData}"); + Debug.WriteLine($"/dir1/item3 = {vs.Item["/dir1/item3"].ItemData}"); + + // Act + vs["/dir2"] += [vs.Item["/dir1/item1"], vs.Item["/dir1/item2"], vs.Item["/dir1/item3"]]; + + // Debug Out + Debug.WriteLine($"\n/dir2/item1 = {vs.Item["/dir2/item1"].ItemData}"); + Debug.WriteLine($"/dir2/item2 = {vs.Item["/dir2/item2"].ItemData}"); + Debug.WriteLine($"/dir2/item3 = {vs.Item["/dir2/item3"].ItemData}"); + + // Assert + Assert.AreEqual(new BinaryData([1, 2, 3]), vs.Item["/dir1/item1"].ItemData); + Assert.AreEqual(new BinaryData([4, 5, 6]), vs.Item["/dir1/item2"].ItemData); + Assert.AreEqual(new BinaryData([7, 8, 9]), vs.Item["/dir1/item3"].ItemData); + + Assert.AreEqual(new BinaryData([1, 2, 3]), vs.Item["/dir2/item1"].ItemData); + Assert.AreEqual(new BinaryData([4, 5, 6]), vs.Item["/dir2/item2"].ItemData); + Assert.AreEqual(new BinaryData([7, 8, 9]), vs.Item["/dir2/item3"].ItemData); + + Assert.AreNotEqual(vs.Item["/dir2/item1"], vs.Item["/dir1/item1"]); + Assert.AreNotEqual(vs.Item["/dir2/item2"], vs.Item["/dir1/item2"]); + Assert.AreNotEqual(vs.Item["/dir2/item3"], vs.Item["/dir1/item3"]); + + Assert.AreNotSame(vs.Item["/dir2/item1"].ItemData, vs.Item["/dir1/item1"].ItemData); + Assert.AreNotSame(vs.Item["/dir2/item2"].ItemData, vs.Item["/dir1/item2"].ItemData); + Assert.AreNotSame(vs.Item["/dir2/item3"].ItemData, vs.Item["/dir1/item3"].ItemData); + } + [TestMethod] [TestCategory("SetNode")] public void SetNode_Directory() diff --git a/AkiraNetwork/VirtualStorageLibrary/VirtualDirectory.cs b/AkiraNetwork/VirtualStorageLibrary/VirtualDirectory.cs index 4724254..68fea6c 100644 --- a/AkiraNetwork/VirtualStorageLibrary/VirtualDirectory.cs +++ b/AkiraNetwork/VirtualStorageLibrary/VirtualDirectory.cs @@ -346,6 +346,28 @@ public VirtualNode Add(VirtualNode node, bool allowOverwrite = false) return node; } + /// + /// Adds multiple nodes to the directory. + /// + /// The nodes to add. + /// If true, allows overwriting existing nodes with the same names. + /// A list of the added nodes. If a node was cloned, the cloned instance is returned in the list. + /// Thrown if an invalid node name is specified in any of the nodes. + /// Thrown if a node with the same name already exists and overwriting is not allowed. + public IList AddRange(IEnumerable nodes, bool allowOverwrite = false) + { + var addedNodes = new List(); + + foreach (var node in nodes) + { + // Add the node and collect the returned node (original or cloned) + var addedNode = Add(node, allowOverwrite); + addedNodes.Add(addedNode); + } + + return addedNodes; + } + /// /// Adds an item with the specified name. /// @@ -572,6 +594,29 @@ public VirtualSymbolicLink GetSymbolicLink(VirtualNodeName name) return directory; } + /// + /// Overloads the addition operator to add multiple nodes to the directory using AddRange. + /// + /// The directory to which the nodes are added. + /// The nodes to add. + /// The directory with the added nodes. + public static VirtualDirectory operator +(VirtualDirectory directory, IList nodes) + { + // Use AddRange to add multiple nodes + var addedNodes = directory.AddRange(nodes, allowOverwrite: false); + + // If the directory is referenced in storage, propagate IsReferencedInStorage to each added node + if (directory.IsReferencedInStorage) + { + foreach (var node in addedNodes) + { + SetIsReferencedInStorageRecursively(node, true); + } + } + + return directory; + } + /// /// Overloads the addition operator to add a node to the directory. /// diff --git a/AkiraNetwork/VirtualStorageLibrary/VirtualStorageLibrary.csproj b/AkiraNetwork/VirtualStorageLibrary/VirtualStorageLibrary.csproj index 3c015e2..d9f5364 100644 --- a/AkiraNetwork/VirtualStorageLibrary/VirtualStorageLibrary.csproj +++ b/AkiraNetwork/VirtualStorageLibrary/VirtualStorageLibrary.csproj @@ -7,7 +7,7 @@ AkiraNetwork.VirtualStorageLibrary True Virtual Storage Library - 0.9.1.0 + 0.9.2.0 Akira Shimodate AkiraNetwork VirtualStorageLibrary is a fully in-memory .NET library that provides a tree-structured collection. @@ -63,8 +63,8 @@ https://shimodateakira.github.io/VirtualStorageLibrary/index.html LGPL-3.0-or-later True ../../packages - 0.9.1.0 - 0.9.1.0 + 0.9.2.0 + 0.9.2.0 True ..\..\..\Signature\VirtualStorageLibrary.snk AkiraNetwork.VirtualStorageLibrary