summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Tests/builtins/BigInt/bigint-basic.js
blob: c32681792fad8dd310541b388e4840626db24954 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
describe("correct behavior", () => {
  test("typeof bigint", () => {
    expect(typeof 1n).toBe("bigint");
  });

  test("bigint string coersion", () => {
    expect("" + 123n).toBe("123");
  });

  test("arithmetic operators", () => {
    let bigint = 123n;
    expect(-bigint).toBe(-123n);

    expect(12n + 34n).toBe(46n);
    expect(12n - 34n).toBe(-22n);
    expect(8n * 12n).toBe(96n);
    expect(123n / 10n).toBe(12n);
    expect(2n ** 3n).toBe(8n);
    expect(5n % 3n).toBe(2n);
    expect(
      45977665298704210987n +
        (714320987142450987412098743217984576n / 4598741987421098765327980n) * 987498743n
    ).toBe(199365500239020623962n);
  });

  test("bitwise operators", () => {
    expect(12n & 5n).toBe(4n);
    expect(1n | 2n).toBe(3n);
    expect(5n ^ 3n).toBe(6n);
    expect(~1n).toBe(-2n);
  });

  test("increment operators", () => {
    let bigint = 1n;
    expect(bigint++).toBe(1n);
    expect(bigint).toBe(2n);
    expect(bigint--).toBe(2n);
    expect(bigint).toBe(1n);
    expect(++bigint).toBe(2n);
    expect(bigint).toBe(2n);
    expect(--bigint).toBe(1n);
    expect(bigint).toBe(1n);
  });

  test("weak equality operators", () => {
    expect(1n == 1n).toBeTrue();
    expect(1n == 1).toBeTrue();
    expect(1 == 1n).toBeTrue();
    expect(1n == 1.23).toBeFalse();
    expect(1.23 == 1n).toBeFalse();

    expect(1n != 1n).toBeFalse();
    expect(1n != 1).toBeFalse();
    expect(1 != 1n).toBeFalse();
    expect(1n != 1.23).toBeTrue();
    expect(1.23 != 1n).toBeTrue();
  });

  test("strong equality operators", () => {
    expect(1n === 1n).toBeTrue();
    expect(1n === 1).toBeFalse();
    expect(1 === 1n).toBeFalse();
    expect(1n === 1.23).toBeFalse();
    expect(1.23 === 1n).toBeFalse();

    expect(1n !== 1n).toBeFalse();
    expect(1n !== 1).toBeTrue();
    expect(1 !== 1n).toBeTrue();
    expect(1n !== 1.23).toBeTrue();
    expect(1.23 !== 1n).toBeTrue();
  });
});

describe("errors", () => {
  test("conversion to number", () => {
    expect(() => {
      +123n;
    }).toThrowWithMessage(TypeError, "Cannot convert BigInt to number");
  });
});