github.com/kolbycrouch/elvish@v0.14.1-0.20210614162631-215b9ac1c423/pkg/eval/vals/num_test.go (about)

     1  package vals
     2  
     3  import (
     4  	"math"
     5  	"math/big"
     6  	"testing"
     7  
     8  	. "src.elv.sh/pkg/tt"
     9  )
    10  
    11  // Test utilities.
    12  
    13  const (
    14  	zeros = "0000000000000000000"
    15  	// Values that exceed the range of int64, used for testing BigInt.
    16  	z   = "1" + zeros + "0"
    17  	z1  = "1" + zeros + "1" // z+1
    18  	z2  = "1" + zeros + "2" // z+2
    19  	z3  = "1" + zeros + "3" // z+3
    20  	zz  = "2" + zeros + "0" // 2z
    21  	zz1 = "2" + zeros + "1" // 2z+1
    22  	zz2 = "2" + zeros + "2" // 2z+2
    23  	zz3 = "2" + zeros + "3" // 2z+3
    24  )
    25  
    26  func TestParseNum(t *testing.T) {
    27  	Test(t, Fn("ParseNum", ParseNum), Table{
    28  		Args("1").Rets(1),
    29  
    30  		Args(z).Rets(bigInt(z)),
    31  
    32  		Args("1/2").Rets(big.NewRat(1, 2)),
    33  		Args("2/1").Rets(2),
    34  		Args(z + "/1").Rets(bigInt(z)),
    35  
    36  		Args("1.0").Rets(1.0),
    37  		Args("1e-5").Rets(1e-5),
    38  
    39  		Args("x").Rets(nil),
    40  		Args("x/y").Rets(nil),
    41  	})
    42  }
    43  
    44  func TestUnifyNums(t *testing.T) {
    45  	Test(t, Fn("UnifyNums", UnifyNums), Table{
    46  		Args([]Num{1, 2, 3, 4}, Int).
    47  			Rets([]int{1, 2, 3, 4}),
    48  
    49  		Args([]Num{1, 2, 3, bigInt(z)}, Int).
    50  			Rets([]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3), bigInt(z)}),
    51  
    52  		Args([]Num{1, 2, 3, big.NewRat(1, 2)}, Int).
    53  			Rets([]*big.Rat{
    54  				big.NewRat(1, 1), big.NewRat(2, 1),
    55  				big.NewRat(3, 1), big.NewRat(1, 2)}),
    56  		Args([]Num{1, 2, bigInt(z), big.NewRat(1, 2)}, Int).
    57  			Rets([]*big.Rat{
    58  				big.NewRat(1, 1), big.NewRat(2, 1), bigRat(z), big.NewRat(1, 2)}),
    59  
    60  		Args([]Num{1, 2, 3, 4.0}, Int).
    61  			Rets([]float64{1, 2, 3, 4}),
    62  		Args([]Num{1, 2, big.NewRat(1, 2), 4.0}, Int).
    63  			Rets([]float64{1, 2, 0.5, 4}),
    64  		Args([]Num{1, 2, big.NewInt(3), 4.0}, Int).
    65  			Rets([]float64{1, 2, 3, 4}),
    66  		Args([]Num{1, 2, bigInt(z), 4.0}, Int).
    67  			Rets([]float64{1, 2, math.Inf(1), 4}),
    68  
    69  		Args([]Num{1, 2, 3, 4}, BigInt).
    70  			Rets([]*big.Int{
    71  				big.NewInt(1), big.NewInt(2), big.NewInt(3), big.NewInt(4)}),
    72  	})
    73  }
    74  
    75  func TestUnifyNums2(t *testing.T) {
    76  	Test(t, Fn("UnifyNums2", UnifyNums2), Table{
    77  		Args(1, 2, Int).Rets(1, 2),
    78  		Args(1, bigInt(z), Int).Rets(big.NewInt(1), bigInt(z)),
    79  		Args(1, big.NewRat(1, 2), Int).Rets(big.NewRat(1, 1), big.NewRat(1, 2)),
    80  		Args(1, 2.0, Int).Rets(1.0, 2.0),
    81  
    82  		Args(1, 2, BigInt).Rets(big.NewInt(1), big.NewInt(2)),
    83  	})
    84  }
    85  
    86  func bigInt(s string) *big.Int {
    87  	z, ok := new(big.Int).SetString(s, 0)
    88  	if !ok {
    89  		panic("cannot parse as big int: " + s)
    90  	}
    91  	return z
    92  }
    93  
    94  func bigRat(s string) *big.Rat {
    95  	z, ok := new(big.Rat).SetString(s)
    96  	if !ok {
    97  		panic("cannot parse as big rat: " + s)
    98  	}
    99  	return z
   100  }