-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.ml
117 lines (91 loc) · 4.17 KB
/
tests.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
open Odist
open Infix
open Fold
open Red
open Cluster
let even n = n mod 2 == 0
let square n = n * n
let sum = monoid 0 (+)
let sum_square_of_evens = filter even >> map square >> reduce sum
type 'a nested_list = L of 'a list | N of 'a nested_list list
let fold_nested_list red =
let rec fold acc l = match l with
| L xs -> List.fold_left red acc xs
| N xxs -> List.fold_left fold acc xxs
in fold
let nested_list xs = Seqcol (
Stream.Stream {
Stream.sfold = (fun red acc -> fold_nested_list red acc xs);
})
module S = MakeSetRed(struct
type t = int
let compare = compare
end)
let _ =
let s = Col.of_range 1 100 |> filter even |> map square |> reduce sum in
assert( s = 171700);
let s = Col.of_range 1 100 |> sum_square_of_evens in
assert( s = 171700);
let s = Col.of_list [1;2;3;4;5] |> sum_square_of_evens in
assert( s = 20);
let (es,os) = Col.of_list [1;2;3;4;5] |> reduce (Red.partition even to_list to_list) in
assert( es = [2;4]);
assert( os = [1;3;5]);
let s = nested_list (N [L [1;2;3]; L[4;5;6;7]; N [ L[]; L[8;9] ]]) |> reduce to_list in
assert (s = [1; 2; 3; 4; 5; 6; 7; 8; 9]);
let fact n = (Col.of_range 1 n |> reduce Int.product) in
assert( 120 = (fact 5));
let f_image f xs = Col.of_list (xs) |> map f |> reduce S.union_reducer |> S.elements in
assert(f_image square [-2; -1; 0; 1; 2 ] = [0; 1; 4]);
let fs = Col.of_files(".") in
let fs_list = fs |> reduce to_list in
let fs_count = fs |> reduce Int.count in
assert( fs_count = List.length fs_list);
let s = Col.of_range 1 1000000000 |> exists even in
assert (s);
let s = Col.of_range 1 1000000000 |> forall even in
assert (not s);
let s = Col.of_range 1 1000000000 |> reduce first in
assert (s = Some(1));
let s = Col.of_range 1 1000000000 |> reduce (sum |> taking 5) in
assert (s = 15);
let sumf = monoid 0.0 (+.) in
let count = sumf |> mapping (fun _ -> 1.0) in
let mean = pair_reducer sumf count |> returning (fun (total,n) -> if n = 0.0 then 0.0 else total /. n) in
let m = Col.of_list [1.2; 2.4; 3.6] |> reduce mean in
assert (m = 2.4);
let s = Col.of_range 0 9 |> map string_of_int |> reduce to_string in
assert ( s = "0123456789");
Col.of_range 1 100 |> map string_of_int |> flatmap (fun s -> Col.of_list [s;"\n"]) |> Action.stream (Action.to_file_printer "/tmp/foo");
let s = Col.of_file_lines "/tmp/foo" |> map int_of_string |> reduce sum in
assert (s = 50 * 101 );
let cores = Cluster.mcores 4 in
let s_par = Col.of_range 1 100 |> cores.distribute |> sum_square_of_evens in
let s_seq = Col.of_range 1 100 |> sum_square_of_evens in
assert (s_par = s_seq);
let split_lines = Str.split_delim (Str.regexp "\n") in
let to_lines = Text.pack_split_reducer split_lines (to_list |> filtering (fun s -> s <> "")) in
let expected = Col.of_range 1 100 |> map string_of_int |> reduce to_list in
let file_lines_1 = "/tmp/foo" |> Col.of_file_chunks 20 |> map Bytes.to_string |> reduce to_lines in
let file_lines_2 = "/tmp/foo" |> Col.of_file_chunks 21 |> map Bytes.to_string |> reduce to_lines in
assert (file_lines_1 = expected);
assert (file_lines_2 = expected);
(* FIXME: use ordering when required.
let file_lines_3 = "/tmp/foo" |> Col.of_file_chunks 21 |> cores.distribute |> reduce to_lines in
assert (file_lines_3 = expected);
*)
let chunk m i = Col.of_range (m*i+1) (m*(i+1)) in
let par_range n m = Col.of_range 0 (n-1) |> cores.distribute |> flatmap (chunk m) in
let seq_range n m = Col.of_range 1 (n*m) in
let s_par = par_range 4 25 |> sum_square_of_evens in
let s_seq = seq_range 4 25 |> sum_square_of_evens in
assert (s_par = s_seq);
let a = Array.init 5 (fun i -> i+1) in
let fact = Col.of_array a |> reduce (monoid 1 ( * )) in
assert (120 = fact);
let a = Col.of_range 0 9 |> map (fun i -> i mod 5,i) |> reduce (Red.array_reducer to_list) in
assert (a = [| [0;5]; [1;6]; [2;7]; [3;8]; [4;9] |]);
let ai = Col.of_array_i a |> reduce to_list in
assert (ai = [ 0,[0;5]; 1,[1;6]; 2,[2;7]; 3,[3;8]; 4,[4;9] ]);
let fai = Col.of_array_i a |> flatmap (fun (i,xs) -> Col.of_list xs |> map (fun x -> (i,x))) |> reduce to_list in
assert (fai = [ 0,0; 0,5; 1,1; 1,6; 2,2; 2,7; 3,3; 3,8; 4,4; 4,9 ])