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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
module Test = (val Containers_testlib.make ~__FILE__ ())
open Test
open Containers_pvec
let spf = Printf.sprintf
let _listuniq =
let g = Q.(small_list (pair small_int small_int)) in
Q.map_same_type
(fun l ->
CCList.sort_uniq ~cmp:(fun a b -> Stdlib.compare (fst a) (fst b)) l)
g
;;
t @@ fun () -> is_empty empty;;
t @@ fun () -> not (is_empty (return 2));;
t @@ fun () -> length (return 2) = 1;;
q ~name:"get over of_list" _listuniq (fun l ->
let m = of_list l in
List.for_all (fun (i, y) -> get m i = y) @@ List.mapi CCPair.make l)
;;
(* regression test for #298 *)
t ~name:"reg 298" @@ fun () ->
let rec consume x =
match pop_opt x with
| None -> ()
| Some (_, x) -> consume x
in
consume (of_list CCList.(1 -- 100));
true
;;
q ~name:"push length pop"
Q.(pair int (small_list int))
(fun (x, l) ->
let q0 = of_list l in
let q = push q0 x in
assert_equal (length q) (length q0 + 1);
let y, q = pop q in
assert_equal x y;
assert_equal (to_list q) (to_list q0);
true)
;;
q
Q.(pair (fun1 Observable.int bool) (small_list int))
(fun (f, l) ->
let f = Q.Fn.apply f in
List.map f l = (of_list l |> map f |> to_list))
;;
q
Q.(pair (small_list int) (small_list int))
(fun (l1, l2) -> l1 @ l2 = (append (of_list l1) (of_list l2) |> to_list))
;;
q Q.(small_list int) (fun l -> l = to_list (of_list l));;
q _listuniq (fun l ->
List.sort Stdlib.compare l
= (l |> Iter.of_list |> of_iter |> to_iter |> Iter.to_list
|> List.sort Stdlib.compare))
;;
q _listuniq (fun l ->
List.sort Stdlib.compare l
= (l |> CCSeq.of_list |> of_seq |> to_seq |> CCSeq.to_list
|> List.sort Stdlib.compare))
;;
t @@ fun () -> choose empty = None;;
t @@ fun () -> choose (of_list [ 1, 1; 2, 2 ]) <> None;;
q
Q.(pair (small_list int) (small_list int))
(fun (l1, l2) -> equal CCInt.equal (of_list l1) (of_list l2) = (l1 = l2))
;;
q Q.(small_list int) (fun l1 -> equal CCInt.equal (of_list l1) (of_list l1))
let arb_list_with_idx =
let open Q in
let shrink (l, i) =
Iter.(Shrink.(list ~shrink:int l) >|= fun l -> l, min i (List.length l - 1))
in
let gen =
Gen.(
let* l = small_list int in
let+ i =
if l = [] then
return 0
else
0 -- (List.length l - 1)
in
l, i)
in
make ~shrink ~print:Print.(pair (list int) int) gen
;;
q arb_list_with_idx (fun (l1, i) ->
if l1 <> [] then (
let l2 =
let x = List.nth l1 i in
CCList.set_at_idx i (x + 1) l1
in
not (equal CCInt.equal (of_list l1) (of_list l2))
) else
true)
module Ref_impl = struct
type +'a t = 'a list
let empty : _ t = []
let length = List.length
let push x l : _ t = l @ [ x ]
let get i l = List.nth l i
let to_list l = l
let to_seq = CCSeq.of_list
let add_list l l2 : _ t = List.append l l2
let to_list_via_reviter m =
let l = ref [] in
iter_rev (fun x -> l := x :: !l) m;
!l
let pop_exn l =
match List.rev l with
| x :: tl -> x, List.rev tl
| [] -> invalid_arg "empty"
let last_opt l =
if l = [] then
None
else
Some (List.nth l (List.length l - 1))
let is_empty l = l = []
let choose l =
match l with
| [] -> false
| _ :: _ -> true
end
let to_list_via_iter m =
let l = ref [] in
iter (fun x -> l := x :: !l) m;
List.rev !l
let to_list_via_reviter m =
let l = ref [] in
iter_rev (fun x -> l := x :: !l) m;
!l
module Op = struct
type 'a t =
| Push of 'a
| Pop
(* TODO: set *)
| Add_list of 'a list
| Check_get of int
| Check_choose
| Check_is_empty
| Check_len
| Check_to_list
| Check_iter
| Check_rev_iter
| Check_to_gen
| Check_last
let well_formed ops : bool =
let rec loop size = function
| [] -> true
| Push _ :: tl -> loop (size + 1) tl
| Pop :: tl -> size >= 0 && loop (size - 1) tl
| Add_list l :: tl -> loop (size + List.length l) tl
| Check_get x :: tl -> x < size && loop size tl
| Check_choose :: tl
| Check_is_empty :: tl
| Check_len :: tl
| Check_to_list :: tl
| Check_iter :: tl
| Check_rev_iter :: tl
| Check_last :: tl
| Check_to_gen :: tl ->
loop size tl
in
loop 0 ops
let show show_x (self : _ t) : string =
match self with
| Push x -> spf "push %s" (show_x x)
| Pop -> "pop"
| Add_list l -> spf "add_list [%s]" (String.concat ";" @@ List.map show_x l)
| Check_get i -> spf "check_get %d" i
| Check_choose -> "check_choose"
| Check_is_empty -> "check_is_empty"
| Check_len -> "check_len"
| Check_to_list -> "check_to_list"
| Check_iter -> "check_rev_iter"
| Check_rev_iter -> "check_rev_iter"
| Check_to_gen -> "check_to_gen"
| Check_last -> "check_last"
let shrink shrink_x (op : _ t) : _ Q.Iter.t =
let open Q.Shrink in
let open Q.Iter in
match op with
| Push x -> shrink_x x >|= fun x -> Push x
| Pop -> empty
| Add_list l -> list ~shrink:shrink_x l >|= fun x -> Add_list x
| Check_get _ | Check_choose | Check_is_empty | Check_len | Check_to_list
| Check_to_gen | Check_last | Check_rev_iter | Check_iter ->
empty
let shrink_l shrink_x : _ t list Q.Shrink.t =
Q.Shrink.list ~shrink:(shrink shrink_x) |> Q.Shrink.filter well_formed
type 'a op = 'a t
(* generate list of length [n] *)
let gen (gen_x : 'a Q.Gen.t) n : 'a t list Q.Gen.t =
let open Q.Gen in
let rec loop size n : 'a op list Q.Gen.t =
if n = 0 then
return []
else (
let op =
frequency
@@ List.flatten
[
[
(3, gen_x >|= fun x -> Push x, size + 1);
1, return (Check_choose, size);
1, return (Check_is_empty, size);
1, return (Check_to_list, size);
1, return (Check_to_gen, size);
1, return (Check_last, size);
1, return (Check_iter, size);
1, return (Check_rev_iter, size);
];
(if size > 0 then
[
1, return (Pop, size - 1);
(1, 0 -- (size - 1) >|= fun x -> Check_get x, size);
]
else
[]);
[
( 1,
small_list gen_x >|= fun l ->
Add_list l, size + List.length l );
];
]
in
op >>= fun (op, size) ->
loop size (n - 1) >>= fun tl -> return (op :: tl)
)
in
loop 0 n
end
let arb_ops_int : int Op.t list Q.arbitrary =
Q.make
~print:(fun o ->
spf "[%s]" @@ String.concat ";" @@ List.map (Op.show @@ spf "%d") o)
~shrink:(Op.shrink_l Q.Shrink.int)
Q.Gen.(0 -- 40 >>= fun len -> Op.gen small_int len)
let check_ops ~show_x (ops : 'a Op.t list) : unit =
let fail () =
Q.Test.fail_reportf "on list [%s]"
(String.concat ";" @@ List.map (Op.show show_x) ops)
in
let cur = ref empty in
let cur_ref = ref Ref_impl.empty in
List.iter
(fun (op : _ Op.t) ->
match op with
| Op.Push x ->
cur := push !cur x;
cur_ref := Ref_impl.push x !cur_ref
| Op.Pop ->
let x1, cur' = pop !cur in
cur := cur';
let x2, cur_ref' = Ref_impl.pop_exn !cur_ref in
cur_ref := cur_ref';
if x1 <> x2 then fail ()
| Op.Add_list l ->
cur := add_list !cur l;
cur_ref := Ref_impl.add_list !cur_ref l
| Op.Check_get i -> if get !cur i <> Ref_impl.get i !cur_ref then fail ()
| Op.Check_is_empty ->
if is_empty !cur <> Ref_impl.is_empty !cur_ref then fail ()
| Op.Check_len -> if length !cur <> Ref_impl.length !cur_ref then fail ()
| Op.Check_to_list ->
if to_list !cur <> Ref_impl.to_list !cur_ref then fail ()
| Op.Check_iter ->
if to_list_via_iter !cur <> Ref_impl.to_list !cur_ref then fail ()
| Op.Check_rev_iter ->
if to_list !cur <> Ref_impl.to_list !cur_ref then fail ()
| Op.Check_choose ->
if Option.is_some (choose !cur) <> Ref_impl.choose !cur_ref then fail ()
| Op.Check_last ->
if last_opt !cur <> Ref_impl.last_opt !cur_ref then fail ()
| Op.Check_to_gen ->
if
to_seq !cur |> CCSeq.to_list
<> (Ref_impl.to_seq !cur_ref |> CCSeq.to_list)
then
fail ())
ops;
()
let () =
q ~count:1000 ~name:"ops" ~long_factor:10 arb_ops_int (fun ops ->
check_ops ~show_x:(spf "%d") ops;
true)
|