Skip to content

test case for errors in evaluate_expression #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
remove error since there is none
  • Loading branch information
spring1843 committed Jun 11, 2023
commit 786cb44e0c1cfc6a551cb15ba001e69ee3108c3c
7 changes: 1 addition & 6 deletions stack/basic_calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ func BasicCalculator(input string) (float64, error) {
return -1, ErrImbalancedParenthesis
}

postfix, err := InfixToPostfix(toInfix(input))
if err != nil {
return -1, err
}

return EvaluatePostfixExpression(postfix)
return EvaluatePostfixExpression(InfixToPostfix(toInfix(input)))
}

func toInfix(input string) []string {
Expand Down
4 changes: 2 additions & 2 deletions stack/infix_to_postfix.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type operators struct {
}

// InfixToPostfix converts an infix expression to a postfix one supporting the 4 basic arithmetic operations and parentheses.
func InfixToPostfix(infix []string) ([]string, error) {
func InfixToPostfix(infix []string) []string {
output := []string{}
stack := new(operators)
for _, element := range infix {
Expand Down Expand Up @@ -47,7 +47,7 @@ func InfixToPostfix(infix []string) ([]string, error) {
output = append(output, stack.pop())
}

return output, nil
return output
}

func (operators *operators) pop() string {
Expand Down
27 changes: 11 additions & 16 deletions stack/infix_to_postfix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,21 @@ import (

func TestInfixToPostfix(t *testing.T) {
tests := []struct {
infix []string
expectErr bool
postfix []string
infix []string
postfix []string
}{
{[]string{""}, false, []string{""}},
{[]string{"a", "+", "b"}, false, []string{"a", "b", "+"}},
{[]string{"a", "-", "b", "+", "c"}, false, []string{"a", "b", "c", "+", "-"}},
{[]string{"a", "-", "(", "b", "+", "c", ")"}, false, []string{"a", "b", "c", "+", "-"}},
{[]string{"a", "+", "b", "-", "c"}, false, []string{"a", "b", "c", "-", "+"}},
{[]string{"a", "/", "b"}, false, []string{"a", "b", "/"}},
{[]string{"1", "*", "2", "+", "3", "+", "4", "*", "5"}, false, []string{"1", "2", "3", "4", "5", "*", "+", "+", "*"}},
{[]string{"1", "*", "(", "2", "+", "3", ")", "+", "4", "*", "5"}, false, []string{"1", "2", "3", "+", "4", "5", "*", "+", "*"}},
{[]string{""}, []string{""}},
{[]string{"a", "+", "b"}, []string{"a", "b", "+"}},
{[]string{"a", "-", "b", "+", "c"}, []string{"a", "b", "c", "+", "-"}},
{[]string{"a", "-", "(", "b", "+", "c", ")"}, []string{"a", "b", "c", "+", "-"}},
{[]string{"a", "+", "b", "-", "c"}, []string{"a", "b", "c", "-", "+"}},
{[]string{"a", "/", "b"}, []string{"a", "b", "/"}},
{[]string{"1", "*", "2", "+", "3", "+", "4", "*", "5"}, []string{"1", "2", "3", "4", "5", "*", "+", "+", "*"}},
{[]string{"1", "*", "(", "2", "+", "3", ")", "+", "4", "*", "5"}, []string{"1", "2", "3", "+", "4", "5", "*", "+", "*"}},
}

for i, test := range tests {
got, err := InfixToPostfix(test.infix)
if err != nil && !test.expectErr {
t.Fatalf("No error expected. Error: %s", err)
}
if !reflect.DeepEqual(got, test.postfix) {
if got := InfixToPostfix(test.infix); !reflect.DeepEqual(got, test.postfix) {
t.Fatalf("Failed test case #%d. Want %#v got %#v", i, test.postfix, got)
}
}
Expand Down