Skip to content

Commit ff138f2

Browse files
committed
more tests
1 parent 87e6fc0 commit ff138f2

File tree

3 files changed

+130
-1
lines changed

3 files changed

+130
-1
lines changed

jscomp/test/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ OTHERS := literals a test_ari test_export2 test_internalOO test_obj_simple_ffi t
7272
gpr_904_test gpr_858_unit2_test inner_unused \
7373
set_gen bal_tree string_set string_set_test \
7474
math_test bal_set_mini gpr_974_test test_cpp\
75-
global_module_alias_test
75+
global_module_alias_test class_fib_open_recursion_test
7676

7777

7878

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
'use strict';
2+
3+
var Caml_builtin_exceptions = require("../../lib/js/caml_builtin_exceptions");
4+
var Hashtbl = require("../../lib/js/hashtbl");
5+
var Mt = require("./mt");
6+
var Block = require("../../lib/js/block");
7+
var Curry = require("../../lib/js/curry");
8+
var CamlinternalOO = require("../../lib/js/camlinternalOO");
9+
10+
var shared = ["calc"];
11+
12+
var suites = [/* [] */0];
13+
14+
var test_id = [0];
15+
16+
function eq(loc, x, y) {
17+
test_id[0] = test_id[0] + 1 | 0;
18+
suites[0] = /* :: */[
19+
/* tuple */[
20+
loc + (" id " + test_id[0]),
21+
function () {
22+
return /* Eq */Block.__(0, [
23+
x,
24+
y
25+
]);
26+
}
27+
],
28+
suites[0]
29+
];
30+
return /* () */0;
31+
}
32+
33+
function fib_init($$class) {
34+
var calc = CamlinternalOO.get_method_label($$class, "calc");
35+
CamlinternalOO.set_method($$class, calc, function (self$neg1, x) {
36+
if (x === 0 || x === 1) {
37+
return 1;
38+
}
39+
else {
40+
return Curry._2(self$neg1[0][calc], self$neg1, x - 1 | 0) + Curry._2(self$neg1[0][calc], self$neg1, x - 2 | 0) | 0;
41+
}
42+
});
43+
return function (_, self) {
44+
return CamlinternalOO.create_object_opt(self, $$class);
45+
};
46+
}
47+
48+
var fib = CamlinternalOO.make_class(shared, fib_init);
49+
50+
function memo_fib_init($$class) {
51+
var ids = CamlinternalOO.new_methods_variables($$class, shared, ["cache"]);
52+
var calc = ids[0];
53+
var cache = ids[1];
54+
var inh = CamlinternalOO.inherits($$class, 0, 0, shared, fib, 1);
55+
var obj_init = inh[0];
56+
var calc$1 = inh[1];
57+
CamlinternalOO.set_method($$class, calc, function (self$neg2, x) {
58+
try {
59+
return Hashtbl.find(self$neg2[cache], x);
60+
}
61+
catch (exn){
62+
if (exn === Caml_builtin_exceptions.not_found) {
63+
var v = Curry._2(calc$1, self$neg2, x);
64+
Hashtbl.add(self$neg2[cache], x, v);
65+
return v;
66+
}
67+
else {
68+
throw exn;
69+
}
70+
}
71+
});
72+
return function (_, self) {
73+
var self$1 = CamlinternalOO.create_object_opt(self, $$class);
74+
self$1[cache] = Hashtbl.create(/* None */0, 31);
75+
Curry._1(obj_init, self$1);
76+
return CamlinternalOO.run_initializers_opt(self, self$1, $$class);
77+
};
78+
}
79+
80+
var memo_fib = CamlinternalOO.make_class(shared, memo_fib_init);
81+
82+
var tmp = Curry._1(memo_fib[0], 0);
83+
84+
eq('File "class_fib_open_recursion_test.ml", line 33, characters 5-12', Curry.js2(-1044768619, 1, tmp, 40), 165580141);
85+
86+
Mt.from_pair_suites("class_fib_open_recursion_test.ml", suites[0]);
87+
88+
exports.suites = suites;
89+
exports.test_id = test_id;
90+
exports.eq = eq;
91+
exports.fib = fib;
92+
exports.memo_fib = memo_fib;
93+
/* fib Not a pure module */
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
let suites : Mt.pair_suites ref = ref []
2+
let test_id = ref 0
3+
let eq loc x y =
4+
incr test_id ;
5+
suites :=
6+
(loc ^" id " ^ (string_of_int !test_id), (fun _ -> Mt.Eq(x,y))) :: !suites
7+
8+
9+
class fib = object (self)
10+
method calc x =
11+
if x =0 || x = 1 then 1
12+
else self#calc (x - 1 ) + self#calc (x - 2)
13+
end
14+
15+
16+
class memo_fib = object (self)
17+
val cache = Hashtbl.create 31
18+
inherit fib as super
19+
method calc x =
20+
match Hashtbl.find cache x with
21+
| exception Not_found ->
22+
let v = (super#calc x) in
23+
Hashtbl.add cache x v ; v
24+
| v -> v
25+
end
26+
27+
28+
29+
30+
let () =
31+
(* print_endline (string_of_int ((new fib)#calc 50)) *)
32+
33+
eq __LOC__ (((new memo_fib)#calc 40)) 165580141
34+
35+
36+
let () = Mt.from_pair_suites __FILE__ !suites

0 commit comments

Comments
 (0)