-
Notifications
You must be signed in to change notification settings - Fork 465
/
Copy pathclass5_test.ml
97 lines (75 loc) · 2.04 KB
/
class5_test.ml
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
let suites : Mt.pair_suites ref = ref []
let test_id = ref 0
let eq loc x y =
incr test_id ;
suites :=
(loc ^" id " ^ (string_of_int !test_id), (fun _ -> Mt.Eq(x,y))) :: !suites
class printable_point x_init =
object (s)
val mutable x = x_init
method get_x = x
method move d = x <- x + d
method print = (string_of_int s#get_x)
end;;
class printable_colored_point y c =
object (self)
val c = c
method color = c
inherit printable_point y as super
method print =
"(" ^
super#print ^
", " ^
(self#color) ^
")"
end;;
let p = new printable_colored_point 17 "red";;
let () = eq __LOC__ p#print "(17, red)"
class ['a] ref x_init =
object
val mutable x = (x_init : 'a)
method get = x
method set y = x <- y
end;;
let v =
let r = new ref 1 in r#set 2; (r#get);;
let () = eq __LOC__ v 2
class ['a] intlist (l : int list) =
object
method empty = (l = [])
method fold f (accu : 'a) = List.fold_left f accu l
end;;
let l = new intlist [1; 2; 3];;
let () =
eq __LOC__ 6 (l#fold (fun x y -> x+y) 0);;
class intlist2 (l : int list) =
object
method empty = (l = [])
method fold : 'a. ('a -> int -> 'a) -> 'a -> 'a =
fun f accu -> List.fold_left f accu l
end;;
let l = new intlist2 [1; 2; 3];;
let () =
eq __LOC__ (6,"1 2 3 ")
(l#fold (fun x y -> x+y) 0,
l#fold (fun s x -> s ^ string_of_int x ^ " ") "")
class type point0 = object method get_x : int end;;
class point x_init =
object
val mutable x = x_init
method get_x = x
method move d = x <- x + d
end;;
class distance_point x =
object
inherit point x
method distance : 'a. (#point0 as 'a) -> int =
fun other -> abs (other#get_x - x)
end;;
let a, b =
let p = new distance_point 3 in
(p#distance (new point 8),
p#distance (new printable_colored_point 1 "blue"));;
let () =
eq __LOC__ (5,2) (a,b)
let () = Mt.from_pair_suites __FILE__ !suites