@@ -7,26 +7,6 @@ namespace DataStructures.Tests.LinkedList;
7
7
[ TestFixture ]
8
8
public static class CircularLinkedListTests
9
9
{
10
- [ Test ]
11
- public static void TestDisplay ( )
12
- {
13
- var cll = new CircularLinkedList < int > ( ) ;
14
- cll . InsertAtEnd ( 10 ) ;
15
- cll . InsertAtEnd ( 20 ) ;
16
- cll . InsertAtEnd ( 30 ) ;
17
-
18
- Assert . That ( "10 20 30" , Is . EqualTo ( GetDisplayOutput ( cll ) . Trim ( ) ) ) ;
19
- }
20
-
21
- [ Test ]
22
- public static void TestDisplayEmptyList ( )
23
- {
24
- var cll = new CircularLinkedList < int > ( ) ;
25
- var ex = Assert . Throws < InvalidOperationException > ( ( ) => cll . Display ( ) ) ;
26
-
27
- Assert . That ( ex ! . Message , Is . EqualTo ( "List is empty." ) ) ;
28
- }
29
-
30
10
[ Test ]
31
11
public static void TestInsertAtBeginning ( )
32
12
{
@@ -177,35 +157,26 @@ public static void TestDeleteNonExistentNode()
177
157
Assert . That ( "10 20 30" , Is . EqualTo ( GetDisplayOutput ( cll ) . Trim ( ) ) ) ;
178
158
}
179
159
180
- /// <summary>
181
- /// Helper method to capture the output of the Display method for assertions.
182
- /// </summary>
183
- /// <param name="list">The CircularLinkedList instance.</param>
184
- /// <returns>A string representation of the list.</returns>
185
- private static string GetDisplayOutput ( CircularLinkedList < int > list )
160
+ private static string GetDisplayOutput < T > ( CircularLinkedList < T > list )
186
161
{
187
- // Save the original output (the default Console output stream)
188
- var originalConsoleOut = Console . Out ;
162
+ var head = list . GetHead ( ) ;
163
+ if ( head == null )
164
+ {
165
+ return string . Empty ;
166
+ }
167
+
168
+ var current = head ;
169
+ var result = new System . Text . StringBuilder ( ) ;
189
170
190
- // Use a StringWriter to capture Console output
191
- using ( var sw = new System . IO . StringWriter ( ) )
171
+ do
192
172
{
193
- try
194
- {
195
- // Redirect Console output to StringWriter
196
- Console . SetOut ( sw ) ;
197
-
198
- // Call the method that outputs to the console
199
- list . Display ( ) ;
200
-
201
- // Return the captured output
202
- return sw . ToString ( ) ;
203
- }
204
- finally
205
- {
206
- // Restore the original Console output stream
207
- Console . SetOut ( originalConsoleOut ) ;
208
- }
173
+ result . Append ( current ! . Data + " " ) ;
174
+ current = current . Next ;
209
175
}
176
+ while ( current != head ) ;
177
+
178
+ return result . ToString ( ) . Trim ( ) ;
210
179
}
180
+
181
+
211
182
}
0 commit comments