Skip to content

Commit 51ae0db

Browse files
committed
added tests for BigInt module; fixed hidden bugs there;
1 parent d6dd46a commit 51ae0db

File tree

2 files changed

+152
-1
lines changed

2 files changed

+152
-1
lines changed

src/BigInt.re

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ external (+): (t, t) => t = "%addfloat";
1010
external (-): (t, t) => t = "%subfloat";
1111
external ( * ): (t, t) => t = "%mulfloat";
1212
external (/): (t, t) => t = "%divfloat";
13-
external (%): (t, t) => t = "%modfloat";
13+
let (%): (t, t) => t = Obj.magic(mod_float);
1414
let ( ** ): (t, t) => t = [%raw {|function (a, b) { return (a ** b); }|}];
1515
external (land): (t, t) => t = "%andint";
1616
external (lor): (t, t) => t = "%orint";

test/__tests__/BigInt_test.re

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
open Jest;
2+
3+
describe("BigInt", () => {
4+
Random.init(84611);
5+
6+
let fourHundred = 400;
7+
let randomInt = _ => Random.int(fourHundred);
8+
9+
test("BigInt.(+)", () => {
10+
let a = randomInt();
11+
let b = randomInt();
12+
let c = a + b;
13+
14+
Expect.(
15+
[@ocaml.warning "-44"] expect(BigInt.(fromInt(a) + fromInt(b)))
16+
|> toEqual(BigInt.fromInt(c))
17+
);
18+
});
19+
20+
test("BigInt.(-)", () => {
21+
let a = randomInt();
22+
let b = randomInt();
23+
let c = a - b;
24+
25+
Expect.(
26+
[@ocaml.warning "-44"] expect(BigInt.(fromInt(a) - fromInt(b)))
27+
|> toEqual(BigInt.fromInt(c))
28+
);
29+
});
30+
31+
test("BigInt.(*)", () => {
32+
let a = randomInt();
33+
let b = randomInt();
34+
let c = a * b;
35+
36+
Expect.(
37+
[@ocaml.warning "-44"] expect(BigInt.(fromInt(a) * fromInt(b)))
38+
|> toEqual(BigInt.fromInt(c))
39+
);
40+
});
41+
42+
test("BigInt.(/)", () => {
43+
let a = randomInt();
44+
let b = randomInt();
45+
let c = a / b;
46+
47+
Expect.(
48+
[@ocaml.warning "-44"] expect(BigInt.(fromInt(a) / fromInt(b)))
49+
|> toEqual(BigInt.fromInt(c))
50+
);
51+
});
52+
53+
test("BigInt.(~-)", () => {
54+
let a = randomInt();
55+
let b = - a;
56+
57+
Expect.(
58+
[@ocaml.warning "-44"] expect(BigInt.(- fromInt(a)))
59+
|> toEqual(BigInt.fromInt(b))
60+
);
61+
});
62+
63+
test("BigInt.(%)", () => {
64+
let a = randomInt();
65+
let b = randomInt();
66+
let c = a mod b;
67+
68+
Expect.(
69+
[@ocaml.warning "-44"] expect(BigInt.(fromInt(a) % fromInt(b)))
70+
|> toEqual(BigInt.fromInt(c))
71+
);
72+
});
73+
74+
test("BigInt.(**)", () => {
75+
let a = randomInt();
76+
let b = Random.int(24);
77+
78+
[@ocaml.warning "-3"]
79+
let c = Js.Math.pow_int(~base=a, ~exp=b);
80+
81+
Expect.(
82+
[@ocaml.warning "-44"] expect(BigInt.(fromInt(a) ** fromInt(b)))
83+
|> toEqual(BigInt.fromInt(c))
84+
);
85+
});
86+
87+
test("BigInt.(land)", () => {
88+
let a = randomInt();
89+
let b = randomInt();
90+
let c = a land b;
91+
92+
Expect.(
93+
[@ocaml.warning "-44"] expect(BigInt.(fromInt(a) land fromInt(b)))
94+
|> toEqual(BigInt.fromInt(c))
95+
);
96+
});
97+
98+
test("BigInt.(lor)", () => {
99+
let a = randomInt();
100+
let b = randomInt();
101+
let c = a lor b;
102+
103+
Expect.(
104+
[@ocaml.warning "-44"] expect(BigInt.(fromInt(a) lor fromInt(b)))
105+
|> toEqual(BigInt.fromInt(c))
106+
);
107+
});
108+
109+
test("BigInt.(lxor)", () => {
110+
let a = randomInt();
111+
let b = randomInt();
112+
let c = a lxor b;
113+
114+
Expect.(
115+
[@ocaml.warning "-44"] expect(BigInt.(fromInt(a) lxor fromInt(b)))
116+
|> toEqual(BigInt.fromInt(c))
117+
);
118+
});
119+
120+
test("BigInt.(lnot)", () => {
121+
let a = randomInt();
122+
let b = lnot(a);
123+
124+
Expect.(
125+
[@ocaml.warning "-44"] expect(BigInt.(lnot(fromInt(a))))
126+
|> toEqual(BigInt.fromInt(b))
127+
);
128+
});
129+
130+
test("BigInt.(lsl)", () => {
131+
let a = 26;
132+
let b = 7;
133+
let c = a lsl b;
134+
135+
Expect.(
136+
[@ocaml.warning "-44"] expect(BigInt.(fromInt(a) lsl fromInt(b)))
137+
|> toEqual(BigInt.fromInt(c))
138+
);
139+
});
140+
141+
test("BigInt.(asr)", () => {
142+
let a = 32;
143+
let b = 5;
144+
let c = a asr b;
145+
146+
Expect.(
147+
[@ocaml.warning "-44"] expect(BigInt.(fromInt(a) asr fromInt(b)))
148+
|> toEqual(BigInt.fromInt(c))
149+
);
150+
});
151+
});

0 commit comments

Comments
 (0)