From 0bee9bdd559ae0876149ae4bf00936067b4a974f Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Thu, 30 Jun 2022 22:28:07 -0400 Subject: [PATCH] wip: remove tests from src/ --- src/core/CCArray.ml | 232 ----------- src/core/CCBool.ml | 13 - src/core/CCByte_buffer.ml | 125 ------ src/core/CCCanonical_sexp.ml | 55 --- src/core/CCChar.ml | 9 - src/core/CCEither.ml | 20 - src/core/CCEqual.ml | 5 - src/core/CCFloat.ml | 20 - src/core/CCFormat.ml | 95 ----- src/core/CCFun.ml | 19 - src/core/CCHash.ml | 15 - src/core/CCList.ml | 778 ----------------------------------- 12 files changed, 1386 deletions(-) diff --git a/src/core/CCArray.ml b/src/core/CCArray.ml index ddd13c4c..50911751 100644 --- a/src/core/CCArray.ml +++ b/src/core/CCArray.ml @@ -9,11 +9,6 @@ type 'a ord = 'a -> 'a -> int type 'a random_gen = Random.State.t -> 'a type 'a printer = Format.formatter -> 'a -> unit -(*$T - let st = Random.State.make [||] in let a = 0--10000 in \ - let b = Array.copy a in shuffle_with st a; a <> b -*) - (** {2 Arrays} *) open CCShims_ @@ -42,26 +37,9 @@ let get_safe a i = then Some (Array.unsafe_get a i) else None -(*$= - (Some 1) (get_safe [|1;2;3|] 0) - (Some 2) (get_safe [|1;2;3|] 1) - (Some 3) (get_safe [|1;2;3|] 2) - None (get_safe [|1;2;3|] 4) - None (get_safe [|1;2;3|] max_int) - None (get_safe [|1;2;3|] ~-1) - None (get_safe [|1;2;3|] ~-42) -*) - let map_inplace f a = Array.iteri (fun i e -> Array.unsafe_set a i (f e)) a -(*$Q - Q.(array int) (fun a -> \ - let b = map ((+) 1) a in \ - map_inplace ((+) 1) a; \ - b = a) -*) - let fold = Array.fold_left let foldi f acc a = @@ -80,10 +58,6 @@ let fold_while f acc a = else acc in fold_while_i f acc 0 -(*$T - fold_while (fun acc b -> if b then acc+1, `Continue else acc, `Stop) 0 (Array.of_list [true;true;false;true]) = 2 -*) - let fold_map f acc a = let n = length a in (* need special case for initializing the result *) @@ -100,16 +74,6 @@ let fold_map f acc a = !acc, res ) -(*$= - (6, [|"1"; "2"; "3"|]) \ - (fold_map (fun acc x->acc+x, string_of_int x) 0 [|1;2;3|]) -*) - -(*$Q - Q.(array int) (fun a -> \ - fold_map (fun acc x -> x::acc, x) [] a = (List.rev @@ Array.to_list a, a)) -*) - let scan_left f acc a = let n = length a in let res = Array.make (n+1) acc in @@ -120,12 +84,6 @@ let scan_left f acc a = a; res -(*$= & ~printer:Q.Print.(array int) - [|0;1;3;6|] (scan_left (+) 0 [|1;2;3|]) - [|0|] (scan_left (+) 0 [||]) -*) - - let reverse_in_place a = let len = Array.length a in if len>0 then ( @@ -136,80 +94,25 @@ let reverse_in_place a = done ) -(*$T - reverse_in_place [| |]; true - reverse_in_place [| 1 |]; true - let a = [| 1; 2; 3; 4; 5 |] in \ - reverse_in_place a; \ - a = [| 5;4;3;2;1 |] - let a = [| 1; 2; 3; 4; 5; 6 |] in \ - reverse_in_place a; \ - a = [| 6;5;4;3;2;1 |] -*) - let sorted cmp a = let b = Array.copy a in Array.sort cmp b; b -(*$= & ~cmp:(=) ~printer:Q.Print.(array int) - [||] (sorted Stdlib.compare [||]) - [|0;1;2;3;4|] (sorted Stdlib.compare [|3;2;1;4;0|]) -*) - -(*$Q - Q.(array int) (fun a -> \ - let b = Array.copy a in \ - Array.sort Stdlib.compare b; b = sorted Stdlib.compare a) -*) - let sort_indices cmp a = let len = Array.length a in let b = Array.init len (fun k->k) in Array.sort (fun k1 k2 -> cmp a.(k1) a.(k2)) b; b -(*$= & ~cmp:(=) ~printer:Q.Print.(array int) - [||] (sort_indices Stdlib.compare [||]) - [|4;2;1;0;3|] (sort_indices Stdlib.compare [|"d";"c";"b";"e";"a"|]) -*) - -(*$Q - Q.(array_of_size Gen.(0 -- 30) printable_string) (fun a -> \ - let b = sort_indices String.compare a in \ - sorted String.compare a = Array.map (Array.get a) b) -*) - let sort_ranking cmp a = sort_indices compare (sort_indices cmp a) -(*$= & ~cmp:(=) ~printer:Q.Print.(array int) - [||] (sort_ranking Stdlib.compare [||]) - [|3;2;1;4;0|] (sort_ranking Stdlib.compare [|"d";"c";"b";"e";"a"|]) -*) - -(*$Q - Q.(array_of_size Gen.(0--50) printable_string) (fun a -> \ - let b = sort_ranking String.compare a in \ - let a_sorted = sorted String.compare a in \ - a = Array.map (Array.get a_sorted) b) -*) - let rev a = let b = Array.copy a in reverse_in_place b; b -(*$Q - Q.(array small_int) (fun a -> rev (rev a) = a) -*) - -(*$T - rev [| 1; 2; 3 |] = [| 3; 2; 1 |] - rev [| 1; 2; |] = [| 2; 1 |] - rev [| |] = [| |] -*) - exception Found let mem ?(eq = Stdlib.(=)) elt a = @@ -218,11 +121,6 @@ let mem ?(eq = Stdlib.(=)) elt a = false with Found -> true -(*$Q mem - Q.(array small_int) (fun a -> \ - mem 1 a = (Array.mem 1 a)) -*) - let rec find_aux f a i = if i >= Array.length a then None else match f i a.(i) with @@ -252,14 +150,6 @@ let filter_map f a = | Some x -> aux (x::acc) (i+1) in aux [] 0 -(*$T - filter_map (fun x -> if x mod 2 = 0 then Some (string_of_int x) else None) \ - [| 1; 2; 3; 4 |] = [| "2"; "4" |] - filter_map (fun x -> if x mod 2 = 0 then Some (string_of_int x) else None) \ - [| 1; 2; 3; 4; 5; 6 |] \ - = [| "2"; "4"; "6" |] -*) - let filter p a = filter_map (fun x -> if p x then Some x else None) a @@ -283,12 +173,6 @@ let flat_map f a = aux (__rev_append_list a' acc 0) (i+1) in aux [] 0 -(*$T - let a = [| 1; 3; 5 |] in \ - let a' = flat_map (fun x -> [| x; x+1 |]) a in \ - a' = [| 1; 2; 3; 4; 5; 6 |] -*) - let monoid_product f a1 a2 = let na1 = length a1 in init (na1 * length a2) @@ -297,11 +181,6 @@ let monoid_product f a1 a2 = let j = i_prod / na1 in f a1.(i) a2.(j)) -(*$= & ~cmp:(=) ~printer:Q.Print.(array int) - [| 11; 12; 21; 22 |] (sorted CCInt.compare @@ monoid_product (+) [| 10; 20 |] [| 1; 2 |]) - [| 11; 12; 13; 14 |] (sorted CCInt.compare @@ monoid_product (+) [| 10 |] [| 1; 2; 3; 4 |]) -*) - let rec _lookup_rec ~cmp k a i j = if i>j then raise Not_found else if i=j @@ -334,16 +213,6 @@ let lookup ~cmp k a = try Some (_lookup_exn ~cmp k a 0 (Array.length a-1)) with Not_found -> None -(*$T - lookup ~cmp:CCInt.compare 2 [|0;1;2;3;4;5|] = Some 2 - lookup ~cmp:CCInt.compare 4 [|0;1;2;3;4;5|] = Some 4 - lookup ~cmp:CCInt.compare 0 [|1;2;3;4;5|] = None - lookup ~cmp:CCInt.compare 6 [|1;2;3;4;5|] = None - lookup ~cmp:CCInt.compare 3 [| |] = None - lookup ~cmp:CCInt.compare 1 [| 1 |] = Some 0 - lookup ~cmp:CCInt.compare 2 [| 1 |] = None -*) - let bsearch ~cmp k a = let rec aux i j = if i > j @@ -362,16 +231,6 @@ let bsearch ~cmp k a = | _, c when c<0 -> `All_lower | _ -> aux 0 (n-1) -(*$T bsearch - bsearch ~cmp:CCInt.compare 3 [|1; 2; 2; 3; 4; 10|] = `At 3 - bsearch ~cmp:CCInt.compare 5 [|1; 2; 2; 3; 4; 10|] = `Just_after 4 - bsearch ~cmp:CCInt.compare 1 [|1; 2; 5; 5; 11; 12|] = `At 0 - bsearch ~cmp:CCInt.compare 12 [|1; 2; 5; 5; 11; 12|] = `At 5 - bsearch ~cmp:CCInt.compare 10 [|1; 2; 2; 3; 4; 9|] = `All_lower - bsearch ~cmp:CCInt.compare 0 [|1; 2; 2; 3; 4; 9|] = `All_bigger - bsearch ~cmp:CCInt.compare 3 [| |] = `Empty -*) - let rec _for_all2 p a1 a2 i1 i2 ~len = len=0 || (p a1.(i1) a2.(i2) && _for_all2 p a1 a2 (i1+1) (i2+1) ~len:(len-1)) @@ -406,28 +265,12 @@ let (--) i j = else Array.init (i-j+1) (fun k -> i-k) -(*$T - (1 -- 4) |> Array.to_list = [1;2;3;4] - (4 -- 1) |> Array.to_list = [4;3;2;1] - (0 -- 0) |> Array.to_list = [0] -*) - -(*$Q - Q.(pair small_int small_int) (fun (a,b) -> \ - (a -- b) |> Array.to_list = CCList.(a -- b)) -*) - let (--^) i j = if i=j then [| |] else if i>j then Array.init (i-j) (fun k -> i-k) else Array.init (j-i) (fun k -> i+k) -(*$Q - Q.(pair small_int small_int) (fun (a,b) -> \ - (a --^ b) |> Array.to_list = CCList.(a --^ b)) -*) - (** all the elements of a, but the i-th, into a list *) let except_idx a i = foldi @@ -443,15 +286,6 @@ let equal eq a b = && aux 0 -(*$Q - Q.(pair (array small_int)(array small_int)) (fun (a,b) -> \ - equal (=) a b = equal (=) b a) -*) - -(*$T - equal (=) [|1|] [|1|] -*) - let compare cmp a b = let rec aux i = if i = Array.length a @@ -464,13 +298,6 @@ let compare cmp a b = in aux 0 -(*$T - compare CCOrd.compare [| 1; 2; 3 |] [| 1; 2; 3 |] = 0 - compare CCOrd.compare [| 1; 2; 3 |] [| 2; 2; 3 |] < 0 - compare CCOrd.compare [| 1; 2; |] [| 1; 2; 3 |] < 0 - compare CCOrd.compare [| 1; 2; 3 |] [| 1; 2; |] > 0 -*) - (* swap elements of array *) let swap a i j = if i<>j then ( @@ -479,27 +306,6 @@ let swap a i j = a.(j) <- tmp; ) -(*$T - let a = [| 1;2;3 |] in \ - swap a 0 1; \ - a = [| 2;1;3 |] - let a = [| 1;2;3 |] in \ - swap a 0 2; \ - a = [| 3;2;1 |] -*) - -(*$QR - Q.(array_of_size Gen.(0 -- 100) small_int) (fun a -> - let b = Array.copy a in - for i = 0 to Array.length a-1 do - for j = i+1 to Array.length a-1 do - swap a i j; done; done; - for i = 0 to Array.length a-1 do - for j = i+1 to Array.length a-1 do - swap a i j; done; done; - a=b) -*) - (* shuffle a[i … j] using the given int random generator See http://en.wikipedia.org/wiki/Fisher-Yates_shuffle *) let _shuffle _rand_int a i j = @@ -555,13 +361,6 @@ let to_string ?(sep=", ") item_to_string a = |> List.map item_to_string |> String.concat sep -(*$= to_string & ~printer:(fun s -> s) - (to_string string_of_int [|1;2;3;4;5;6|]) "1, 2, 3, 4, 5, 6" - (to_string string_of_int [||]) "" - (to_string ~sep:" " string_of_int [|1;2;3;4;5;6|]) "1 2 3 4 5 6" - (to_string string_of_int [|1|]) "1" -*) - let to_seq a = let rec aux i () = if i>= length a then Seq.Nil @@ -569,12 +368,6 @@ let to_seq a = in aux 0 -(*$= - [] (to_seq [||] |> CCList.of_seq) - [1;2;3] (to_seq [|1;2;3|] |> CCList.of_seq) - CCList.(1 -- 1000) (to_seq (1--1000) |> CCList.of_seq) -*) - let to_iter a k = iter k a let to_gen a = @@ -698,31 +491,6 @@ let sort_generic (type arr)(type elt) let module S = SortGeneric(A) in S.sort ~cmp a -(*$inject - module IA = struct - let get = Array.get - let set = Array.set - let length = Array.length - type elt = int - type t = int array - end - - let gen_arr = Q.Gen.(array_size (1--100) small_int) - let arr_arbitrary = Q.make - ~print:Q.Print.(array int) - ~small:Array.length - ~shrink:Q.Shrink.(array ?shrink:None) - gen_arr -*) - -(*$Q & ~count:300 - arr_arbitrary (fun a -> \ - let a1 = Array.copy a and a2 = Array.copy a in \ - Array.sort CCInt.compare a1; sort_generic (module IA) ~cmp:CCInt.compare a2; \ - a1 = a2 ) -*) - - module Infix = struct let (>>=) a f = flat_map f a let (>>|) a f = map f a diff --git a/src/core/CCBool.ml b/src/core/CCBool.ml index cc2187f9..5b64184c 100644 --- a/src/core/CCBool.ml +++ b/src/core/CCBool.ml @@ -10,21 +10,8 @@ let compare (a:bool) b = Stdlib.compare a b let to_int (x:bool) : int = if x then 1 else 0 -(*$= - 1 (to_int true) - 0 (to_int false) -*) - let of_int x : t = x<>0 -(*$= - true (of_int 1) - false (of_int 0) - true (of_int 42) - true (of_int max_int) - true (of_int min_int) -*) - type 'a printer = Format.formatter -> 'a -> unit let pp = Format.pp_print_bool diff --git a/src/core/CCByte_buffer.ml b/src/core/CCByte_buffer.ml index b2bdd448..fa5e1d28 100644 --- a/src/core/CCByte_buffer.ml +++ b/src/core/CCByte_buffer.ml @@ -17,13 +17,6 @@ let[@inline] length self = self.sz let[@inline] is_empty self = self.sz = 0 let[@inline] clear self = self.sz <- 0 -(*$T - (let b = create() in is_empty b) - (let b = create ~cap:32 () in is_empty b) - (let b = create() in length b = 0) - (let b = create ~cap:32 () in length b = 0) -*) - let grow_cap_ self = min Sys.max_string_length (let n = capacity self in n + n lsl 1 + 5) @@ -124,121 +117,3 @@ let to_seq self = s 0 (* TODO: unicode operators.*) - -(*$inject - let test_count = 2_500 - - open QCheck - - type op = - | Add_char of char - | Add_string of string - | Get_contents - | Get of int - | Clear - | Shrink_to of int - | Set of int * char - - let spf = Printf.sprintf - - let str_op = function - | Add_char c -> spf "add_char %C" c - | Add_string s -> spf "add_string %S" s - | Get_contents -> "contents" - | Get i -> spf "get %d" i - | Clear -> "clear" - | Shrink_to n -> spf "shrink %d" n - | Set (i,c) -> spf "set %d %C" i c - - let gen_op size : (_*_) Gen.t = - let open Gen in - let base = if size>0 then - [1, ((0--size) >|= fun x -> Get x, size); - 1, ((0--size) >>= fun x -> printable >|= fun c -> Set (x,c), size); - 1, ((0--size) >|= fun x -> Shrink_to x, x); - ] - else [] - in - frequency (base @ [ - 1, return (Get_contents, size); - 1, return (Clear, 0); - 3, (printable >|= fun c -> Add_char c, size+1); - 1, (string_size (0 -- 100) ~gen:printable >|= fun s -> - Add_string s, size+String.length s); - ]) - - let rec gen_l acc sz n = - let open Gen in - if n=0 then return (List.rev acc) - else ( - gen_op sz >>= fun (op, sz) -> - gen_l (op::acc) sz (n-1) - ) - - let gen : op list Gen.t = Gen.sized (gen_l [] 0) - - let is_valid ops = - let rec loop sz = function - | [] -> true - | Add_char _ :: tl -> loop (sz+1) tl - | Clear :: tl -> loop 0 tl - | Add_string s :: tl -> loop (sz+String.length s) tl - | (Get n | Set (n,_)) :: tl -> n < sz && loop sz tl - | Get_contents :: tl -> loop sz tl - | Shrink_to x :: tl -> x <= sz && loop x tl - in loop 0 ops - - let shrink_op = Iter.(function - | Get_contents | Clear -> empty - | Get n -> Shrink.int n >|= fun n->Get n - | Add_char c -> Shrink.char c >|= fun c -> Add_char c - | Add_string s -> Shrink.string s >|= fun s -> Add_string s - | Shrink_to n -> Shrink.int n >|= fun n -> Shrink_to n - | Set (n,c) -> - (Shrink.int n >|= fun n-> Set(n,c)) <+> - (Shrink.char c >|= fun c-> Set(n,c)) - ) - - let arb = make gen ~print:(Print.list str_op) - ~shrink:Shrink.(filter is_valid @@ list ~shrink:shrink_op) - - exception Nope of string - let prop_consistent ops = - let buf = ref "" in - let b = create ~cap:32 () in - - let run_op op = - match op with - | Get i -> - assert (String.length !buf = length b); - let c1 = (!buf).[i] in - let c2 = get b i in - if c1<>c2 then raise (Nope (spf "c1=%C, c2=%C" c1 c2)) - - | Get_contents -> - let s1 = !buf in - let s2 = contents b in - if s1<>s2 then raise (Nope (spf "s1=%S, s2=%S" s1 s2)) - - | Add_char c -> buf := !buf ^ String.make 1 c; add_char b c - | Add_string s -> buf := !buf ^ s; append_string b s - | Clear -> buf := ""; clear b - | Shrink_to n -> buf := String.sub !buf 0 n; shrink_to b n - | Set (n,c) -> - ( - let b' = Bytes.of_string !buf in - Bytes.set b' n c; - buf := Bytes.unsafe_to_string b'; - ); - set b n c - in - - assume (is_valid ops); - try List.iter run_op ops; true - with Nope str -> - Test.fail_reportf "consistent ops failed:\n%s" str -*) - -(*$Q - arb (fun ops -> prop_consistent ops) - *) diff --git a/src/core/CCCanonical_sexp.ml b/src/core/CCCanonical_sexp.ml index a50d99eb..6c1ff9be 100644 --- a/src/core/CCCanonical_sexp.ml +++ b/src/core/CCCanonical_sexp.ml @@ -273,58 +273,3 @@ module Basic_ = struct end include (Make(Basic_) : S with type t := t) - -(*$inject - let csexp_bijective s = to_string s |> parse_string = Ok s -*) - -(*$= & ~printer:CCFormat.(to_string (Dump.result pp)) - (Ok (`List [`Atom ""])) (parse_string {|(0:)|}) - (Ok (`List [`Atom "a"; `Atom "b "])) (parse_string {|(1:a2:b )|}) -*) - -(*$T - csexp_bijective (`List [`Atom ""]) -*) - -(*$inject - let sexp_gen = - let mkatom a = `Atom a and mklist l = `List l in - let atom = Q.Gen.(map mkatom (string_size ~gen:char (1 -- 30))) in - let gen = Q.Gen.( - sized (fix - (fun self n st -> match n with - | 0 -> atom st - | _ -> - frequency - [ 1, atom - ; 2, map mklist (list_size (0 -- 10) (self (n/10))) - ] st - ) - )) in - let rec small = function - | `Atom s -> String.length s - | `List l -> List.fold_left (fun n x->n+small x) 0 l - and print = function - | `Atom s -> Printf.sprintf "`Atom \"%s\"" s - | `List l -> "`List " ^ Q.Print.list print l - and shrink = function - | `Atom s -> Q.Iter.map mkatom (Q.Shrink.string s) - | `List l -> Q.Iter.map mklist (Q.Shrink.list ~shrink l) - in - Q.make ~print ~small ~shrink gen -*) - -(*$Q & ~count:100 - sexp_gen csexp_bijective -*) - -(*$R - let s1 = - `List (CCList.init 100_000 - (fun i -> `List [`Atom "-"; `Atom (string_of_int i); `Atom ")(\n]"])) in - let str = to_string s1 in - match parse_string str with - | Ok s2 -> assert_equal s1 s2 - | Error e -> assert_failure e -*) diff --git a/src/core/CCChar.ml b/src/core/CCChar.ml index 56de6534..d70ac7b7 100644 --- a/src/core/CCChar.ml +++ b/src/core/CCChar.ml @@ -13,17 +13,8 @@ let of_int_exn = Char.chr let of_int c = try Some (of_int_exn c) with Invalid_argument _ -> None let to_int = Char.code -(*$= - (Some 'a') (of_int (to_int 'a')) - None (of_int 257) -*) - let to_string c = String.make 1 c -(*$Q to_string - (Q.string_of_size (Q.Gen.return 1)) (fun s -> CCShims_.Stdlib.(=) (to_string s.[0]) s) -*) - module Infix = struct let (=) : t -> t -> bool = CCShims_.Stdlib.(=) let (<>) : t -> t -> bool = CCShims_.Stdlib.(<>) diff --git a/src/core/CCEither.ml b/src/core/CCEither.ml index 3f1d7849..c9ae21ec 100644 --- a/src/core/CCEither.ml +++ b/src/core/CCEither.ml @@ -15,32 +15,12 @@ let right r = Right r let is_left = function Left _ -> true | Right _ -> false -(*$= - (is_left (Left 1)) (true) - (is_left (Right 1)) (false) -*) - let is_right = function Left _ -> false | Right _ -> true -(*$= - (is_right (Left 1)) (false) - (is_right (Right 1)) (true) -*) - let find_left = function Left l -> Some l | Right _ -> None -(*$= - (find_left (Left 1)) (Some 1) - (find_left (Right 1)) (None) -*) - let find_right = function Left _ -> None | Right r -> Some r -(*$= - (find_right (Left 1)) (None) - (find_right (Right 1)) (Some 1) -*) - let map_left f = function Left l -> Left (f l) | Right r -> Right r let map_right f = function Left l -> Left l | Right r -> Right (f r) diff --git a/src/core/CCEqual.ml b/src/core/CCEqual.ml index 407ae6fd..a0a32165 100644 --- a/src/core/CCEqual.ml +++ b/src/core/CCEqual.ml @@ -41,11 +41,6 @@ let triple f g h (x1,y1,z1)(x2,y2,z2) = f x1 x2 && g y1 y2 && h z1 z2 let map f eq x y = eq (f x) (f y) -(*$Q - Q.(let p = small_list (pair small_int bool) in pair p p) (fun (l1,l2) -> \ - CCEqual.(list (pair int bool)) l1 l2 = (l1=l2)) -*) - let always_eq _ _ = true let never_eq _ _ = false diff --git a/src/core/CCFloat.ml b/src/core/CCFloat.ml index 68c0b412..98fb3cbd 100644 --- a/src/core/CCFloat.ml +++ b/src/core/CCFloat.ml @@ -62,20 +62,6 @@ let max (x : t) y = | _, FP_nan -> x | _ -> if x > y then x else y -(*$T - max nan 1. = 1. - min nan 1. = 1. - max 1. nan = 1. - min 1. nan = 1. -*) - -(*$Q - Q.(pair float float) (fun (x,y) -> \ - is_nan x || is_nan y || (min x y <= x && min x y <= y)) - Q.(pair float float) (fun (x,y) -> \ - is_nan x || is_nan y || (max x y >= x && max x y >= y)) - *) - let equal (a:float) b = a=b let hash : t -> int = Hashtbl.hash @@ -102,12 +88,6 @@ let round x = let high = ceil x in if x-.low > high-.x then high else low -(*$= - 2. (round 1.6) - 1. (round 1.4) - 0. (round 0.) -*) - let to_int (a:float) = Stdlib.int_of_float a let of_int (a:int) = Stdlib.float_of_int a diff --git a/src/core/CCFormat.ml b/src/core/CCFormat.ml index f0547d92..0c82f526 100644 --- a/src/core/CCFormat.ml +++ b/src/core/CCFormat.ml @@ -16,17 +16,6 @@ let silent _fmt _ = () let return fmt_str out () = Format.fprintf out "%(%)" fmt_str -(*$inject - let to_string_test s = CCFormat.sprintf_no_color "@[%a@]%!" s () -*) - -(*$= & ~printer:(fun s->CCFormat.sprintf "%S" s) - "a b" (to_string_test (return "a@ b")) - ", " (to_string_test (return ",@ ")) - "and then" (to_string_test (return "@{and then@}@,")) - "a b" (to_string_test (return "@[a@ b@]")) -*) - let unit fmt () = Format.pp_print_string fmt "()" let int fmt i = Format.pp_print_string fmt (string_of_int i) let string = Format.pp_print_string @@ -51,11 +40,6 @@ let substring out (s,i,len): unit = let text = Format.pp_print_text -(*$= & ~printer:(fun s->CCFormat.sprintf "%S" s) - "a\nb\nc" (sprintf_no_color "@[%a@]%!" text "a b c") - "a b\nc" (sprintf_no_color "@[%a@]%!" text "a b\nc") -*) - let string_lines out (s:string) : unit = fprintf out "@["; let i = ref 0 in @@ -69,10 +53,6 @@ let string_lines out (s:string) : unit = done; fprintf out "@]" -(*$= & ~printer:(fun s->CCFormat.sprintf "%S" s) - "(a\n b\n c)" (sprintf_no_color "(@[%a@])" string_lines "a\nb\nc") -*) - let list ?(sep=return ",@ ") pp fmt l = let rec pp_list l = match l with | x::((_::_) as l) -> @@ -128,21 +108,9 @@ let append ppa ppb fmt () = ppa fmt (); ppb fmt () -(*$= append & ~printer:(fun s -> CCFormat.sprintf "%S" s) - "foobar" (to_string_test (append (return "foo") (return "bar"))) - "bar" (to_string_test (append (return "") (return "bar"))) - "foo" (to_string_test (append (return "foo") (return ""))) -*) - let append_l ppl fmt () = List.iter (fun pp -> pp fmt ()) ppl -(*$= append_l & ~printer:(fun s -> CCFormat.sprintf "%S" s) - "" (to_string_test @@ append_l []) - "foobarbaz" (to_string_test @@ append_l (List.map return ["foo"; "bar"; "baz"])) - "3141" (to_string_test @@ append_l (List.map (const int) [3; 14; 1])) -*) - let within a b p out x = string out a; p out x; @@ -227,17 +195,6 @@ let tee a b = fb.Format.out_string str i len) (fun () -> fa.Format.out_flush (); fb.Format.out_flush ()) -(*$R - let buf1 = Buffer.create 42 in - let buf2 = Buffer.create 42 in - let f1 = Format.formatter_of_buffer buf1 in - let f2 = Format.formatter_of_buffer buf2 in - let fmt = tee f1 f2 in - Format.fprintf fmt "coucou@."; - assert_equal ~printer:CCFun.id "coucou\n" (Buffer.contents buf1); - assert_equal ~printer:CCFun.id "coucou\n" (Buffer.contents buf2); -*) - let to_file filename format = let oc = open_out filename in let fmt = Format.formatter_of_out_channel oc in @@ -287,10 +244,6 @@ module ANSI_codes = struct let reset = string_of_style `Reset - (*$= - ANSI_codes.reset "\x1b[0m" - *) - let string_of_style_list = function | [] -> reset | [a] -> string_of_style a @@ -407,19 +360,6 @@ let set_color_tag_handling ppf = in pp_set_formatter_stag_functions ppf funs' -(*$R - set_color_default true; - let s = sprintf - "what is your %a? %a! No, %a! Ahhhhhhh@." - (styling [`FG `White; `Bold] string) "favorite color" - (styling [`FG `Blue] string) "blue" - (styling [`FG `Red] string) "red" - in - assert_equal ~printer:CCFun.id - "what is your \027[37;1mfavorite color\027[0m? \027[34mblue\027[0m! No, \027[31mred\027[0m! Ahhhhhhh\n" - s -*) - [@@@else_] (* either prints the tag of [s] or delegate to [or_else] *) @@ -466,16 +406,6 @@ let set_color_default = ); ) else if not b && !color_enabled then color_enabled := false -(*$R - set_color_default true; - let s = sprintf - "what is your @{favorite color@}? @{blue@}! No, @{red@}! Ahhhhhhh@." - in - assert_equal ~printer:CCFun.id - "what is your \027[37;1mfavorite color\027[0m? \027[34mblue\027[0m! No, \027[31mred\027[0m! Ahhhhhhh\n" - s -*) - let with_color s pp out x = pp_open_tag out s; pp out x; @@ -522,18 +452,6 @@ let fprintf_dyn_color ~colors out fmt = (fun out -> Format.pp_set_mark_tags out old_tags) out fmt -(*$T - sprintf "yolo %s %d" "a b" 42 = "yolo a b 42" - sprintf "%d " 0 = "0 " - sprintf_no_color "%d " 0 = "0 " -*) - -(*$R - set_color_default true; - assert_equal "\027[31myolo\027[0m" (sprintf "@{yolo@}"); - assert_equal "yolo" (sprintf_no_color "@{yolo@}"); -*) - let ksprintf ?margin ~f fmt = let buf = Buffer.create 32 in let out = Format.formatter_of_buffer buf in @@ -543,11 +461,6 @@ let ksprintf ?margin ~f fmt = (fun _ -> Format.pp_print_flush out (); f (Buffer.contents buf)) out fmt -(*$= & ~printer:CCFormat.(to_string (opt string)) - (Some "hello world") \ - (ksprintf ~f:(fun s -> Some s) "hello %a" CCFormat.string "world") -*) - module Dump = struct type 'a t = 'a printer let unit = unit @@ -574,14 +487,6 @@ module Dump = struct let to_string = to_string end -(*$= & ~printer:(fun s->s) - "[1;2;3]" (to_string Dump.(list int) [1;2;3]) - "Some 1" (to_string Dump.(option int) (Some 1)) - "[None;Some \"a b\"]" (to_string Dump.(list (option string)) [None; Some "a b"]) - "[(Ok \"a b c\");(Error \"nope\")]" \ - (to_string Dump.(list (result string)) [Ok "a b c"; Error "nope"]) -*) - module Infix = struct let (++) = append end diff --git a/src/core/CCFun.ml b/src/core/CCFun.ml index 9424c667..362ddf35 100644 --- a/src/core/CCFun.ml +++ b/src/core/CCFun.ml @@ -80,18 +80,6 @@ let rec iterate n f x = else iterate (n - 1) f (f x) -(*$= iterate & ~printer:Q.Print.int - 10 (iterate 0 succ 10) - 11 (iterate 1 succ 10) - 12 (iterate 2 succ 10) - 15 (iterate 5 succ 10) -*) -(*$R - assert_raises - (Invalid_argument "CCFun.iterate") - (fun () -> iterate (-1) succ 10) -*) - module Infix = struct (* default implem for some operators *) let (|>) x f = f x @@ -101,13 +89,6 @@ module Infix = struct let (%) f g x = f (g x) end -(*$T - CCFun.((succ %> string_of_int) 2 = "3") - CCFun.((( * ) 3 % succ) 5 = 18) - CCFun.(succ @@ ( * ) 2 @@ pred @@ 3 = 5) - CCFun.(3 |> succ |> ( * ) 5 |> pred = 19) -*) - include Infix module Monad(X : sig type t end) = struct diff --git a/src/core/CCHash.ml b/src/core/CCHash.ml index 5405ef4e..dd55287d 100644 --- a/src/core/CCHash.ml +++ b/src/core/CCHash.ml @@ -100,16 +100,6 @@ let bytes (x:bytes) = let string (x:string) = bytes (Bytes.unsafe_of_string x) -(*$T - int 42 >= 0 - int max_int >= 0 - int max_int = int max_int - int min_int >= 0 - int 0 >= 0 - char 'c' >= 0 - int 152352 = int 152352 -*) - let slice x i len = let j=i+len in let rec aux i s = @@ -147,11 +137,6 @@ let list_comm f l = List.iteri (fun i x -> arr.(i) <- f x) l; array_of_hashes_ arr -(*$T - list_comm int [1;2] = list_comm int [2;1] - list_comm int [1;2] <> list_comm int [2;3] -*) - let iter f seq = let h = ref 0x43 in seq (fun x -> h := combine f !h x); diff --git a/src/core/CCList.ml b/src/core/CCList.ml index af9cb738..0fd242a5 100644 --- a/src/core/CCList.ml +++ b/src/core/CCList.ml @@ -5,10 +5,6 @@ open CCShims_ -(*$inject - let lsort l = List.sort Stdlib.compare l -*) - (* backport new functions from stdlib here *) let nth_opt l n = @@ -20,11 +16,6 @@ let nth_opt l n = in aux l n -(*$Q - Q.(pair small_nat (list int)) (fun (i,l) -> \ - nth_opt l i = get_at_idx i l) -*) - let rec find_opt p l = match l with | [] -> None | x :: _ when p x -> Some x @@ -36,24 +27,12 @@ let rec compare_lengths l1 l2 = match l1, l2 with | _::_, [] -> 1 | _::tail1, _::tail2 -> compare_lengths tail1 tail2 -(*$Q - Q.(pair (list int) (list int)) (fun (l1,l2) -> \ - CCOrd.equiv (CCList.compare_lengths l1 l2) \ - (CCInt.compare (length l1)(length l2))) -*) - let rec compare_length_with l n = match l, n with | _ when n<0 -> 1 | [], 0 -> 0 | [], _ -> -1 | _::tail, _ -> compare_length_with tail (n-1) -(*$Q - Q.(pair (list int) small_int) (fun (l,n) -> \ - CCOrd.equiv (CCList.compare_length_with l n) \ - (CCInt.compare (length l) n)) -*) - let rec assoc_opt x = function | [] -> None | (y,v) :: _ when Stdlib.(=) x y -> Some v @@ -128,12 +107,6 @@ let map f l = in direct f direct_depth_default_ l -(*$Q - (Q.list Q.small_int) (fun l -> \ - let f x = x+1 in \ - List.rev (List.rev_map f l) = map f l) -*) - let direct_depth_append_ = 10_000 let append l1 l2 = @@ -152,26 +125,12 @@ let append l1 l2 = let (@) = append -(*$T - [1;2;3] @ [4;5;6] = [1;2;3;4;5;6] - (1-- 10_000) @ (10_001 -- 20_000) = 1 -- 20_000 -*) - let[@inline] cons' l x = x::l -(*$Q - Q.(small_list int)(fun l -> List.rev l = List.fold_left cons' [] l) - *) - let cons_maybe o l = match o with | Some x -> x :: l | None -> l -(*$T - cons_maybe (Some 1) [2;3] = [1;2;3] - cons_maybe None [2;3] = [2;3] -*) - let direct_depth_filter_ = 10_000 let filter p l = @@ -187,12 +146,6 @@ let filter p l = in direct direct_depth_filter_ p l -(*$= & ~printer:CCInt.to_string - 500 (filter (fun x->x mod 2 = 0) (1 -- 1000) |> List.length) - 50_000 (filter (fun x->x mod 2 = 0) (1 -- 100_000) |> List.length) - 500_000 (filter (fun x->x mod 2 = 0) (1 -- 1_000_000) |> List.length) -*) - let fold_right f l acc = let rec direct i f l acc = match l with | [] -> acc @@ -208,16 +161,6 @@ let fold_right f l acc = in direct direct_depth_default_ f l acc -(*$T - fold_right (+) (1 -- 1_000_000) 0 = \ - List.fold_left (+) 0 (1 -- 1_000_000) -*) - -(*$Q - (Q.list Q.small_int) (fun l -> \ - l = fold_right (fun x y->x::y) l []) -*) - let rec fold_while f acc = function | [] -> acc | e::l -> let acc, cont = f acc e in @@ -225,10 +168,6 @@ let rec fold_while f acc = function | `Stop -> acc | `Continue -> fold_while f acc l -(*$T - fold_while (fun acc b -> if b then acc+1, `Continue else acc, `Stop) 0 [true;true;false;true] = 2 -*) - let fold_map f acc l = let rec aux f acc map_acc l = match l with | [] -> acc, List.rev map_acc @@ -238,16 +177,6 @@ let fold_map f acc l = in aux f acc [] l -(*$= - (6, ["1"; "2"; "3"]) \ - (fold_map (fun acc x->acc+x, string_of_int x) 0 [1;2;3]) -*) - -(*$Q - Q.(list int) (fun l -> \ - fold_map (fun acc x -> x::acc, x) [] l = (List.rev l, l)) -*) - let fold_map_i f acc l = let rec aux f acc i map_acc l = match l with | [] -> acc, List.rev map_acc @@ -266,10 +195,6 @@ let fold_on_map ~f ~reduce acc l = in aux acc l -(*$= - 6 (fold_on_map ~f:int_of_string ~reduce:(+) 0 ["1";"2";"3"]) -*) - let scan_left f acc l = let rec aux f acc l_acc l = match l with | [] -> List.rev l_acc @@ -288,29 +213,6 @@ let reduce_exn f = function | [] -> raise (Invalid_argument "CCList.reduce_exn") | x :: l -> fold_left f x l -(*$= & ~printer:Q.Print.(option int) - (Some 15) (reduce (+) [1; 2; 3; 4; 5]) - (Some 3) (reduce CCInt.min [5; 3; 8; 9]) -*) - -(*$= & ~printer:Q.Print.string - "hello world" (reduce_exn (^) ["hello"; " "; "world"]) -*) - -(*$T - try ignore (reduce_exn (+.) []); false with Invalid_argument _ -> true -*) - -(*$= & ~printer:Q.Print.(list int) - [0;1;3;6] (scan_left (+) 0 [1;2;3]) - [0] (scan_left (+) 0 []) -*) - -(*$Q - Q.(list int) (fun l -> \ - List.length l + 1 = List.length (scan_left (+) 0 l)) -*) - let fold_map2 f acc l1 l2 = let rec aux f acc map_acc l1 l2 = match l1, l2 with | [], [] -> acc, List.rev map_acc @@ -322,17 +224,6 @@ let fold_map2 f acc l1 l2 = in aux f acc [] l1 l2 -(*$= - (310, ["1 10"; "2 0"; "3 100"]) \ - (fold_map2 (fun acc x y->acc+x*y, string_of_int x ^ " " ^ string_of_int y) \ - 0 [1;2;3] [10;0;100]) -*) - -(*$T - (try ignore (fold_map2 (fun _ _ _ -> assert false) 42 [] [1]); false \ - with Invalid_argument _ -> true) -*) - let fold_filter_map f acc l = let rec aux f acc map_acc l = match l with | [] -> acc, List.rev map_acc @@ -342,12 +233,6 @@ let fold_filter_map f acc l = in aux f acc [] l -(*$= & ~printer:Q.Print.(pair int (list int)) - (List.fold_left (+) 0 (1--10), [2;4;6;8;10]) \ - (fold_filter_map (fun acc x -> acc+x, if x mod 2 = 0 then Some x else None) \ - 0 (1--10)) -*) - let fold_filter_map_i f acc l = let rec aux f acc i map_acc l = match l with | [] -> acc, List.rev map_acc @@ -366,12 +251,6 @@ let fold_flat_map f acc l = in aux f acc [] l -(*$= - (6, ["1"; "a1"; "2"; "a2"; "3"; "a3"]) \ - (let pf = Printf.sprintf in \ - fold_flat_map (fun acc x->acc+x, [pf "%d" x; pf "a%d" x]) 0 [1;2;3]) -*) - let fold_flat_map_i f acc l = let rec aux f acc i map_acc l = match l with | [] -> acc, List.rev map_acc @@ -381,12 +260,6 @@ let fold_flat_map_i f acc l = in aux f acc 0 [] l -(*$Q - Q.(list int) (fun l -> \ - fold_flat_map (fun acc x -> x::acc, [x;x+10]) [] l = \ - (List.rev l, flat_map (fun x->[x;x+10]) l) ) -*) - let init len f = let rec indirect_ i acc = if i=len then List.rev acc @@ -408,24 +281,6 @@ let init len f = else if len=0 then [] else direct_ 0 -(*$T - init 0 (fun _ -> 0) = [] - init 1 (fun x->x) = [0] - init 1000 (fun x->x) = 0--999 -*) - -(* see: #256 *) -(*$R - let r = ref [] in - ignore (CCList.init 5 (fun x -> r := x :: !r; ())); - assert_equal ~printer:Q.Print.(list int) (List.rev !r) [0;1;2;3;4] -*) -(*$R - let r = ref [] in - ignore (CCList.init 200_000 (fun x -> r := x :: !r; ())); - assert_equal ~printer:Q.Print.(list int) (List.rev !r) (0--(200_000-1)) -*) - let rec compare f l1 l2 = match l1, l2 with | [], [] -> 0 | _, [] -> 1 @@ -439,10 +294,6 @@ let rec equal f l1 l2 = match l1, l2 with | [], _ | _, [] -> false | x1::l1', x2::l2' -> f x1 x2 && equal f l1' l2' -(*$T - equal CCInt.equal (1--1_000_000) (1--1_000_000) -*) - let flat_map f l = let rec aux f l kont = match l with | [] -> kont [] @@ -458,11 +309,6 @@ let flat_map f l = in aux f l (fun l->l) -(*$T - flat_map (fun x -> [x+1; x*2]) [10;100] = [11;20;101;200] - List.length (flat_map (fun x->[x]) (1--300_000)) = 300_000 -*) - let flat_map_i f l = let rec aux f i l kont = match l with | [] -> kont [] @@ -478,39 +324,17 @@ let flat_map_i f l = in aux f 0 l (fun l->l) -(*$= - [1;2;2;3;3;3] (flat_map_i (fun i x->replicate (i+1) x) [1;2;3]) -*) - let flatten l = fold_right append l [] -(*$T - flatten [[1]; [2;3;4]; []; []; [5;6]] = 1--6 - flatten (init 300_001 (fun x->[x])) = 0--300_000 -*) - let count f l = fold_left (fun n x -> if f x then succ n else n) 0 l -(*$T - count (fun x -> x mod 2 = 0) [] = 0 - count (fun x -> x mod 2 = 0) [0; 0; 2; 4] = 4 - count (fun x -> x mod 2 = 0) [1; 3; 5; 7] = 0 - count (fun x -> x mod 2 = 0) [2; 6; 9; 4] = 3 -*) - let count_true_false p l = fold_left (fun (ok, ko) x -> if p x then ok + 1, ko else ok, ko + 1) (0, 0) l -(*$T - count_true_false (fun x -> x mod 2 = 0) [] = (0, 0) - count_true_false (fun x -> x mod 2 = 0) [0; 0; 2; 4] = (4, 0) - count_true_false (fun x -> x mod 2 = 0) [1; 3; 5; 7] = (0, 4) - count_true_false (fun x -> x mod 2 = 0) [2; 6; 9; 4] = (3, 1) -*) let[@inline] product f l1 l2 = flat_map (fun x -> map (fun y -> f x y) l2) l1 @@ -532,13 +356,6 @@ let diagonal l = in gen [] l -(*$T - diagonal [] = [] - diagonal [1] = [] - diagonal [1;2] = [1,2] - diagonal [1;2;3] |> List.sort Stdlib.compare = [1, 2; 1, 3; 2, 3] -*) - let partition_map_either f l = let rec iter f l1 l2 l = match l with | [] -> List.rev l1, List.rev l2 @@ -549,17 +366,6 @@ let partition_map_either f l = in iter f [] [] l -(*$R - let l1, l2 = - partition_map_either (function - | n when n mod 2 = 0 -> CCEither.Left n - | n -> CCEither.Right n - ) [0;1;2;3;4] - in - assert_equal [0;2;4] l1; - assert_equal [1;3] l2 -*) - let partition_filter_map f l = let rec iter f l1 l2 l = match l with | [] -> List.rev l1, List.rev l2 @@ -573,18 +379,6 @@ let partition_filter_map f l = let partition_map = partition_filter_map -(*$R - let l1, l2 = - partition_filter_map (function - | n when n = 0 -> `Drop - | n when n mod 2 = 0 -> `Left n - | n -> `Right n - ) [0;1;2;3;4] - in - assert_equal [2;4] l1; - assert_equal [1;3] l2 -*) - let combine l1 l2 = let rec direct i l1 l2 = match l1, l2 with | ([], []) -> [] @@ -598,20 +392,6 @@ let combine l1 l2 = in direct direct_depth_default_ l1 l2 -(*$T - try ignore (combine [1] []); false with Invalid_argument _ -> true - try ignore (combine (1--1001) (1--1002)); false with Invalid_argument _ -> true - combine [1;2;3] [3;2;1] = List.combine [1;2;3] [3;2;1] - combine (1 -- 100_000) (1 -- 100_000) = List.combine (1 -- 100_000) (1 -- 100_000) -*) - -(*$Q - Q.(let p = small_list int in pair p p)(fun (l1,l2) -> \ - if List.length l1=List.length l2 \ - then CCList.combine l1 l2 = List.combine l1 l2 \ - else Q.assume_fail() ) -*) - let combine_gen l1 l2 = let l1 = ref l1 in let l2 = ref l2 in @@ -623,14 +403,6 @@ let combine_gen l1 l2 = l2 := tail2; Some (x1,x2) -(*$Q - Q.(let p = small_list int in pair p p)(fun (l1,l2) -> \ - let n = min (List.length l1) (List.length l2) in \ - let res1 = combine (take n l1) (take n l2) in \ - let res2 = combine_gen l1 l2 |> of_gen in \ - res1 = res2) -*) - let combine_shortest l1 l2 = let rec direct i l1 l2 = match l1, l2 with | (_, []) | ([], _) -> [] @@ -644,17 +416,6 @@ let combine_shortest l1 l2 = in direct direct_depth_default_ l1 l2 -(*$T - (combine_shortest [] []) = [] - (combine_shortest [1] []) = [] - (combine_shortest [] [1]) = [] - (combine_shortest (1--1025) (1--1026)) = List.combine (1--1025) (1--1025) - (combine_shortest (1--1026) (1--1025)) = List.combine (1--1025) (1--1025) - combine_shortest [1;2;3] [3;2;1] = List.combine [1;2;3] [3;2;1] - combine_shortest (1 -- 100_000) (1 -- 100_000) = List.combine (1 -- 100_000) (1 -- 100_000) - combine_shortest (1 -- 100_001) (1 -- 100_000) = List.combine (1 -- 100_000) (1 -- 100_000) -*) - let split l = let rec direct i l = match l with @@ -677,16 +438,6 @@ let split l = in direct direct_depth_default_ l -(*$Q - (Q.(list_of_size Gen.(0--10_000) (pair small_int small_string))) (fun l -> \ - let (l1, l2) = split l in \ - List.length l1 = List.length l \ - && List.length l2 = List.length l) - - Q.(list_of_size Gen.(0--10_000) (pair small_int small_int)) (fun l -> \ - split l = List.split l) -*) - let return x = [x] let pure = return @@ -707,30 +458,11 @@ let cartesian_product l = in prod_rec [] l (fun acc l' -> l' :: acc) [] -(*$inject - let cmp_lii_unord l1 l2 : bool = - List.sort CCOrd.compare l1 = List.sort CCOrd.compare l2 -*) - -(*$= & ~printer:Q.Print.(list (list int)) ~cmp:cmp_lii_unord - [[1;3;4];[1;3;5];[1;3;6];[2;3;4];[2;3;5];[2;3;6]] \ - (cartesian_product [[1;2];[3];[4;5;6]]) - [] (cartesian_product [[1;2];[];[4;5;6]]) - [[]] (cartesian_product []) - [[1;3;4;5;6];[2;3;4;5;6]] \ - (cartesian_product [[1;2];[3];[4];[5];[6]]) -*) - (* cartesian product of lists of lists *) let map_product_l f l = let l = List.map f l in cartesian_product l -(*$Q - Q.(list_of_size Gen.(1--4) (list_of_size Gen.(0--4) small_int)) (fun l-> \ - cmp_lii_unord (cartesian_product l) (map_product_l CCFun.id l)) -*) - let rec sorted_mem ~cmp x l = match l with | [] -> false | y :: tail -> @@ -739,11 +471,6 @@ let rec sorted_mem ~cmp x l = match l with | n when n<0 -> false | _ -> (sorted_mem[@tailcall]) ~cmp x tail -(*$Q - Q.(pair small_int (list small_int)) (fun (x,l) -> \ - sorted_mem ~cmp:CCInt.compare x (List.sort CCInt.compare l) = mem ~eq:CCInt.equal x l) -*) - let sorted_merge ~cmp l1 l2 = let rec recurse cmp acc l1 l2 = match l1,l2 with | [], _ -> List.rev_append acc l2 @@ -756,17 +483,6 @@ let sorted_merge ~cmp l1 l2 = in recurse cmp [] l1 l2 -(*$T - equal CCInt.equal (List.sort CCInt.compare ([(( * )2); ((+)1)] <*> [10;100])) \ - [11; 20; 101; 200] - equal CCInt.equal (sorted_merge ~cmp:CCInt.compare [1;1;2] [1;2;3]) [1;1;1;2;2;3] -*) - -(*$Q - Q.(pair (list int) (list int)) (fun (l1,l2) -> \ - List.length (sorted_merge ~cmp:CCInt.compare l1 l2) = List.length l1 + List.length l2) -*) - let sorted_diff ~cmp l1 l2 = let rec recurse cmp acc l1 l2 = match l1,l2 with | [], _ -> List.rev acc @@ -779,34 +495,8 @@ let sorted_diff ~cmp l1 l2 = in recurse cmp [] l1 l2 -(*$T - equal CCInt.equal (sorted_diff ~cmp:CCInt.compare [0;1;1;2;4] [1;2;2;2;3]) [0;1;4] - equal CCInt.equal (sorted_diff ~cmp:CCInt.compare [2] [1;2;2;2;3]) [] -*) - -(*$Q - Q.(pair (list small_int) (list small_int)) (fun (l1,l2) -> \ - List.length (sorted_merge ~cmp:CCInt.compare l1 l2) = List.length l1 + List.length l2) - Q.(pair (list small_int) (list small_int)) (fun (l1,l2) -> \ - let l = sorted_diff ~cmp:CCInt.compare (List.sort CCInt.compare l1) (List.sort CCInt.compare l2) in \ - l = sort CCInt.compare l) (* [is_sorted] is after this function *) - Q.(triple small_nat small_nat int) (fun (n1,n2,x) -> \ - let l = sorted_diff ~cmp:CCInt.compare (CCList.init n1 (fun _ -> x)) (CCList.init n2 (fun _ -> x)) in \ - count (CCInt.equal x) l = CCInt.max (n1 - n2) 0) - Q.(pair (list small_int) (list small_int)) (fun (l1,l2) -> \ - let l1 = List.sort CCInt.compare l1 in \ - let l2 = List.sort CCInt.compare l2 in \ - l1 = sorted_diff ~cmp:CCInt.compare (sorted_merge ~cmp:CCInt.compare l1 l2) l2) -*) - let sort_uniq ~cmp l = List.sort_uniq cmp l -(*$T - sort_uniq ~cmp:CCInt.compare [1;2;5;3;6;1;4;2;3] = [1;2;3;4;5;6] - sort_uniq ~cmp:CCInt.compare [] = [] - sort_uniq ~cmp:CCInt.compare [10;10;10;10;1;10] = [1;10] -*) - let is_sorted ~cmp l = let rec aux cmp = function | [] | [_] -> true @@ -814,11 +504,6 @@ let is_sorted ~cmp l = in aux cmp l -(*$Q - Q.(list small_int) (fun l -> \ - is_sorted ~cmp:CCInt.compare (List.sort Stdlib.compare l)) -*) - let sorted_insert ~cmp ?(uniq=false) x l = let rec aux cmp uniq x left l = match l with | [] -> List.rev_append left [x] @@ -832,25 +517,6 @@ let sorted_insert ~cmp ?(uniq=false) x l = in aux cmp uniq x [] l -(*$Q - Q.(pair small_int (list small_int)) (fun (x,l) -> \ - let l = List.sort Stdlib.compare l in \ - is_sorted ~cmp:CCInt.compare (sorted_insert ~cmp:CCInt.compare x l)) - Q.(pair small_int (list small_int)) (fun (x,l) -> \ - let l = List.sort Stdlib.compare l in \ - is_sorted ~cmp:CCInt.compare (sorted_insert ~cmp:CCInt.compare ~uniq:true x l)) - Q.(pair small_int (list small_int)) (fun (x,l) -> \ - let l = List.sort Stdlib.compare l in \ - is_sorted ~cmp:CCInt.compare (sorted_insert ~cmp:CCInt.compare ~uniq:false x l)) - Q.(pair small_int (list small_int)) (fun (x,l) -> \ - let l = List.sort Stdlib.compare l in \ - let l' = sorted_insert ~cmp:CCInt.compare ~uniq:false x l in \ - List.length l' = List.length l + 1) - Q.(pair small_int (list small_int)) (fun (x,l) -> \ - let l = List.sort Stdlib.compare l in \ - List.mem x (sorted_insert ~cmp:CCInt.compare x l)) -*) - let sorted_remove ~cmp ?(all=false) x l = let rec aux cmp all x left l = match l with | [] -> List.rev left @@ -863,41 +529,6 @@ let sorted_remove ~cmp ?(all=false) x l = in aux cmp all x [] l -(*$Q - Q.(pair small_int (list small_int)) (fun (x,l) -> \ - let l = List.sort Stdlib.compare l in \ - is_sorted ~cmp:CCInt.compare (sorted_remove ~cmp:CCInt.compare x l)) - Q.(pair small_int (list small_int)) (fun (x,l) -> \ - let l = List.sort Stdlib.compare l in \ - is_sorted ~cmp:CCInt.compare (sorted_remove ~cmp:CCInt.compare ~all:false x l)) - Q.(pair small_int (list small_int)) (fun (x,l) -> \ - let l = List.sort Stdlib.compare l in \ - is_sorted ~cmp:CCInt.compare (sorted_remove ~cmp:CCInt.compare ~all:true x l)) - Q.(pair small_int (list small_int)) (fun (x,l) -> \ - let l = List.sort Stdlib.compare l in \ - let l' = sorted_remove ~cmp:CCInt.compare x l in \ - List.length l' = List.length l - (if List.mem x l then 1 else 0)) - Q.(pair small_int (list small_int)) (fun (x,l) -> \ - let l = List.sort Stdlib.compare l in \ - let l' = sorted_remove ~cmp:CCInt.compare ~all:true x l in \ - List.length l' = List.length l - count (CCInt.equal x) l) - Q.(pair small_int (list small_int)) (fun (x,l) -> \ - let l = List.sort Stdlib.compare l in \ - let l' = sorted_remove ~cmp:CCInt.compare ~all:false x l in \ - List.length l' = List.length l - (if List.mem x l then 1 else 0)) - Q.(pair small_int (list small_int)) (fun (x,l) -> \ - let l = List.sort Stdlib.compare l in \ - let l' = sorted_remove ~cmp:CCInt.compare x l in \ - count (CCInt.equal x) l' = count (CCInt.equal x) l - (if List.mem x l then 1 else 0)) - Q.(pair small_int (list small_int)) (fun (x,l) -> \ - let l = List.sort Stdlib.compare l in \ - let l' = sorted_remove ~cmp:CCInt.compare ~all:false x l in \ - count (CCInt.equal x) l' = count (CCInt.equal x) l - (if List.mem x l then 1 else 0)) - Q.(pair small_int (list small_int)) (fun (x,l) -> \ - let l = List.sort Stdlib.compare l in \ - not (List.mem x (sorted_remove ~cmp:CCInt.compare ~all:true x l))) -*) - let uniq_succ ~eq l = let rec f acc l = match l with | [] -> List.rev acc @@ -907,10 +538,6 @@ let uniq_succ ~eq l = in f [] l -(*$T - uniq_succ ~eq:CCInt.equal [1;1;2;3;1;6;6;4;6;1] = [1;2;3;1;6;4;6;1] -*) - let group_succ ~eq l = let rec f ~eq acc cur l = match cur, l with | [], [] -> List.rev acc @@ -921,15 +548,6 @@ let group_succ ~eq l = in f ~eq [] [] l -(*$T - group_succ ~eq:CCInt.equal [1;2;3;1;1;2;4] = [[1]; [2]; [3]; [1;1]; [2]; [4]] - group_succ ~eq:CCInt.equal [] = [] - group_succ ~eq:CCInt.equal [1;1;1] = [[1;1;1]] - group_succ ~eq:CCInt.equal [1;2;2;2] = [[1]; [2;2;2]] - group_succ ~eq:(fun (x,_)(y,_)-> x=y) [1, 1; 1, 2; 1, 3; 2, 0] \ - = [[1, 1; 1, 2; 1, 3]; [2, 0]] -*) - let sorted_merge_uniq ~cmp l1 l2 = let push ~cmp acc x = match acc with | [] -> [x] @@ -949,24 +567,6 @@ let sorted_merge_uniq ~cmp l1 l2 = in recurse ~cmp [] l1 l2 -(*$T - sorted_merge_uniq ~cmp:CCInt.compare [1; 1; 2; 3; 5; 8] [1; 2; 3; 4; 6; 8; 9; 9] = [1;2;3;4;5;6;8;9] -*) - -(*$Q - Q.(list int) (fun l -> \ - let l = List.sort Stdlib.compare l in \ - sorted_merge_uniq ~cmp:CCInt.compare l [] = uniq_succ ~eq:CCInt.equal l) - Q.(list int) (fun l -> \ - let l = List.sort Stdlib.compare l in \ - sorted_merge_uniq ~cmp:CCInt.compare [] l = uniq_succ ~eq:CCInt.equal l) - Q.(pair (list int) (list int)) (fun (l1, l2) -> \ - let l1 = List.sort Stdlib.compare l1 \ - and l2 = List.sort Stdlib.compare l2 in \ - let l3 = sorted_merge_uniq ~cmp:CCInt.compare l1 l2 in \ - uniq_succ ~eq:CCInt.equal l3 = l3) -*) - let sorted_diff_uniq ~cmp l1 l2 = let push ~cmp acc x = match acc with | [] -> [x] @@ -986,21 +586,6 @@ let sorted_diff_uniq ~cmp l1 l2 = in recurse ~cmp [] l1 l2 -(*$T - sorted_diff_uniq ~cmp:CCInt.compare [1; 1; 1; 2; 2; 3; 5; 8; 8; 8] [1; 2; 2; 2; 2; 8; 13; 13; 13] = [1;3;5;8] -*) - -(*$Q - Q.(pair (list small_int) (list small_int)) (fun (l1, l2) -> \ - let l1 = List.sort CCInt.compare l1 in \ - let l2 = List.sort CCInt.compare l2 in \ - is_sorted ~cmp:CCInt.compare (sorted_diff_uniq ~cmp:CCInt.compare l1 l2)) - Q.(pair (list small_int) (list small_int)) (fun (l1, l2) -> \ - let l1 = List.sort CCInt.compare l1 in \ - let l2 = List.sort CCInt.compare l2 in \ - sorted_diff_uniq ~cmp:CCInt.compare l1 l2 = uniq_succ ~eq:CCInt.equal (sorted_diff ~cmp:CCInt.compare l1 l2)) -*) - let take n l = let rec direct i n l = match l with | [] -> [] @@ -1016,20 +601,6 @@ let take n l = in direct direct_depth_default_ n l -(*$T - take 2 [1;2;3;4;5] = [1;2] - take 10_000 (range 0 100_000) |> List.length = 10_000 - take 10_000 (range 0 2_000) = range 0 2_000 - take 300_000 (1 -- 400_000) = 1 -- 300_000 -*) - -(*$Q - (Q.pair (Q.list Q.small_int) Q.int) (fun (l,i) -> \ - let i = abs i in \ - let l1 = take i l in \ - List.length l1 <= i && ((List.length l1 = i) = (List.length l >= i))) -*) - let rec drop n l = match l with | [] -> [] | _ when n=0 -> l @@ -1039,20 +610,8 @@ let hd_tl = function | [] -> failwith "hd_tl" | x :: l -> x, l -(*$T - try ignore (hd_tl []); false with Failure _ -> true - hd_tl [1;2;3] = (1, [2;3]) -*) - let take_drop n l = take n l, drop n l -(*$Q - (Q.pair (Q.list Q.small_int) Q.int) (fun (l,i) -> \ - let i = abs i in \ - let l1, l2 = take_drop i l in \ - l1 @ l2 = l ) -*) - let sublists_of_len ?(last=fun _ -> None) ?offset n l = if n < 1 then invalid_arg "sublists_of_len: n must be > 0"; let offset = match offset with @@ -1075,28 +634,8 @@ let sublists_of_len ?(last=fun _ -> None) ?offset n l = in List.rev (aux [] l) -(*$= sublists_of_len as subs & ~printer:Q.Print.(list (list int)) - [[1;2;3]] (subs 3 [1;2;3;4]) - [[1;2]; [3;4]; [5;6]] (subs 2 [1;2;3;4;5;6]) - [] (subs 3 [1;2]) - [[1;2];[3;4]] (subs ~offset:2 2 [1;2;3;4]) - [[1;2];[2;3]] (subs ~offset:1 2 [1;2;3]) - [[1;2];[4;5]] (subs ~offset:3 2 [1;2;3;4;5;6]) - [[1;2;3];[4]] (subs ~last:CCOption.return 3 [1;2;3;4]) - [[1;2]; [3;4]] (subs 2 [1;2;3;4;5]) -*) - let chunks n l = sublists_of_len ~last:(fun x -> Some x) n l -(*$Q - Q.(small_list small_int) (fun l -> \ - l = (chunks 3 l |> List.flatten)) - Q.(small_list small_int) (fun l -> \ - l = (chunks 5 l |> List.flatten)) - Q.(small_list small_int) (fun l -> \ - List.for_all (fun u -> List.length u <= 5) (chunks 5 l)) -*) - let intersperse x l = let rec aux_direct i x l = match l with | [] -> [] @@ -1110,19 +649,6 @@ let intersperse x l = in aux_direct 1_000 x l -(*$= - [] (intersperse 0 []) - [1] (intersperse 0 [1]) - [1;0;2;0;3;0;4] (intersperse 0 [1;2;3;4]) -*) - -(*$Q - Q.(pair int (list int)) (fun (x,l) -> \ - length (intersperse x l) = (if length l <= 1 then length l else 2 * length l-1)) - Q.(pair int (list int)) (fun (x,l) -> \ - rev (intersperse x l) = intersperse x (rev l)) -*) - let interleave l1 l2 : _ list = let rec aux acc l1 l2 = match l1, l2 with | [], [] -> List.rev acc @@ -1133,18 +659,6 @@ let interleave l1 l2 : _ list = in aux [] l1 l2 -(*$= - [1;2;3;4;5] (interleave [1;3] [2;4;5]) - [1;2;3] (interleave [1] [2;3]) -*) - -(*$Q - Q.(pair (small_list int)(small_list int)) (fun (l1,l2) -> \ - length (interleave l1 l2) = length l1 + length l2) - Q.(small_list int) (fun l -> l = interleave [] l) - Q.(small_list int) (fun l -> l = interleave l []) -*) - let take_while p l = let rec direct i p l = match l with | [] -> [] @@ -1158,28 +672,10 @@ let take_while p l = in direct direct_depth_default_ p l -(*$T - take_while (fun x->x<10) (1 -- 20) = (1--9) - take_while (fun x->x <> 0) [0;1;2;3] = [] - take_while (fun _ -> true) [] = [] - take_while (fun _ -> true) (1--10) = (1--10) -*) - -(*$Q - Q.(pair (fun1 Observable.int bool) (list small_int)) (fun (f,l) -> \ - let l1 = take_while (Q.Fn.apply f) l in \ - List.for_all (Q.Fn.apply f) l1) -*) - let rec drop_while p l = match l with | [] -> [] | x :: l' -> if p x then drop_while p l' else l -(*$Q - Q.(pair (fun1 Observable.int bool) (list small_int)) (fun (f,l) -> \ - take_while (Q.Fn.apply f) l @ drop_while (Q.Fn.apply f) l = l) -*) - let take_drop_while p l = let rec direct i p l = match l with | [] -> [], [] @@ -1197,12 +693,6 @@ let take_drop_while p l = in direct direct_depth_default_ p l -(*$Q - Q.(pair (fun1 Observable.int bool) (list small_int)) (fun (f,l) -> \ - let l1,l2 = take_drop_while (Q.Fn.apply f) l in \ - (l1 = take_while (Q.Fn.apply f) l) && (l2 = drop_while (Q.Fn.apply f) l)) -*) - let last n l = let len = List.length l in if len < n then l else drop (len-n) l @@ -1215,39 +705,17 @@ let tail_opt = function | [] -> None | _ :: tail -> Some tail -(*$= & ~printer:Q.Print.(option (list int)) - (Some [2;3]) (tail_opt [1;2;3]) - (Some []) (tail_opt [1]) - None (tail_opt []) -*) - let rec last_opt = function | [] -> None | [x] -> Some x | _ :: tail -> last_opt tail -(*$= & ~printer:Q.Print.(option int) - (Some 1) (head_opt [1;2;3]) - (Some 1) (head_opt [1]) - None (head_opt []) - (Some 3) (last_opt [1;2;3]) - (Some 1) (last_opt [1]) - None (last_opt []) -*) - let find_pred = find_opt let find_pred_exn p l = match find_pred p l with | None -> raise Not_found | Some x -> x -(*$T - find_pred ((=) 4) [1;2;5;4;3;0] = Some 4 - find_pred (fun _ -> true) [] = None - find_pred (fun _ -> false) (1 -- 10) = None - find_pred (fun x -> x < 10) (1 -- 9) = Some 1 -*) - let find_mapi f l = let rec aux f i = function | [] -> None @@ -1261,11 +729,6 @@ let find_map f l = find_mapi (fun _ -> f) l let find_idx p l = find_mapi (fun i x -> if p x then Some (i, x) else None) l -(*$T - find_map (fun x -> if x=3 then Some "a" else None) [1;2;3;4] = Some "a" - find_map (fun x -> if x=3 then Some "a" else None) [1;2;4;5] = None -*) - let remove ~eq x l = let rec remove' eq x acc l = match l with | [] -> List.rev acc @@ -1274,11 +737,6 @@ let remove ~eq x l = in remove' eq x [] l -(*$T - remove ~eq:CCInt.equal ~key:1 [2;1;3;3;2;1] = [2;3;3;2] - remove ~eq:CCInt.equal ~key:10 [1;2;3] = [1;2;3] -*) - let filter_map f l = let rec recurse acc l = match l with | [] -> List.rev acc @@ -1287,15 +745,6 @@ let filter_map f l = recurse acc' l' in recurse [] l -(*$= - ["2"; "4"] \ - (filter_map (fun x -> if x mod 2 = 0 then Some (string_of_int x) else None) \ - [1;2;3;4;5]) - [ "2"; "4"; "6" ] \ - (filter_map (fun x -> if x mod 2 = 0 then Some (string_of_int x) else None) \ - [ 1; 2; 3; 4; 5; 6 ]) -*) - let keep_some l = filter_map (fun x->x) l let keep_ok l = @@ -1309,12 +758,6 @@ let all_some l = try Some (map (function Some x -> x | None -> raise Exit) l) with Exit -> None -(*$= - (Some []) (all_some []) - (Some [1;2;3]) (all_some [Some 1; Some 2; Some 3]) - None (all_some [Some 1; None; None; Some 4]) -*) - let all_ok l = let err = ref None in try @@ -1342,16 +785,6 @@ let group_by (type k) ?(hash=Hashtbl.hash) ?(eq=Stdlib.(=)) l = let join ~join_row s1 s2 : _ t = flat_map (fun a -> filter_map (join_row a) s2) s1 -(*$R - let s1 = (1 -- 3) in - let s2 = ["1"; "2"] in - let join_row i j = - if string_of_int i = j then Some (string_of_int i ^ " = " ^ j) else None - in - let s = join ~join_row s1 s2 in - OUnit2.assert_equal ["1 = 1"; "2 = 2"] s; -*) - let join_by (type a) ?(eq=Stdlib.(=)) ?(hash=Hashtbl.hash) f1 f2 ~merge c1 c2 = let module Tbl = Hashtbl.Make(struct type t = a let equal = eq let hash = hash end) in let tbl = Tbl.create 32 in @@ -1421,34 +854,12 @@ let group_join_by (type a) ?(eq=Stdlib.(=)) ?(hash=Hashtbl.hash) f c1 c2 = c2; Tbl.fold (fun k v l -> (k,v) :: l) tbl [] -(*$= - ['a', ["abc"; "attic"]; \ - 'b', ["barbary"; "boom"; "bop"]; \ - 'c', []] \ - (group_join_by (fun s->s.[0]) \ - (CCString.to_list "abc") \ - ["abc"; "boom"; "attic"; "deleted"; "barbary"; "bop"] \ - |> map (fun (c,l)->c,List.sort Stdlib.compare l) \ - |> sort Stdlib.compare) -*) - -(*$= - (Ok []) (all_ok []) - (Ok [1;2;3]) (all_ok [Ok 1; Ok 2; Ok 3]) - (Error "e2") (all_ok [Ok 1; Error "e2"; Error "e3"; Ok 4]) -*) - let mem ?(eq=Stdlib.(=)) x l = let rec search eq x l = match l with | [] -> false | y::l' -> eq x y || search eq x l' in search eq x l -(*$Q mem - Q.(small_list small_int) (fun l -> \ - mem 1 l = (List.mem 1 l)) -*) - let add_nodup ~eq x l = if mem ~eq x l then l else x::l @@ -1460,15 +871,6 @@ let remove_one ~eq x l = in if mem ~eq x l then remove_one ~eq x [] l else l -(*$Q - Q.(pair int (list int)) (fun (x,l) -> \ - remove_one ~eq:CCInt.equal x (add_nodup ~eq:CCInt.equal x l) = l) - Q.(pair int (list int)) (fun (x,l) -> \ - mem ~eq:CCInt.equal x l || List.length (add_nodup ~eq:CCInt.equal x l) = List.length l + 1) - Q.(pair int (list int)) (fun (x,l) -> \ - not (mem ~eq:CCInt.equal x l) || List.length (remove_one ~eq:CCInt.equal x l) = List.length l - 1) -*) - let subset ~eq l1 l2 = List.for_all (fun t -> mem ~eq t l2) @@ -1481,16 +883,6 @@ let uniq ~eq l = | x::xs -> uniq eq (x::acc) xs in uniq eq [] l -(*$T - uniq ~eq:CCInt.equal [1;2;3] |> List.sort Stdlib.compare = [1;2;3] - uniq ~eq:CCInt.equal [1;1;2;2;3;4;4;2;4;1;5] |> List.sort Stdlib.compare = [1;2;3;4;5] -*) - -(*$Q - Q.(small_list small_int) (fun l -> \ - sort_uniq ~cmp:CCInt.compare l = (uniq ~eq:CCInt.equal l |> sort Stdlib.compare)) -*) - let union ~eq l1 l2 = let rec union eq acc l1 l2 = match l1 with | [] -> List.rev_append acc l2 @@ -1498,10 +890,6 @@ let union ~eq l1 l2 = | x::xs -> union eq (x::acc) xs l2 in union eq [] l1 l2 -(*$T - union ~eq:CCInt.equal [1;2;4] [2;3;4;5] = [1;2;3;4;5] -*) - let inter ~eq l1 l2 = let rec inter eq acc l1 l2 = match l1 with | [] -> List.rev acc @@ -1509,10 +897,6 @@ let inter ~eq l1 l2 = | _::xs -> inter eq acc xs l2 in inter eq [] l1 l2 -(*$T - inter ~eq:CCInt.equal [1;2;4] [2;3;4;5] = [2;4] -*) - let mapi f l = let r = ref 0 in map @@ -1521,10 +905,6 @@ let mapi f l = incr r; y ) l -(*$T - mapi (fun i x -> i*x) [10;10;10] = [0;10;20] -*) - let iteri f l = let rec aux f i l = match l with | [] -> () @@ -1574,15 +954,6 @@ let get_at_idx i l = try Some (get_at_idx_exn i l) with Not_found -> None -(*$T - get_at_idx 0 (range 0 10) = Some 0 - get_at_idx 5 (range 0 10) = Some 5 - get_at_idx 11 (range 0 10) = None - get_at_idx (-1) (range 0 10) = Some 10 - get_at_idx 0 [] = None - get_at_idx (-1) [] = None -*) - let set_at_idx i x l0 = let rec aux l acc i = match l with | [] -> l0 @@ -1593,13 +964,6 @@ let set_at_idx i x l0 = let i = if i<0 then length l0 + i else i in aux l0 [] i -(*$T - set_at_idx 0 10 [1;2;3] = [10;2;3] - set_at_idx 4 10 [1;2;3] = [1;2;3] - set_at_idx 1 10 [1;2;3] = [1;10;3] - set_at_idx (-2) 10 [1;2;3] = [1;10;3] -*) - let insert_at_idx i x l = let rec aux l acc i x = match l with | [] -> List.rev_append acc [x] @@ -1610,13 +974,6 @@ let insert_at_idx i x l = let i = if i<0 then length l + i else i in aux l [] i x -(*$T - insert_at_idx 0 10 [1;2;3] = [10;1;2;3] - insert_at_idx 4 10 [1;2;3] = [1;2;3;10] - insert_at_idx 1 10 [1;2;3] = [1;10;2;3] - insert_at_idx (-2) 10 [1;2;3] = [1;10;2;3] -*) - let remove_at_idx i l0 = let rec aux l acc i = match l with | [] -> l0 @@ -1627,16 +984,6 @@ let remove_at_idx i l0 = let i = if i<0 then length l0 + i else i in aux l0 [] i -(*$T - remove_at_idx 0 [1;2;3;4] = [2;3;4] - remove_at_idx 3 [1;2;3;4] = [1;2;3] - remove_at_idx 5 [1;2;3;4] = [1;2;3;4] - remove_at_idx (-1) [1;2;3;4] = [1;2;3] - remove_at_idx (-2) [1;2;3;4] = [1;2;4] - remove_at_idx (-3) [1;2;3;4] = [1;3;4] - remove_at_idx (-4) [1;2;3;4] = [2;3;4] -*) - let range_by ~step i j = let rec range i j acc = if i=j then i::acc else range i (j-step) (j::acc) @@ -1648,27 +995,6 @@ let range_by ~step i j = else range i ((j-i)/step*step + i) [] -(* note: the last test checks that no error occurs due to overflows. *) -(*$T - range_by ~step:1 0 0 = [0] - range_by ~step:1 5 0 = [] - range_by ~step:2 1 0 = [] - range_by ~step:2 0 4 = [0;2;4] - range_by ~step:2 0 5 = [0;2;4] - range_by ~step:~-1 0 0 = [0] - range_by ~step:~-1 0 5 = [] - range_by ~step:~-2 0 1 = [] - range_by ~step:~-2 5 1 = [5;3;1] - range_by ~step:~-2 5 0 = [5;3;1] - range_by ~step:max_int 0 2 = [0] -*) - -(*$Q - Q.(pair small_int small_int) (fun (i,j) -> \ - let i = min i j and j = max i j in \ - range_by ~step:1 i j = range i j) -*) - let range i j = let rec up i j acc = if i=j then i::acc else up i (j-1) (j::acc) @@ -1677,54 +1003,21 @@ let range i j = in if i<=j then up i j [] else down i j [] -(*$T - range 0 5 = [0;1;2;3;4;5] - range 0 0 = [0] - range 5 2 = [5;4;3;2] -*) - let range' i j = if i \ - let l = (a--^b) in not (List.mem b l)) -*) - let replicate i x = let rec aux acc i = if i = 0 then acc else aux (x::acc) (i-1) in aux [] i - -(*$T - repeat 2 [1;2;3] = [1;2;3;1;2;3] -*) - -(*$Q - Q.(pair small_int (small_list int)) (fun (n,l) -> \ - if n>0 then repeat n l = flat_map (fun _ -> l) (1--n) \ - else Q.assume_fail()) -*) - let repeat i l = let rec aux acc i = if i = 0 then List.rev acc @@ -1745,13 +1038,6 @@ module Assoc = struct try Some (search_exn eq l x) with Not_found -> None - (*$T - Assoc.get ~eq:CCInt.equal 1 [1, "1"; 2, "2"] = Some "1" - Assoc.get ~eq:CCInt.equal 2 [1, "1"; 2, "2"] = Some "2" - Assoc.get ~eq:CCInt.equal 3 [1, "1"; 2, "2"] = None - Assoc.get ~eq:CCInt.equal 42 [] = None - *) - (* search for a binding for [x] in [l], and calls [f x (Some v) rest] or [f x None rest] depending on whether it finds the binding. [rest] is the list of the other bindings *) @@ -1766,39 +1052,16 @@ module Assoc = struct search_set eq [] l x ~f:(fun x _ l -> (x,y)::l) - (*$T - Assoc.set ~eq:CCInt.equal 2 "two" [1,"1"; 2, "2"] |> List.sort Stdlib.compare \ - = [1, "1"; 2, "two"] - Assoc.set ~eq:CCInt.equal 3 "3" [1,"1"; 2, "2"] |> List.sort Stdlib.compare \ - = [1, "1"; 2, "2"; 3, "3"] - *) - let mem ?(eq=Stdlib.(=)) x l = try ignore (search_exn eq l x); true with Not_found -> false - (*$T - Assoc.mem ~eq:CCInt.equal 1 [1,"1"; 2,"2"; 3, "3"] - not (Assoc.mem ~eq:CCInt.equal 4 [1,"1"; 2,"2"; 3, "3"]) - *) - let update ~eq f x l = search_set eq [] l x ~f:(fun x opt_y rest -> match f opt_y with | None -> rest (* drop *) | Some y' -> (x,y') :: rest) - (*$= - [1,"1"; 2,"22"] \ - (Assoc.update ~eq:CCInt.equal \ - ~f:(function Some "2" -> Some "22" | _ -> assert false) 2 [1,"1"; 2,"2"] |> lsort) - [1,"1"; 3,"3"] \ - (Assoc.update ~eq:CCInt.equal \ - ~f:(function Some "2" -> None | _ -> assert false) 2 [1,"1"; 2,"2"; 3,"3"] |> lsort) - [1,"1"; 2,"2"; 3,"3"] \ - (Assoc.update ~eq:CCInt.equal \ - ~f:(function None -> Some "3" | _ -> assert false) 3 [1,"1"; 2,"2"] |> lsort) - *) let remove ~eq x l = search_set eq [] l x @@ -1806,15 +1069,6 @@ module Assoc = struct | None -> l (* keep as is *) | Some _ -> rest) - (*$= - [1,"1"] \ - (Assoc.remove ~eq:CCInt.equal 2 [1,"1"; 2,"2"] |> lsort) - [1,"1"; 3,"3"] \ - (Assoc.remove ~eq:CCInt.equal 2 [1,"1"; 2,"2"; 3,"3"] |> lsort) - [1,"1"; 2,"2"] \ - (Assoc.remove ~eq:CCInt.equal 3 [1,"1"; 2,"2"] |> lsort) - *) - let keys l = map (fun (k, _) -> k) l let values l = map (fun (_, v) -> v) l @@ -1854,10 +1108,6 @@ module Ref = struct let push_list r l = r := List.rev_append l !r - - (*$T - let l = Ref.create() in Ref.push l 1; Ref.push_list l [2;3]; !l = [3;2;1] - *) end (** {2 Monadic Operations} *) @@ -1907,10 +1157,6 @@ type 'a random_gen = Random.State.t -> 'a let random_len len g st = init len (fun _ -> g st) -(*$T - random_len 10 CCInt.random_small (Random.State.make [||]) |> List.length = 10 -*) - let random g st = let len = Random.State.int st 1_000 in random_len len g st @@ -1933,14 +1179,6 @@ let to_string ?(start="") ?(stop="") ?(sep=", ") item_to_string l = let l = List.map item_to_string l in start ^ (String.concat sep l) ^ stop -(*$= to_string & ~printer:(fun s -> s) - (to_string string_of_int []) "" - (to_string ~start:"[" ~stop:"]" string_of_int []) "[]" - (to_string ~start:"[" ~stop:"]" string_of_int [1]) "[1]" - (to_string ~start:"[" ~stop:"]" string_of_int [1;2;3;4]) "[1, 2, 3, 4]" - (to_string ~sep:" " string_of_int [1;2;3;4]) "1 2 3 4" -*) - let to_iter l k = List.iter k l let rec to_seq l () = match l with @@ -1970,10 +1208,6 @@ let of_seq l = in direct direct_depth_default_ l -(*$Q - Q.(list int) (fun l -> of_iter (to_iter l) = l) -*) - let to_gen l = let l = ref l in fun () -> @@ -1994,10 +1228,6 @@ let of_gen g = in direct direct_depth_default_ g -(*$Q - Q.(list int) (fun l -> of_gen(to_gen l) = l) -*) - module Infix = struct let[@inline] (>|=) l f = map f l let[@inline] (>>=) l f = flat_map f l @@ -2036,11 +1266,3 @@ let pp ?(pp_start=fun _ () -> ()) ?(pp_stop=fun _ () -> ()) pp_start fmt (); print fmt l; pp_stop fmt () - -(*$= & ~printer:(fun s->s) - "[1, 2, 3]" \ - (CCFormat.to_string \ - (CCFormat.hbox(CCList.pp ~pp_start:(fun fmt () -> Format.fprintf fmt "[") \ - ~pp_stop:(fun fmt () -> Format.fprintf fmt "]") CCFormat.int)) \ - [1;2;3]) -*)