Repository: goplus/gop
Pull request #1156 (2022-04-23 03:09:01)

TestUint128_cast


Patch:
diff --git a/builtin/ng/big.go b/builtin/ng/big.go
index 9f755eaf..4bb173b1 100644
--- a/builtin/ng/big.go
+++ b/builtin/ng/big.go
@@ -244,19 +244,19 @@ func (a Bigint) Gop_RshAssign(n Gop_ninteger) {
 	a.Int.Rsh(a.Int, uint(n))
 }
 
-// Bigint_Cast: func bigint() bigint
-func Bigint_Cast__0() Bigint {
-	return Bigint{new(big.Int)}
+// Bigint_Cast: func bigint(x untyped_int) bigint
+func Bigint_Cast__0(x int) Bigint {
+	return Bigint{big.NewInt(int64(x))}
 }
 
-// Bigint_Cast: func bigint(x int64) bigint
-func Bigint_Cast__1(x int64) Bigint {
-	return Bigint{big.NewInt(x)}
+// Bigint_Cast: func bigint(x untyped_bigint) bigint
+func Bigint_Cast__1(x UntypedBigint) Bigint {
+	return Bigint{x}
 }
 
-// Bigint_Cast: func bigint(x int) bigint
-func Bigint_Cast__2(x int) Bigint {
-	return Bigint{big.NewInt(int64(x))}
+// Bigint_Cast: func bigint(x int64) bigint
+func Bigint_Cast__2(x int64) Bigint {
+	return Bigint{big.NewInt(x)}
 }
 
 // Bigint_Cast: func bigint(x uint64) bigint
@@ -283,22 +283,24 @@ func Bigint_Cast__6(x *big.Rat) Bigint {
 	return Bigint{ret}
 }
 
+// Bigint_Cast: func bigint() bigint
+func Bigint_Cast__7() Bigint {
+	return Bigint{new(big.Int)}
+}
+
 // Bigint_Init: func bigint.init(x int) bigint
 func Bigint_Init__0(x int) Bigint {
 	return Bigint{big.NewInt(int64(x))}
 }
 
-// Bigint_Init: func bigint.init(x *big.Int) bigint
-func Bigint_Init__1(x *big.Int) Bigint {
+// Bigint_Init: func bigint.init(x untyped_bigint) bigint
+func Bigint_Init__1(x UntypedBigint) Bigint {
 	return Bigint{x}
 }
 
-// Bigint_Init: func bigint.init(x *big.Rat) bigint
-func Bigint_Init__2(x *big.Rat) Bigint {
-	if x.IsInt() {
-		return Bigint{x.Num()}
-	}
-	panic("TODO: can't init bigint from bigrat")
+// Bigint_Init: func bigint.init(x *big.Int) bigint
+func Bigint_Init__2(x *big.Int) Bigint {
+	return Bigint{x}
 }
 
 // -----------------------------------------------------------------------------
@@ -425,14 +427,14 @@ func (a Bigrat) Gop_QuoAssign(b Bigrat) {
 	a.Rat.Quo(a.Rat, b.Rat)
 }
 
-// Bigrat_Cast: func bigrat() bigrat
-func Bigrat_Cast__0() Bigrat {
-	return Bigrat{new(big.Rat)}
+// Bigrat_Cast: func bigrat(x untyped_int) bigrat
+func Bigrat_Cast__0(x int) Bigrat {
+	return Bigrat{big.NewRat(int64(x), 1)}
 }
 
-// Bigrat_Cast: func bigrat(a bigint) bigrat
-func Bigrat_Cast__1(a Bigint) Bigrat {
-	return Bigrat{new(big.Rat).SetInt(a.Int)}
+// Bigrat_Cast: func bigrat(a untyped_bigint) bigrat
+func Bigrat_Cast__1(a UntypedBigint) Bigrat {
+	return Bigrat{new(big.Rat).SetInt(a)}
 }
 
 // Bigrat_Cast: func bigrat(a *big.Int) bigrat
@@ -440,9 +442,9 @@ func Bigrat_Cast__2(a *big.Int) Bigrat {
 	return Bigrat{new(big.Rat).SetInt(a)}
 }
 
-// Bigrat_Cast: func bigrat(a, b int64) bigrat
-func Bigrat_Cast__3(a, b int64) Bigrat {
-	return Bigrat{big.NewRat(a, b)}
+// Bigrat_Cast: func bigrat(a bigint) bigrat
+func Bigrat_Cast__3(a Bigint) Bigrat {
+	return Bigrat{new(big.Rat).SetInt(a.Int)}
 }
 
 // Bigrat_Cast: func bigrat(a *big.Rat) bigrat
@@ -450,6 +452,16 @@ func Bigrat_Cast__4(a *big.Rat) Bigrat {
 	return Bigrat{a}
 }
 
+// Bigrat_Cast: func bigrat() bigrat
+func Bigrat_Cast__5() Bigrat {
+	return Bigrat{new(big.Rat)}
+}
+
+// Bigrat_Cast: func bigrat(a, b int64) bigrat
+func Bigrat_Cast__6(a, b int64) Bigrat {
+	return Bigrat{big.NewRat(a, b)}
+}
+
 // Bigrat_Init: func bigrat.init(x untyped_int) bigrat
 func Bigrat_Init__0(x int) Bigrat {
 	return Bigrat{big.NewRat(int64(x), 1)}
diff --git a/builtin/ng/uint128.go b/builtin/ng/uint128.go
index ca442562..32b85d41 100644
--- a/builtin/ng/uint128.go
+++ b/builtin/ng/uint128.go
@@ -48,19 +48,19 @@ func Uint128_Init__1(v UntypedBigint) (out Uint128) {
 	return
 }
 
-// Uint128_Cast: func uint128(hi, lo uint64) uint128
-func Uint128_Cast__0(hi, lo uint64) Uint128 {
-	return Uint128{hi: hi, lo: lo}
+// Uint128_Cast: func uint128(v untyped_int) uint128
+func Uint128_Cast__0(v int) Uint128 {
+	return Uint128_Cast__6(int64(v))
 }
 
-// Uint128_Cast: func uint128(v uint64) uint128
-func Uint128_Cast__1(v uint64) Uint128 {
-	return Uint128{lo: v}
+// Uint128_Cast: func uint128(v untyped_bigint) uint128
+func Uint128_Cast__1(v UntypedBigint) Uint128 {
+	return Uint128_Init__1(v)
 }
 
-// Uint128_Cast: func uint128(v uint) uint128
-func Uint128_Cast__2(v uint) Uint128 {
-	return Uint128{lo: uint64(v)}
+// Uint128_Cast: func uint128(v uint64) uint128
+func Uint128_Cast__2(v uint64) Uint128 {
+	return Uint128{lo: v}
 }
 
 // Uint128_Cast: func uint128(v uint32) uint128
@@ -146,6 +146,21 @@ func Uint128_Cast__9(v *big.Int) (out Uint128, inRange bool) {
 	}
 }
 
+// Uint128_Cast: func uint128() uint128
+func Uint128_Cast__a() Uint128 {
+	return Uint128{}
+}
+
+// Uint128_Cast: func uint128(v uint) uint128
+func Uint128_Cast__b(v uint) Uint128 {
+	return Uint128{lo: uint64(v)}
+}
+
+// Uint128_Cast: func uint128(hi, lo uint64) uint128
+func Uint128_Cast__c(hi, lo uint64) Uint128 {
+	return Uint128{hi: hi, lo: lo}
+}
+
 // Gop_Rcast: func float64(v uint128) float64
 func (u Uint128) Gop_Rcast__0() float64 {
 	if u.hi == 0 {
diff --git a/go.mod b/go.mod
index 1ad6dbba..6c6e02df 100644
--- a/go.mod
+++ b/go.mod
@@ -3,7 +3,7 @@ module github.com/goplus/gop
 go 1.16
 
 require (
-	github.com/goplus/gox v1.10.6
+	github.com/goplus/gox v1.10.7
 	github.com/qiniu/x v1.11.5
 	golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3
 	golang.org/x/tools v0.1.10
diff --git a/go.sum b/go.sum
index 271feb5e..e785d092 100644
--- a/go.sum
+++ b/go.sum
@@ -1,5 +1,5 @@
-github.com/goplus/gox v1.10.6 h1:nv3goIZ85GbcBIyI2GdzUmwzSVWg+RGP+Wyuep4eO5I=
-github.com/goplus/gox v1.10.6/go.mod h1:gu7fuQF8RmWPZUjd+tEJGuV3m/vOEv0bHrct0x/KatM=
+github.com/goplus/gox v1.10.7 h1:OeoppVRhcNuuze2Dxy3fwY26iKB8O39Y/Ba5TuKuH9Y=
+github.com/goplus/gox v1.10.7/go.mod h1:gu7fuQF8RmWPZUjd+tEJGuV3m/vOEv0bHrct0x/KatM=
 github.com/qiniu/x v1.11.5 h1:TYr5cl4g2yoHAZeDK4MTjKF6CMoG+IHlCDvvM5qym6U=
 github.com/qiniu/x v1.11.5/go.mod h1:03Ni9tj+N2h2aKnAz+6N0Xfl8FwMEDRC2PAlxekASDs=
 github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=


Test patch:
diff --git a/cl/run_test.go b/cl/run_test.go
index 2672f522..4d41cc49 100644
--- a/cl/run_test.go
+++ b/cl/run_test.go
@@ -109,7 +109,9 @@ func testRun(t *testing.T, gopcode, expected string) {
 	}
 }
 
-func TestInt128_println(t *testing.T) {
+// -----------------------------------------------------------------------------
+
+func TestUint128_println(t *testing.T) {
 	testRun(t, `
 var x uint128 = 1
 println x
@@ -117,7 +119,7 @@ println x
 `)
 }
 
-func TestInt128_init(t *testing.T) {
+func TestUint128_init(t *testing.T) {
 	testRun(t, `
 var x uint128 = 1 << 65
 var y = x + 1
@@ -128,7 +130,14 @@ println y
 `)
 }
 
-func TestInt128_printf(t *testing.T) {
+func TestUint128_cast(t *testing.T) {
+	testRun(t, `
+println uint128(1 << 65), uint128()
+`, `36893488147419103232 0
+`)
+}
+
+func TestUint128_printf(t *testing.T) {
 	testRun(t, `
 var x uint128 = 1
 printf "%4d\n", x
@@ -136,7 +145,7 @@ printf "%4d\n", x
 `)
 }
 
-func TestInt128_scanf(t *testing.T) {
+func TestUint128_scanf(t *testing.T) {
 	testRun(t, `
 import "fmt"
 
@@ -147,3 +156,41 @@ println name, age
 `, `Kim 22
 `)
 }
+
+// -----------------------------------------------------------------------------
+
+func TestBigint_println(t *testing.T) {
+	testRun(t, `
+var x bigint = 1
+println x
+`, `1
+`)
+}
+
+func TestBigint_init(t *testing.T) {
+	testRun(t, `
+var x bigint = 1 << 65
+var y = x + 1
+println x
+println y
+`, `36893488147419103232
+36893488147419103233
+`)
+}
+
+func TestBigint_cast(t *testing.T) {
+	testRun(t, `
+println bigint(1 << 65), bigint()
+`, `36893488147419103232 0
+`)
+}
+
+func TestBigint_printf(t *testing.T) {
+	testRun(t, `
+var x bigint = 1
+printf "%4d\n", x
+`, `   1
+`)
+}
+
+// -----------------------------------------------------------------------------