@@ -13,7 +13,6 @@ private static string Convert(string infixExpression) =>
1313 private static int Evaluate ( string postfixExpression ) =>
1414 InfixToPostfix . PostfixExpressionEvaluation ( postfixExpression ) ;
1515
16- // ---------- Infix to Postfix Tests ---------- //
1716
1817 [ Test ]
1918 public void InfixToPostfix_EmptyString_ThrowsArgumentException ( )
@@ -22,12 +21,25 @@ public void InfixToPostfix_EmptyString_ThrowsArgumentException()
2221 Assert . That ( ex ! . Message , Is . EqualTo ( "Infix cannot be null or empty." ) ) ;
2322 }
2423
24+ [ Test ]
25+ public void InfixToPostfix_WhitespaceOnly_ThrowsArgumentException ( )
26+ {
27+ var ex = Assert . Throws < ArgumentException > ( ( ) => Convert ( " " ) ) ;
28+ Assert . That ( ex ! . Message , Is . EqualTo ( "Infix cannot be null or empty." ) ) ;
29+ }
30+
2531 [ Test ]
2632 public void InfixToPostfix_SimpleExpression_ReturnsCorrectPostfix ( )
2733 {
2834 Assert . That ( Convert ( "A+B" ) , Is . EqualTo ( "AB+" ) ) ;
2935 }
3036
37+ [ Test ]
38+ public void InfixToPostfix_HandlesWhitespaceCorrectly ( )
39+ {
40+ Assert . That ( Convert ( " A + B " ) , Is . EqualTo ( "AB+" ) ) ;
41+ }
42+
3143 [ Test ]
3244 public void InfixToPostfix_WithParentheses_ReturnsCorrectPostfix ( )
3345 {
@@ -37,60 +49,173 @@ public void InfixToPostfix_WithParentheses_ReturnsCorrectPostfix()
3749 [ Test ]
3850 public void InfixToPostfix_ComplexExpression_ReturnsCorrectPostfix ( )
3951 {
40- Assert . That ( Convert ( "A+(B*C-(D/E^F)*G)*H" ) , Is . EqualTo ( "ABC*DEF^/G*-H*+" ) ) ;
52+ Assert . That ( Convert ( "A+(B*C-(D/E^F)*G)*H" ) ,
53+ Is . EqualTo ( "ABC*DEF^/G*-H*+" ) ) ;
54+ }
55+
56+ [ Test ]
57+ public void InfixToPostfix_OperatorPrecedence_IsCorrect ( )
58+ {
59+ Assert . That ( Convert ( "A+B*C^D" ) , Is . EqualTo ( "ABCD^*+" ) ) ;
60+ }
61+
62+ [ Test ]
63+ public void InfixToPostfix_MismatchedParentheses_ThrowsInvalidOperation ( )
64+ {
65+ var ex = Assert . Throws < InvalidOperationException > ( ( ) => Convert ( "(A+B" ) ) ;
66+ Assert . That ( ex ! . Message , Is . EqualTo ( "Mismatched parentheses." ) ) ;
4167 }
4268
4369 [ Test ]
4470 public void InfixToPostfix_InvalidCharacter_ThrowsArgumentException ( )
4571 {
4672 var ex = Assert . Throws < ArgumentException > ( ( ) => Convert ( "A+B#C" ) ) ;
47- Assert . That ( ex ! . Message , Does . Contain ( "Invalid character" ) ) ;
73+ Assert . That ( ex ! . Message , Is . EqualTo ( "Invalid character #. " ) ) ;
4874 }
4975
5076 [ Test ]
51- public void InfixToPostfix_MismatchedParentheses_ThrowsInvalidOperation ( )
77+ public void InfixToPostfix_LeftoverOperators_AreFlushedCorrectly ( )
5278 {
53- Assert . Throws < InvalidOperationException > ( ( ) => Convert ( "( A+B" ) ) ;
79+ Assert . That ( Convert ( "A+B+C" ) , Is . EqualTo ( "AB+C+ ") ) ;
5480 }
5581
56- // ---------- Postfix Evaluation Tests ---------- //
82+ // ---------- NEW FULL-COVERAGE TESTS BELOW ---------- //
5783
5884 [ Test ]
59- public void PostfixEvaluation_ComplexExpression_ReturnsCorrectValue ( )
85+ public void InfixToPostfix_ClosingParenthesisWithoutOpening_Throws ( )
6086 {
61- Assert . That ( Evaluate ( "23*54*+" ) , Is . EqualTo ( 26 ) ) ; // (2*3)+(5*4)
87+ var ex = Assert . Throws < InvalidOperationException > ( ( ) => Convert ( "A+B)C" ) ) ;
88+ Assert . That ( ex ! . Message , Is . EqualTo ( "Mismatched parentheses in expression." ) ) ;
6289 }
6390
91+ [ Test ]
92+ public void InfixToPostfix_LeftoverOpeningParenthesis_Throws ( )
93+ {
94+ var ex = Assert . Throws < InvalidOperationException > ( ( ) => Convert ( "(A+B" ) ) ;
95+ Assert . That ( ex ! . Message , Is . EqualTo ( "Mismatched parentheses." ) ) ;
96+ }
97+
98+ [ Test ]
99+ public void InfixToPostfix_TrailingOperator_ReturnsPostfixBasedOnLogic ( )
100+ {
101+ Assert . That ( Convert ( "A+" ) , Is . EqualTo ( "A+" ) ) ;
102+ }
103+
104+ [ Test ]
105+ public void InfixToPostfix_DeeplyNestedParentheses_WorksCorrectly ( )
106+ {
107+ Assert . That ( Convert ( "A+(B+(C+(D+E)))" ) , Is . EqualTo ( "ABCDE++++" ) ) ;
108+ }
109+
110+ [ Test ]
111+ public void InfixToPostfix_DoubleOperator_StillProcessesWithoutException ( )
112+ {
113+ Assert . That ( Convert ( "A++B" ) , Is . EqualTo ( "A+B+" ) ) ;
114+ }
115+
116+ // ---------------------------------------------------------
117+ // POSTFIX EVALUATION TESTS
118+ // ---------------------------------------------------------
119+
64120 [ Test ]
65121 public void PostfixEvaluation_EmptyString_ThrowsArgumentException ( )
66122 {
67123 var ex = Assert . Throws < ArgumentException > ( ( ) => Evaluate ( "" ) ) ;
68124 Assert . That ( ex ! . Message , Is . EqualTo ( "Postfix cannot be null or empty." ) ) ;
69125 }
70126
127+ [ Test ]
128+ public void PostfixEvaluation_WhitespaceOnly_ThrowsArgumentException ( )
129+ {
130+ Assert . Throws < ArgumentException > ( ( ) => Evaluate ( " " ) ) ;
131+ }
132+
133+ [ Test ]
134+ public void PostfixEvaluation_SimpleExpression_Works ( )
135+ {
136+ Assert . That ( Evaluate ( "23+" ) , Is . EqualTo ( 5 ) ) ;
137+ }
138+
139+ [ Test ]
140+ public void PostfixEvaluation_ComplexExpression_Works ( )
141+ {
142+ Assert . That ( Evaluate ( "23*54*+" ) , Is . EqualTo ( 26 ) ) ;
143+ }
144+
145+ [ Test ]
146+ public void PostfixEvaluation_HandlesWhitespaceCorrectly ( )
147+ {
148+ Assert . That ( Evaluate ( " 2 3 + " ) , Is . EqualTo ( 5 ) ) ;
149+ }
150+
151+ [ Test ]
152+ public void PostfixEvaluation_ExponentOperator_Works ( )
153+ {
154+ Assert . That ( Evaluate ( "23^" ) , Is . EqualTo ( 8 ) ) ;
155+ }
156+
71157 [ Test ]
72158 public void PostfixEvaluation_DivideByZero_ThrowsException ( )
73159 {
74- Assert . Throws < DivideByZeroException > ( ( ) => Evaluate ( "50/" ) ) ;
160+ var ex = Assert . Throws < DivideByZeroException > ( ( ) => Evaluate ( "50/" ) ) ;
161+ Assert . That ( ex ! . Message , Is . EqualTo ( "Cannot divide by zero" ) ) ;
75162 }
76163
77164 [ Test ]
78165 public void PostfixEvaluation_InsufficientOperands_ThrowsException ( )
79166 {
80- Assert . Throws < InvalidOperationException > ( ( ) => Evaluate ( "2+" ) ) ;
167+ var ex = Assert . Throws < InvalidOperationException > ( ( ) => Evaluate ( "2+" ) ) ;
168+ Assert . That ( ex ! . Message , Is . EqualTo ( "Insufficient operands" ) ) ;
81169 }
82170
83171 [ Test ]
84172 public void PostfixEvaluation_InvalidCharacter_ThrowsException ( )
85173 {
86174 var ex = Assert . Throws < InvalidOperationException > ( ( ) => Evaluate ( "23*X" ) ) ;
87- Assert . That ( ex ! . Message , Does . Contain ( "Invalid character" ) ) ;
175+ Assert . That ( ex ! . Message , Is . EqualTo ( "Invalid character in expression: X " ) ) ;
88176 }
89177
90178 [ Test ]
91179 public void PostfixEvaluation_LeftoverOperands_ThrowsException ( )
92180 {
93- Assert . Throws < InvalidOperationException > ( ( ) => Evaluate ( "23" ) ) ;
181+ var ex = Assert . Throws < InvalidOperationException > ( ( ) => Evaluate ( "23" ) ) ;
182+ Assert . That ( ex ! . Message , Is . EqualTo ( "Invalid postfix expression: Leftover operands." ) ) ;
183+ }
184+
185+ // ---------- NEW FULL-COVERAGE TESTS BELOW ---------- //
186+
187+ [ Test ]
188+ public void PostfixEvaluation_UnknownOperator_Throws ( )
189+ {
190+ var ex = Assert . Throws < InvalidOperationException > ( ( ) => Evaluate ( "23&" ) ) ;
191+ Assert . That ( ex ! . Message , Is . EqualTo ( "Invalid character in expression: &" ) ) ;
192+ }
193+
194+ [ Test ]
195+ public void PostfixEvaluation_OperatorWithoutEnoughOperands_Throws ( )
196+ {
197+ var ex = Assert . Throws < InvalidOperationException > ( ( ) => Evaluate ( "+" ) ) ;
198+ Assert . That ( ex ! . Message , Is . EqualTo ( "Insufficient operands" ) ) ;
199+ }
200+
201+ [ Test ]
202+ public void PostfixEvaluation_InvalidCharacterAmidExpression_Throws ( )
203+ {
204+ var ex = Assert . Throws < InvalidOperationException > ( ( ) => Evaluate ( "23X+" ) ) ;
205+ Assert . That ( ex ! . Message , Is . EqualTo ( "Invalid character in expression: X" ) ) ;
206+ }
207+
208+ [ Test ]
209+ public void PostfixEvaluation_ParenthesisCharacter_Throws ( )
210+ {
211+ var ex = Assert . Throws < InvalidOperationException > ( ( ) => Evaluate ( "23+)" ) ) ;
212+ Assert . That ( ex ! . Message , Does . Contain ( "Invalid character" ) ) ;
213+ }
214+
215+ [ Test ]
216+ public void PostfixEvaluation_LongExpression_Works ( )
217+ {
218+ Assert . That ( Evaluate ( "23*54*+62/-" ) , Is . EqualTo ( 23 ) ) ;
94219 }
95220 }
96221}
0 commit comments