|
package postfix |
|
|
|
import ( |
|
"strings" |
|
"testing" |
|
|
|
"github.com/stretchr/testify/assert" |
|
) |
|
|
|
func TestIsNumeric(t *testing.T) { |
|
assert.False(t, isNumeric(" ")) |
|
assert.False(t, isNumeric("+")) |
|
assert.False(t, isNumeric("-")) |
|
assert.False(t, isNumeric("/")) |
|
assert.False(t, isNumeric("*")) |
|
assert.False(t, isNumeric("A")) // Intentional |
|
assert.False(t, isNumeric("B")) // Intentional |
|
assert.False(t, isNumeric("1 ")) // Intentional |
|
assert.False(t, isNumeric("-1")) // Intentional |
|
assert.False(t, isNumeric("2.5")) // Intentional |
|
assert.False(t, isNumeric("2,5")) // Intentional |
|
assert.True(t, isNumeric("0")) |
|
assert.True(t, isNumeric("1")) |
|
assert.True(t, isNumeric("2")) |
|
assert.True(t, isNumeric("251")) |
|
assert.True(t, isNumeric("0251")) |
|
} |
|
|
|
func TestIsOperator(t *testing.T) { |
|
assert.False(t, isOperator(" ")) |
|
assert.False(t, isOperator("a")) |
|
assert.False(t, isOperator("A")) |
|
assert.False(t, isOperator("!")) |
|
assert.False(t, isOperator("&")) |
|
assert.False(t, isOperator("*")) // Intentional |
|
assert.False(t, isOperator("/")) // Intentional |
|
assert.False(t, isOperator("- ")) // Intentional |
|
assert.True(t, isOperator("-")) |
|
assert.True(t, isOperator("+")) |
|
} |
|
|
|
func TestTokens(t *testing.T) { |
|
assert.Equal(t, []string{"25", "+", "8"}, tokens("25+8")) |
|
assert.Equal(t, []string{"25", "+", "8"}, tokens("25 + 8")) |
|
assert.Equal(t, []string{"25", "-", "8"}, tokens("25 - 8")) |
|
assert.Equal(t, []string{"25", "-", "(", "8", "+", "2", ")"}, tokens("25 - (8 + 2)")) |
|
} |
|
|
|
func TestFromInfix(t *testing.T) { |
|
assert.Equal(t, "3 4 +", fromInfix("3 + 4")) |
|
assert.Equal(t, "1 2 + 3 +", fromInfix("1+2+3")) |
|
assert.Equal(t, "25 3 + 2 +", fromInfix("25+3+2")) |
|
assert.Equal(t, "25 8 2 + -", fromInfix("25 - (8 + 2)")) |
|
} |
|
|
|
func fromInfix(infix string) string { |
|
return strings.Join(FromInfix(infix), " ") |
|
} |
|
|
|
func TestEval(t *testing.T) { |
|
assert.Equal(t, 3, Evaluate(FromInfix("2 + 1"))) |
|
assert.Equal(t, 6, Evaluate(FromInfix("1 + 2 + 3"))) |
|
assert.Equal(t, 10, Evaluate(FromInfix("3 + 3 + (2 + 2)"))) |
|
assert.Equal(t, 13, Evaluate(FromInfix("4 + 5 + (2 - 4 + (3 + 3))"))) |
|
assert.Equal(t, 13, Evaluate(FromInfix("(4 + 5 + (2 - 4 + (3 + 3)))"))) |
|
assert.Equal(t, 11, Evaluate(FromInfix("(4 + 5 + (2 - 4 + (3 + 3))) - 2"))) |
|
assert.Equal(t, 15, Evaluate(FromInfix("25 - (8 + 2)"))) |
|
assert.Equal(t, 15, Evaluate(FromInfix("(25 - (8 + 2))"))) |
|
assert.Equal(t, 16, Evaluate(FromInfix("(25 + 1 - (8 + 2))"))) |
|
assert.Equal(t, 14, Evaluate(FromInfix("(25 - 1 - (8 + 2))"))) |
|
assert.Equal(t, 23, Evaluate(FromInfix("(25 - 1 - (8 - (2 + 5)))"))) |
|
} |
|
|
|
func BenchmarkTokens(b *testing.B) { |
|
for n := 0; n < b.N; n++ { |
|
tokens("(4 + 5 + (2 - 4 + (3 + 3))) - 2") |
|
} |
|
} |
|
|
|
func BenchmarkFromInfix(b *testing.B) { |
|
for n := 0; n < b.N; n++ { |
|
FromInfix("(4 + 5 + (2 - 4 + (3 + 3))) - 2") |
|
} |
|
} |
|
|
|
func BenchmarkEvaluate(b *testing.B) { |
|
for n := 0; n < b.N; n++ { |
|
Evaluate([]string{"4", "5", "+", "2", "4", "-", "3", "3", "+", "+", "+", "2", "-"}) |
|
} |
|
} |
|
|
|
func BenchmarkEvaluateFromInfix(b *testing.B) { |
|
for n := 0; n < b.N; n++ { |
|
Evaluate(FromInfix("(4 + 5 + (2 - 4 + (3 + 3))) - 2")) |
|
} |
|
} |