@@ -151,7 +151,78 @@ public void TestMethod(string input)
151151 Console . WriteLine ( input ) ;
152152 }
153153 }
154+
155+ [ Test ]
156+ public void Circular_Reference_Clone ( )
157+ {
158+ // Arrange
159+ CircularClass original = new CircularClass
160+ {
161+ Name = "Test"
162+ } ;
163+
164+ original . Reference = original ;
165+
166+ // Act
167+ CircularClass cloned = original . DeepClone ( ) ;
168+
169+ // Assert
170+ Assert . Multiple ( ( ) =>
171+ {
172+ Assert . That ( cloned , Is . Not . SameAs ( original ) , "Cloned object should be a new instance" ) ;
173+ Assert . That ( cloned . Name , Is . EqualTo ( original . Name ) , "Properties should be copied" ) ;
174+ Assert . That ( cloned . Reference , Is . SameAs ( cloned ) , "Circular reference should point to the cloned instance" ) ;
175+ Assert . That ( cloned . Reference . Reference , Is . SameAs ( cloned ) , "Nested circular reference should point to the cloned instance" ) ;
176+ } ) ;
177+ }
178+
179+ private class CircularClass
180+ {
181+ public string Name { get ; set ; }
182+ public CircularClass Reference { get ; set ; }
183+ }
154184
185+ [ Test ]
186+ public void Complex_Circular_Reference_Clone ( )
187+ {
188+ // Arrange
189+ Node nodeA = new Node { Name = "A" } ;
190+ Node nodeB = new Node { Name = "B" } ;
191+ Node nodeC = new Node { Name = "C" } ;
192+
193+ // A -> B -> C -> A
194+ nodeA . Next = nodeB ;
195+ nodeB . Next = nodeC ;
196+ nodeC . Next = nodeA ;
197+
198+ // Act
199+ Node clonedA = nodeA . DeepClone ( ) ;
200+ Node clonedB = clonedA . Next ;
201+ Node clonedC = clonedB . Next ;
202+
203+ // Assert
204+ Assert . Multiple ( ( ) =>
205+ {
206+ Assert . That ( clonedA , Is . Not . SameAs ( nodeA ) , "Node A should be cloned" ) ;
207+ Assert . That ( clonedB , Is . Not . SameAs ( nodeB ) , "Node B should be cloned" ) ;
208+ Assert . That ( clonedC , Is . Not . SameAs ( nodeC ) , "Node C should be cloned" ) ;
209+
210+ Assert . That ( clonedA . Name , Is . EqualTo ( "A" ) , "Node A name should be copied" ) ;
211+ Assert . That ( clonedB . Name , Is . EqualTo ( "B" ) , "Node B name should be copied" ) ;
212+ Assert . That ( clonedC . Name , Is . EqualTo ( "C" ) , "Node C name should be copied" ) ;
213+
214+ Assert . That ( clonedC . Next , Is . SameAs ( clonedA ) , "Cycle should be preserved" ) ;
215+ Assert . That ( clonedA . Next , Is . SameAs ( clonedB ) , "References should point to new instances" ) ;
216+ Assert . That ( clonedB . Next , Is . SameAs ( clonedC ) , "References should point to new instances" ) ;
217+ } ) ;
218+ }
219+
220+ private class Node
221+ {
222+ public string Name { get ; set ; }
223+ public Node Next { get ; set ; }
224+ }
225+
155226 [ Test ]
156227 public void Test_ExpressionTree_OrderBy2 ( )
157228 {
0 commit comments