Skip to content

Commit c59223d

Browse files
committed
add some tests for random (#262)
1 parent 6e72fa4 commit c59223d

File tree

8 files changed

+229
-31
lines changed

8 files changed

+229
-31
lines changed

jscomp/test/.depend

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,10 @@ qcc.cmx : ../stdlib/sys.cmx ../stdlib/string.cmx ../stdlib/printf.cmx \
300300
../stdlib/array.cmx
301301
queue_test.cmo : ../stdlib/queue.cmi mt.cmi ../stdlib/array.cmi
302302
queue_test.cmx : ../stdlib/queue.cmx mt.cmx ../stdlib/array.cmx
303-
random_test.cmo : ../stdlib/random.cmi
304-
random_test.cmx : ../stdlib/random.cmx
303+
random_test.cmo : ../stdlib/random.cmi ../stdlib/printf.cmi mt_global.cmi \
304+
mt.cmi ../stdlib/int64.cmi ../stdlib/array.cmi
305+
random_test.cmx : ../stdlib/random.cmx ../stdlib/printf.cmx mt_global.cmx \
306+
mt.cmx ../stdlib/int64.cmx ../stdlib/array.cmx
305307
rec_module_test.cmo : ../stdlib/set.cmi ../stdlib/pervasives.cmi mt.cmi
306308
rec_module_test.cmx : ../stdlib/set.cmx ../stdlib/pervasives.cmx mt.cmx
307309
rec_value_test.cmo : mt.cmi ../stdlib/list.cmi ../stdlib/lazy.cmi
@@ -836,8 +838,10 @@ qcc.cmj : ../stdlib/sys.cmj ../stdlib/string.cmj ../stdlib/printf.cmj \
836838
../stdlib/array.cmj
837839
queue_test.cmo : ../stdlib/queue.cmi mt.cmi ../stdlib/array.cmi
838840
queue_test.cmj : ../stdlib/queue.cmj mt.cmj ../stdlib/array.cmj
839-
random_test.cmo : ../stdlib/random.cmi
840-
random_test.cmj : ../stdlib/random.cmj
841+
random_test.cmo : ../stdlib/random.cmi ../stdlib/printf.cmi mt_global.cmi \
842+
mt.cmi ../stdlib/int64.cmi ../stdlib/array.cmi
843+
random_test.cmj : ../stdlib/random.cmj ../stdlib/printf.cmj mt_global.cmj \
844+
mt.cmj ../stdlib/int64.cmj ../stdlib/array.cmj
841845
rec_module_test.cmo : ../stdlib/set.cmi ../stdlib/pervasives.cmi mt.cmi
842846
rec_module_test.cmj : ../stdlib/set.cmj ../stdlib/pervasives.cmj mt.cmj
843847
rec_value_test.cmo : mt.cmi ../stdlib/list.cmi ../stdlib/lazy.cmi

jscomp/test/list_test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ var list_suites_001 = /* :: */[
5959
"long_length",
6060
function () {
6161
return /* Eq */{
62-
0: 100000,
63-
1: List.length($$Array.to_list($$Array.init(100000, function () {
62+
0: 30000,
63+
1: List.length($$Array.to_list($$Array.init(30000, function () {
6464
return 0;
6565
}))),
6666
length: 2,

jscomp/test/list_test.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let list_suites = Mt.[
88
Eq (5 , List.length [0;1;2;3;4]) (* This is tuple haha*)
99
) ;
1010
"long_length", (fun _ ->
11-
let v = 100000 in
11+
let v = 30_000 in
1212
Eq (v ,
1313
(List.length
1414
(Array.to_list (Array.init v (fun _ -> 0))))

jscomp/test/mt_global.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,25 @@ function collect_eq(test_id, suites, loc, x, y) {
2121
return /* () */0;
2222
}
2323

24+
function collect_neq(test_id, suites, loc, x, y) {
25+
test_id[0] = test_id[0] + 1 | 0;
26+
suites[0] = /* :: */[
27+
/* tuple */[
28+
loc + (" id " + test_id[0]),
29+
function () {
30+
return /* Neq */{
31+
0: x,
32+
1: y,
33+
length: 2,
34+
tag: 1
35+
};
36+
}
37+
],
38+
suites[0]
39+
];
40+
return /* () */0;
41+
}
42+
2443
function collect_approx(test_id, suites, loc, x, y) {
2544
test_id[0] = test_id[0] + 1 | 0;
2645
suites[0] = /* :: */[
@@ -41,5 +60,6 @@ function collect_approx(test_id, suites, loc, x, y) {
4160
}
4261

4362
exports.collect_eq = collect_eq;
63+
exports.collect_neq = collect_neq;
4464
exports.collect_approx = collect_approx;
4565
/* No side effect */

jscomp/test/mt_global.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ let collect_eq test_id suites loc x y =
44
incr test_id ;
55
suites :=
66
(loc ^" id " ^ (string_of_int !test_id), (fun _ -> Mt.Eq(x,y))) :: !suites
7+
let collect_neq test_id suites loc x y =
8+
incr test_id ;
9+
suites :=
10+
(loc ^" id " ^ (string_of_int !test_id), (fun _ -> Mt.Neq(x,y))) :: !suites
711

812
let collect_approx test_id suites loc x y =
913
incr test_id ;

jscomp/test/mt_global.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ val collect_eq :
33
int ref -> Mt.pair_suites ref
44
-> string -> 'b -> 'b -> unit
55

6+
val collect_neq :
7+
int ref -> Mt.pair_suites ref
8+
-> string -> 'b -> 'b -> unit
9+
610
val collect_approx :
711
int ref -> Mt.pair_suites ref
812
-> string -> float -> float -> unit

jscomp/test/random_test.js

Lines changed: 153 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,158 @@
11
// Generated CODE, PLEASE EDIT WITH CARE
22
'use strict';
33

4-
var Random = require("../stdlib/random");
5-
6-
function f() {
7-
Random.self_init(/* () */0);
8-
var x = Random.$$int(10000);
9-
Random.self_init(/* () */0);
10-
var y = Random.$$int(1000);
11-
if (x === y) {
12-
console.log("FAILED");
13-
return /* () */0;
14-
}
15-
else {
16-
console.log("PASSED");
17-
return /* () */0;
18-
}
4+
var Mt = require("./mt");
5+
var Mt_global = require("./mt_global");
6+
var Printf = require("../stdlib/printf");
7+
var Int64 = require("../stdlib/int64");
8+
var Caml_array = require("../runtime/caml_array");
9+
var Caml_curry = require("../runtime/caml_curry");
10+
var Random = require("../stdlib/random");
11+
12+
var id = [0];
13+
14+
var suites = [/* [] */0];
15+
16+
function eq(f) {
17+
return function (param, param$1) {
18+
return Mt_global.collect_eq(id, suites, f, param, param$1);
19+
};
20+
}
21+
22+
function neq(f) {
23+
return function (param, param$1) {
24+
return Mt_global.collect_neq(id, suites, f, param, param$1);
25+
};
26+
}
27+
28+
function approx(f) {
29+
return function (param, param$1) {
30+
return Mt_global.collect_approx(id, suites, f, param, param$1);
31+
};
1932
}
2033

21-
exports.f = f;
22-
/* No side effect */
34+
Random.self_init(/* () */0);
35+
36+
var param = Random.$$int(1000);
37+
38+
Random.self_init(/* () */0);
39+
40+
var param$1 = Random.$$int(10000);
41+
42+
Mt_global.collect_neq(id, suites, 'File "random_test.ml", line 12, characters 6-13', param$1, param);
43+
44+
Random.init(0);
45+
46+
var v = Caml_array.caml_make_vect(10, /* false */0);
47+
48+
for(var i = 0; i<= 9; ++i){
49+
v[i] = Random.bool(/* () */0);
50+
}
51+
52+
var param$2 = /* array */[
53+
/* true */1,
54+
/* true */1,
55+
/* true */1,
56+
/* true */1,
57+
/* true */1,
58+
/* false */0,
59+
/* true */1,
60+
/* true */1,
61+
/* true */1,
62+
/* false */0
63+
];
64+
65+
Mt_global.collect_eq(id, suites, 'File "random_test.ml", line 26, characters 5-12', v, param$2);
66+
67+
var f = Random.int64(Int64.max_int);
68+
69+
var h = Random.int64(/* int64 */[
70+
0,
71+
3
72+
]);
73+
74+
var vv = Random.bits(/* () */0);
75+
76+
var xx = Random.$$float(3.0);
77+
78+
var xxx = Random.int32(103);
79+
80+
Caml_curry.app5(Printf.printf(/* Format */[
81+
/* Int64 */{
82+
0: /* Int_d */0,
83+
1: /* No_padding */0,
84+
2: /* No_precision */0,
85+
3: /* Char_literal */{
86+
0: /* " " */32,
87+
1: /* Int64 */{
88+
0: /* Int_d */0,
89+
1: /* No_padding */0,
90+
2: /* No_precision */0,
91+
3: /* Char_literal */{
92+
0: /* " " */32,
93+
1: /* Int */{
94+
0: /* Int_d */0,
95+
1: /* No_padding */0,
96+
2: /* No_precision */0,
97+
3: /* Char_literal */{
98+
0: /* " " */32,
99+
1: /* Float */{
100+
0: /* Float_f */0,
101+
1: /* No_padding */0,
102+
2: /* No_precision */0,
103+
3: /* Char_literal */{
104+
0: /* " " */32,
105+
1: /* Int32 */{
106+
0: /* Int_d */0,
107+
1: /* No_padding */0,
108+
2: /* No_precision */0,
109+
3: /* String_literal */{
110+
0: " \n",
111+
1: /* End_of_format */0,
112+
length: 2,
113+
tag: 11
114+
},
115+
length: 4,
116+
tag: 5
117+
},
118+
length: 2,
119+
tag: 12
120+
},
121+
length: 4,
122+
tag: 8
123+
},
124+
length: 2,
125+
tag: 12
126+
},
127+
length: 4,
128+
tag: 4
129+
},
130+
length: 2,
131+
tag: 12
132+
},
133+
length: 4,
134+
tag: 7
135+
},
136+
length: 2,
137+
tag: 12
138+
},
139+
length: 4,
140+
tag: 7
141+
},
142+
"%Ld %Ld %d %f %ld \n"
143+
]), f, h, vv, xx, xxx);
144+
145+
Mt.from_pair_suites("random_test.ml", suites[0]);
146+
147+
exports.id = id;
148+
exports.suites = suites;
149+
exports.eq = eq;
150+
exports.neq = neq;
151+
exports.approx = approx;
152+
exports.v = v;
153+
exports.f = f;
154+
exports.h = h;
155+
exports.vv = vv;
156+
exports.xx = xx;
157+
exports.xxx = xxx;
158+
/* Not a pure module */

jscomp/test/random_test.ml

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,37 @@
1-
(** TODO: need [caml_md5_string] support *)
2-
let f () =
3-
Random.self_init ();
4-
let x = Random.int 10000 in
5-
Random.self_init ();
6-
let y = Random.int 1000 in
7-
if x = y then print_endline "FAILED" else print_endline "PASSED"
1+
2+
let id = ref 0
3+
let suites = ref []
4+
5+
let eq f = Mt_global.collect_eq id suites f
6+
let neq f = Mt_global.collect_neq id suites f
7+
let approx f = Mt_global.collect_approx id suites f
8+
9+
10+
let ()
11+
=
12+
neq __LOC__
13+
(Random.self_init (); Random.int 10000)
14+
(Random.self_init (); Random.int 1000 )
15+
16+
(* determinism acutally *)
17+
let v = Random.init 0
18+
19+
let v = Array.make 10 false
20+
21+
let () =
22+
for i = 0 to 9 do
23+
v.(i) <- Random.bool ()
24+
done
25+
let () =
26+
eq __LOC__ v [| true; true; true; true; true; false; true; true; true; false |]
27+
28+
let f = Random.int64 Int64.max_int
29+
let h = Random.int64 3L
30+
let vv = Random.bits ()
31+
let xx = Random.float 3.0
32+
let xxx = Random.int32 103l
33+
let () =
34+
Printf.printf "%Ld %Ld %d %f %ld \n" f h vv xx xxx
35+
36+
let () =
37+
Mt.from_pair_suites __FILE__ !suites

0 commit comments

Comments
 (0)