github.com/goplus/gogen@v1.16.0/gop_test.go (about)

     1  /*
     2   Copyright 2021 The GoPlus Authors (goplus.org)
     3   Licensed under the Apache License, Version 2.0 (the "License");
     4   you may not use this file except in compliance with the License.
     5   You may obtain a copy of the License at
     6       http://www.apache.org/licenses/LICENSE-2.0
     7   Unless required by applicable law or agreed to in writing, software
     8   distributed under the License is distributed on an "AS IS" BASIS,
     9   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10   See the License for the specific language governing permissions and
    11   limitations under the License.
    12  */
    13  
    14  package gogen_test
    15  
    16  import (
    17  	"go/ast"
    18  	"go/constant"
    19  	"go/token"
    20  	"go/types"
    21  	"math/big"
    22  	"testing"
    23  
    24  	"github.com/goplus/gogen"
    25  )
    26  
    27  func initGopBuiltin(big gogen.PkgRef, conf *gogen.Config) {
    28  	conf.UntypedBigInt = big.Ref("Gop_untyped_bigint").Type().(*types.Named)
    29  	conf.UntypedBigRat = big.Ref("Gop_untyped_bigrat").Type().(*types.Named)
    30  	conf.UntypedBigFloat = big.Ref("Gop_untyped_bigfloat").Type().(*types.Named)
    31  }
    32  
    33  func newGopBuiltinDefault(pkg *gogen.Package, conf *gogen.Config) *types.Package {
    34  	fmt := pkg.Import("fmt")
    35  	big := pkg.Import("github.com/goplus/gogen/internal/builtin")
    36  	builtin := types.NewPackage("", "")
    37  	if builtin.Scope().Insert(gogen.NewOverloadFunc(token.NoPos, builtin, "println", fmt.Ref("Println"))) != nil {
    38  		panic("println exists")
    39  	}
    40  	gogen.InitBuiltin(pkg, builtin, conf)
    41  	initGopBuiltin(big, conf)
    42  	return builtin
    43  }
    44  
    45  func newGopMainPackage() *gogen.Package {
    46  	conf := &gogen.Config{
    47  		Fset:       gblFset,
    48  		Importer:   gblImp,
    49  		NewBuiltin: newGopBuiltinDefault,
    50  	}
    51  	return gogen.NewPackage("", "main", conf)
    52  }
    53  
    54  // ----------------------------------------------------------------------------
    55  
    56  func TestGopoConst(t *testing.T) {
    57  	pkg := newPackage("foo")
    58  	pkg.CB().NewConstStart(nil, "Gopo_x").
    59  		Val("Hello").EndInit(1)
    60  	domTest(t, pkg, `package foo
    61  
    62  const GopPackage = true
    63  const Gopo_x = "Hello"
    64  `)
    65  }
    66  
    67  func TestFmtPrintln(t *testing.T) {
    68  	pkg := newGopMainPackage()
    69  	pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
    70  		DefineVarStart(token.NoPos, "p").Val(ctxRef(pkg, "println")).EndInit(1).
    71  		VarRef(ctxRef(pkg, "p")).Val(ctxRef(pkg, "println")).Assign(1).EndStmt().
    72  		End()
    73  	domTest(t, pkg, `package main
    74  
    75  import "fmt"
    76  
    77  func main() {
    78  	p := fmt.Println
    79  	p = fmt.Println
    80  }
    81  `)
    82  }
    83  
    84  func TestBigRatConstant(t *testing.T) {
    85  	a := constant.Make(new(big.Rat).SetInt64(1))
    86  	b := constant.Make(new(big.Rat).SetInt64(2))
    87  	c := constant.BinaryOp(a, token.ADD, b)
    88  	if c.Kind() != constant.Float {
    89  		t.Fatal("c.Kind() != constant.Float -", c)
    90  	}
    91  	d := constant.BinaryOp(a, token.QUO, b)
    92  	if !constant.Compare(constant.Make(big.NewRat(1, 2)), token.EQL, d) {
    93  		t.Fatal("d != 1/2r")
    94  	}
    95  
    96  	e := constant.MakeFromLiteral("1.2", token.FLOAT, 0)
    97  	if _, ok := constant.Val(e).(*big.Rat); !ok {
    98  		t.Fatal("constant.MakeFromLiteral 1.2 not *big.Rat", constant.Val(e))
    99  	}
   100  }
   101  
   102  func TestBigIntVar(t *testing.T) {
   103  	pkg := newGopMainPackage()
   104  	big := pkg.Import("github.com/goplus/gogen/internal/builtin")
   105  	pkg.CB().NewVar(big.Ref("Gop_bigint").Type(), "a")
   106  	domTest(t, pkg, `package main
   107  
   108  import "github.com/goplus/gogen/internal/builtin"
   109  
   110  var a builtin.Gop_bigint
   111  `)
   112  }
   113  
   114  func TestBigIntVarInit(t *testing.T) {
   115  	pkg := newGopMainPackage()
   116  	mbig := pkg.Import("github.com/goplus/gogen/internal/builtin")
   117  	pkg.CB().NewVarStart(mbig.Ref("Gop_bigint").Type(), "a").
   118  		UntypedBigInt(big.NewInt(6)).EndInit(1)
   119  	domTest(t, pkg, `package main
   120  
   121  import (
   122  	"github.com/goplus/gogen/internal/builtin"
   123  	"math/big"
   124  )
   125  
   126  var a builtin.Gop_bigint = builtin.Gop_bigint_Init__1(big.NewInt(6))
   127  `)
   128  }
   129  
   130  func TestBigInt(t *testing.T) {
   131  	pkg := newGopMainPackage()
   132  	big := pkg.Import("github.com/goplus/gogen/internal/builtin")
   133  	pkg.CB().NewVar(big.Ref("Gop_bigint").Type(), "a", "b")
   134  	pkg.CB().NewVarStart(big.Ref("Gop_bigint").Type(), "c").
   135  		VarVal("a").VarVal("b").BinaryOp(token.ADD).EndInit(1)
   136  	domTest(t, pkg, `package main
   137  
   138  import "github.com/goplus/gogen/internal/builtin"
   139  
   140  var a, b builtin.Gop_bigint
   141  var c builtin.Gop_bigint = (builtin.Gop_bigint).Gop_Add(a, b)
   142  `)
   143  }
   144  
   145  func TestBigInt2(t *testing.T) {
   146  	pkg := newGopMainPackage()
   147  	big := pkg.Import("github.com/goplus/gogen/internal/builtin")
   148  	typ := types.NewPointer(big.Ref("Gop_bigint").Type())
   149  	pkg.CB().NewVar(typ, "a", "b")
   150  	pkg.CB().NewVarStart(typ, "c").
   151  		VarVal("a").VarVal("b").BinaryOp(token.AND_NOT).EndInit(1)
   152  	domTest(t, pkg, `package main
   153  
   154  import "github.com/goplus/gogen/internal/builtin"
   155  
   156  var a, b *builtin.Gop_bigint
   157  var c *builtin.Gop_bigint = (*builtin.Gop_bigint).Gop_AndNot__0(a, b)
   158  `)
   159  }
   160  
   161  func TestBigRat(t *testing.T) {
   162  	pkg := newGopMainPackage()
   163  	big := pkg.Import("github.com/goplus/gogen/internal/builtin")
   164  	pkg.NewVar(token.NoPos, big.Ref("Gop_bigrat").Type(), "a", "b")
   165  	pkg.CB().NewVarStart(big.Ref("Gop_bigrat").Type(), "c").
   166  		VarVal("a").VarVal("b").BinaryOp(token.QUO).EndInit(1)
   167  	pkg.CB().NewVarStart(big.Ref("Gop_bigrat").Type(), "d").
   168  		VarVal("a").UnaryOp(token.SUB).EndInit(1)
   169  	pkg.CB().NewVarStart(big.Ref("Gop_bigrat").Type(), "e").
   170  		Val(big.Ref("Gop_bigrat_Cast")).Call(0).EndInit(1)
   171  	pkg.CB().NewVarStart(big.Ref("Gop_bigrat").Type(), "f").
   172  		Val(big.Ref("Gop_bigrat_Cast")).Val(1).Val(2).Call(2).EndInit(1)
   173  	pkg.CB().NewVarStart(big.Ref("Gop_bigint").Type(), "g")
   174  	pkg.CB().NewVarStart(big.Ref("Gop_bigrat").Type(), "h").
   175  		Val(big.Ref("Gop_bigrat_Cast")).Val(ctxRef(pkg, "g")).Call(1).EndInit(1)
   176  	domTest(t, pkg, `package main
   177  
   178  import "github.com/goplus/gogen/internal/builtin"
   179  
   180  var a, b builtin.Gop_bigrat
   181  var c builtin.Gop_bigrat = (builtin.Gop_bigrat).Gop_Quo(a, b)
   182  var d builtin.Gop_bigrat = a.Gop_Neg()
   183  var e builtin.Gop_bigrat = builtin.Gop_bigrat_Cast__5()
   184  var f builtin.Gop_bigrat = builtin.Gop_bigrat_Cast__3(1, 2)
   185  var g builtin.Gop_bigint
   186  var h builtin.Gop_bigrat = builtin.Gop_bigrat_Cast__1(g)
   187  `)
   188  }
   189  
   190  func TestBigRatInit(t *testing.T) {
   191  	pkg := newGopMainPackage()
   192  	ng := pkg.Import("github.com/goplus/gogen/internal/builtin")
   193  	pkg.CB().NewVarStart(ng.Ref("Gop_bigrat").Type(), "a").
   194  		Val(1).Val(65).BinaryOp(token.SHL).
   195  		EndInit(1)
   196  	domTest(t, pkg, `package main
   197  
   198  import (
   199  	"github.com/goplus/gogen/internal/builtin"
   200  	"math/big"
   201  )
   202  
   203  var a builtin.Gop_bigrat = builtin.Gop_bigrat_Init__1(func() *big.Int {
   204  	v, _ := new(big.Int).SetString("36893488147419103232", 10)
   205  	return v
   206  }())
   207  `)
   208  }
   209  
   210  func TestBigRatInit2(t *testing.T) {
   211  	pkg := newGopMainPackage()
   212  	ng := pkg.Import("github.com/goplus/gogen/internal/builtin")
   213  	pkg.CB().NewVarStart(ng.Ref("Gop_bigrat").Type(), "a").
   214  		Val(-1).Val(65).BinaryOp(token.SHL).
   215  		EndInit(1)
   216  	domTest(t, pkg, `package main
   217  
   218  import (
   219  	"github.com/goplus/gogen/internal/builtin"
   220  	"math/big"
   221  )
   222  
   223  var a builtin.Gop_bigrat = builtin.Gop_bigrat_Init__1(func() *big.Int {
   224  	v, _ := new(big.Int).SetString("-36893488147419103232", 10)
   225  	return v
   226  }())
   227  `)
   228  }
   229  
   230  func TestBigRatCast(t *testing.T) {
   231  	pkg := newGopMainPackage()
   232  	fmt := pkg.Import("fmt")
   233  	ng := pkg.Import("github.com/goplus/gogen/internal/builtin")
   234  	pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
   235  		Val(fmt.Ref("Println")).
   236  		Val(ng.Ref("Gop_bigrat")).Val(1).Val(65).BinaryOp(token.SHL).Call(1).           // bigrat(1 << 65)
   237  		Typ(types.Typ[types.Float64]).Val(ng.Ref("Gop_bigrat")).Call(0).Call(1).        // float64(bigrat())
   238  		Typ(types.Typ[types.Float64]).Val(ng.Ref("Gop_bigint")).Val(1).Call(1).Call(1). // float64(bigint(1))
   239  		Typ(types.Typ[types.Int]).Call(0).                                              // int()
   240  		Call(4).EndStmt().
   241  		End()
   242  	domTest(t, pkg, `package main
   243  
   244  import (
   245  	"fmt"
   246  	"github.com/goplus/gogen/internal/builtin"
   247  	"math/big"
   248  )
   249  
   250  func main() {
   251  	fmt.Println(builtin.Gop_bigrat_Cast__0(func() *big.Int {
   252  		v, _ := new(big.Int).SetString("36893488147419103232", 10)
   253  		return v
   254  	}()), builtin.Gop_bigrat_Cast__5().Gop_Rcast__2(), builtin.Gop_bigint_Cast__0(1).Gop_Rcast(), 0)
   255  }
   256  `)
   257  }
   258  
   259  func TestCastIntTwoValue(t *testing.T) {
   260  	pkg := newGopMainPackage()
   261  	ng := pkg.Import("github.com/goplus/gogen/internal/builtin")
   262  	pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
   263  		DefineVarStart(0, "v", "inRange").
   264  		Typ(types.Typ[types.Int]).
   265  		Val(ng.Ref("Gop_bigrat")).Val(1).Call(1).
   266  		CallWith(1, gogen.InstrFlagTwoValue).
   267  		EndInit(1).
   268  		End()
   269  	domTest(t, pkg, `package main
   270  
   271  import (
   272  	"github.com/goplus/gogen/internal/builtin"
   273  	"math/big"
   274  )
   275  
   276  func main() {
   277  	v, inRange := builtin.Gop_bigrat_Cast__0(big.NewInt(1)).Gop_Rcast__0()
   278  }
   279  `)
   280  }
   281  
   282  func TestCastBigIntTwoValue(t *testing.T) {
   283  	pkg := newGopMainPackage()
   284  	ng := pkg.Import("github.com/goplus/gogen/internal/builtin")
   285  	pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
   286  		DefineVarStart(0, "v", "inRange").
   287  		Val(ng.Ref("Gop_bigint")).
   288  		Val(ng.Ref("Gop_bigrat")).Val(1).Call(1).
   289  		CallWith(1, gogen.InstrFlagTwoValue).
   290  		EndInit(1).
   291  		End()
   292  	domTest(t, pkg, `package main
   293  
   294  import (
   295  	"github.com/goplus/gogen/internal/builtin"
   296  	"math/big"
   297  )
   298  
   299  func main() {
   300  	v, inRange := builtin.Gop_bigint_Cast__7(builtin.Gop_bigrat_Cast__0(big.NewInt(1)))
   301  }
   302  `)
   303  }
   304  
   305  func TestErrCast(t *testing.T) {
   306  	defer func() {
   307  		if e := recover(); e == nil {
   308  			t.Fatal("TestErrCast: no error?")
   309  		}
   310  	}()
   311  	pkg := newGopMainPackage()
   312  	ng := pkg.Import("github.com/goplus/gogen/internal/builtin")
   313  	pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
   314  		DefineVarStart(0, "v", "inRange").
   315  		Typ(types.Typ[types.Int64]).
   316  		Val(ng.Ref("Gop_bigrat")).Val(1).Call(1).
   317  		CallWith(1, gogen.InstrFlagTwoValue).
   318  		EndInit(1).
   319  		End()
   320  }
   321  
   322  func TestUntypedBigIntAdd(t *testing.T) {
   323  	pkg := newGopMainPackage()
   324  	pkg.CB().NewVarStart(nil, "a").
   325  		UntypedBigInt(big.NewInt(6)).
   326  		UntypedBigInt(big.NewInt(63)).
   327  		BinaryOp(token.ADD).
   328  		EndInit(1)
   329  	domTest(t, pkg, `package main
   330  
   331  import (
   332  	"github.com/goplus/gogen/internal/builtin"
   333  	"math/big"
   334  )
   335  
   336  var a = builtin.Gop_bigint_Init__1(big.NewInt(69))
   337  `)
   338  }
   339  
   340  func TestBigRatIncDec(t *testing.T) {
   341  	pkg := newGopMainPackage()
   342  	big := pkg.Import("github.com/goplus/gogen/internal/builtin")
   343  	pkg.NewVar(token.NoPos, big.Ref("Gop_bigrat").Type(), "a")
   344  	pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
   345  		VarRef("a").IncDec(token.INC).
   346  		End()
   347  	domTest(t, pkg, `package main
   348  
   349  import "github.com/goplus/gogen/internal/builtin"
   350  
   351  var a builtin.Gop_bigrat
   352  
   353  func main() {
   354  	a.Gop_Inc()
   355  }
   356  `)
   357  }
   358  
   359  func TestErrValRef(t *testing.T) {
   360  	defer func() {
   361  		if e := recover(); e == nil ||
   362  			e.(error).Error() != "-:  is not a variable" {
   363  			t.Fatal("TestErrValRef:", e)
   364  		}
   365  	}()
   366  	pkg := newGopMainPackage()
   367  	pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
   368  		VarRef("a").
   369  		End()
   370  }
   371  
   372  func TestErrBigRatIncDec(t *testing.T) {
   373  	defer func() {
   374  		if e := recover(); e == nil ||
   375  			e.(error).Error() != "-: operator Gop_Dec should return no results\n" {
   376  			t.Fatal("TestErrBigRatIncDec:", e)
   377  		}
   378  	}()
   379  	pkg := newGopMainPackage()
   380  	big := pkg.Import("github.com/goplus/gogen/internal/builtin")
   381  	pkg.NewVar(token.NoPos, big.Ref("Gop_bigrat").Type(), "a")
   382  	pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
   383  		VarRef("a").IncDec(token.DEC).
   384  		End()
   385  }
   386  
   387  func TestErrBigRatAssignOp(t *testing.T) {
   388  	defer func() {
   389  		if e := recover(); e == nil {
   390  			t.Fatal("TestErrBigRatAssignOp: no error?")
   391  		}
   392  	}()
   393  	pkg := newGopMainPackage()
   394  	big := pkg.Import("github.com/goplus/gogen/internal/builtin")
   395  	pkg.NewVar(token.NoPos, big.Ref("Gop_bigrat").Type(), "a", "b")
   396  	pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
   397  		VarRef(ctxRef(pkg, "a")).
   398  		VarVal("b").
   399  		AssignOp(token.SUB_ASSIGN).
   400  		End()
   401  }
   402  
   403  func TestBigRatAssignOp(t *testing.T) {
   404  	pkg := newGopMainPackage()
   405  	big := pkg.Import("github.com/goplus/gogen/internal/builtin")
   406  	pkg.NewVar(token.NoPos, big.Ref("Gop_bigrat").Type(), "a", "b")
   407  	pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
   408  		VarRef(ctxRef(pkg, "a")).
   409  		VarVal("b").
   410  		AssignOp(token.ADD_ASSIGN).
   411  		End()
   412  	domTest(t, pkg, `package main
   413  
   414  import "github.com/goplus/gogen/internal/builtin"
   415  
   416  var a, b builtin.Gop_bigrat
   417  
   418  func main() {
   419  	a.Gop_AddAssign(b)
   420  }
   421  `)
   422  }
   423  
   424  func TestBigRatAssignOp2(t *testing.T) {
   425  	pkg := newGopMainPackage()
   426  	big := pkg.Import("github.com/goplus/gogen/internal/builtin")
   427  	pkg.NewVar(token.NoPos, big.Ref("Gop_bigrat").Type(), "a")
   428  	pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
   429  		VarRef(ctxRef(pkg, "a")).
   430  		Val(1).
   431  		AssignOp(token.ADD_ASSIGN).
   432  		End()
   433  	domTest(t, pkg, `package main
   434  
   435  import "github.com/goplus/gogen/internal/builtin"
   436  
   437  var a builtin.Gop_bigrat
   438  
   439  func main() {
   440  	a.Gop_AddAssign(builtin.Gop_bigrat_Init__0(1))
   441  }
   442  `)
   443  }
   444  
   445  func TestUntypedBigIntQuo(t *testing.T) {
   446  	pkg := newGopMainPackage()
   447  	pkg.CB().NewVarStart(nil, "a").
   448  		UntypedBigInt(big.NewInt(6)).
   449  		UntypedBigInt(big.NewInt(63)).
   450  		BinaryOp(token.QUO).
   451  		EndInit(1)
   452  	domTest(t, pkg, `package main
   453  
   454  import (
   455  	"github.com/goplus/gogen/internal/builtin"
   456  	"math/big"
   457  )
   458  
   459  var a = builtin.Gop_bigrat_Init__2(big.NewRat(2, 21))
   460  `)
   461  }
   462  
   463  func TestUntypedBigIntQuo2(t *testing.T) {
   464  	pkg := newGopMainPackage()
   465  	pkg.CB().NewVarStart(nil, "a").
   466  		Val(6).
   467  		UntypedBigInt(big.NewInt(63)).
   468  		BinaryOp(token.QUO).
   469  		EndInit(1)
   470  	domTest(t, pkg, `package main
   471  
   472  import (
   473  	"github.com/goplus/gogen/internal/builtin"
   474  	"math/big"
   475  )
   476  
   477  var a = builtin.Gop_bigrat_Init__2(big.NewRat(2, 21))
   478  `)
   479  }
   480  
   481  func TestUntypedBigIntQuo3(t *testing.T) {
   482  	pkg := newGopMainPackage()
   483  	pkg.CB().NewVarStart(nil, "a").
   484  		UntypedBigInt(big.NewInt(63)).
   485  		Val(6).
   486  		BinaryOp(token.QUO).
   487  		EndInit(1)
   488  	domTest(t, pkg, `package main
   489  
   490  import (
   491  	"github.com/goplus/gogen/internal/builtin"
   492  	"math/big"
   493  )
   494  
   495  var a = builtin.Gop_bigrat_Init__2(big.NewRat(21, 2))
   496  `)
   497  }
   498  
   499  func TestUntypedBigIntRem(t *testing.T) {
   500  	pkg := newGopMainPackage()
   501  	pkg.CB().NewVarStart(nil, "a").
   502  		UntypedBigInt(big.NewInt(100)).
   503  		UntypedBigInt(big.NewInt(7)).
   504  		BinaryOp(token.REM).
   505  		EndInit(1)
   506  	domTest(t, pkg, `package main
   507  
   508  import (
   509  	"github.com/goplus/gogen/internal/builtin"
   510  	"math/big"
   511  )
   512  
   513  var a = builtin.Gop_bigint_Init__1(big.NewInt(2))
   514  `)
   515  }
   516  
   517  func TestUntypedBigIntShift(t *testing.T) {
   518  	pkg := newGopMainPackage()
   519  	pkg.CB().NewVarStart(nil, "a").
   520  		UntypedBigInt(big.NewInt(1)).
   521  		Val(128).
   522  		BinaryOp(token.SHL).
   523  		EndInit(1)
   524  	domTest(t, pkg, `package main
   525  
   526  import (
   527  	"github.com/goplus/gogen/internal/builtin"
   528  	"math/big"
   529  )
   530  
   531  var a = builtin.Gop_bigint_Init__1(func() *big.Int {
   532  	v, _ := new(big.Int).SetString("340282366920938463463374607431768211456", 10)
   533  	return v
   534  }())
   535  `)
   536  }
   537  
   538  func TestUntypedBigRatAdd(t *testing.T) {
   539  	pkg := newGopMainPackage()
   540  	pkg.CB().NewVarStart(nil, "a").
   541  		UntypedBigRat(big.NewRat(1, 6)).
   542  		UntypedBigRat(big.NewRat(1, 3)).
   543  		BinaryOp(token.ADD).
   544  		EndInit(1)
   545  	domTest(t, pkg, `package main
   546  
   547  import (
   548  	"github.com/goplus/gogen/internal/builtin"
   549  	"math/big"
   550  )
   551  
   552  var a = builtin.Gop_bigrat_Init__2(big.NewRat(1, 2))
   553  `)
   554  }
   555  
   556  func TestUntypedBigRatAdd2(t *testing.T) {
   557  	pkg := newGopMainPackage()
   558  	pkg.CB().NewVarStart(nil, "a").
   559  		UntypedBigRat(big.NewRat(1, 6)).
   560  		UntypedBigInt(big.NewInt(3)).
   561  		BinaryOp(token.ADD).
   562  		EndInit(1)
   563  	domTest(t, pkg, `package main
   564  
   565  import (
   566  	"github.com/goplus/gogen/internal/builtin"
   567  	"math/big"
   568  )
   569  
   570  var a = builtin.Gop_bigrat_Init__2(big.NewRat(19, 6))
   571  `)
   572  }
   573  
   574  func TestUntypedBigRatAdd3(t *testing.T) {
   575  	pkg := newGopMainPackage()
   576  	pkg.CB().NewVarStart(nil, "a").
   577  		UntypedBigInt(big.NewInt(3)).
   578  		UntypedBigRat(big.NewRat(1, 6)).
   579  		BinaryOp(token.ADD).
   580  		EndInit(1)
   581  	domTest(t, pkg, `package main
   582  
   583  import (
   584  	"github.com/goplus/gogen/internal/builtin"
   585  	"math/big"
   586  )
   587  
   588  var a = builtin.Gop_bigrat_Init__2(big.NewRat(19, 6))
   589  `)
   590  }
   591  
   592  func TestUntypedBigRatAdd4(t *testing.T) {
   593  	pkg := newGopMainPackage()
   594  	mbig := pkg.Import("github.com/goplus/gogen/internal/builtin")
   595  	pkg.NewVar(token.NoPos, mbig.Ref("Gop_bigrat").Type(), "a")
   596  	pkg.CB().NewVarStart(nil, "b").
   597  		VarVal("a").
   598  		UntypedBigRat(big.NewRat(1, 6)).
   599  		BinaryOp(token.ADD).
   600  		EndInit(1)
   601  	domTest(t, pkg, `package main
   602  
   603  import (
   604  	"github.com/goplus/gogen/internal/builtin"
   605  	"math/big"
   606  )
   607  
   608  var a builtin.Gop_bigrat
   609  var b = (builtin.Gop_bigrat).Gop_Add(a, builtin.Gop_bigrat_Init__2(big.NewRat(1, 6)))
   610  `)
   611  }
   612  
   613  func TestUntypedBigRatAdd5(t *testing.T) {
   614  	pkg := newGopMainPackage()
   615  	mbig := pkg.Import("github.com/goplus/gogen/internal/builtin")
   616  	pkg.NewVar(token.NoPos, mbig.Ref("Gop_bigrat").Type(), "a")
   617  	pkg.CB().NewVarStart(nil, "b").
   618  		VarVal("a").
   619  		Val(100).
   620  		BinaryOp(token.ADD).
   621  		EndInit(1)
   622  	domTest(t, pkg, `package main
   623  
   624  import "github.com/goplus/gogen/internal/builtin"
   625  
   626  var a builtin.Gop_bigrat
   627  var b = (builtin.Gop_bigrat).Gop_Add(a, builtin.Gop_bigrat_Init__0(100))
   628  `)
   629  }
   630  
   631  func TestUntypedBigRatAdd6(t *testing.T) {
   632  	pkg := newGopMainPackage()
   633  	mbig := pkg.Import("github.com/goplus/gogen/internal/builtin")
   634  	pkg.NewVar(token.NoPos, mbig.Ref("Gop_bigrat").Type(), "a")
   635  	pkg.CB().NewVarStart(nil, "b").
   636  		Val(100).
   637  		VarVal("a").
   638  		BinaryOp(token.ADD).
   639  		EndInit(1)
   640  	domTest(t, pkg, `package main
   641  
   642  import "github.com/goplus/gogen/internal/builtin"
   643  
   644  var a builtin.Gop_bigrat
   645  var b = (builtin.Gop_bigrat).Gop_Add(builtin.Gop_bigrat_Init__0(100), a)
   646  `)
   647  }
   648  
   649  func TestUntypedBigRatSub(t *testing.T) {
   650  	pkg := newGopMainPackage()
   651  	pkg.CB().NewVarStart(nil, "a").
   652  		UntypedBigRat(big.NewRat(1, 6)).
   653  		UntypedBigRat(big.NewRat(1, 3)).
   654  		BinaryOp(token.SUB).
   655  		EndInit(1)
   656  	domTest(t, pkg, `package main
   657  
   658  import (
   659  	"github.com/goplus/gogen/internal/builtin"
   660  	"math/big"
   661  )
   662  
   663  var a = builtin.Gop_bigrat_Init__2(big.NewRat(-1, 6))
   664  `)
   665  }
   666  
   667  func TestUntypedBigRatSub2(t *testing.T) {
   668  	pkg := newGopMainPackage()
   669  	mbig := pkg.Import("github.com/goplus/gogen/internal/builtin")
   670  	pkg.NewVar(token.NoPos, mbig.Ref("Gop_bigrat").Type(), "a")
   671  	pkg.CB().NewVarStart(nil, "b").
   672  		VarVal("a").
   673  		UntypedBigRat(big.NewRat(1, 6)).
   674  		BinaryOp(token.SUB).
   675  		EndInit(1)
   676  	domTest(t, pkg, `package main
   677  
   678  import (
   679  	"github.com/goplus/gogen/internal/builtin"
   680  	"math/big"
   681  )
   682  
   683  var a builtin.Gop_bigrat
   684  var b = (builtin.Gop_bigrat).Gop_Sub__0(a, builtin.Gop_bigrat_Init__2(big.NewRat(1, 6)))
   685  `)
   686  }
   687  
   688  func TestUntypedBigRatLT(t *testing.T) {
   689  	pkg := newGopMainPackage()
   690  	pkg.CB().NewVarStart(nil, "a").
   691  		UntypedBigRat(big.NewRat(1, 6)).
   692  		UntypedBigRat(big.NewRat(1, 3)).
   693  		BinaryOp(token.LSS).
   694  		EndInit(1)
   695  	domTest(t, pkg, `package main
   696  
   697  var a = true
   698  `)
   699  }
   700  
   701  func TestUntypedBigRat(t *testing.T) {
   702  	pkg := newGopMainPackage()
   703  	mbig := pkg.Import("github.com/goplus/gogen/internal/builtin")
   704  	pkg.CB().NewVarStart(nil, "a").UntypedBigRat(big.NewRat(6, 63)).EndInit(1)
   705  	pkg.CB().NewVarStart(mbig.Ref("Gop_bigrat").Type(), "b").VarVal("a").EndInit(1)
   706  	domTest(t, pkg, `package main
   707  
   708  import (
   709  	"github.com/goplus/gogen/internal/builtin"
   710  	"math/big"
   711  )
   712  
   713  var a = builtin.Gop_bigrat_Init__2(big.NewRat(2, 21))
   714  var b builtin.Gop_bigrat = a
   715  `)
   716  }
   717  
   718  func TestUntypedBigRat2(t *testing.T) {
   719  	pkg := newGopMainPackage()
   720  	one := big.NewInt(1)
   721  	denom := new(big.Int).Lsh(one, 128)
   722  	v := new(big.Rat).SetFrac(one, denom)
   723  	pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
   724  		DefineVarStart(0, "a").UntypedBigRat(v).EndInit(1).
   725  		End()
   726  	domTest(t, pkg, `package main
   727  
   728  import (
   729  	"github.com/goplus/gogen/internal/builtin"
   730  	"math/big"
   731  )
   732  
   733  func main() {
   734  	a := builtin.Gop_bigrat_Init__2(new(big.Rat).SetFrac(big.NewInt(1), func() *big.Int {
   735  		v, _ := new(big.Int).SetString("340282366920938463463374607431768211456", 10)
   736  		return v
   737  	}()))
   738  }
   739  `)
   740  }
   741  
   742  // ----------------------------------------------------------------------------
   743  
   744  func TestForRangeUDT(t *testing.T) {
   745  	pkg := newMainPackage()
   746  	foo := pkg.Import("github.com/goplus/gogen/internal/foo")
   747  	nodeSet := foo.Ref("NodeSet").Type()
   748  	v := pkg.NewParam(token.NoPos, "v", nodeSet)
   749  	pkg.NewFunc(nil, "bar", types.NewTuple(v), nil, false).BodyStart(pkg).
   750  		ForRange("_", "val").Val(v).RangeAssignThen(token.NoPos).
   751  		Val(pkg.Import("fmt").Ref("Println")).Val(ctxRef(pkg, "val")).Call(1).EndStmt().
   752  		End().End()
   753  	domTest(t, pkg, `package main
   754  
   755  import (
   756  	"fmt"
   757  	"github.com/goplus/gogen/internal/foo"
   758  )
   759  
   760  func bar(v foo.NodeSet) {
   761  	for _gop_it := v.Gop_Enum(); ; {
   762  		var _gop_ok bool
   763  		_, val, _gop_ok := _gop_it.Next()
   764  		if !_gop_ok {
   765  			break
   766  		}
   767  		fmt.Println(val)
   768  	}
   769  }
   770  `)
   771  }
   772  
   773  func TestForRangeUDT2(t *testing.T) {
   774  	pkg := newMainPackage()
   775  	foo := pkg.Import("github.com/goplus/gogen/internal/foo")
   776  	bar := foo.Ref("Bar").Type()
   777  	v := pkg.NewParam(token.NoPos, "v", types.NewPointer(bar))
   778  	pkg.NewFunc(nil, "bar", types.NewTuple(v), nil, false).BodyStart(pkg).
   779  		ForRange("_", "val").Val(v).RangeAssignThen(token.NoPos).
   780  		Val(pkg.Import("fmt").Ref("Println")).Val(ctxRef(pkg, "val")).Call(1).EndStmt().
   781  		End().End()
   782  	domTest(t, pkg, `package main
   783  
   784  import (
   785  	"fmt"
   786  	"github.com/goplus/gogen/internal/foo"
   787  )
   788  
   789  func bar(v *foo.Bar) {
   790  	for _gop_it := v.Gop_Enum(); ; {
   791  		var _gop_ok bool
   792  		val, _gop_ok := _gop_it.Next()
   793  		if !_gop_ok {
   794  			break
   795  		}
   796  		fmt.Println(val)
   797  	}
   798  }
   799  `)
   800  }
   801  
   802  func TestForRangeUDT3_WithAssign(t *testing.T) {
   803  	pkg := newMainPackage()
   804  	foo := pkg.Import("github.com/goplus/gogen/internal/foo")
   805  	bar := foo.Ref("Bar").Type()
   806  	v := pkg.NewParam(token.NoPos, "v", types.NewPointer(bar))
   807  	pkg.NewFunc(nil, "bar", types.NewTuple(v), nil, false).BodyStart(pkg).
   808  		NewVar(types.Typ[types.String], "val").
   809  		ForRange().VarRef(ctxRef(pkg, "val")).Val(v).RangeAssignThen(token.NoPos).
   810  		Val(pkg.Import("fmt").Ref("Println")).Val(ctxRef(pkg, "val")).Call(1).EndStmt().
   811  		End().End()
   812  	domTest(t, pkg, `package main
   813  
   814  import (
   815  	"fmt"
   816  	"github.com/goplus/gogen/internal/foo"
   817  )
   818  
   819  func bar(v *foo.Bar) {
   820  	var val string
   821  	for _gop_it := v.Gop_Enum(); ; {
   822  		var _gop_ok bool
   823  		val, _gop_ok = _gop_it.Next()
   824  		if !_gop_ok {
   825  			break
   826  		}
   827  		fmt.Println(val)
   828  	}
   829  }
   830  `)
   831  }
   832  
   833  // bugfix: for range udt { ... }
   834  func TestForRangeUDT3_NoAssign(t *testing.T) {
   835  	pkg := newMainPackage()
   836  	foo := pkg.Import("github.com/goplus/gogen/internal/foo")
   837  	bar := foo.Ref("Bar").Type()
   838  	v := pkg.NewParam(token.NoPos, "v", types.NewPointer(bar))
   839  	pkg.NewFunc(nil, "bar", types.NewTuple(v), nil, false).BodyStart(pkg).
   840  		ForRange().Val(v).RangeAssignThen(token.NoPos).
   841  		Val(pkg.Import("fmt").Ref("Println")).Val("Hi").Call(1).EndStmt().
   842  		End().End()
   843  	domTest(t, pkg, `package main
   844  
   845  import (
   846  	"fmt"
   847  	"github.com/goplus/gogen/internal/foo"
   848  )
   849  
   850  func bar(v *foo.Bar) {
   851  	for _gop_it := v.Gop_Enum(); ; {
   852  		var _gop_ok bool
   853  		_, _gop_ok = _gop_it.Next()
   854  		if !_gop_ok {
   855  			break
   856  		}
   857  		fmt.Println("Hi")
   858  	}
   859  }
   860  `)
   861  }
   862  
   863  // bugfix: for _ = range udt { ... }
   864  // bugfix: [" " for _ <- :10]
   865  func TestForRangeUDT_UNDERLINE(t *testing.T) {
   866  	pkg := newMainPackage()
   867  	foo := pkg.Import("github.com/goplus/gogen/internal/foo")
   868  	bar := foo.Ref("Bar").Type()
   869  	v := pkg.NewParam(token.NoPos, "v", types.NewPointer(bar))
   870  	pkg.NewFunc(nil, "bar", types.NewTuple(v), nil, false).BodyStart(pkg).
   871  		ForRange().VarRef(ctxRef(pkg, "_")).Val(v).RangeAssignThen(token.NoPos).
   872  		Val(pkg.Import("fmt").Ref("Println")).Val("Hi").Call(1).EndStmt().
   873  		End().End()
   874  	domTest(t, pkg, `package main
   875  
   876  import (
   877  	"fmt"
   878  	"github.com/goplus/gogen/internal/foo"
   879  )
   880  
   881  func bar(v *foo.Bar) {
   882  	for _gop_it := v.Gop_Enum(); ; {
   883  		var _gop_ok bool
   884  		_, _gop_ok = _gop_it.Next()
   885  		if !_gop_ok {
   886  			break
   887  		}
   888  		fmt.Println("Hi")
   889  	}
   890  }
   891  `)
   892  }
   893  
   894  // bugfix: for _,_ = range udt { ... }
   895  func TestForRangeUDT_UNDERLINE2(t *testing.T) {
   896  	pkg := newMainPackage()
   897  	foo := pkg.Import("github.com/goplus/gogen/internal/foo")
   898  	nodeSet := foo.Ref("NodeSet").Type()
   899  	v := pkg.NewParam(token.NoPos, "v", nodeSet)
   900  	pkg.NewFunc(nil, "bar", types.NewTuple(v), nil, false).BodyStart(pkg).
   901  		ForRange("_", "_").Val(v).RangeAssignThen(token.NoPos).
   902  		Val(pkg.Import("fmt").Ref("Println")).Val("Hi").Call(1).EndStmt().
   903  		End().End()
   904  	domTest(t, pkg, `package main
   905  
   906  import (
   907  	"fmt"
   908  	"github.com/goplus/gogen/internal/foo"
   909  )
   910  
   911  func bar(v foo.NodeSet) {
   912  	for _gop_it := v.Gop_Enum(); ; {
   913  		var _gop_ok bool
   914  		_, _, _gop_ok = _gop_it.Next()
   915  		if !_gop_ok {
   916  			break
   917  		}
   918  		fmt.Println("Hi")
   919  	}
   920  }
   921  `)
   922  }
   923  
   924  func TestForRangeUDT4(t *testing.T) {
   925  	pkg := newMainPackage()
   926  	foo := pkg.Import("github.com/goplus/gogen/internal/foo")
   927  	bar := foo.Ref("Foo").Type()
   928  	v := pkg.NewParam(token.NoPos, "v", types.NewPointer(bar))
   929  	pkg.NewFunc(nil, "bar", types.NewTuple(v), nil, false).BodyStart(pkg).
   930  		ForRange("elem").Val(v).RangeAssignThen(token.NoPos).
   931  		Val(pkg.Import("fmt").Ref("Println")).Val(ctxRef(pkg, "elem")).Call(1).EndStmt().
   932  		SetBodyHandler(func(body *ast.BlockStmt, kind int) {
   933  			gogen.InsertStmtFront(body, &ast.ExprStmt{X: ast.NewIdent("__sched__")})
   934  		}).
   935  		End().End()
   936  	domTest(t, pkg, `package main
   937  
   938  import (
   939  	"fmt"
   940  	"github.com/goplus/gogen/internal/foo"
   941  )
   942  
   943  func bar(v *foo.Foo) {
   944  	v.Gop_Enum(func(elem string) {
   945  		__sched__
   946  		fmt.Println(elem)
   947  	})
   948  }
   949  `)
   950  }
   951  
   952  func TestForRangeUDT5(t *testing.T) {
   953  	pkg := newMainPackage()
   954  	foo := pkg.Import("github.com/goplus/gogen/internal/foo")
   955  	bar := foo.Ref("Foo2").Type()
   956  	v := pkg.NewParam(token.NoPos, "v", types.NewPointer(bar))
   957  	pkg.NewFunc(nil, "bar", types.NewTuple(v), nil, false).BodyStart(pkg).
   958  		ForRange("key", "elem").Val(v).RangeAssignThen(token.NoPos).
   959  		Val(pkg.Import("fmt").Ref("Println")).Val(ctxRef(pkg, "key")).Val(ctxRef(pkg, "elem")).
   960  		Call(2).EndStmt().
   961  		End().End()
   962  	domTest(t, pkg, `package main
   963  
   964  import (
   965  	"fmt"
   966  	"github.com/goplus/gogen/internal/foo"
   967  )
   968  
   969  func bar(v *foo.Foo2) {
   970  	v.Gop_Enum(func(key int, elem string) {
   971  		fmt.Println(key, elem)
   972  	})
   973  }
   974  `)
   975  }
   976  
   977  // ----------------------------------------------------------------------------
   978  
   979  func TestStaticMethod(t *testing.T) {
   980  	pkg := newMainPackage()
   981  	bar := pkg.Import("github.com/goplus/gogen/internal/bar")
   982  	pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
   983  		Typ(bar.Ref("Game").Type()).MemberVal("New").Call(0).EndStmt().
   984  		End()
   985  	domTest(t, pkg, `package main
   986  
   987  import "github.com/goplus/gogen/internal/bar"
   988  
   989  func main() {
   990  	bar.Gops_Game_New()
   991  }
   992  `)
   993  }
   994  
   995  func TestTemplateRecvMethod(t *testing.T) {
   996  	pkg := newMainPackage()
   997  	bar := pkg.Import("github.com/goplus/gogen/internal/bar")
   998  	pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
   999  		NewVar(bar.Ref("Game").Type(), "g").
  1000  		VarVal("g").MemberVal("Run").Val("Hi").Call(1).EndStmt().
  1001  		End()
  1002  	domTest(t, pkg, `package main
  1003  
  1004  import "github.com/goplus/gogen/internal/bar"
  1005  
  1006  func main() {
  1007  	var g bar.Game
  1008  	bar.Gopt_Game_Run(&g, "Hi")
  1009  }
  1010  `)
  1011  }
  1012  
  1013  func TestTemplateRecvMethod2(t *testing.T) {
  1014  	pkg := newMainPackage()
  1015  	bar := pkg.Import("github.com/goplus/gogen/internal/bar")
  1016  	tyGame := bar.Ref("Game").Type()
  1017  	pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
  1018  		NewVar(tyGame, "g").
  1019  		Typ(tyGame).MemberVal("Run").VarRef("g").UnaryOp(token.AND).Val("Hi").Call(2).EndStmt().
  1020  		End()
  1021  	domTest(t, pkg, `package main
  1022  
  1023  import "github.com/goplus/gogen/internal/bar"
  1024  
  1025  func main() {
  1026  	var g bar.Game
  1027  	bar.Gopt_Game_Run(&g, "Hi")
  1028  }
  1029  `)
  1030  }
  1031  
  1032  func TestErrTemplateRecvMethod(t *testing.T) {
  1033  	pkg := newMainPackage()
  1034  	bar := pkg.Import("github.com/goplus/gogen/internal/bar")
  1035  	defer func() {
  1036  		if e := recover(); e == nil {
  1037  			t.Fatal("TestErrTemplateRecvMethod: no error?")
  1038  		}
  1039  	}()
  1040  	pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
  1041  		NewVar(types.NewPointer(bar.Ref("Game").Type()), "g").
  1042  		VarVal("g").MemberVal("Run").Call(0).EndStmt().
  1043  		End()
  1044  }
  1045  
  1046  func TestBigIntCastUntypedFloat(t *testing.T) {
  1047  	pkg := newGopMainPackage()
  1048  	mbig := pkg.Import("github.com/goplus/gogen/internal/builtin")
  1049  	pkg.CB().NewVarStart(nil, "a").
  1050  		Val(mbig.Ref("Gop_bigint")).
  1051  		Val(&ast.BasicLit{Kind: token.FLOAT, Value: "1e20"}).Call(1).EndInit(1)
  1052  	domTest(t, pkg, `package main
  1053  
  1054  import (
  1055  	"github.com/goplus/gogen/internal/builtin"
  1056  	"math/big"
  1057  )
  1058  
  1059  var a = builtin.Gop_bigint_Cast__1(func() *big.Int {
  1060  	v, _ := new(big.Int).SetString("100000000000000000000", 10)
  1061  	return v
  1062  }())
  1063  `)
  1064  }
  1065  
  1066  func TestBigIntCastUntypedFloatError(t *testing.T) {
  1067  	defer func() {
  1068  		if e := recover(); e == nil {
  1069  			t.Fatal("TestBigIntCastUntypedFloatError: no error?")
  1070  		}
  1071  	}()
  1072  	pkg := newGopMainPackage()
  1073  	mbig := pkg.Import("github.com/goplus/gogen/internal/builtin")
  1074  	pkg.CB().NewVarStart(nil, "a").
  1075  		Val(mbig.Ref("Gop_bigint")).
  1076  		Val(&ast.BasicLit{Kind: token.FLOAT, Value: "10000000000000000000.1"}).
  1077  		Call(1).EndInit(1)
  1078  }
  1079  
  1080  func TestUntypedBigDefault(t *testing.T) {
  1081  	pkg := newGopMainPackage()
  1082  	pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
  1083  		Val(ctxRef(pkg, "println")).
  1084  		UntypedBigInt(big.NewInt(1)).Call(1).EndStmt().
  1085  		Val(ctxRef(pkg, "println")).
  1086  		UntypedBigRat(big.NewRat(1, 2)).Call(1).EndStmt().End()
  1087  	domTest(t, pkg, `package main
  1088  
  1089  import (
  1090  	"fmt"
  1091  	"github.com/goplus/gogen/internal/builtin"
  1092  	"math/big"
  1093  )
  1094  
  1095  func main() {
  1096  	fmt.Println(builtin.Gop_bigint_Init__1(big.NewInt(1)))
  1097  	fmt.Println(builtin.Gop_bigrat_Init__2(big.NewRat(1, 2)))
  1098  }
  1099  `)
  1100  }
  1101  
  1102  func TestUntypedBigDefaultCall(t *testing.T) {
  1103  	pkg := newGopMainPackage()
  1104  	pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
  1105  		UntypedBigInt(big.NewInt(1)).MemberVal("Int64").Call(0).EndStmt().
  1106  		UntypedBigRat(big.NewRat(1, 2)).MemberVal("Float64").Call(0).EndStmt().End()
  1107  	domTest(t, pkg, `package main
  1108  
  1109  import (
  1110  	"github.com/goplus/gogen/internal/builtin"
  1111  	"math/big"
  1112  )
  1113  
  1114  func main() {
  1115  	builtin.Gop_bigint_Init__1(big.NewInt(1)).Int64()
  1116  	builtin.Gop_bigrat_Init__2(big.NewRat(1, 2)).Float64()
  1117  }
  1118  `)
  1119  }
  1120  
  1121  func TestUntypedBigIntToInterface(t *testing.T) {
  1122  	pkg := newGopMainPackage()
  1123  	methods := []*types.Func{
  1124  		types.NewFunc(token.NoPos, pkg.Types, "Int64", types.NewSignatureType(nil, nil, nil, nil,
  1125  			types.NewTuple(types.NewVar(token.NoPos, nil, "v", types.Typ[types.Int64])), false)),
  1126  	}
  1127  	tyInterf := types.NewInterfaceType(methods, nil).Complete()
  1128  	tyA := pkg.NewType("A").InitType(pkg, tyInterf)
  1129  	pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
  1130  		NewVarStart(tyA, "a").UntypedBigInt(big.NewInt(1)).EndInit(1).
  1131  		Val(ctxRef(pkg, "println")).
  1132  		VarVal("a").MemberVal("Int64").Call(0).Call(1).EndStmt().End()
  1133  	domTest(t, pkg, `package main
  1134  
  1135  import (
  1136  	"fmt"
  1137  	"github.com/goplus/gogen/internal/builtin"
  1138  	"math/big"
  1139  )
  1140  
  1141  type A interface {
  1142  	Int64() (v int64)
  1143  }
  1144  
  1145  func main() {
  1146  	var a A = builtin.Gop_bigint_Init__1(big.NewInt(1))
  1147  	fmt.Println(a.Int64())
  1148  }
  1149  `)
  1150  }
  1151  
  1152  func TestInt128(t *testing.T) {
  1153  	pkg := newGopMainPackage()
  1154  	builtin := pkg.Import("github.com/goplus/gogen/internal/builtin")
  1155  	n1 := big.NewInt(1)
  1156  	n1.Lsh(n1, 127).Sub(n1, big.NewInt(1))
  1157  	n2 := big.NewInt(-1)
  1158  	n2.Lsh(n2, 127)
  1159  	uint128 := builtin.Ref("Uint128").Type()
  1160  	int128 := builtin.Ref("Int128").Type()
  1161  	pkg.CB().NewVarStart(int128, "a").UntypedBigInt(n1).EndInit(1)
  1162  	pkg.CB().NewVarStart(int128, "b").Val(1).EndInit(1)
  1163  	pkg.CB().NewVarStart(int128, "c").Val(&ast.BasicLit{Kind: token.FLOAT, Value: "1e30"}).EndInit(1)
  1164  	pkg.CB().NewVarStart(int128, "d").Typ(int128).UntypedBigInt(n2).Call(1).EndInit(1)
  1165  	pkg.CB().NewVarStart(int128, "e").Typ(int128).Typ(uint128).Val(1).Call(1).Call(1).EndInit(1)
  1166  	domTest(t, pkg, `package main
  1167  
  1168  import (
  1169  	"github.com/goplus/gogen/internal/builtin"
  1170  	"math/big"
  1171  )
  1172  
  1173  var a builtin.Int128 = builtin.Int128_Init__1(func() *big.Int {
  1174  	v, _ := new(big.Int).SetString("170141183460469231731687303715884105727", 10)
  1175  	return v
  1176  }())
  1177  var b builtin.Int128 = builtin.Int128_Init__0(1)
  1178  var c builtin.Int128 = builtin.Int128_Init__1(func() *big.Int {
  1179  	v, _ := new(big.Int).SetString("1000000000000000000000000000000", 10)
  1180  	return v
  1181  }())
  1182  var d builtin.Int128 = builtin.Int128_Cast__1(func() *big.Int {
  1183  	v, _ := new(big.Int).SetString("-170141183460469231731687303715884105728", 10)
  1184  	return v
  1185  }())
  1186  var e builtin.Int128 = builtin.Int128(builtin.Uint128_Cast__0(1))
  1187  `)
  1188  }
  1189  
  1190  func TestUint128(t *testing.T) {
  1191  	pkg := newGopMainPackage()
  1192  	builtin := pkg.Import("github.com/goplus/gogen/internal/builtin")
  1193  	n1 := big.NewInt(1)
  1194  	n1.Lsh(n1, 128).Sub(n1, big.NewInt(1))
  1195  	uint128 := builtin.Ref("Uint128").Type()
  1196  	int128 := builtin.Ref("Int128").Type()
  1197  	pkg.CB().NewVarStart(uint128, "a").UntypedBigInt(n1).EndInit(1)
  1198  	pkg.CB().NewVarStart(uint128, "b").Val(0).EndInit(1)
  1199  	pkg.CB().NewVarStart(uint128, "c").Val(&ast.BasicLit{Kind: token.FLOAT, Value: "1e30"}).EndInit(1)
  1200  	pkg.CB().NewVarStart(uint128, "d").Typ(uint128).Typ(int128).Val(1).Call(1).Call(1).EndInit(1)
  1201  	domTest(t, pkg, `package main
  1202  
  1203  import (
  1204  	"github.com/goplus/gogen/internal/builtin"
  1205  	"math/big"
  1206  )
  1207  
  1208  var a builtin.Uint128 = builtin.Uint128_Init__1(func() *big.Int {
  1209  	v, _ := new(big.Int).SetString("340282366920938463463374607431768211455", 10)
  1210  	return v
  1211  }())
  1212  var b builtin.Uint128 = builtin.Uint128_Init__0(0)
  1213  var c builtin.Uint128 = builtin.Uint128_Init__1(func() *big.Int {
  1214  	v, _ := new(big.Int).SetString("1000000000000000000000000000000", 10)
  1215  	return v
  1216  }())
  1217  var d builtin.Uint128 = builtin.Uint128(builtin.Int128_Cast__0(1))
  1218  `)
  1219  }
  1220  
  1221  func TestErrInt128(t *testing.T) {
  1222  	t.Run("Int128_Max", func(t *testing.T) {
  1223  		defer func() {
  1224  			if e := recover(); e == nil {
  1225  				t.Fatal("Int128_Max: no error?")
  1226  			} else {
  1227  				t.Log(e)
  1228  			}
  1229  		}()
  1230  		pkg := newGopMainPackage()
  1231  		builtin := pkg.Import("github.com/goplus/gogen/internal/builtin")
  1232  		n := big.NewInt(1)
  1233  		n.Lsh(n, 127)
  1234  		int128 := builtin.Ref("Int128").Type()
  1235  		pkg.CB().NewVarStart(int128, "a").UntypedBigInt(n).EndInit(1)
  1236  	})
  1237  	t.Run("Int128_Max_Float", func(t *testing.T) {
  1238  		defer func() {
  1239  			if e := recover(); e == nil {
  1240  				t.Fatal("Int128_Max_Float: no error?")
  1241  			} else {
  1242  				t.Log(e)
  1243  			}
  1244  		}()
  1245  		pkg := newGopMainPackage()
  1246  		builtin := pkg.Import("github.com/goplus/gogen/internal/builtin")
  1247  		int128 := builtin.Ref("Int128").Type()
  1248  		pkg.CB().NewVarStart(int128, "a").Val(&ast.BasicLit{Kind: token.FLOAT, Value: "1e60"}).EndInit(1)
  1249  	})
  1250  	t.Run("Int128_Min", func(t *testing.T) {
  1251  		defer func() {
  1252  			if e := recover(); e == nil {
  1253  				t.Fatal("Int128_Min: no error?")
  1254  			} else {
  1255  				t.Log(e)
  1256  			}
  1257  		}()
  1258  		pkg := newGopMainPackage()
  1259  		builtin := pkg.Import("github.com/goplus/gogen/internal/builtin")
  1260  		n := big.NewInt(-1)
  1261  		n.Lsh(n, 127).Sub(n, big.NewInt(1))
  1262  		int128 := builtin.Ref("Int128").Type()
  1263  		pkg.CB().NewVarStart(int128, "a").Typ(int128).UntypedBigInt(n).Call(1).EndInit(1)
  1264  	})
  1265  	t.Run("Int128_Uint128", func(t *testing.T) {
  1266  		defer func() {
  1267  			if e := recover(); e == nil {
  1268  				t.Fatal("Int128_Uint128: no error?")
  1269  			} else {
  1270  				t.Log(e)
  1271  			}
  1272  		}()
  1273  		pkg := newGopMainPackage()
  1274  		builtin := pkg.Import("github.com/goplus/gogen/internal/builtin")
  1275  		n := big.NewInt(1)
  1276  		n.Lsh(n, 127)
  1277  		int128 := builtin.Ref("Int128").Type()
  1278  		uint128 := builtin.Ref("Uint128").Type()
  1279  		pkg.CB().NewVarStart(int128, "a").Typ(int128).Typ(uint128).UntypedBigInt(n).Call(1).Call(1).EndInit(1)
  1280  	})
  1281  }
  1282  
  1283  func TestErrUint128(t *testing.T) {
  1284  	t.Run("Uint128_Max", func(t *testing.T) {
  1285  		defer func() {
  1286  			if e := recover(); e == nil {
  1287  				t.Fatal("Uint128_Max: no error?")
  1288  			} else {
  1289  				t.Log(e)
  1290  			}
  1291  		}()
  1292  		pkg := newGopMainPackage()
  1293  		builtin := pkg.Import("github.com/goplus/gogen/internal/builtin")
  1294  		n := big.NewInt(1)
  1295  		n.Lsh(n, 128)
  1296  		uint128 := builtin.Ref("Uint128").Type()
  1297  		pkg.CB().NewVarStart(uint128, "a").UntypedBigInt(n).EndInit(1)
  1298  	})
  1299  	t.Run("Uint128_Max_Float", func(t *testing.T) {
  1300  		defer func() {
  1301  			if e := recover(); e == nil {
  1302  				t.Fatal("Uint128_Max_Float: no error?")
  1303  			} else {
  1304  				t.Log(e)
  1305  			}
  1306  		}()
  1307  		pkg := newGopMainPackage()
  1308  		builtin := pkg.Import("github.com/goplus/gogen/internal/builtin")
  1309  		uint128 := builtin.Ref("Uint128").Type()
  1310  		pkg.CB().NewVarStart(uint128, "a").Val(&ast.BasicLit{Kind: token.FLOAT, Value: "1e60"}).EndInit(1)
  1311  	})
  1312  	t.Run("Uint128_Min", func(t *testing.T) {
  1313  		defer func() {
  1314  			if e := recover(); e == nil {
  1315  				t.Fatal("Uint128_Min: no error?")
  1316  			} else {
  1317  				t.Log(e)
  1318  			}
  1319  		}()
  1320  		pkg := newGopMainPackage()
  1321  		builtin := pkg.Import("github.com/goplus/gogen/internal/builtin")
  1322  		uint128 := builtin.Ref("Uint128").Type()
  1323  		pkg.CB().NewVarStart(uint128, "a").Typ(uint128).Val(-1).Call(1).EndInit(1)
  1324  	})
  1325  	t.Run("Unt128_Int128", func(t *testing.T) {
  1326  		defer func() {
  1327  			if e := recover(); e == nil {
  1328  				t.Fatal("Unt128_Int128: no error?")
  1329  			} else {
  1330  				t.Log(e)
  1331  			}
  1332  		}()
  1333  		pkg := newGopMainPackage()
  1334  		builtin := pkg.Import("github.com/goplus/gogen/internal/builtin")
  1335  		int128 := builtin.Ref("Int128").Type()
  1336  		uint128 := builtin.Ref("Uint128").Type()
  1337  		pkg.CB().NewVarStart(uint128, "a").Typ(uint128).Typ(int128).Val(-1).Call(1).Call(1).EndInit(1)
  1338  	})
  1339  }
  1340  
  1341  // ----------------------------------------------------------------------------