-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add BalancedParenthesesChecker, NextGreaterElement, and ReverseStack (#…
…473)
- Loading branch information
1 parent
cf19352
commit 36a7dd6
Showing
7 changed files
with
493 additions
and
0 deletions.
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
Algorithms.Tests/Stack/BalancedParenthesesCheckerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
using System; | ||
using Algorithms.Stack; | ||
using NUnit.Framework; | ||
|
||
namespace Algorithms.Tests.Stack | ||
{ | ||
[TestFixture] | ||
public class BalancedParenthesesCheckerTests | ||
{ | ||
public static bool IsBalanced(string expression) | ||
{ | ||
var checker = new BalancedParenthesesChecker(); | ||
return checker.IsBalanced(expression); | ||
} | ||
|
||
[Test] | ||
public void IsBalanced_EmptyString_ThrowsArgumentException() | ||
{ | ||
// Arrange | ||
var expression = string.Empty; | ||
|
||
// Act & Assert | ||
var ex = Assert.Throws<ArgumentException>(() => IsBalanced(expression)); | ||
|
||
if(ex!=null) | ||
{ | ||
Assert.That(ex.Message, Is.EqualTo("The input expression cannot be null or empty.")); | ||
} | ||
|
||
} | ||
|
||
[Test] | ||
public void IsBalanced_ValidBalancedExpression_ReturnsTrue() | ||
{ | ||
// Arrange | ||
var expression = "{[()]}"; | ||
|
||
// Act | ||
var result = IsBalanced(expression); | ||
|
||
// Assert | ||
Assert.That(result, Is.EqualTo(true)); | ||
} | ||
|
||
[Test] | ||
public void IsBalanced_ValidUnbalancedExpression_ReturnsFalse() | ||
{ | ||
// Arrange | ||
var expression = "{[(])}"; | ||
|
||
// Act | ||
var result = IsBalanced(expression); | ||
|
||
// Assert | ||
Assert.That(result, Is.EqualTo(false)); | ||
} | ||
|
||
[Test] | ||
public void IsBalanced_UnbalancedWithExtraClosingBracket_ReturnsFalse() | ||
{ | ||
// Arrange | ||
var expression = "{[()]}]"; | ||
|
||
// Act | ||
var result = IsBalanced(expression); | ||
|
||
// Assert | ||
Assert.That(result, Is.EqualTo(false)); | ||
} | ||
|
||
[Test] | ||
public void IsBalanced_ExpressionWithInvalidCharacters_ThrowsArgumentException() | ||
{ | ||
// Arrange | ||
var expression = "{[a]}"; | ||
|
||
// Act & Assert | ||
var ex = Assert.Throws<ArgumentException>(() => IsBalanced(expression)); | ||
if (ex != null) | ||
{ | ||
Assert.That(ex.Message, Is.EqualTo("Invalid character 'a' found in the expression.")); | ||
} | ||
} | ||
|
||
[Test] | ||
public void IsBalanced_SingleOpeningBracket_ReturnsFalse() | ||
{ | ||
// Arrange | ||
var expression = "("; | ||
|
||
// Act | ||
var result = IsBalanced(expression); | ||
|
||
// Assert | ||
Assert.That(result, Is.EqualTo(false)); | ||
} | ||
|
||
[Test] | ||
public void IsBalanced_SingleClosingBracket_ReturnsFalse() | ||
{ | ||
// Arrange | ||
var expression = ")"; | ||
|
||
// Act | ||
var result = IsBalanced(expression); | ||
|
||
// Assert | ||
Assert.That(result, Is.EqualTo(false)); | ||
} | ||
|
||
[Test] | ||
public void IsBalanced_ExpressionWithMultipleBalancedBrackets_ReturnsTrue() | ||
{ | ||
// Arrange | ||
var expression = "[{()}]()"; | ||
|
||
// Act | ||
var result = IsBalanced(expression); | ||
|
||
// Assert | ||
Assert.That(result, Is.EqualTo(true)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
using System; | ||
using Algorithms.Stack; | ||
using NUnit.Framework; | ||
|
||
namespace Algorithms.Tests.Stack | ||
{ | ||
[TestFixture] | ||
public class NextGreaterElementTests | ||
{ | ||
private static int[] FindNextGreaterElement(int[] input) | ||
{ | ||
var obj = new NextGreaterElement(); | ||
return obj.FindNextGreaterElement(input); | ||
} | ||
|
||
[Test] | ||
public void FindNextGreaterElement_InputIsEmpty_ReturnsEmptyArray() | ||
{ | ||
// Arrange | ||
int[] input = Array.Empty<int>(); | ||
int[] expected = Array.Empty<int>(); | ||
|
||
// Act | ||
var result = FindNextGreaterElement(input); | ||
|
||
// Assert | ||
Assert.That(result, Is.EqualTo(expected)); | ||
} | ||
|
||
[Test] | ||
public void FindNextGreaterElement_BasicScenario_ReturnsCorrectResult() | ||
{ | ||
// Arrange | ||
int[] input = { 4, 5, 2, 25 }; | ||
int[] expected = { 5, 25, 25, -1 }; | ||
|
||
// Act | ||
var result = FindNextGreaterElement(input); | ||
|
||
// Assert | ||
Assert.That(result, Is.EqualTo(expected)); | ||
} | ||
|
||
[Test] | ||
public void FindNextGreaterElement_NoNextGreaterElement_ReturnsCorrectResult() | ||
{ | ||
// Arrange | ||
int[] input = { 13, 7, 6, 12 }; | ||
int[] expected = { -1, 12, 12, -1 }; | ||
|
||
// Act | ||
var result = FindNextGreaterElement(input); | ||
|
||
// Assert | ||
Assert.That(result, Is.EqualTo(expected)); | ||
} | ||
|
||
[Test] | ||
public void FindNextGreaterElement_AllElementsHaveNoGreaterElement_ReturnsAllNegativeOnes() | ||
{ | ||
// Arrange | ||
int[] input = { 5, 4, 3, 2, 1 }; | ||
int[] expected = { -1, -1, -1, -1, -1 }; | ||
|
||
// Act | ||
var result = FindNextGreaterElement(input); | ||
|
||
// Assert | ||
Assert.That(result, Is.EqualTo(expected)); | ||
} | ||
|
||
[Test] | ||
public void FindNextGreaterElement_InputWithDuplicates_ReturnsCorrectResult() | ||
{ | ||
// Arrange | ||
int[] input = { 4, 4, 3, 2, 4 }; | ||
int[] expected = { -1, -1, 4, 4, -1 }; | ||
|
||
// Act | ||
var result = FindNextGreaterElement(input); | ||
|
||
// Assert | ||
Assert.That(result, Is.EqualTo(expected)); | ||
} | ||
|
||
[Test] | ||
public void FindNextGreaterElement_SingleElementArray_ReturnsNegativeOne() | ||
{ | ||
// Arrange | ||
int[] input = { 10 }; | ||
int[] expected = { -1 }; | ||
|
||
// Act | ||
var result = FindNextGreaterElement(input); | ||
|
||
// Assert | ||
Assert.That(result, Is.EqualTo(expected)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using Algorithms.Stack; | ||
using NUnit.Framework; | ||
using System.Collections.Generic; | ||
|
||
|
||
namespace Algorithms.Tests.Stack | ||
{ | ||
public class ReverseStackTests | ||
{ | ||
public static void Reverse<T>(Stack<T> stack) | ||
{ | ||
var obj = new ReverseStack(); | ||
obj.Reverse(stack); | ||
} | ||
|
||
[Test] | ||
public void Reverse_EmptyStack_DoesNotChangeStack() | ||
{ | ||
// Arrange | ||
Stack<int> stack = new Stack<int>(); | ||
|
||
// Act | ||
Reverse(stack); | ||
|
||
// Assert | ||
Assert.That(stack.Count, Is.EqualTo(0)); | ||
} | ||
|
||
[Test] | ||
public void Reverse_SingleElementStack_DoesNotChangeStack() | ||
{ | ||
// Arrange | ||
Stack<int> stack = new Stack<int>(); | ||
stack.Push(1); | ||
|
||
// Act | ||
Reverse(stack); | ||
|
||
// Assert | ||
Assert.That(stack.Count, Is.EqualTo(1)); | ||
Assert.That(stack.Peek(), Is.EqualTo(1)); | ||
} | ||
|
||
[Test] | ||
public void Reverse_MultipleElementStack_ReturnsCorrectOrder() | ||
{ | ||
// Arrange | ||
Stack<int> stack = new Stack<int>(); | ||
stack.Push(1); | ||
stack.Push(2); | ||
stack.Push(3); | ||
// The stack is now [3, 2, 1] (top to bottom) | ||
|
||
// Act | ||
Reverse(stack); | ||
|
||
// Assert | ||
Assert.That(stack.Count, Is.EqualTo(3)); | ||
Assert.That(stack.Pop(), Is.EqualTo(1)); // Should return 1 | ||
Assert.That(stack.Pop(), Is.EqualTo(2)); // Should return 2 | ||
Assert.That(stack.Pop(), Is.EqualTo(3)); // Should return 3 | ||
} | ||
|
||
[Test] | ||
public void Reverse_StackWithDuplicates_ReturnsCorrectOrder() | ||
{ | ||
// Arrange | ||
Stack<int> stack = new Stack<int>(); | ||
stack.Push(1); | ||
stack.Push(2); | ||
stack.Push(1); | ||
// The stack is now [1, 2, 1] (top to bottom) | ||
|
||
// Act | ||
Reverse(stack); | ||
|
||
// Assert | ||
Assert.That(stack.Count, Is.EqualTo(3)); | ||
Assert.That(stack.Pop(), Is.EqualTo(1)); // Should return 1 | ||
Assert.That(stack.Pop(), Is.EqualTo(2)); // Should return 2 | ||
Assert.That(stack.Pop(), Is.EqualTo(1)); // Should return 1 | ||
} | ||
} | ||
} |
Oops, something went wrong.