summaryrefslogtreecommitdiff
path: root/testsuite/tests/match-exception/streams.ml
blob: a9a4c9e81af8fa1132708324fbcafae42dd765fe (plain)
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
(* TEST *)

(**
   Test the stream example .
*)
type stream = Stream of (int * stream Lazy.t)
;;

exception End_of_stream
;;

let make_stream_up_to n =
  let rec loop i =
    if i = n then Stream (i, lazy (raise End_of_stream))
    else Stream (i, lazy (loop (i + 1)))
  in loop 0
;;

let stream_get (Stream (x, s)) = (x, Lazy.force s)
;;

let rec iter_stream_match f s =
  match stream_get s
  with exception End_of_stream -> ()
  | (x, s') ->
    begin
      f x;
      iter_stream_match f s'
    end
;;

let test_iter_stream =
  let limit = 10000000 in
  try
    iter_stream_match ignore (make_stream_up_to limit);
    print_endline "iter_stream with handler case (match) is tail recursive"
  with Stack_overflow ->
    assert false
;;