mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-05 03:17:50 +08:00
Remove the "fix int" terminology; just call it "int".
This commit is contained in:
parent
e2c4030728
commit
6052a4dc4a
|
@ -219,7 +219,7 @@ func rangeFn(fm *Frame, opts rangeOpts, args ...vals.Num) error {
|
|||
What: "step", Valid: "positive", Actual: vals.ToString(step)}
|
||||
}
|
||||
}
|
||||
nums := vals.UnifyNums(rawNums, vals.FixInt)
|
||||
nums := vals.UnifyNums(rawNums, vals.Int)
|
||||
|
||||
out := fm.OutputChan()
|
||||
switch nums := nums.(type) {
|
||||
|
|
|
@ -14,7 +14,7 @@ import (
|
|||
|
||||
const (
|
||||
zeros = "0000000000000000000"
|
||||
// Values that exceed the range of int64, used for testing BigInt.
|
||||
// Values that exceed the range of int64, used for testing bigint.
|
||||
z = "1" + zeros + "0"
|
||||
z1 = "1" + zeros + "1" // z+1
|
||||
z2 = "1" + zeros + "2" // z+2
|
||||
|
@ -43,92 +43,92 @@ func TestFloat64(t *testing.T) {
|
|||
|
||||
func TestNumCmp(t *testing.T) {
|
||||
Test(t,
|
||||
// FixInt
|
||||
// int
|
||||
That("< 1 2 3").Puts(true),
|
||||
That("< 1 3 2").Puts(false),
|
||||
// BigInt
|
||||
// bigint
|
||||
That("< "+args(z1, z2, z3)).Puts(true),
|
||||
That("< "+args(z1, z3, z2)).Puts(false),
|
||||
// BigInt and FixInt
|
||||
// bigint and int
|
||||
That("< "+args("1", z1)).Puts(true),
|
||||
// BigRat
|
||||
// bigrat
|
||||
That("< 1/4 1/3 1/2").Puts(true),
|
||||
That("< 1/4 1/2 1/3").Puts(false),
|
||||
// BigRat, BigInt and FixInt
|
||||
// bigrat, bigint and int
|
||||
That("< "+args("1/2", "1", z1)).Puts(true),
|
||||
That("< "+args("1/2", z1, "1")).Puts(false),
|
||||
// Float
|
||||
// float64
|
||||
That("< 1.0 2.0 3.0").Puts(true),
|
||||
That("< 1.0 3.0 2.0").Puts(false),
|
||||
// Float, BigRat and FixInt
|
||||
// float64, bigrat and int
|
||||
That("< 1.0 3/2 2").Puts(true),
|
||||
That("< 1.0 2 3/2").Puts(false),
|
||||
|
||||
// Mixing of types not tested for commands below; they share the same
|
||||
// code path as <.
|
||||
|
||||
// FixInt
|
||||
// int
|
||||
That("<= 1 1 2").Puts(true),
|
||||
That("<= 1 2 1").Puts(false),
|
||||
// BigInt
|
||||
// bigint
|
||||
That("<= "+args(z1, z1, z2)).Puts(true),
|
||||
That("<= "+args(z1, z2, z1)).Puts(false),
|
||||
// BigRat
|
||||
// bigrat
|
||||
That("<= 1/3 1/3 1/2").Puts(true),
|
||||
That("<= 1/3 1/2 1/1").Puts(true),
|
||||
// Float
|
||||
// float64
|
||||
That("<= 1.0 1.0 2.0").Puts(true),
|
||||
That("<= 1.0 2.0 1.0").Puts(false),
|
||||
|
||||
// FixInt
|
||||
// int
|
||||
That("== 1 1 1").Puts(true),
|
||||
That("== 1 2 1").Puts(false),
|
||||
// BigInt
|
||||
// bigint
|
||||
That("== "+args(z1, z1, z1)).Puts(true),
|
||||
That("== "+args(z1, z2, z1)).Puts(false),
|
||||
// BigRat
|
||||
// bigrat
|
||||
That("== 1/2 1/2 1/2").Puts(true),
|
||||
That("== 1/2 1/3 1/2").Puts(false),
|
||||
// Float
|
||||
// float64
|
||||
That("== 1.0 1.0 1.0").Puts(true),
|
||||
That("== 1.0 2.0 1.0").Puts(false),
|
||||
|
||||
// FixInt
|
||||
// int
|
||||
That("!= 1 2 1").Puts(true),
|
||||
That("!= 1 1 2").Puts(false),
|
||||
// BigInt
|
||||
// bigint
|
||||
That("!= "+args(z1, z2, z1)).Puts(true),
|
||||
That("!= "+args(z1, z1, z2)).Puts(false),
|
||||
// BigRat
|
||||
// bigrat
|
||||
That("!= 1/2 1/3 1/2").Puts(true),
|
||||
That("!= 1/2 1/2 1/3").Puts(false),
|
||||
// Float
|
||||
// float64
|
||||
That("!= 1.0 2.0 1.0").Puts(true),
|
||||
That("!= 1.0 1.0 2.0").Puts(false),
|
||||
|
||||
// FixInt
|
||||
// int
|
||||
That("> 3 2 1").Puts(true),
|
||||
That("> 3 1 2").Puts(false),
|
||||
// BigInt
|
||||
// bigint
|
||||
That("> "+args(z3, z2, z1)).Puts(true),
|
||||
That("> "+args(z3, z1, z2)).Puts(false),
|
||||
// BigRat
|
||||
// bigrat
|
||||
That("> 1/2 1/3 1/4").Puts(true),
|
||||
That("> 1/2 1/4 1/3").Puts(false),
|
||||
// Float
|
||||
// float64
|
||||
That("> 3.0 2.0 1.0").Puts(true),
|
||||
That("> 3.0 1.0 2.0").Puts(false),
|
||||
|
||||
// FixInt
|
||||
// int
|
||||
That(">= 3 3 2").Puts(true),
|
||||
That(">= 3 2 3").Puts(false),
|
||||
// BigInt
|
||||
// bigint
|
||||
That(">= "+args(z3, z3, z2)).Puts(true),
|
||||
That(">= "+args(z3, z2, z3)).Puts(false),
|
||||
// BigRat
|
||||
// bigrat
|
||||
That(">= 1/2 1/2 1/3").Puts(true),
|
||||
That(">= 1/2 1/3 1/2").Puts(false),
|
||||
// Float
|
||||
// float64
|
||||
That(">= 3.0 3.0 2.0").Puts(true),
|
||||
That(">= 3.0 2.0 3.0").Puts(false),
|
||||
)
|
||||
|
@ -138,19 +138,19 @@ func TestArithmeticCommands(t *testing.T) {
|
|||
Test(t,
|
||||
// No argument
|
||||
That("+").Puts(0),
|
||||
// FixInt
|
||||
// int
|
||||
That("+ 233100 233").Puts(233333),
|
||||
// BigInt
|
||||
// bigint
|
||||
That("+ "+args(z, z1)).Puts(bigInt(zz1)),
|
||||
// BigInt and FixInt
|
||||
// bigint and int
|
||||
That("+ 1 2 "+z).Puts(bigInt(z3)),
|
||||
// BigRat
|
||||
// bigrat
|
||||
That("+ 1/2 1/3 1/4").Puts(big.NewRat(13, 12)),
|
||||
// BigRat, BigInt and FixInt
|
||||
// bigrat, bigint and int
|
||||
That("+ 1/2 1/2 1 "+z).Puts(bigInt(z2)),
|
||||
// Float
|
||||
// float64
|
||||
That("+ 0.5 0.25 1.0").Puts(1.75),
|
||||
// Float and other types
|
||||
// float64 and other types
|
||||
That("+ 0.5 1/4 1").Puts(1.75),
|
||||
|
||||
// Mixing of types not tested for commands below; they share the same
|
||||
|
@ -161,20 +161,20 @@ func TestArithmeticCommands(t *testing.T) {
|
|||
That("- "+z).Puts(bigInt("-"+z)),
|
||||
That("- 1/2").Puts(big.NewRat(-1, 2)),
|
||||
That("- 1.0").Puts(-1.0),
|
||||
// FixInt
|
||||
// int
|
||||
That("- 20 10 2").Puts(8),
|
||||
// BigInt
|
||||
// bigint
|
||||
That("- "+args(zz3, z1)).Puts(bigInt(z2)),
|
||||
// Float
|
||||
// float64
|
||||
That("- 2.0 1.0 0.5").Puts(0.5),
|
||||
|
||||
// No argument
|
||||
That("*").Puts(1),
|
||||
// FixInt
|
||||
// int
|
||||
That("* 2 7 4").Puts(56),
|
||||
// BigInt
|
||||
// bigint
|
||||
That("* 2 "+z1).Puts(bigInt(zz2)),
|
||||
// Float
|
||||
// float64
|
||||
That("* 2.0 0.5 1.75").Puts(1.75),
|
||||
// 0 * non-infinity
|
||||
That("* 0 1/2 1.0").Puts(0),
|
||||
|
@ -185,14 +185,14 @@ func TestArithmeticCommands(t *testing.T) {
|
|||
That("/ 2").Puts(big.NewRat(1, 2)),
|
||||
That("/ "+z).Puts(bigRat("1/"+z)),
|
||||
That("/ 2.0").Puts(0.5),
|
||||
// FixInt
|
||||
// int
|
||||
That("/ 233333 353").Puts(661),
|
||||
That("/ 3 4 2").Puts(big.NewRat(3, 8)),
|
||||
// BigInt
|
||||
// bigint
|
||||
That("/ "+args(zz, z)).Puts(2),
|
||||
That("/ "+args(zz, "2")).Puts(bigInt(z)),
|
||||
That("/ "+args(z1, z)).Puts(bigRat(z1+"/"+z)),
|
||||
// Float
|
||||
// float64
|
||||
That("/ 1.0 2.0 4.0").Puts(0.125),
|
||||
// 0 / non-zero
|
||||
That("/ 0 1/2 0.1").Puts(0),
|
||||
|
|
|
@ -108,13 +108,13 @@ func ScanToGo(src interface{}, ptr interface{}) error {
|
|||
func FromGo(a interface{}) interface{} {
|
||||
switch a := a.(type) {
|
||||
case *big.Int:
|
||||
if i, ok := getFixInt(a); ok {
|
||||
if i, ok := getInt(a); ok {
|
||||
return i
|
||||
}
|
||||
return a
|
||||
case *big.Rat:
|
||||
if a.IsInt() {
|
||||
if i, ok := getFixInt(a.Num()); ok {
|
||||
if i, ok := getInt(a.Num()); ok {
|
||||
return i
|
||||
}
|
||||
return a.Num()
|
||||
|
|
|
@ -43,7 +43,7 @@ type NumType uint8
|
|||
// Possible values for NumType, sorted in the order of implicit conversion
|
||||
// (lower types can be implicitly converted to higher types).
|
||||
const (
|
||||
FixInt NumType = iota
|
||||
Int NumType = iota
|
||||
BigInt
|
||||
BigRat
|
||||
Float64
|
||||
|
@ -59,7 +59,7 @@ func UnifyNums(nums []Num, typ NumType) NumSlice {
|
|||
}
|
||||
}
|
||||
switch typ {
|
||||
case FixInt:
|
||||
case Int:
|
||||
unified := make([]int, len(nums))
|
||||
for i, num := range nums {
|
||||
unified[i] = num.(int)
|
||||
|
@ -126,7 +126,7 @@ func UnifyNums(nums []Num, typ NumType) NumSlice {
|
|||
func getNumType(n Num) NumType {
|
||||
switch n.(type) {
|
||||
case int:
|
||||
return FixInt
|
||||
return Int
|
||||
case *big.Int:
|
||||
return BigInt
|
||||
case *big.Rat:
|
||||
|
@ -141,7 +141,7 @@ func getNumType(n Num) NumType {
|
|||
// NormalizeBigInt converts a big.Int to an int if it is within the range of
|
||||
// int. Otherwise it returns n as is.
|
||||
func NormalizeBigInt(z *big.Int) Num {
|
||||
if i, ok := getFixInt(z); ok {
|
||||
if i, ok := getInt(z); ok {
|
||||
return i
|
||||
}
|
||||
return z
|
||||
|
@ -152,7 +152,7 @@ func NormalizeBigInt(z *big.Int) Num {
|
|||
func NormalizeBigRat(z *big.Rat) Num {
|
||||
if z.IsInt() {
|
||||
n := z.Num()
|
||||
if i, ok := getFixInt(n); ok {
|
||||
if i, ok := getInt(n); ok {
|
||||
return i
|
||||
}
|
||||
return n
|
||||
|
@ -160,7 +160,7 @@ func NormalizeBigRat(z *big.Rat) Num {
|
|||
return z
|
||||
}
|
||||
|
||||
func getFixInt(z *big.Int) (int, bool) {
|
||||
func getInt(z *big.Int) (int, bool) {
|
||||
// TODO: Use a more efficient implementation by examining z.Bits
|
||||
if z.IsInt64() {
|
||||
i64 := z.Int64()
|
||||
|
|
|
@ -43,27 +43,27 @@ func TestParseNum(t *testing.T) {
|
|||
|
||||
func TestUnifyNums(t *testing.T) {
|
||||
Test(t, Fn("UnifyNums", UnifyNums), Table{
|
||||
Args([]Num{1, 2, 3, 4}, FixInt).
|
||||
Args([]Num{1, 2, 3, 4}, Int).
|
||||
Rets([]int{1, 2, 3, 4}),
|
||||
|
||||
Args([]Num{1, 2, 3, bigInt(z)}, FixInt).
|
||||
Args([]Num{1, 2, 3, bigInt(z)}, Int).
|
||||
Rets([]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3), bigInt(z)}),
|
||||
|
||||
Args([]Num{1, 2, 3, big.NewRat(1, 2)}, FixInt).
|
||||
Args([]Num{1, 2, 3, big.NewRat(1, 2)}, Int).
|
||||
Rets([]*big.Rat{
|
||||
big.NewRat(1, 1), big.NewRat(2, 1),
|
||||
big.NewRat(3, 1), big.NewRat(1, 2)}),
|
||||
Args([]Num{1, 2, bigInt(z), big.NewRat(1, 2)}, FixInt).
|
||||
Args([]Num{1, 2, bigInt(z), big.NewRat(1, 2)}, Int).
|
||||
Rets([]*big.Rat{
|
||||
big.NewRat(1, 1), big.NewRat(2, 1), bigRat(z), big.NewRat(1, 2)}),
|
||||
|
||||
Args([]Num{1, 2, 3, 4.0}, FixInt).
|
||||
Args([]Num{1, 2, 3, 4.0}, Int).
|
||||
Rets([]float64{1, 2, 3, 4}),
|
||||
Args([]Num{1, 2, big.NewRat(1, 2), 4.0}, FixInt).
|
||||
Args([]Num{1, 2, big.NewRat(1, 2), 4.0}, Int).
|
||||
Rets([]float64{1, 2, 0.5, 4}),
|
||||
Args([]Num{1, 2, big.NewInt(3), 4.0}, FixInt).
|
||||
Args([]Num{1, 2, big.NewInt(3), 4.0}, Int).
|
||||
Rets([]float64{1, 2, 3, 4}),
|
||||
Args([]Num{1, 2, bigInt(z), 4.0}, FixInt).
|
||||
Args([]Num{1, 2, bigInt(z), 4.0}, Int).
|
||||
Rets([]float64{1, 2, math.Inf(1), 4}),
|
||||
|
||||
Args([]Num{1, 2, 3, 4}, BigInt).
|
||||
|
|
Loading…
Reference in New Issue
Block a user