|
1 | 1 | using System.Collections.Concurrent; |
| 2 | +using System.Collections.Frozen; |
2 | 3 | using System.Reflection; |
| 4 | +using System.Runtime.CompilerServices; |
3 | 5 | using System.Threading.Channels; |
4 | 6 | using System.Threading.Tasks.Dataflow; |
5 | 7 | using System.Threading.Tasks; |
6 | 8 |
|
7 | 9 | namespace FastCloner.Tests; |
8 | 10 | public class FailureHypothesisTests |
9 | 11 | { |
| 12 | + private sealed class IdentityHashedKey |
| 13 | + { |
| 14 | + public string Tag { get; set; } = ""; |
| 15 | + public override int GetHashCode() => RuntimeHelpers.GetHashCode(this); |
| 16 | + public override bool Equals(object? obj) => ReferenceEquals(this, obj); |
| 17 | + } |
| 18 | + |
| 19 | + [Test] |
| 20 | + public async Task HashSet_With_IdentityBased_OverriddenGetHashCode_Should_Be_Lookupable_After_Clone() |
| 21 | + { |
| 22 | + IdentityHashedKey item = new IdentityHashedKey { Tag = "a" }; |
| 23 | + HashSet<IdentityHashedKey> original = [item]; |
| 24 | + |
| 25 | + HashSet<IdentityHashedKey> clone = original.DeepClone(); |
| 26 | + |
| 27 | + await Assert.That(clone).IsNotSameReferenceAs(original); |
| 28 | + await Assert.That(clone.Count).IsEqualTo(1); |
| 29 | + |
| 30 | + IdentityHashedKey cloneItem = clone.Single(); |
| 31 | + await Assert.That(cloneItem).IsNotSameReferenceAs(item) |
| 32 | + .Because("Element is a reference type and should be deep-cloned"); |
| 33 | + |
| 34 | + await Assert.That(clone.Contains(cloneItem)).IsTrue() |
| 35 | + .Because("Looking up the actual element of the cloned set must succeed; " + |
| 36 | + "FastCloner copies the original identity-based hash into the cloned bucket, " + |
| 37 | + "while the cloned element has a new identity hash, so lookup misses."); |
| 38 | + } |
| 39 | + |
| 40 | + [Test] |
| 41 | + public async Task Dictionary_With_IdentityBased_OverriddenGetHashCode_Key_Should_Be_Lookupable_After_Clone() |
| 42 | + { |
| 43 | + IdentityHashedKey key = new IdentityHashedKey { Tag = "k" }; |
| 44 | + Dictionary<IdentityHashedKey, int> original = new Dictionary<IdentityHashedKey, int> { [key] = 42 }; |
| 45 | + |
| 46 | + Dictionary<IdentityHashedKey, int> clone = original.DeepClone(); |
| 47 | + |
| 48 | + await Assert.That(clone).IsNotSameReferenceAs(original); |
| 49 | + await Assert.That(clone.Count).IsEqualTo(1); |
| 50 | + |
| 51 | + IdentityHashedKey cloneKey = clone.Keys.Single(); |
| 52 | + await Assert.That(cloneKey).IsNotSameReferenceAs(key); |
| 53 | + |
| 54 | + await Assert.That(clone.TryGetValue(cloneKey, out int value)).IsTrue() |
| 55 | + .Because("The cloned dictionary must be able to find its own key. " + |
| 56 | + "FastCloner stores stale identity hashes from the original key in the cloned bucket."); |
| 57 | + await Assert.That(value).IsEqualTo(42); |
| 58 | + } |
| 59 | + |
| 60 | + [FastClonerStableHash] |
| 61 | + private sealed class ProbeUnfriendlyButStableKey |
| 62 | + { |
| 63 | + public string Tag { get; set; } = ""; |
| 64 | + public override int GetHashCode() => Tag.ToUpperInvariant().GetHashCode(); |
| 65 | + public override bool Equals(object? obj) |
| 66 | + => obj is ProbeUnfriendlyButStableKey other |
| 67 | + && string.Equals(Tag, other.Tag, StringComparison.OrdinalIgnoreCase); |
| 68 | + } |
| 69 | + |
| 70 | + private sealed class ProbeUnfriendlyKeyNoAttribute |
| 71 | + { |
| 72 | + public string Tag { get; set; } = ""; |
| 73 | + public override int GetHashCode() => Tag.ToUpperInvariant().GetHashCode(); |
| 74 | + public override bool Equals(object? obj) |
| 75 | + => obj is ProbeUnfriendlyKeyNoAttribute other |
| 76 | + && string.Equals(Tag, other.Tag, StringComparison.OrdinalIgnoreCase); |
| 77 | + } |
| 78 | + |
| 79 | + [Test] |
| 80 | + public async Task FastClonerStableHashAttribute_Marks_Type_As_Stable() |
| 81 | + { |
| 82 | + await Assert.That(Code.FastClonerSafeTypes.HasStableHashSemantics(typeof(ProbeUnfriendlyButStableKey))) |
| 83 | + .IsTrue() |
| 84 | + .Because("[FastClonerStableHash] must short-circuit the probe and declare stable semantics, " + |
| 85 | + "even when GetHashCode would throw on default-state instances."); |
| 86 | + |
| 87 | + await Assert.That(Code.FastClonerSafeTypes.HasStableHashSemantics(typeof(ProbeUnfriendlyKeyNoAttribute))) |
| 88 | + .IsTrue() |
| 89 | + .Because("The smarter probe substitutes string.Empty for stable-hash reference fields, " + |
| 90 | + "so an override of Tag.ToUpperInvariant().GetHashCode() no longer NREs and is " + |
| 91 | + "correctly classified as stable even without the [FastClonerStableHash] opt-in."); |
| 92 | + } |
| 93 | + |
| 94 | + [Test] |
| 95 | + public async Task FastClonerStableHashAttribute_Allows_FastPath_With_Correct_Lookup() |
| 96 | + { |
| 97 | + ProbeUnfriendlyButStableKey key = new ProbeUnfriendlyButStableKey { Tag = "Alpha" }; |
| 98 | + HashSet<ProbeUnfriendlyButStableKey> original = [key]; |
| 99 | + |
| 100 | + HashSet<ProbeUnfriendlyButStableKey> clone = original.DeepClone(); |
| 101 | + |
| 102 | + await Assert.That(clone).IsNotSameReferenceAs(original); |
| 103 | + await Assert.That(clone.Count).IsEqualTo(1); |
| 104 | + |
| 105 | + ProbeUnfriendlyButStableKey cloneKey = clone.Single(); |
| 106 | + await Assert.That(cloneKey).IsNotSameReferenceAs(key); |
| 107 | + await Assert.That(clone.Contains(cloneKey)).IsTrue(); |
| 108 | + |
| 109 | + // Equality is case-insensitive, so a fresh key with different casing must also resolve. |
| 110 | + await Assert.That(clone.Contains(new ProbeUnfriendlyButStableKey { Tag = "alpha" })).IsTrue() |
| 111 | + .Because("Hash is value-based on Tag (case-insensitive) and survives the clone unchanged."); |
| 112 | + } |
| 113 | + |
| 114 | + private sealed class IdentityNode |
| 115 | + { |
| 116 | + public string Tag { get; set; } = ""; |
| 117 | + } |
| 118 | + |
| 119 | + private struct IdentityDelegatingStruct : IEquatable<IdentityDelegatingStruct> |
| 120 | + { |
| 121 | + public IdentityNode? Node; |
| 122 | + |
| 123 | + public override int GetHashCode() => Node is null ? 0 : Node.GetHashCode(); |
| 124 | + |
| 125 | + public bool Equals(IdentityDelegatingStruct other) => ReferenceEquals(Node, other.Node); |
| 126 | + |
| 127 | + public override bool Equals(object? obj) |
| 128 | + => obj is IdentityDelegatingStruct other && Equals(other); |
| 129 | + } |
| 130 | + |
| 131 | + [Test] |
| 132 | + public async Task HashSet_Of_Struct_With_IdentityHashed_Reference_Field_Should_Be_Lookupable_After_Clone() |
| 133 | + { |
| 134 | + IdentityNode node = new IdentityNode { Tag = "n" }; |
| 135 | + IdentityDelegatingStruct entry = new IdentityDelegatingStruct { Node = node }; |
| 136 | + |
| 137 | + HashSet<IdentityDelegatingStruct> original = [entry]; |
| 138 | + HashSet<IdentityDelegatingStruct> clone = original.DeepClone(); |
| 139 | + |
| 140 | + await Assert.That(clone).IsNotSameReferenceAs(original); |
| 141 | + await Assert.That(clone.Count).IsEqualTo(1); |
| 142 | + |
| 143 | + IdentityDelegatingStruct cloneEntry = clone.Single(); |
| 144 | + await Assert.That(cloneEntry.Node).IsNotSameReferenceAs(node) |
| 145 | + .Because("Reference field inside the struct must be deep-cloned to a fresh instance."); |
| 146 | + |
| 147 | + await Assert.That(clone.Contains(cloneEntry)).IsTrue() |
| 148 | + .Because("FastCloner treats every value type as having stable hash semantics, but this struct's " + |
| 149 | + "hash delegates to an identity-hashed reference field that gets a new identity on clone, " + |
| 150 | + "so the bucket-stored hash no longer matches the element's actual hash and lookup misses."); |
| 151 | + } |
| 152 | + |
10 | 153 | [Test] |
11 | 154 | public async Task BufferBlock_Should_Be_Deep_Cloned_Independently() |
12 | 155 | { |
@@ -227,6 +370,47 @@ public async Task ReaderWriterLockSlim_Should_Be_Deep_Cloned_Independently() |
227 | 370 | // Ignore cleanup errors |
228 | 371 | } |
229 | 372 | } |
| 373 | + |
| 374 | + [Test] |
| 375 | + public async Task FrozenSet_Should_Be_Cloned_Without_StackOverflow() |
| 376 | + { |
| 377 | + FrozenSet<string> original = new[] { "alpha", "beta", "gamma" }.ToFrozenSet(); |
| 378 | + |
| 379 | + FrozenSet<string> clone = original.DeepClone(); |
| 380 | + |
| 381 | + await Assert.That(clone.Count).IsEqualTo(3) |
| 382 | + .Because("All elements must survive the clone."); |
| 383 | + await Assert.That(clone.Contains("alpha")).IsTrue(); |
| 384 | + await Assert.That(clone.Contains("beta")).IsTrue(); |
| 385 | + await Assert.That(clone.Contains("gamma")).IsTrue(); |
| 386 | + await Assert.That(clone.Contains("delta")).IsFalse(); |
| 387 | + } |
| 388 | + |
| 389 | + private sealed class StructWrappedSelfReferenceContainer |
| 390 | + { |
| 391 | + public List<int> Payload { get; set; } = [1, 2, 3]; |
| 392 | + public StructBackRef BackRef; |
| 393 | + } |
| 394 | + |
| 395 | + private struct StructBackRef |
| 396 | + { |
| 397 | + public StructWrappedSelfReferenceContainer? Owner; |
| 398 | + } |
| 399 | + |
| 400 | + [Test] |
| 401 | + public async Task Container_With_StructMediated_SelfReference_Should_Clone_Without_Overflow() |
| 402 | + { |
| 403 | + StructWrappedSelfReferenceContainer original = new StructWrappedSelfReferenceContainer(); |
| 404 | + original.BackRef = new StructBackRef { Owner = original }; |
| 405 | + await Assert.That(original.BackRef.Owner).IsSameReferenceAs(original); |
| 406 | + |
| 407 | + StructWrappedSelfReferenceContainer clone = original.DeepClone(); |
| 408 | + |
| 409 | + await Assert.That(clone).IsNotSameReferenceAs(original); |
| 410 | + await Assert.That(clone.Payload).IsEquivalentTo(new[] { 1, 2, 3 }); |
| 411 | + await Assert.That(clone.BackRef.Owner).IsSameReferenceAs(clone) |
| 412 | + .Because("The cycle must be rebound to the clone, not left dangling at the original."); |
| 413 | + } |
230 | 414 |
|
231 | 415 | [Test] |
232 | 416 | public async Task Task_Should_Not_Be_Deep_Cloned() |
|
0 commit comments