Skip to content

Commit 3d28581

Browse files
replaced console outputs with exception
1 parent 70c3d74 commit 3d28581

File tree

2 files changed

+20
-25
lines changed

2 files changed

+20
-25
lines changed

DataStructures.Tests/LinkedList/CircularLinkedListTests.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using DataStructures.LinkedList.CircularLinkedList;
3-
using Microsoft.VisualStudio.TestPlatform.Utilities;
43
using NUnit.Framework;
54

65
namespace DataStructures.Tests.LinkedList;
@@ -22,10 +21,10 @@ public static void TestDisplay()
2221
[Test]
2322
public static void TestDisplayEmptyList()
2423
{
25-
var cll = new CircularLinkedList<int>();
26-
cll.Display();
24+
var cll = new CircularLinkedList<int>();
25+
var ex = Assert.Throws<InvalidOperationException>(() => cll.Display());
2726

28-
Assert.That("List is empty.", Is.EqualTo(GetDisplayOutput(cll).Trim()));
27+
Assert.That(ex!.Message, Is.EqualTo("List is empty."));
2928
}
3029

3130
[Test]
@@ -57,8 +56,8 @@ public static void TestInsertAfter()
5756
cll.InsertAtEnd(10);
5857
cll.InsertAtEnd(20);
5958
cll.InsertAtEnd(30);
60-
6159
cll.InsertAfter(20, 25);
60+
6261
Assert.That("10 20 25 30", Is.EqualTo(GetDisplayOutput(cll).Trim()));
6362
}
6463

@@ -67,6 +66,7 @@ public static void TestInsertAtBeginningInEmptyList()
6766
{
6867
var cll = new CircularLinkedList<int>();
6968
cll.InsertAtBeginning(10);
69+
7070
Assert.That("10", Is.EqualTo(GetDisplayOutput(cll).Trim()));
7171
}
7272

@@ -83,22 +83,20 @@ public static void TestInsertAtEndInEmptyList()
8383
public static void TestInsertAfterInEmptyList()
8484
{
8585
var cll = new CircularLinkedList<int>();
86-
cll.InsertAfter(10, 20);
86+
var ex = Assert.Throws<InvalidOperationException>(() => cll.InsertAfter(10, 20));
8787

88-
Assert.That("List is empty.", Is.EqualTo(GetDisplayOutput(cll).Trim()));
88+
Assert.That(ex!.Message, Is.EqualTo("List is empty."));
8989
}
9090

91-
92-
9391
[Test]
9492
public static void TestInsertAfterSpecificNode()
9593
{
9694
var cll = new CircularLinkedList<int>();
9795
cll.InsertAtEnd(10);
9896
cll.InsertAtEnd(20);
9997
cll.InsertAtEnd(30);
100-
10198
cll.InsertAfter(20, 25); // Insert after node with value 20
99+
102100
Assert.That("10 20 25 30", Is.EqualTo(GetDisplayOutput(cll).Trim()));
103101
}
104102

@@ -108,6 +106,7 @@ public static void TestInsertAfterOnNonExistingValue()
108106
var cll = new CircularLinkedList<int>();
109107
cll.InsertAtEnd(10);
110108
cll.InsertAfter(99, 25); // 99 does not exist
109+
111110
Assert.That("10", Is.EqualTo(GetDisplayOutput(cll).Trim()));
112111
}
113112

@@ -118,8 +117,8 @@ public static void TestDeleteNode()
118117
cll.InsertAtEnd(10);
119118
cll.InsertAtEnd(20);
120119
cll.InsertAtEnd(30);
121-
122120
cll.DeleteNode(20);
121+
123122
Assert.That("10 30", Is.EqualTo(GetDisplayOutput(cll).Trim()));
124123
}
125124

@@ -129,6 +128,7 @@ public static void TestDeleteOnlyNode()
129128
var cll = new CircularLinkedList<int>();
130129
cll.InsertAtBeginning(10);
131130
cll.DeleteNode(10);
131+
132132
Assert.That(cll.IsEmpty(), Is.EqualTo(true));
133133
}
134134

@@ -140,6 +140,7 @@ public static void TestDeleteHeadNode()
140140
cll.InsertAtEnd(20);
141141
cll.InsertAtEnd(30);
142142
cll.DeleteNode(10);
143+
143144
Assert.That("20 30", Is.EqualTo(GetDisplayOutput(cll).Trim()));
144145
}
145146

@@ -151,15 +152,17 @@ public static void TestDeleteTailNode()
151152
cll.InsertAtEnd(20);
152153
cll.InsertAtEnd(30);
153154
cll.DeleteNode(30);
155+
154156
Assert.That("10 20", Is.EqualTo(GetDisplayOutput(cll).Trim()));
155157
}
156158

157159
[Test]
158160
public static void TestDeleteFromEmptyList()
159161
{
160162
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."));
163166
}
164167

165168
[Test]
@@ -169,8 +172,8 @@ public static void TestDeleteNonExistentNode()
169172
cll.InsertAtEnd(10);
170173
cll.InsertAtEnd(20);
171174
cll.InsertAtEnd(30);
172-
173175
cll.DeleteNode(40); // Attempting to delete a node that doesn't exist
176+
174177
Assert.That("10 20 30", Is.EqualTo(GetDisplayOutput(cll).Trim()));
175178
}
176179

DataStructures/LinkedList/CircularLinkedList/CircularLinkedList.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ public void InsertAfter(T value, T data)
7979
{
8080
if (IsEmpty())
8181
{
82-
Console.WriteLine("List is empty.");
83-
return;
82+
throw new InvalidOperationException("List is empty.");
8483
}
8584

8685
var current = tail!.Next;
@@ -103,8 +102,6 @@ public void InsertAfter(T value, T data)
103102
current = current.Next;
104103
}
105104
while (current != tail.Next);
106-
107-
Console.WriteLine($"Node with value {value} not found.");
108105
}
109106

110107
/// <summary>
@@ -115,8 +112,7 @@ public void DeleteNode(T value)
115112
{
116113
if (IsEmpty())
117114
{
118-
Console.WriteLine("List is empty.");
119-
return;
115+
throw new InvalidOperationException("List is empty.");
120116
}
121117

122118
var current = tail!.Next;
@@ -144,16 +140,13 @@ public void DeleteNode(T value)
144140
previous!.Next = current.Next;
145141
}
146142

147-
Console.WriteLine($"Node with value {value} deleted.");
148143
return;
149144
}
150145

151146
previous = current;
152147
current = current.Next;
153148
}
154149
while (current != tail!.Next);
155-
156-
Console.WriteLine($"Node with value {value} not found.");
157150
}
158151

159152
/// <summary>
@@ -163,8 +156,7 @@ public void Display()
163156
{
164157
if (IsEmpty())
165158
{
166-
Console.WriteLine("List is empty.");
167-
return;
159+
throw new InvalidOperationException("List is empty.");
168160
}
169161

170162
CircularLinkedListNode<T>? current = tail!.Next;

0 commit comments

Comments
 (0)