72 lines
2.6 KiB
Go
72 lines
2.6 KiB
Go
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
|
// All rights reserved.
|
|
|
|
// t_fractions_test.go
|
|
package expr
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"git.portale-stac.it/go-pkg/expr/kern"
|
|
)
|
|
|
|
func TestFractionsParser(t *testing.T) {
|
|
section := "Fraction"
|
|
inputs := []inputType{
|
|
/* 1 */ {`1:2`, kern.NewFraction(1, 2), nil},
|
|
/* 2 */ {`1:2 + 1`, kern.NewFraction(3, 2), nil},
|
|
/* 3 */ {`1:2 - 1`, kern.NewFraction(-1, 2), nil},
|
|
/* 4 */ {`1:2 * 1`, kern.NewFraction(1, 2), nil},
|
|
/* 5 */ {`1:2 * 2:3`, kern.NewFraction(2, 6), nil},
|
|
/* 6 */ {`1:2 / 2:3`, kern.NewFraction(3, 4), nil},
|
|
/* 7 */ {`1:"5"`, nil, `denominator must be integer, got string (5)`},
|
|
/* 8 */ {`"1":5`, nil, `numerator must be integer, got string (1)`},
|
|
/* 9 */ {`1:+5`, kern.NewFraction(1, 5), nil},
|
|
/* 10 */ {`1:(-2)`, kern.NewFraction(-1, 2), nil},
|
|
/* 11 */ {`builtin "math.arith"; add(1:2, 2:3)`, kern.NewFraction(7, 6), nil},
|
|
/* 12 */ {`builtin "math.arith"; add(1:2, 1.0, 2)`, float64(3.5), nil},
|
|
/* 13 */ {`builtin "math.arith"; mul(1:2, 2:3)`, kern.NewFraction(2, 6), nil},
|
|
/* 14 */ {`builtin "math.arith"; mul(1:2, 1.0, 2)`, float64(1.0), nil},
|
|
/* 15 */ {`1:0`, nil, `[1:3] division by zero`},
|
|
/* 16 */ {`fract(-0.5)`, kern.NewFraction(-1, 2), nil},
|
|
/* 17 */ {`fract("")`, (*kern.FractionType)(nil), `bad syntax`},
|
|
/* 18 */ {`fract("-1")`, kern.NewFraction(-1, 1), nil},
|
|
/* 19 */ {`fract("+1")`, kern.NewFraction(1, 1), nil},
|
|
/* 20 */ {`fract("1a")`, (*kern.FractionType)(nil), `strconv.ParseInt: parsing "1a": invalid syntax`},
|
|
/* 21 */ {`fract(1,0)`, nil, `fract(): division by zero`},
|
|
/* 22 */ {`string(1:2)`, "1:2", nil},
|
|
/* 23 */ {`1+1:2+0.5`, float64(2), nil},
|
|
/* 24 */ {`1:(2-2)`, nil, `[1:3] division by zero`},
|
|
/* 25 */ {`[0,1][1-1]:1`, kern.NewFraction(0, 1), nil},
|
|
}
|
|
// runTestSuiteSpec(t, section, inputs, 25)
|
|
runTestSuite(t, section, inputs)
|
|
}
|
|
|
|
func TestFractionToStringSimple(t *testing.T) {
|
|
source := kern.NewFraction(1, 2)
|
|
want := "1:2"
|
|
got := source.ToString(0)
|
|
if got != want {
|
|
t.Errorf(`(1,2) -> result = %v [%T], want = %v [%T]`, got, got, want, want)
|
|
}
|
|
}
|
|
|
|
func TestFractionToStringMultiline(t *testing.T) {
|
|
source := kern.NewFraction(1, 2)
|
|
want := "1\n-\n2"
|
|
got := source.ToString(kern.MultiLine)
|
|
if got != want {
|
|
t.Errorf(`(1,2) -> result = %v [%T], want = %v [%T]`, got, got, want, want)
|
|
}
|
|
}
|
|
|
|
func TestToStringMultilineTty(t *testing.T) {
|
|
source := kern.NewFraction(-1, 2)
|
|
want := "\x1b[4m-1\x1b[0m\n 2"
|
|
got := source.ToString(kern.MultiLine | kern.TTY)
|
|
if got != want {
|
|
t.Errorf(`(1,2) -> result = %#v [%T], want = %#v [%T]`, got, got, want, want)
|
|
}
|
|
}
|