Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 68 additions & 1 deletion DeepClone.Test/DeepCloneFromExceptionSafetyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@

namespace ktsu.DeepClone.Test;

using System.Collections.Concurrent;

/// <summary>
/// Tests that a <c>DeepCloneFrom</c> that throws part-way leaves the destination with its original contents,
/// instead of cleared and half-filled with the source's (ktsu-dev/DeepClone#90).
/// instead of cleared and half-filled with the source's (ktsu-dev/DeepClone#90, #124).
/// </summary>
[TestClass]
public class DeepCloneFromExceptionSafetyTests
{
private static readonly string[] OriginalNames = ["keep1", "keep2"];

// A stack built from [keep1, keep2] pushes keep2 last, so it pops keep2 first
private static readonly string[] PopOrderNames = ["keep2", "keep1"];

/// <summary>
/// Tests that an element whose DeepClone throws leaves the destination list unchanged.
/// </summary>
Expand Down Expand Up @@ -77,6 +82,68 @@ public void Collection_Succeeds_ReplacesContentsWithClones()
Assert.AreNotSame(a, dest[0]);
}

private static ThrowingItem[] ThrowingSource() => [new("a"), new("boom"), new("c")];

/// <summary>
/// Tests that an element whose DeepClone throws leaves the destination stack unchanged, in its original pop order.
/// </summary>
[TestMethod]
public void Stack_ElementCloneThrows_DestinationUnchanged()
{
ThrowingItem keep1 = new("keep1");
ThrowingItem keep2 = new("keep2");
Stack<ThrowingItem> dest = new([keep1, keep2]);

Assert.ThrowsExactly<InvalidOperationException>(() => dest.DeepCloneFrom(ThrowingSource()));

Assert.AreSequenceEqual(PopOrderNames, dest.Select(item => item.Name));
}

/// <summary>
/// Tests that an element whose DeepClone throws leaves the destination queue unchanged, in its original dequeue order.
/// </summary>
[TestMethod]
public void Queue_ElementCloneThrows_DestinationUnchanged()
{
ThrowingItem keep1 = new("keep1");
ThrowingItem keep2 = new("keep2");
Queue<ThrowingItem> dest = new([keep1, keep2]);

Assert.ThrowsExactly<InvalidOperationException>(() => dest.DeepCloneFrom(ThrowingSource()));

Assert.AreSequenceEqual(OriginalNames, dest.Select(item => item.Name));
}

/// <summary>
/// Tests that an element whose DeepClone throws leaves the destination concurrent stack unchanged, in its original pop order.
/// </summary>
[TestMethod]
public void ConcurrentStack_ElementCloneThrows_DestinationUnchanged()
{
ThrowingItem keep1 = new("keep1");
ThrowingItem keep2 = new("keep2");
ConcurrentStack<ThrowingItem> dest = new([keep1, keep2]);

Assert.ThrowsExactly<InvalidOperationException>(() => dest.DeepCloneFrom(ThrowingSource()));

Assert.AreSequenceEqual(PopOrderNames, dest.Select(item => item.Name));
}

/// <summary>
/// Tests that an element whose DeepClone throws leaves the destination concurrent queue unchanged, in its original dequeue order.
/// </summary>
[TestMethod]
public void ConcurrentQueue_ElementCloneThrows_DestinationUnchanged()
{
ThrowingItem keep1 = new("keep1");
ThrowingItem keep2 = new("keep2");
ConcurrentQueue<ThrowingItem> dest = new([keep1, keep2]);

Assert.ThrowsExactly<InvalidOperationException>(() => dest.DeepCloneFrom(ThrowingSource()));

Assert.AreSequenceEqual(OriginalNames, dest.Select(item => item.Name));
}

/// <summary>
/// A cloneable item whose clone throws when its name is "boom".
/// </summary>
Expand Down
34 changes: 25 additions & 9 deletions DeepClone/DeepCloneContainerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
/// </code>
/// </remarks>
/// <exception cref="ArgumentNullException">Thrown if dest or source is null.</exception>
public static void DeepCloneFrom<T>(this ICollection<T> dest, IEnumerable<T> source)

Check warning on line 43 in DeepClone/DeepCloneContainerExtensions.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

All 'DeepCloneFrom' method overloads should be adjacent.

Check warning on line 43 in DeepClone/DeepCloneContainerExtensions.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

All 'DeepCloneFrom' method overloads should be adjacent.

Check warning on line 43 in DeepClone/DeepCloneContainerExtensions.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

All 'DeepCloneFrom' method overloads should be adjacent.

Check warning on line 43 in DeepClone/DeepCloneContainerExtensions.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

All 'DeepCloneFrom' method overloads should be adjacent.
{
Ensure.NotNull(dest);

Expand Down Expand Up @@ -147,11 +147,11 @@

Ensure.NotNull(source);

T[] items = [.. source];
T[] clones = CloneAll(source);
dest.Clear();
for (int i = items.Length - 1; i >= 0; i--)
for (int i = clones.Length - 1; i >= 0; i--)
{
dest.Push(DeepClone(items[i]));
dest.Push(clones[i]);
}
}

Expand All @@ -173,11 +173,11 @@

Ensure.NotNull(source);

T[] items = [.. source];
T[] clones = CloneAll(source);
dest.Clear();
foreach (T item in items)
foreach (T clone in clones)
{
dest.Enqueue(DeepClone(item));
dest.Enqueue(clone);
}
}

Expand Down Expand Up @@ -207,7 +207,7 @@
}

dest.Clear();
if (clones.Length > 0)

Check warning on line 210 in DeepClone/DeepCloneContainerExtensions.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Change this condition so that it does not always evaluate to 'True'.

Check warning on line 210 in DeepClone/DeepCloneContainerExtensions.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Change this condition so that it does not always evaluate to 'True'.

Check warning on line 210 in DeepClone/DeepCloneContainerExtensions.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Change this condition so that it does not always evaluate to 'True'.
{
dest.PushRange(clones);
}
Expand All @@ -230,7 +230,7 @@

Ensure.NotNull(source);

T[] items = [.. source];
T[] clones = CloneAll(source);
#if NETSTANDARD2_1 || NET
dest.Clear();
#else
Expand All @@ -239,12 +239,28 @@
// Intentionally empty: netstandard2.0 has no ConcurrentQueue.Clear, so drain it instead.
}
#endif
foreach (T item in items)
foreach (T clone in clones)
{
dest.Enqueue(DeepClone(item));
dest.Enqueue(clone);
}
}

/// <summary>
/// Snapshots <paramref name="source"/> and deep clones every item, in order, before any destination is touched,
/// so an item whose clone throws leaves the destination unchanged (ktsu-dev/DeepClone#90, #124).
/// </summary>
private static T[] CloneAll<T>(IEnumerable<T> source)
{
T[] items = [.. source];
T[] clones = new T[items.Length];
for (int i = 0; i < items.Length; i++)
{
clones[i] = DeepClone(items[i]);
}

return clones;
}

/// <summary>
/// Helper method to deep clone an object if it implements IDeepCloneable, otherwise returns the object unchanged.
/// </summary>
Expand Down Expand Up @@ -330,7 +346,7 @@
/// </code>
/// </remarks>
/// <exception cref="ArgumentNullException">Thrown if source is null.</exception>
public static IEnumerable<T> DeepClone<T>(this IEnumerable<T> source)

Check warning on line 349 in DeepClone/DeepCloneContainerExtensions.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

All 'DeepClone' method overloads should be adjacent.

Check warning on line 349 in DeepClone/DeepCloneContainerExtensions.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

All 'DeepClone' method overloads should be adjacent.

Check warning on line 349 in DeepClone/DeepCloneContainerExtensions.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

All 'DeepClone' method overloads should be adjacent.
{
Ensure.NotNull(source);

Expand Down
Loading