1
1
using System ;
2
2
using DataStructures . LinkedList . CircularLinkedList ;
3
- using Microsoft . VisualStudio . TestPlatform . Utilities ;
4
3
using NUnit . Framework ;
5
4
6
5
namespace DataStructures . Tests . LinkedList ;
@@ -22,10 +21,10 @@ public static void TestDisplay()
22
21
[ Test ]
23
22
public static void TestDisplayEmptyList ( )
24
23
{
25
- var cll = new CircularLinkedList < int > ( ) ;
26
- cll . Display ( ) ;
24
+ var cll = new CircularLinkedList < int > ( ) ;
25
+ var ex = Assert . Throws < InvalidOperationException > ( ( ) => cll . Display ( ) ) ;
27
26
28
- Assert . That ( "List is empty." , Is . EqualTo ( GetDisplayOutput ( cll ) . Trim ( ) ) ) ;
27
+ Assert . That ( ex ! . Message , Is . EqualTo ( "List is empty." ) ) ;
29
28
}
30
29
31
30
[ Test ]
@@ -57,8 +56,8 @@ public static void TestInsertAfter()
57
56
cll . InsertAtEnd ( 10 ) ;
58
57
cll . InsertAtEnd ( 20 ) ;
59
58
cll . InsertAtEnd ( 30 ) ;
60
-
61
59
cll . InsertAfter ( 20 , 25 ) ;
60
+
62
61
Assert . That ( "10 20 25 30" , Is . EqualTo ( GetDisplayOutput ( cll ) . Trim ( ) ) ) ;
63
62
}
64
63
@@ -67,6 +66,7 @@ public static void TestInsertAtBeginningInEmptyList()
67
66
{
68
67
var cll = new CircularLinkedList < int > ( ) ;
69
68
cll . InsertAtBeginning ( 10 ) ;
69
+
70
70
Assert . That ( "10" , Is . EqualTo ( GetDisplayOutput ( cll ) . Trim ( ) ) ) ;
71
71
}
72
72
@@ -83,22 +83,20 @@ public static void TestInsertAtEndInEmptyList()
83
83
public static void TestInsertAfterInEmptyList ( )
84
84
{
85
85
var cll = new CircularLinkedList < int > ( ) ;
86
- cll . InsertAfter ( 10 , 20 ) ;
86
+ var ex = Assert . Throws < InvalidOperationException > ( ( ) => cll . InsertAfter ( 10 , 20 ) ) ;
87
87
88
- Assert . That ( "List is empty." , Is . EqualTo ( GetDisplayOutput ( cll ) . Trim ( ) ) ) ;
88
+ Assert . That ( ex ! . Message , Is . EqualTo ( "List is empty." ) ) ;
89
89
}
90
90
91
-
92
-
93
91
[ Test ]
94
92
public static void TestInsertAfterSpecificNode ( )
95
93
{
96
94
var cll = new CircularLinkedList < int > ( ) ;
97
95
cll . InsertAtEnd ( 10 ) ;
98
96
cll . InsertAtEnd ( 20 ) ;
99
97
cll . InsertAtEnd ( 30 ) ;
100
-
101
98
cll . InsertAfter ( 20 , 25 ) ; // Insert after node with value 20
99
+
102
100
Assert . That ( "10 20 25 30" , Is . EqualTo ( GetDisplayOutput ( cll ) . Trim ( ) ) ) ;
103
101
}
104
102
@@ -108,6 +106,7 @@ public static void TestInsertAfterOnNonExistingValue()
108
106
var cll = new CircularLinkedList < int > ( ) ;
109
107
cll . InsertAtEnd ( 10 ) ;
110
108
cll . InsertAfter ( 99 , 25 ) ; // 99 does not exist
109
+
111
110
Assert . That ( "10" , Is . EqualTo ( GetDisplayOutput ( cll ) . Trim ( ) ) ) ;
112
111
}
113
112
@@ -118,8 +117,8 @@ public static void TestDeleteNode()
118
117
cll . InsertAtEnd ( 10 ) ;
119
118
cll . InsertAtEnd ( 20 ) ;
120
119
cll . InsertAtEnd ( 30 ) ;
121
-
122
120
cll . DeleteNode ( 20 ) ;
121
+
123
122
Assert . That ( "10 30" , Is . EqualTo ( GetDisplayOutput ( cll ) . Trim ( ) ) ) ;
124
123
}
125
124
@@ -129,6 +128,7 @@ public static void TestDeleteOnlyNode()
129
128
var cll = new CircularLinkedList < int > ( ) ;
130
129
cll . InsertAtBeginning ( 10 ) ;
131
130
cll . DeleteNode ( 10 ) ;
131
+
132
132
Assert . That ( cll . IsEmpty ( ) , Is . EqualTo ( true ) ) ;
133
133
}
134
134
@@ -140,6 +140,7 @@ public static void TestDeleteHeadNode()
140
140
cll . InsertAtEnd ( 20 ) ;
141
141
cll . InsertAtEnd ( 30 ) ;
142
142
cll . DeleteNode ( 10 ) ;
143
+
143
144
Assert . That ( "20 30" , Is . EqualTo ( GetDisplayOutput ( cll ) . Trim ( ) ) ) ;
144
145
}
145
146
@@ -151,15 +152,17 @@ public static void TestDeleteTailNode()
151
152
cll . InsertAtEnd ( 20 ) ;
152
153
cll . InsertAtEnd ( 30 ) ;
153
154
cll . DeleteNode ( 30 ) ;
155
+
154
156
Assert . That ( "10 20" , Is . EqualTo ( GetDisplayOutput ( cll ) . Trim ( ) ) ) ;
155
157
}
156
158
157
159
[ Test ]
158
160
public static void TestDeleteFromEmptyList ( )
159
161
{
160
162
var cll = new CircularLinkedList < int > ( ) ;
161
- cll . DeleteNode ( 10 ) ;
162
- Assert . That ( cll . IsEmpty ( ) , Is . EqualTo ( true ) ) ;
163
+ var ex = Assert . Throws < InvalidOperationException > ( ( ) => cll . DeleteNode ( 10 ) ) ;
164
+
165
+ Assert . That ( ex ! . Message , Is . EqualTo ( "List is empty." ) ) ;
163
166
}
164
167
165
168
[ Test ]
@@ -169,8 +172,8 @@ public static void TestDeleteNonExistentNode()
169
172
cll . InsertAtEnd ( 10 ) ;
170
173
cll . InsertAtEnd ( 20 ) ;
171
174
cll . InsertAtEnd ( 30 ) ;
172
-
173
175
cll . DeleteNode ( 40 ) ; // Attempting to delete a node that doesn't exist
176
+
174
177
Assert . That ( "10 20 30" , Is . EqualTo ( GetDisplayOutput ( cll ) . Trim ( ) ) ) ;
175
178
}
176
179
0 commit comments