staros/math_test.go

62 lines
1.2 KiB
Go
Raw Permalink Normal View History

package staros
import (
"math"
"testing"
)
func TestCalcCompatibilityExpressions(t *testing.T) {
tests := []struct {
expr string
want float64
}{
{"1+2*3", 7},
{"(1+2)*3", 9},
{"60*60*24", 86400},
{"-1+2", 1},
{"sqrt(4)+abs(-3)", 5},
{"sin(pi/2)", 1},
{"arcsin(1)", math.Pi / 2},
{"asin(1)", math.Pi / 2},
{"loge(e)", 1},
{"ln(e)", 1},
{"log10(100)+log2(8)", 5},
{"floor(1.9)+ceil(1.1)+round(1.5)+trunc(1.9)", 6},
{"1.2e3+3", 1203},
{"pow(2,3)+hypot(3,4)", 13},
{"min(3,1,2)+max(3,1,2)", 4},
{"log(100)+rad(180)/pi+deg(pi)/180", 4},
{"cbrt(27)+exp(0)", 4},
{"sinh(0)+cosh(0)+tanh(0)", 1},
}
for _, tt := range tests {
got, err := Calc(tt.expr)
if err != nil {
t.Fatalf("Calc(%q) failed: %v", tt.expr, err)
}
if math.Abs(got-tt.want) > 1e-12 {
t.Fatalf("Calc(%q)=%v, want %v", tt.expr, got, tt.want)
}
}
}
func TestCalcRejectsInvalidExpressions(t *testing.T) {
tests := []string{
"",
"1/",
"(1+2",
"1/0",
"unknown(1)",
"min()",
"pow(2)",
"pow(2,3,4)",
"sqrt(1,2)",
"pi()",
}
for _, expr := range tests {
if got, err := Calc(expr); err == nil {
t.Fatalf("Calc(%q)=%v, expected error", expr, got)
}
}
}