Skip to content

Commit 64fe596

Browse files
Added tests, updated document comments and resolved build and codacy issues
1 parent 03b60ad commit 64fe596

File tree

7 files changed

+382
-35
lines changed

7 files changed

+382
-35
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
using System;
2+
using Algorithms.Stack;
3+
using NUnit.Framework;
4+
5+
namespace Algorithms.Tests.Stack
6+
{
7+
[TestFixture]
8+
public class BalancedParenthesesCheckerTests
9+
{
10+
public static bool IsBalanced(string expression)
11+
{
12+
var checker = new BalancedParenthesesChecker();
13+
return checker.IsBalanced(expression);
14+
}
15+
16+
[Test]
17+
public void IsBalanced_EmptyString_ThrowsArgumentException()
18+
{
19+
// Arrange
20+
var expression = string.Empty;
21+
22+
// Act & Assert
23+
var ex = Assert.Throws<ArgumentException>(() => IsBalanced(expression));
24+
25+
if(ex!=null)
26+
{
27+
Assert.That(ex.Message, Is.EqualTo("The input expression cannot be null or empty."));
28+
}
29+
30+
}
31+
32+
[Test]
33+
public void IsBalanced_ValidBalancedExpression_ReturnsTrue()
34+
{
35+
// Arrange
36+
var expression = "{[()]}";
37+
38+
// Act
39+
var result = IsBalanced(expression);
40+
41+
// Assert
42+
Assert.That(result, Is.EqualTo(true));
43+
}
44+
45+
[Test]
46+
public void IsBalanced_ValidUnbalancedExpression_ReturnsFalse()
47+
{
48+
// Arrange
49+
var expression = "{[(])}";
50+
51+
// Act
52+
var result = IsBalanced(expression);
53+
54+
// Assert
55+
Assert.That(result, Is.EqualTo(false));
56+
}
57+
58+
[Test]
59+
public void IsBalanced_UnbalancedWithExtraClosingBracket_ReturnsFalse()
60+
{
61+
// Arrange
62+
var expression = "{[()]}]";
63+
64+
// Act
65+
var result = IsBalanced(expression);
66+
67+
// Assert
68+
Assert.That(result, Is.EqualTo(false));
69+
}
70+
71+
[Test]
72+
public void IsBalanced_ExpressionWithInvalidCharacters_ThrowsArgumentException()
73+
{
74+
// Arrange
75+
var expression = "{[a]}";
76+
77+
// Act & Assert
78+
var ex = Assert.Throws<ArgumentException>(() => IsBalanced(expression));
79+
if (ex != null)
80+
{
81+
Assert.That(ex.Message, Is.EqualTo("Invalid character 'a' found in the expression."));
82+
}
83+
}
84+
85+
[Test]
86+
public void IsBalanced_SingleOpeningBracket_ReturnsFalse()
87+
{
88+
// Arrange
89+
var expression = "(";
90+
91+
// Act
92+
var result = IsBalanced(expression);
93+
94+
// Assert
95+
Assert.That(result, Is.EqualTo(false));
96+
}
97+
98+
[Test]
99+
public void IsBalanced_SingleClosingBracket_ReturnsFalse()
100+
{
101+
// Arrange
102+
var expression = ")";
103+
104+
// Act
105+
var result = IsBalanced(expression);
106+
107+
// Assert
108+
Assert.That(result, Is.EqualTo(false));
109+
}
110+
111+
[Test]
112+
public void IsBalanced_ExpressionWithMultipleBalancedBrackets_ReturnsTrue()
113+
{
114+
// Arrange
115+
var expression = "[{()}]()";
116+
117+
// Act
118+
var result = IsBalanced(expression);
119+
120+
// Assert
121+
Assert.That(result, Is.EqualTo(true));
122+
}
123+
}
124+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using System;
2+
using Algorithms.Stack;
3+
using NUnit.Framework;
4+
5+
namespace Algorithms.Tests.Stack
6+
{
7+
[TestFixture]
8+
public class NextGreaterElementTests
9+
{
10+
private static int[] FindNextGreaterElement(int[] input)
11+
{
12+
var obj = new NextGreaterElement();
13+
return obj.FindNextGreaterElement(input);
14+
}
15+
16+
[Test]
17+
public void FindNextGreaterElement_InputIsEmpty_ReturnsEmptyArray()
18+
{
19+
// Arrange
20+
int[] input = Array.Empty<int>();
21+
int[] expected = Array.Empty<int>();
22+
23+
// Act
24+
var result = FindNextGreaterElement(input);
25+
26+
// Assert
27+
Assert.That(result, Is.EqualTo(expected));
28+
}
29+
30+
[Test]
31+
public void FindNextGreaterElement_BasicScenario_ReturnsCorrectResult()
32+
{
33+
// Arrange
34+
int[] input = { 4, 5, 2, 25 };
35+
int[] expected = { 5, 25, 25, -1 };
36+
37+
// Act
38+
var result = FindNextGreaterElement(input);
39+
40+
// Assert
41+
Assert.That(result, Is.EqualTo(expected));
42+
}
43+
44+
[Test]
45+
public void FindNextGreaterElement_NoNextGreaterElement_ReturnsCorrectResult()
46+
{
47+
// Arrange
48+
int[] input = { 13, 7, 6, 12 };
49+
int[] expected = { -1, 12, 12, -1 };
50+
51+
// Act
52+
var result = FindNextGreaterElement(input);
53+
54+
// Assert
55+
Assert.That(result, Is.EqualTo(expected));
56+
}
57+
58+
[Test]
59+
public void FindNextGreaterElement_AllElementsHaveNoGreaterElement_ReturnsAllNegativeOnes()
60+
{
61+
// Arrange
62+
int[] input = { 5, 4, 3, 2, 1 };
63+
int[] expected = { -1, -1, -1, -1, -1 };
64+
65+
// Act
66+
var result = FindNextGreaterElement(input);
67+
68+
// Assert
69+
Assert.That(result, Is.EqualTo(result));
70+
}
71+
72+
[Test]
73+
public void FindNextGreaterElement_InputWithDuplicates_ReturnsCorrectResult()
74+
{
75+
// Arrange
76+
int[] input = { 4, 4, 3, 2, 4 };
77+
int[] expected = { -1, -1, 4, 4, -1 };
78+
79+
// Act
80+
var result = FindNextGreaterElement(input);
81+
82+
// Assert
83+
Assert.That(result, Is.EqualTo(expected));
84+
}
85+
86+
[Test]
87+
public void FindNextGreaterElement_SingleElementArray_ReturnsNegativeOne()
88+
{
89+
// Arrange
90+
int[] input = { 10 };
91+
int[] expected = { -1 };
92+
93+
// Act
94+
var result = FindNextGreaterElement(input);
95+
96+
// Assert
97+
Assert.That(result, Is.EqualTo(expected));
98+
}
99+
}
100+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using Algorithms.Stack;
2+
using NUnit.Framework;
3+
using System.Collections.Generic;
4+
5+
6+
namespace Algorithms.Tests.Stack
7+
{
8+
public class ReverseStackTests
9+
{
10+
public static void Reverse<T>(Stack<T> stack)
11+
{
12+
var obj = new ReverseStack();
13+
obj.Reverse(stack);
14+
}
15+
16+
[Test]
17+
public void Reverse_EmptyStack_DoesNotChangeStack()
18+
{
19+
// Arrange
20+
Stack<int> stack = new Stack<int>();
21+
22+
// Act
23+
Reverse(stack);
24+
25+
// Assert
26+
Assert.That(stack.Count, Is.EqualTo(0));
27+
}
28+
29+
[Test]
30+
public void Reverse_SingleElementStack_DoesNotChangeStack()
31+
{
32+
// Arrange
33+
Stack<int> stack = new Stack<int>();
34+
stack.Push(1);
35+
36+
// Act
37+
Reverse(stack);
38+
39+
// Assert
40+
Assert.That(stack.Count, Is.EqualTo(1));
41+
Assert.That(stack.Peek(), Is.EqualTo(1));
42+
}
43+
44+
[Test]
45+
public void Reverse_MultipleElementStack_ReturnsCorrectOrder()
46+
{
47+
// Arrange
48+
Stack<int> stack = new Stack<int>();
49+
stack.Push(1);
50+
stack.Push(2);
51+
stack.Push(3);
52+
// The stack is now [3, 2, 1] (top to bottom)
53+
54+
// Act
55+
Reverse(stack);
56+
57+
// Assert
58+
Assert.That(stack.Count, Is.EqualTo(3));
59+
Assert.That(stack.Pop(), Is.EqualTo(1)); // Should return 1
60+
Assert.That(stack.Pop(), Is.EqualTo(2)); // Should return 2
61+
Assert.That(stack.Pop(), Is.EqualTo(3)); ; // Should return 3
62+
}
63+
64+
[Test]
65+
public void Reverse_StackWithDuplicates_ReturnsCorrectOrder()
66+
{
67+
// Arrange
68+
Stack<int> stack = new Stack<int>();
69+
stack.Push(1);
70+
stack.Push(2);
71+
stack.Push(1);
72+
// The stack is now [1, 2, 1] (top to bottom)
73+
74+
// Act
75+
Reverse(stack);
76+
77+
// Assert
78+
Assert.That(stack.Count, Is.EqualTo(3));
79+
Assert.That(stack.Pop(), Is.EqualTo(1)); // Should return 1
80+
Assert.That(stack.Pop(), Is.EqualTo(2)); // Should return 2
81+
Assert.That(stack.Pop(), Is.EqualTo(1)); // Should return 1
82+
}
83+
}
84+
}
Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,43 @@
11
using System;
2-
namespace DataStructures.Stack
2+
using System.Collections.Generic;
3+
4+
namespace Algorithms.Stack
35
{
46
/// <summary>
57
/// It checks if an expression has matching and balanced parentheses.
6-
/// @author Mohit Singh
7-
/// @author <a href="https://github.com/mohit-gogitter">mohit-gogitter</a>
8+
/// @author Mohit Singh. <a href="https://github.com/mohit-gogitter">mohit-gogitter</a>
89
/// </summary>
910
public class BalancedParenthesesChecker
1011
{
11-
private static readonly Dictionary<char, char> ParenthesesMap = new Dictionary<char, char>()
12+
private static readonly Dictionary<char, char> ParenthesesMap = new Dictionary<char, char>
1213
{
1314
{ '(', ')' },
1415
{ '{', '}' },
1516
{ '[', ']' },
1617
};
18+
1719
/// <summary>
18-
/// This method checks if an expression has matching and balanced parentheses.
20+
/// Determines if a given string expression containing brackets is balanced.
21+
/// A string is considered balanced if all opening brackets have corresponding closing brackets
22+
/// in the correct order. The supported brackets are '()', '{}', and '[]'.
1923
/// </summary>
20-
/// <param name="expression">string containing parenthesis</param>
21-
/// <returns>Boolean value</returns>
22-
public static bool IsBalanced(string expression)
24+
/// <param name="expression">
25+
/// The input string expression containing the brackets to check for balance.
26+
/// </param>
27+
/// <returns>
28+
/// <c>true</c> if the brackets in the expression are balanced; otherwise, <c>false</c>.
29+
/// </returns>
30+
/// <exception cref="ArgumentException">
31+
/// Thrown when the input expression contains invalid characters or is null/empty.
32+
/// Only '(', ')', '{', '}', '[', ']' characters are allowed.
33+
/// </exception>
34+
public bool IsBalanced(string expression)
2335
{
36+
if (string.IsNullOrEmpty(expression))
37+
{
38+
throw new ArgumentException("The input expression cannot be null or empty.");
39+
}
40+
2441
Stack<char> stack = new Stack<char>();
2542
foreach (char c in expression)
2643
{
@@ -34,6 +51,7 @@ public static bool IsBalanced(string expression)
3451
{
3552
return false;
3653
}
54+
3755
char open = stack.Pop();
3856

3957
if (!IsMatchingPair(open, c))
@@ -43,14 +61,17 @@ public static bool IsBalanced(string expression)
4361
}
4462
else
4563
{
46-
//since there are no other brackets, this is unreachable code
64+
// Throw an exception if an invalid character is found
65+
throw new ArgumentException($"Invalid character '{c}' found in the expression.");
4766
}
4867
}
68+
4969
return stack.Count == 0;
5070
}
71+
5172
private static bool IsMatchingPair(char open, char close)
5273
{
5374
return ParenthesesMap.ContainsKey(open) && ParenthesesMap[open] == close;
5475
}
5576
}
56-
}
77+
}

0 commit comments

Comments
 (0)