Commits

Anonymous committed f165eb1

List: + rev_iter

Comments (0)

Files changed (2)

     ;
 
 
+
+    value rec rev_iter_loop depth f lst =
+      if depth > 500
+      then
+        List.iter f (List.rev lst)
+      else
+        match lst with
+        [ [] -> ()
+        | [h :: t] ->
+            ( rev_iter_loop (depth + 1) f t
+            ; f h
+            )
+        ]
+    ;
+
+    value rev_iter f lst = rev_iter_loop 0 f lst
+    ;
+
     (* ** в Cd_List реализовал похожее, потом надо будет слить.
 
     value assoc_count ?(cmp=Pervasives.compare) k t =
 ;
 
 
+value rec generate_list len get_elt =
+  if len = 0
+  then []
+  else
+    let h = get_elt () in
+    [h :: generate_list (len - 1) get_elt]
+;
+
+value test_list_rev_iter () =
+  let get_res lst rev_iter =
+    let b = Buffer.create 100 in
+    let () =
+      rev_iter
+        (fun i ->
+           ( Buffer.add_char b ' '
+           ; Buffer.add_string b (string_of_int i)
+           )
+        )
+        lst
+    in
+      Buffer.contents b
+  in
+    List.iter
+      (fun len ->
+         let lst = generate_list len (fun () -> Random.int 100000) in
+         assert_equal
+           (get_res lst (fun f lst -> List.iter f (List.rev lst)))
+           (get_res lst List.rev_iter)
+      )
+      [0; 100; 500; 1000]
+;
+
+value misc =
+  [ "List.rev_iter" >:: test_list_rev_iter
+  ]
+;
+
 (****************************************************************)
 
 value suite =
 
     @ cdt_tests
 
+    @ misc
+
     @ trie
     @ strings
     @ maps_merge