github.com/goplus/gox@v1.14.13-0.20240308130321-6ff7f61cfae8/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 gox_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/gox" 25 ) 26 27 func initGopBuiltin(big gox.PkgRef, conf *gox.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 *gox.Package, conf *gox.Config) *types.Package { 34 fmt := pkg.Import("fmt") 35 big := pkg.Import("github.com/goplus/gox/internal/builtin") 36 builtin := types.NewPackage("", "") 37 if builtin.Scope().Insert(gox.NewOverloadFunc(token.NoPos, builtin, "println", fmt.Ref("Println"))) != nil { 38 panic("println exists") 39 } 40 gox.InitBuiltin(pkg, builtin, conf) 41 initGopBuiltin(big, conf) 42 return builtin 43 } 44 45 func newGopMainPackage() *gox.Package { 46 conf := &gox.Config{ 47 Fset: gblFset, 48 Importer: gblImp, 49 NewBuiltin: newGopBuiltinDefault, 50 } 51 return gox.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/gox/internal/builtin") 105 pkg.CB().NewVar(big.Ref("Gop_bigint").Type(), "a") 106 domTest(t, pkg, `package main 107 108 import "github.com/goplus/gox/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/gox/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/gox/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/gox/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/gox/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/gox/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/gox/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/gox/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/gox/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/gox/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/gox/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/gox/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/gox/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/gox/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/gox/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/gox/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, gox.InstrFlagTwoValue). 267 EndInit(1). 268 End() 269 domTest(t, pkg, `package main 270 271 import ( 272 "github.com/goplus/gox/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/gox/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, gox.InstrFlagTwoValue). 290 EndInit(1). 291 End() 292 domTest(t, pkg, `package main 293 294 import ( 295 "github.com/goplus/gox/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/gox/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, gox.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/gox/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/gox/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(ctxRef(pkg, "a")). 346 IncDec(token.INC). 347 End() 348 domTest(t, pkg, `package main 349 350 import "github.com/goplus/gox/internal/builtin" 351 352 var a builtin.Gop_bigrat 353 354 func main() { 355 a.Gop_Inc() 356 } 357 `) 358 } 359 360 func TestErrBigRatIncDec(t *testing.T) { 361 defer func() { 362 if e := recover(); e == nil { 363 t.Fatal("TestErrBigRatIncDec: no error?") 364 } 365 }() 366 pkg := newGopMainPackage() 367 big := pkg.Import("github.com/goplus/gox/internal/builtin") 368 pkg.NewVar(token.NoPos, big.Ref("Gop_bigrat").Type(), "a") 369 pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg). 370 VarRef(ctxRef(pkg, "a")). 371 IncDec(token.DEC). 372 End() 373 } 374 375 func TestErrBigRatAssignOp(t *testing.T) { 376 defer func() { 377 if e := recover(); e == nil { 378 t.Fatal("TestErrBigRatAssignOp: no error?") 379 } 380 }() 381 pkg := newGopMainPackage() 382 big := pkg.Import("github.com/goplus/gox/internal/builtin") 383 pkg.NewVar(token.NoPos, big.Ref("Gop_bigrat").Type(), "a", "b") 384 pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg). 385 VarRef(ctxRef(pkg, "a")). 386 VarVal("b"). 387 AssignOp(token.SUB_ASSIGN). 388 End() 389 } 390 391 func TestBigRatAssignOp(t *testing.T) { 392 pkg := newGopMainPackage() 393 big := pkg.Import("github.com/goplus/gox/internal/builtin") 394 pkg.NewVar(token.NoPos, big.Ref("Gop_bigrat").Type(), "a", "b") 395 pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg). 396 VarRef(ctxRef(pkg, "a")). 397 VarVal("b"). 398 AssignOp(token.ADD_ASSIGN). 399 End() 400 domTest(t, pkg, `package main 401 402 import "github.com/goplus/gox/internal/builtin" 403 404 var a, b builtin.Gop_bigrat 405 406 func main() { 407 a.Gop_AddAssign(b) 408 } 409 `) 410 } 411 412 func TestBigRatAssignOp2(t *testing.T) { 413 pkg := newGopMainPackage() 414 big := pkg.Import("github.com/goplus/gox/internal/builtin") 415 pkg.NewVar(token.NoPos, big.Ref("Gop_bigrat").Type(), "a") 416 pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg). 417 VarRef(ctxRef(pkg, "a")). 418 Val(1). 419 AssignOp(token.ADD_ASSIGN). 420 End() 421 domTest(t, pkg, `package main 422 423 import "github.com/goplus/gox/internal/builtin" 424 425 var a builtin.Gop_bigrat 426 427 func main() { 428 a.Gop_AddAssign(builtin.Gop_bigrat_Init__0(1)) 429 } 430 `) 431 } 432 433 func TestUntypedBigIntQuo(t *testing.T) { 434 pkg := newGopMainPackage() 435 pkg.CB().NewVarStart(nil, "a"). 436 UntypedBigInt(big.NewInt(6)). 437 UntypedBigInt(big.NewInt(63)). 438 BinaryOp(token.QUO). 439 EndInit(1) 440 domTest(t, pkg, `package main 441 442 import ( 443 "github.com/goplus/gox/internal/builtin" 444 "math/big" 445 ) 446 447 var a = builtin.Gop_bigrat_Init__2(big.NewRat(2, 21)) 448 `) 449 } 450 451 func TestUntypedBigIntQuo2(t *testing.T) { 452 pkg := newGopMainPackage() 453 pkg.CB().NewVarStart(nil, "a"). 454 Val(6). 455 UntypedBigInt(big.NewInt(63)). 456 BinaryOp(token.QUO). 457 EndInit(1) 458 domTest(t, pkg, `package main 459 460 import ( 461 "github.com/goplus/gox/internal/builtin" 462 "math/big" 463 ) 464 465 var a = builtin.Gop_bigrat_Init__2(big.NewRat(2, 21)) 466 `) 467 } 468 469 func TestUntypedBigIntQuo3(t *testing.T) { 470 pkg := newGopMainPackage() 471 pkg.CB().NewVarStart(nil, "a"). 472 UntypedBigInt(big.NewInt(63)). 473 Val(6). 474 BinaryOp(token.QUO). 475 EndInit(1) 476 domTest(t, pkg, `package main 477 478 import ( 479 "github.com/goplus/gox/internal/builtin" 480 "math/big" 481 ) 482 483 var a = builtin.Gop_bigrat_Init__2(big.NewRat(21, 2)) 484 `) 485 } 486 487 func TestUntypedBigIntRem(t *testing.T) { 488 pkg := newGopMainPackage() 489 pkg.CB().NewVarStart(nil, "a"). 490 UntypedBigInt(big.NewInt(100)). 491 UntypedBigInt(big.NewInt(7)). 492 BinaryOp(token.REM). 493 EndInit(1) 494 domTest(t, pkg, `package main 495 496 import ( 497 "github.com/goplus/gox/internal/builtin" 498 "math/big" 499 ) 500 501 var a = builtin.Gop_bigint_Init__1(big.NewInt(2)) 502 `) 503 } 504 505 func TestUntypedBigIntShift(t *testing.T) { 506 pkg := newGopMainPackage() 507 pkg.CB().NewVarStart(nil, "a"). 508 UntypedBigInt(big.NewInt(1)). 509 Val(128). 510 BinaryOp(token.SHL). 511 EndInit(1) 512 domTest(t, pkg, `package main 513 514 import ( 515 "github.com/goplus/gox/internal/builtin" 516 "math/big" 517 ) 518 519 var a = builtin.Gop_bigint_Init__1(func() *big.Int { 520 v, _ := new(big.Int).SetString("340282366920938463463374607431768211456", 10) 521 return v 522 }()) 523 `) 524 } 525 526 func TestUntypedBigRatAdd(t *testing.T) { 527 pkg := newGopMainPackage() 528 pkg.CB().NewVarStart(nil, "a"). 529 UntypedBigRat(big.NewRat(1, 6)). 530 UntypedBigRat(big.NewRat(1, 3)). 531 BinaryOp(token.ADD). 532 EndInit(1) 533 domTest(t, pkg, `package main 534 535 import ( 536 "github.com/goplus/gox/internal/builtin" 537 "math/big" 538 ) 539 540 var a = builtin.Gop_bigrat_Init__2(big.NewRat(1, 2)) 541 `) 542 } 543 544 func TestUntypedBigRatAdd2(t *testing.T) { 545 pkg := newGopMainPackage() 546 pkg.CB().NewVarStart(nil, "a"). 547 UntypedBigRat(big.NewRat(1, 6)). 548 UntypedBigInt(big.NewInt(3)). 549 BinaryOp(token.ADD). 550 EndInit(1) 551 domTest(t, pkg, `package main 552 553 import ( 554 "github.com/goplus/gox/internal/builtin" 555 "math/big" 556 ) 557 558 var a = builtin.Gop_bigrat_Init__2(big.NewRat(19, 6)) 559 `) 560 } 561 562 func TestUntypedBigRatAdd3(t *testing.T) { 563 pkg := newGopMainPackage() 564 pkg.CB().NewVarStart(nil, "a"). 565 UntypedBigInt(big.NewInt(3)). 566 UntypedBigRat(big.NewRat(1, 6)). 567 BinaryOp(token.ADD). 568 EndInit(1) 569 domTest(t, pkg, `package main 570 571 import ( 572 "github.com/goplus/gox/internal/builtin" 573 "math/big" 574 ) 575 576 var a = builtin.Gop_bigrat_Init__2(big.NewRat(19, 6)) 577 `) 578 } 579 580 func TestUntypedBigRatAdd4(t *testing.T) { 581 pkg := newGopMainPackage() 582 mbig := pkg.Import("github.com/goplus/gox/internal/builtin") 583 pkg.NewVar(token.NoPos, mbig.Ref("Gop_bigrat").Type(), "a") 584 pkg.CB().NewVarStart(nil, "b"). 585 VarVal("a"). 586 UntypedBigRat(big.NewRat(1, 6)). 587 BinaryOp(token.ADD). 588 EndInit(1) 589 domTest(t, pkg, `package main 590 591 import ( 592 "github.com/goplus/gox/internal/builtin" 593 "math/big" 594 ) 595 596 var a builtin.Gop_bigrat 597 var b = (builtin.Gop_bigrat).Gop_Add(a, builtin.Gop_bigrat_Init__2(big.NewRat(1, 6))) 598 `) 599 } 600 601 func TestUntypedBigRatAdd5(t *testing.T) { 602 pkg := newGopMainPackage() 603 mbig := pkg.Import("github.com/goplus/gox/internal/builtin") 604 pkg.NewVar(token.NoPos, mbig.Ref("Gop_bigrat").Type(), "a") 605 pkg.CB().NewVarStart(nil, "b"). 606 VarVal("a"). 607 Val(100). 608 BinaryOp(token.ADD). 609 EndInit(1) 610 domTest(t, pkg, `package main 611 612 import "github.com/goplus/gox/internal/builtin" 613 614 var a builtin.Gop_bigrat 615 var b = (builtin.Gop_bigrat).Gop_Add(a, builtin.Gop_bigrat_Init__0(100)) 616 `) 617 } 618 619 func TestUntypedBigRatAdd6(t *testing.T) { 620 pkg := newGopMainPackage() 621 mbig := pkg.Import("github.com/goplus/gox/internal/builtin") 622 pkg.NewVar(token.NoPos, mbig.Ref("Gop_bigrat").Type(), "a") 623 pkg.CB().NewVarStart(nil, "b"). 624 Val(100). 625 VarVal("a"). 626 BinaryOp(token.ADD). 627 EndInit(1) 628 domTest(t, pkg, `package main 629 630 import "github.com/goplus/gox/internal/builtin" 631 632 var a builtin.Gop_bigrat 633 var b = (builtin.Gop_bigrat).Gop_Add(builtin.Gop_bigrat_Init__0(100), a) 634 `) 635 } 636 637 func TestUntypedBigRatSub(t *testing.T) { 638 pkg := newGopMainPackage() 639 pkg.CB().NewVarStart(nil, "a"). 640 UntypedBigRat(big.NewRat(1, 6)). 641 UntypedBigRat(big.NewRat(1, 3)). 642 BinaryOp(token.SUB). 643 EndInit(1) 644 domTest(t, pkg, `package main 645 646 import ( 647 "github.com/goplus/gox/internal/builtin" 648 "math/big" 649 ) 650 651 var a = builtin.Gop_bigrat_Init__2(big.NewRat(-1, 6)) 652 `) 653 } 654 655 func TestUntypedBigRatSub2(t *testing.T) { 656 pkg := newGopMainPackage() 657 mbig := pkg.Import("github.com/goplus/gox/internal/builtin") 658 pkg.NewVar(token.NoPos, mbig.Ref("Gop_bigrat").Type(), "a") 659 pkg.CB().NewVarStart(nil, "b"). 660 VarVal("a"). 661 UntypedBigRat(big.NewRat(1, 6)). 662 BinaryOp(token.SUB). 663 EndInit(1) 664 domTest(t, pkg, `package main 665 666 import ( 667 "github.com/goplus/gox/internal/builtin" 668 "math/big" 669 ) 670 671 var a builtin.Gop_bigrat 672 var b = (builtin.Gop_bigrat).Gop_Sub__0(a, builtin.Gop_bigrat_Init__2(big.NewRat(1, 6))) 673 `) 674 } 675 676 func TestUntypedBigRatLT(t *testing.T) { 677 pkg := newGopMainPackage() 678 pkg.CB().NewVarStart(nil, "a"). 679 UntypedBigRat(big.NewRat(1, 6)). 680 UntypedBigRat(big.NewRat(1, 3)). 681 BinaryOp(token.LSS). 682 EndInit(1) 683 domTest(t, pkg, `package main 684 685 var a = true 686 `) 687 } 688 689 func TestUntypedBigRat(t *testing.T) { 690 pkg := newGopMainPackage() 691 mbig := pkg.Import("github.com/goplus/gox/internal/builtin") 692 pkg.CB().NewVarStart(nil, "a").UntypedBigRat(big.NewRat(6, 63)).EndInit(1) 693 pkg.CB().NewVarStart(mbig.Ref("Gop_bigrat").Type(), "b").VarVal("a").EndInit(1) 694 domTest(t, pkg, `package main 695 696 import ( 697 "github.com/goplus/gox/internal/builtin" 698 "math/big" 699 ) 700 701 var a = builtin.Gop_bigrat_Init__2(big.NewRat(2, 21)) 702 var b builtin.Gop_bigrat = a 703 `) 704 } 705 706 func TestUntypedBigRat2(t *testing.T) { 707 pkg := newGopMainPackage() 708 one := big.NewInt(1) 709 denom := new(big.Int).Lsh(one, 128) 710 v := new(big.Rat).SetFrac(one, denom) 711 pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg). 712 DefineVarStart(0, "a").UntypedBigRat(v).EndInit(1). 713 End() 714 domTest(t, pkg, `package main 715 716 import ( 717 "github.com/goplus/gox/internal/builtin" 718 "math/big" 719 ) 720 721 func main() { 722 a := builtin.Gop_bigrat_Init__2(new(big.Rat).SetFrac(big.NewInt(1), func() *big.Int { 723 v, _ := new(big.Int).SetString("340282366920938463463374607431768211456", 10) 724 return v 725 }())) 726 } 727 `) 728 } 729 730 // ---------------------------------------------------------------------------- 731 732 func TestForRangeUDT(t *testing.T) { 733 pkg := newMainPackage() 734 foo := pkg.Import("github.com/goplus/gox/internal/foo") 735 nodeSet := foo.Ref("NodeSet").Type() 736 v := pkg.NewParam(token.NoPos, "v", nodeSet) 737 pkg.NewFunc(nil, "bar", types.NewTuple(v), nil, false).BodyStart(pkg). 738 ForRange("_", "val").Val(v).RangeAssignThen(token.NoPos). 739 Val(pkg.Import("fmt").Ref("Println")).Val(ctxRef(pkg, "val")).Call(1).EndStmt(). 740 End().End() 741 domTest(t, pkg, `package main 742 743 import ( 744 "fmt" 745 "github.com/goplus/gox/internal/foo" 746 ) 747 748 func bar(v foo.NodeSet) { 749 for _gop_it := v.Gop_Enum(); ; { 750 var _gop_ok bool 751 _, val, _gop_ok := _gop_it.Next() 752 if !_gop_ok { 753 break 754 } 755 fmt.Println(val) 756 } 757 } 758 `) 759 } 760 761 func TestForRangeUDT2(t *testing.T) { 762 pkg := newMainPackage() 763 foo := pkg.Import("github.com/goplus/gox/internal/foo") 764 bar := foo.Ref("Bar").Type() 765 v := pkg.NewParam(token.NoPos, "v", types.NewPointer(bar)) 766 pkg.NewFunc(nil, "bar", types.NewTuple(v), nil, false).BodyStart(pkg). 767 ForRange("_", "val").Val(v).RangeAssignThen(token.NoPos). 768 Val(pkg.Import("fmt").Ref("Println")).Val(ctxRef(pkg, "val")).Call(1).EndStmt(). 769 End().End() 770 domTest(t, pkg, `package main 771 772 import ( 773 "fmt" 774 "github.com/goplus/gox/internal/foo" 775 ) 776 777 func bar(v *foo.Bar) { 778 for _gop_it := v.Gop_Enum(); ; { 779 var _gop_ok bool 780 val, _gop_ok := _gop_it.Next() 781 if !_gop_ok { 782 break 783 } 784 fmt.Println(val) 785 } 786 } 787 `) 788 } 789 790 func TestForRangeUDT3_WithAssign(t *testing.T) { 791 pkg := newMainPackage() 792 foo := pkg.Import("github.com/goplus/gox/internal/foo") 793 bar := foo.Ref("Bar").Type() 794 v := pkg.NewParam(token.NoPos, "v", types.NewPointer(bar)) 795 pkg.NewFunc(nil, "bar", types.NewTuple(v), nil, false).BodyStart(pkg). 796 NewVar(types.Typ[types.String], "val"). 797 ForRange().VarRef(ctxRef(pkg, "val")).Val(v).RangeAssignThen(token.NoPos). 798 Val(pkg.Import("fmt").Ref("Println")).Val(ctxRef(pkg, "val")).Call(1).EndStmt(). 799 End().End() 800 domTest(t, pkg, `package main 801 802 import ( 803 "fmt" 804 "github.com/goplus/gox/internal/foo" 805 ) 806 807 func bar(v *foo.Bar) { 808 var val string 809 for _gop_it := v.Gop_Enum(); ; { 810 var _gop_ok bool 811 val, _gop_ok = _gop_it.Next() 812 if !_gop_ok { 813 break 814 } 815 fmt.Println(val) 816 } 817 } 818 `) 819 } 820 821 // bugfix: for range udt { ... } 822 func TestForRangeUDT3_NoAssign(t *testing.T) { 823 pkg := newMainPackage() 824 foo := pkg.Import("github.com/goplus/gox/internal/foo") 825 bar := foo.Ref("Bar").Type() 826 v := pkg.NewParam(token.NoPos, "v", types.NewPointer(bar)) 827 pkg.NewFunc(nil, "bar", types.NewTuple(v), nil, false).BodyStart(pkg). 828 ForRange().Val(v).RangeAssignThen(token.NoPos). 829 Val(pkg.Import("fmt").Ref("Println")).Val("Hi").Call(1).EndStmt(). 830 End().End() 831 domTest(t, pkg, `package main 832 833 import ( 834 "fmt" 835 "github.com/goplus/gox/internal/foo" 836 ) 837 838 func bar(v *foo.Bar) { 839 for _gop_it := v.Gop_Enum(); ; { 840 var _gop_ok bool 841 _, _gop_ok = _gop_it.Next() 842 if !_gop_ok { 843 break 844 } 845 fmt.Println("Hi") 846 } 847 } 848 `) 849 } 850 851 // bugfix: for _ = range udt { ... } 852 // bugfix: [" " for _ <- :10] 853 func TestForRangeUDT_UNDERLINE(t *testing.T) { 854 pkg := newMainPackage() 855 foo := pkg.Import("github.com/goplus/gox/internal/foo") 856 bar := foo.Ref("Bar").Type() 857 v := pkg.NewParam(token.NoPos, "v", types.NewPointer(bar)) 858 pkg.NewFunc(nil, "bar", types.NewTuple(v), nil, false).BodyStart(pkg). 859 ForRange().VarRef(ctxRef(pkg, "_")).Val(v).RangeAssignThen(token.NoPos). 860 Val(pkg.Import("fmt").Ref("Println")).Val("Hi").Call(1).EndStmt(). 861 End().End() 862 domTest(t, pkg, `package main 863 864 import ( 865 "fmt" 866 "github.com/goplus/gox/internal/foo" 867 ) 868 869 func bar(v *foo.Bar) { 870 for _gop_it := v.Gop_Enum(); ; { 871 var _gop_ok bool 872 _, _gop_ok = _gop_it.Next() 873 if !_gop_ok { 874 break 875 } 876 fmt.Println("Hi") 877 } 878 } 879 `) 880 } 881 882 // bugfix: for _,_ = range udt { ... } 883 func TestForRangeUDT_UNDERLINE2(t *testing.T) { 884 pkg := newMainPackage() 885 foo := pkg.Import("github.com/goplus/gox/internal/foo") 886 nodeSet := foo.Ref("NodeSet").Type() 887 v := pkg.NewParam(token.NoPos, "v", nodeSet) 888 pkg.NewFunc(nil, "bar", types.NewTuple(v), nil, false).BodyStart(pkg). 889 ForRange("_", "_").Val(v).RangeAssignThen(token.NoPos). 890 Val(pkg.Import("fmt").Ref("Println")).Val("Hi").Call(1).EndStmt(). 891 End().End() 892 domTest(t, pkg, `package main 893 894 import ( 895 "fmt" 896 "github.com/goplus/gox/internal/foo" 897 ) 898 899 func bar(v foo.NodeSet) { 900 for _gop_it := v.Gop_Enum(); ; { 901 var _gop_ok bool 902 _, _, _gop_ok = _gop_it.Next() 903 if !_gop_ok { 904 break 905 } 906 fmt.Println("Hi") 907 } 908 } 909 `) 910 } 911 912 func TestForRangeUDT4(t *testing.T) { 913 pkg := newMainPackage() 914 foo := pkg.Import("github.com/goplus/gox/internal/foo") 915 bar := foo.Ref("Foo").Type() 916 v := pkg.NewParam(token.NoPos, "v", types.NewPointer(bar)) 917 pkg.NewFunc(nil, "bar", types.NewTuple(v), nil, false).BodyStart(pkg). 918 ForRange("elem").Val(v).RangeAssignThen(token.NoPos). 919 Val(pkg.Import("fmt").Ref("Println")).Val(ctxRef(pkg, "elem")).Call(1).EndStmt(). 920 SetBodyHandler(func(body *ast.BlockStmt, kind int) { 921 gox.InsertStmtFront(body, &ast.ExprStmt{X: ast.NewIdent("__sched__")}) 922 }). 923 End().End() 924 domTest(t, pkg, `package main 925 926 import ( 927 "fmt" 928 "github.com/goplus/gox/internal/foo" 929 ) 930 931 func bar(v *foo.Foo) { 932 v.Gop_Enum(func(elem string) { 933 __sched__ 934 fmt.Println(elem) 935 }) 936 } 937 `) 938 } 939 940 func TestForRangeUDT5(t *testing.T) { 941 pkg := newMainPackage() 942 foo := pkg.Import("github.com/goplus/gox/internal/foo") 943 bar := foo.Ref("Foo2").Type() 944 v := pkg.NewParam(token.NoPos, "v", types.NewPointer(bar)) 945 pkg.NewFunc(nil, "bar", types.NewTuple(v), nil, false).BodyStart(pkg). 946 ForRange("key", "elem").Val(v).RangeAssignThen(token.NoPos). 947 Val(pkg.Import("fmt").Ref("Println")).Val(ctxRef(pkg, "key")).Val(ctxRef(pkg, "elem")). 948 Call(2).EndStmt(). 949 End().End() 950 domTest(t, pkg, `package main 951 952 import ( 953 "fmt" 954 "github.com/goplus/gox/internal/foo" 955 ) 956 957 func bar(v *foo.Foo2) { 958 v.Gop_Enum(func(key int, elem string) { 959 fmt.Println(key, elem) 960 }) 961 } 962 `) 963 } 964 965 // ---------------------------------------------------------------------------- 966 967 func TestTemplateRecvMethod(t *testing.T) { 968 pkg := newMainPackage() 969 bar := pkg.Import("github.com/goplus/gox/internal/bar") 970 pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg). 971 NewVar(bar.Ref("Game").Type(), "g"). 972 VarVal("g").MemberVal("Run").Val("Hi").Call(1).EndStmt(). 973 End() 974 domTest(t, pkg, `package main 975 976 import "github.com/goplus/gox/internal/bar" 977 978 func main() { 979 var g bar.Game 980 bar.Gopt_Game_Run(&g, "Hi") 981 } 982 `) 983 } 984 985 func TestErrTemplateRecvMethod(t *testing.T) { 986 pkg := newMainPackage() 987 bar := pkg.Import("github.com/goplus/gox/internal/bar") 988 defer func() { 989 if e := recover(); e == nil { 990 t.Fatal("TestErrTemplateRecvMethod: no error?") 991 } 992 }() 993 pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg). 994 NewVar(types.NewPointer(bar.Ref("Game").Type()), "g"). 995 VarVal("g").MemberVal("Run").Call(0).EndStmt(). 996 End() 997 } 998 999 func TestBigIntCastUntypedFloat(t *testing.T) { 1000 pkg := newGopMainPackage() 1001 mbig := pkg.Import("github.com/goplus/gox/internal/builtin") 1002 pkg.CB().NewVarStart(nil, "a"). 1003 Val(mbig.Ref("Gop_bigint")). 1004 Val(&ast.BasicLit{Kind: token.FLOAT, Value: "1e20"}).Call(1).EndInit(1) 1005 domTest(t, pkg, `package main 1006 1007 import ( 1008 "github.com/goplus/gox/internal/builtin" 1009 "math/big" 1010 ) 1011 1012 var a = builtin.Gop_bigint_Cast__1(func() *big.Int { 1013 v, _ := new(big.Int).SetString("100000000000000000000", 10) 1014 return v 1015 }()) 1016 `) 1017 } 1018 1019 func TestBigIntCastUntypedFloatError(t *testing.T) { 1020 defer func() { 1021 if e := recover(); e == nil { 1022 t.Fatal("TestBigIntCastUntypedFloatError: no error?") 1023 } 1024 }() 1025 pkg := newGopMainPackage() 1026 mbig := pkg.Import("github.com/goplus/gox/internal/builtin") 1027 pkg.CB().NewVarStart(nil, "a"). 1028 Val(mbig.Ref("Gop_bigint")). 1029 Val(&ast.BasicLit{Kind: token.FLOAT, Value: "10000000000000000000.1"}). 1030 Call(1).EndInit(1) 1031 } 1032 1033 func TestUntypedBigDefault(t *testing.T) { 1034 pkg := newGopMainPackage() 1035 pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg). 1036 Val(ctxRef(pkg, "println")). 1037 UntypedBigInt(big.NewInt(1)).Call(1).EndStmt(). 1038 Val(ctxRef(pkg, "println")). 1039 UntypedBigRat(big.NewRat(1, 2)).Call(1).EndStmt().End() 1040 domTest(t, pkg, `package main 1041 1042 import ( 1043 "fmt" 1044 "github.com/goplus/gox/internal/builtin" 1045 "math/big" 1046 ) 1047 1048 func main() { 1049 fmt.Println(builtin.Gop_bigint_Init__1(big.NewInt(1))) 1050 fmt.Println(builtin.Gop_bigrat_Init__2(big.NewRat(1, 2))) 1051 } 1052 `) 1053 } 1054 1055 func TestUntypedBigDefaultCall(t *testing.T) { 1056 pkg := newGopMainPackage() 1057 pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg). 1058 UntypedBigInt(big.NewInt(1)).MemberVal("Int64").Call(0).EndStmt(). 1059 UntypedBigRat(big.NewRat(1, 2)).MemberVal("Float64").Call(0).EndStmt().End() 1060 domTest(t, pkg, `package main 1061 1062 import ( 1063 "github.com/goplus/gox/internal/builtin" 1064 "math/big" 1065 ) 1066 1067 func main() { 1068 builtin.Gop_bigint_Init__1(big.NewInt(1)).Int64() 1069 builtin.Gop_bigrat_Init__2(big.NewRat(1, 2)).Float64() 1070 } 1071 `) 1072 } 1073 1074 func TestUntypedBigIntToInterface(t *testing.T) { 1075 pkg := newGopMainPackage() 1076 methods := []*types.Func{ 1077 types.NewFunc(token.NoPos, pkg.Types, "Int64", types.NewSignatureType(nil, nil, nil, nil, 1078 types.NewTuple(types.NewVar(token.NoPos, nil, "v", types.Typ[types.Int64])), false)), 1079 } 1080 tyInterf := types.NewInterfaceType(methods, nil).Complete() 1081 tyA := pkg.NewType("A").InitType(pkg, tyInterf) 1082 pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg). 1083 NewVarStart(tyA, "a").UntypedBigInt(big.NewInt(1)).EndInit(1). 1084 Val(ctxRef(pkg, "println")). 1085 VarVal("a").MemberVal("Int64").Call(0).Call(1).EndStmt().End() 1086 domTest(t, pkg, `package main 1087 1088 import ( 1089 "fmt" 1090 "github.com/goplus/gox/internal/builtin" 1091 "math/big" 1092 ) 1093 1094 type A interface { 1095 Int64() (v int64) 1096 } 1097 1098 func main() { 1099 var a A = builtin.Gop_bigint_Init__1(big.NewInt(1)) 1100 fmt.Println(a.Int64()) 1101 } 1102 `) 1103 } 1104 1105 func TestInt128(t *testing.T) { 1106 pkg := newGopMainPackage() 1107 builtin := pkg.Import("github.com/goplus/gox/internal/builtin") 1108 n1 := big.NewInt(1) 1109 n1.Lsh(n1, 127).Sub(n1, big.NewInt(1)) 1110 n2 := big.NewInt(-1) 1111 n2.Lsh(n2, 127) 1112 uint128 := builtin.Ref("Uint128").Type() 1113 int128 := builtin.Ref("Int128").Type() 1114 pkg.CB().NewVarStart(int128, "a").UntypedBigInt(n1).EndInit(1) 1115 pkg.CB().NewVarStart(int128, "b").Val(1).EndInit(1) 1116 pkg.CB().NewVarStart(int128, "c").Val(&ast.BasicLit{Kind: token.FLOAT, Value: "1e30"}).EndInit(1) 1117 pkg.CB().NewVarStart(int128, "d").Typ(int128).UntypedBigInt(n2).Call(1).EndInit(1) 1118 pkg.CB().NewVarStart(int128, "e").Typ(int128).Typ(uint128).Val(1).Call(1).Call(1).EndInit(1) 1119 domTest(t, pkg, `package main 1120 1121 import ( 1122 "github.com/goplus/gox/internal/builtin" 1123 "math/big" 1124 ) 1125 1126 var a builtin.Int128 = builtin.Int128_Init__1(func() *big.Int { 1127 v, _ := new(big.Int).SetString("170141183460469231731687303715884105727", 10) 1128 return v 1129 }()) 1130 var b builtin.Int128 = builtin.Int128_Init__0(1) 1131 var c builtin.Int128 = builtin.Int128_Init__1(func() *big.Int { 1132 v, _ := new(big.Int).SetString("1000000000000000000000000000000", 10) 1133 return v 1134 }()) 1135 var d builtin.Int128 = builtin.Int128_Cast__1(func() *big.Int { 1136 v, _ := new(big.Int).SetString("-170141183460469231731687303715884105728", 10) 1137 return v 1138 }()) 1139 var e builtin.Int128 = builtin.Int128(builtin.Uint128_Cast__0(1)) 1140 `) 1141 } 1142 1143 func TestUint128(t *testing.T) { 1144 pkg := newGopMainPackage() 1145 builtin := pkg.Import("github.com/goplus/gox/internal/builtin") 1146 n1 := big.NewInt(1) 1147 n1.Lsh(n1, 128).Sub(n1, big.NewInt(1)) 1148 uint128 := builtin.Ref("Uint128").Type() 1149 int128 := builtin.Ref("Int128").Type() 1150 pkg.CB().NewVarStart(uint128, "a").UntypedBigInt(n1).EndInit(1) 1151 pkg.CB().NewVarStart(uint128, "b").Val(0).EndInit(1) 1152 pkg.CB().NewVarStart(uint128, "c").Val(&ast.BasicLit{Kind: token.FLOAT, Value: "1e30"}).EndInit(1) 1153 pkg.CB().NewVarStart(uint128, "d").Typ(uint128).Typ(int128).Val(1).Call(1).Call(1).EndInit(1) 1154 domTest(t, pkg, `package main 1155 1156 import ( 1157 "github.com/goplus/gox/internal/builtin" 1158 "math/big" 1159 ) 1160 1161 var a builtin.Uint128 = builtin.Uint128_Init__1(func() *big.Int { 1162 v, _ := new(big.Int).SetString("340282366920938463463374607431768211455", 10) 1163 return v 1164 }()) 1165 var b builtin.Uint128 = builtin.Uint128_Init__0(0) 1166 var c builtin.Uint128 = builtin.Uint128_Init__1(func() *big.Int { 1167 v, _ := new(big.Int).SetString("1000000000000000000000000000000", 10) 1168 return v 1169 }()) 1170 var d builtin.Uint128 = builtin.Uint128(builtin.Int128_Cast__0(1)) 1171 `) 1172 } 1173 1174 func TestErrInt128(t *testing.T) { 1175 t.Run("Int128_Max", func(t *testing.T) { 1176 defer func() { 1177 if e := recover(); e == nil { 1178 t.Fatal("Int128_Max: no error?") 1179 } else { 1180 t.Log(e) 1181 } 1182 }() 1183 pkg := newGopMainPackage() 1184 builtin := pkg.Import("github.com/goplus/gox/internal/builtin") 1185 n := big.NewInt(1) 1186 n.Lsh(n, 127) 1187 int128 := builtin.Ref("Int128").Type() 1188 pkg.CB().NewVarStart(int128, "a").UntypedBigInt(n).EndInit(1) 1189 }) 1190 t.Run("Int128_Max_Float", func(t *testing.T) { 1191 defer func() { 1192 if e := recover(); e == nil { 1193 t.Fatal("Int128_Max_Float: no error?") 1194 } else { 1195 t.Log(e) 1196 } 1197 }() 1198 pkg := newGopMainPackage() 1199 builtin := pkg.Import("github.com/goplus/gox/internal/builtin") 1200 int128 := builtin.Ref("Int128").Type() 1201 pkg.CB().NewVarStart(int128, "a").Val(&ast.BasicLit{Kind: token.FLOAT, Value: "1e60"}).EndInit(1) 1202 }) 1203 t.Run("Int128_Min", func(t *testing.T) { 1204 defer func() { 1205 if e := recover(); e == nil { 1206 t.Fatal("Int128_Min: no error?") 1207 } else { 1208 t.Log(e) 1209 } 1210 }() 1211 pkg := newGopMainPackage() 1212 builtin := pkg.Import("github.com/goplus/gox/internal/builtin") 1213 n := big.NewInt(-1) 1214 n.Lsh(n, 127).Sub(n, big.NewInt(1)) 1215 int128 := builtin.Ref("Int128").Type() 1216 pkg.CB().NewVarStart(int128, "a").Typ(int128).UntypedBigInt(n).Call(1).EndInit(1) 1217 }) 1218 t.Run("Int128_Uint128", func(t *testing.T) { 1219 defer func() { 1220 if e := recover(); e == nil { 1221 t.Fatal("Int128_Uint128: no error?") 1222 } else { 1223 t.Log(e) 1224 } 1225 }() 1226 pkg := newGopMainPackage() 1227 builtin := pkg.Import("github.com/goplus/gox/internal/builtin") 1228 n := big.NewInt(1) 1229 n.Lsh(n, 127) 1230 int128 := builtin.Ref("Int128").Type() 1231 uint128 := builtin.Ref("Uint128").Type() 1232 pkg.CB().NewVarStart(int128, "a").Typ(int128).Typ(uint128).UntypedBigInt(n).Call(1).Call(1).EndInit(1) 1233 }) 1234 } 1235 1236 func TestErrUint128(t *testing.T) { 1237 t.Run("Uint128_Max", func(t *testing.T) { 1238 defer func() { 1239 if e := recover(); e == nil { 1240 t.Fatal("Uint128_Max: no error?") 1241 } else { 1242 t.Log(e) 1243 } 1244 }() 1245 pkg := newGopMainPackage() 1246 builtin := pkg.Import("github.com/goplus/gox/internal/builtin") 1247 n := big.NewInt(1) 1248 n.Lsh(n, 128) 1249 uint128 := builtin.Ref("Uint128").Type() 1250 pkg.CB().NewVarStart(uint128, "a").UntypedBigInt(n).EndInit(1) 1251 }) 1252 t.Run("Uint128_Max_Float", func(t *testing.T) { 1253 defer func() { 1254 if e := recover(); e == nil { 1255 t.Fatal("Uint128_Max_Float: no error?") 1256 } else { 1257 t.Log(e) 1258 } 1259 }() 1260 pkg := newGopMainPackage() 1261 builtin := pkg.Import("github.com/goplus/gox/internal/builtin") 1262 uint128 := builtin.Ref("Uint128").Type() 1263 pkg.CB().NewVarStart(uint128, "a").Val(&ast.BasicLit{Kind: token.FLOAT, Value: "1e60"}).EndInit(1) 1264 }) 1265 t.Run("Uint128_Min", func(t *testing.T) { 1266 defer func() { 1267 if e := recover(); e == nil { 1268 t.Fatal("Uint128_Min: no error?") 1269 } else { 1270 t.Log(e) 1271 } 1272 }() 1273 pkg := newGopMainPackage() 1274 builtin := pkg.Import("github.com/goplus/gox/internal/builtin") 1275 uint128 := builtin.Ref("Uint128").Type() 1276 pkg.CB().NewVarStart(uint128, "a").Typ(uint128).Val(-1).Call(1).EndInit(1) 1277 }) 1278 t.Run("Unt128_Int128", func(t *testing.T) { 1279 defer func() { 1280 if e := recover(); e == nil { 1281 t.Fatal("Unt128_Int128: no error?") 1282 } else { 1283 t.Log(e) 1284 } 1285 }() 1286 pkg := newGopMainPackage() 1287 builtin := pkg.Import("github.com/goplus/gox/internal/builtin") 1288 int128 := builtin.Ref("Int128").Type() 1289 uint128 := builtin.Ref("Uint128").Type() 1290 pkg.CB().NewVarStart(uint128, "a").Typ(uint128).Typ(int128).Val(-1).Call(1).Call(1).EndInit(1) 1291 }) 1292 } 1293 1294 // ----------------------------------------------------------------------------