builtin_bigint_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package goja
  2. import (
  3. "math/big"
  4. "testing"
  5. )
  6. func TestBigInt(t *testing.T) {
  7. const SCRIPT = `0xabcdef0123456789abcdef0123n`
  8. b := new(big.Int)
  9. b.SetString("0xabcdef0123456789abcdef0123", 0)
  10. testScript(SCRIPT, (*valueBigInt)(b), t)
  11. }
  12. func TestBigIntExportTo(t *testing.T) {
  13. vm := New()
  14. t.Run("bigint exportType", func(t *testing.T) {
  15. v, err := vm.RunString(`BigInt(Number.MAX_SAFE_INTEGER + 10);`)
  16. if err != nil {
  17. t.Fatal(err)
  18. }
  19. if typ := v.ExportType(); typ != typeBigInt {
  20. t.Fatal(typ)
  21. }
  22. })
  23. t.Run("bigint", func(t *testing.T) {
  24. var b big.Int
  25. err := vm.ExportTo(vm.ToValue(big.NewInt(10)), &b)
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. if b.Cmp(big.NewInt(10)) != 0 {
  30. t.Fatalf("bigint: %s", b.String())
  31. }
  32. })
  33. }
  34. func TestBigIntFormat(t *testing.T) {
  35. const SCRIPT = `
  36. assert.sameValue((1n).toString(undefined), "1", "radius undefined");
  37. assert.throws(RangeError, () => { (1n).toString(-1); }, "radius -1");
  38. assert.throws(RangeError, () => { (1n).toString(37); }, "radius 37");
  39. assert.sameValue((1n).toString(2), "1", "radius 2");
  40. assert.sameValue((10n).toString(3), "101", "radius 3");
  41. `
  42. testScriptWithTestLib(SCRIPT, _undefined, t)
  43. }
  44. func TestBigIntOperator(t *testing.T) {
  45. const SCRIPT = `
  46. assert.throws(TypeError, () => { 1 - 1n; }, "mix type add");
  47. assert.throws(TypeError, () => { 1n - 1; }, "mix type add");
  48. assert.throws(TypeError, () => { 1n + 1; }, "mix type sub");
  49. assert.throws(TypeError, () => { 1 + 1n; }, "mix type sub");
  50. assert.throws(TypeError, () => { 1 * 1n; }, "mix type mul");
  51. assert.throws(TypeError, () => { 1n * 1; }, "mix type mul");
  52. assert.throws(TypeError, () => { 1 / 1n; }, "mix type div");
  53. assert.throws(TypeError, () => { 1n / 1; }, "mix type div");
  54. assert.throws(TypeError, () => { 1 % 1n; }, "mix type mod");
  55. assert.throws(TypeError, () => { 1n % 1; }, "mix type mod");
  56. assert.throws(TypeError, () => { 1n ** 1; }, "mix type exp");
  57. assert.throws(TypeError, () => { 1 ** 1n; }, "mix type exp");
  58. assert.throws(TypeError, () => { 1 & 1n; }, "mix type and");
  59. assert.throws(TypeError, () => { 1n & 1; }, "mix type and");
  60. assert.throws(TypeError, () => { 1 | 1n; }, "mix type or");
  61. assert.throws(TypeError, () => { 1n | 1; }, "mix type or");
  62. assert.throws(TypeError, () => { 1 ^ 1n; }, "mix type xor");
  63. assert.throws(TypeError, () => { 1n ^ 1; }, "mix type xor");
  64. assert.throws(TypeError, () => { 1 << 1n; }, "mix type lsh");
  65. assert.throws(TypeError, () => { 1n << 1; }, "mix type lsh");
  66. assert.throws(TypeError, () => { 1 >> 1n; }, "mix type rsh");
  67. assert.throws(TypeError, () => { 1n >> 1; }, "mix type rsh");
  68. assert.throws(TypeError, () => { 1 >>> 1n; }, "mix type ursh");
  69. assert.throws(TypeError, () => { 1n >>> 1; }, "mix type ursh");
  70. assert.sameValue(1n + 1n, 2n, "add");
  71. assert.sameValue(1n - 1n, 0n, "sub");
  72. assert.sameValue(1n * 2n, 2n, "mul");
  73. assert.sameValue(1n / 2n, 0n, "div");
  74. assert.sameValue(1n % 2n, 1n, "mod");
  75. assert.sameValue(1n ** 2n, 1n, "exp");
  76. assert.sameValue(1n & 1n, 1n, "and");
  77. assert.sameValue(1n | 1n, 1n, "or");
  78. assert.sameValue(2n ^ 1n, 3n, "xor");
  79. assert.sameValue(1n << 1n, 2n, "lsh");
  80. assert.sameValue(4n << -1n, 2n, "neg lsh");
  81. assert.sameValue(4n >> 1n, 2n, "rsh");
  82. assert.sameValue(2n >> -2n, 8n, "neg rsh");
  83. let a = 1n;
  84. assert.sameValue(++a, 2n, "inc");
  85. assert.sameValue(--a, 1n, "dec");
  86. assert.sameValue(Object(1n) - 1n, 0n, "primitive sub");
  87. assert.sameValue(Object(Object(1n)) - 1n, 0n, "primitive sub");
  88. assert.sameValue({ [Symbol.toPrimitive]: () => 1n } - 1n, 0n, "primitive sub");
  89. assert.sameValue({ valueOf: () => 1n } - 1n, 0n, "valueOf sub");
  90. assert.sameValue(1n > 0, true, "gt");
  91. assert.sameValue(0 > 1n, false, "gt");
  92. assert.sameValue(Object(1n) > 0, true, "gt");
  93. assert.sameValue(0 > Object(1n), false, "gt");
  94. assert.sameValue(1n < 0, false, "lt");
  95. assert.sameValue(0 < 1n, true, "lt");
  96. assert.sameValue(Object(1n) < 0, false, "lt");
  97. assert.sameValue(0 < Object(1n), true, "lt");
  98. assert.sameValue(1n >= 0, true, "ge");
  99. assert.sameValue(0 >= 1n, false, "ge");
  100. assert.sameValue(1n <= 0, false, "le");
  101. assert.sameValue(0 <= 1n, true, "le");
  102. `
  103. testScriptWithTestLib(SCRIPT, _undefined, t)
  104. }