art with code

2009-01-21

Revised quickcheck.ml syntax

Changed the quickcheck.ml syntax a bit to make it easier to pretty-print lists, arrays and tuples. Now you can use e.g. Q.int instead of Q.uig as a parameter to the list generator (though you need parens now.) Yesterday's example looks like this with the new syntax:

let reverse l =
let rec aux res l = match l with
| [] -> res
| (h::t) -> aux (h::res) t in
aux [] l
(**Q
(Q.list_of_size (fun () -> Random.int 2) Q.int) (fun l -> reverse l = l)
(Q.list Q.int) (fun l -> reverse (reverse l) = l)

(* Thanks to notfancy on reddit for the following law. *)
(* Demonstrating tuple generation *)
(Q.pair (Q.list Q.int) (Q.list Q.int)) (fun (l,m) -> reverse (l @ m) = reverse m @ reverse l)
**)

Saving that to reverse.ml and running the test extractor on it prints out:

open OUnit
module Q = Quickcheck
open Reverse

let _iteri f l = ignore (List.fold_left (fun i v -> f v i; i + 1) 0 l)
let _TL = _iteri (fun (n,b) i ->
OUnit.assert_bool ("Line " ^ string_of_int (i+1) ^ " of bool test: " ^ n) b)

let test_reverse_line_6 () =
Quickcheck.laws_exn
"(Q.list_of_size (fun () -> Random.int 2) Q.int) (fun l -> reverse l = l)"
(Q.list_of_size (fun () -> Random.int 2) Q.int) (fun l -> reverse l = l);
Quickcheck.laws_exn
"(Q.list Q.int) (fun l -> reverse (reverse l) = l)"
(Q.list Q.int) (fun l -> reverse (reverse l) = l);

(* Thanks to notfancy on reddit for the following law. *)
(* Demonstrating tuple generation *)
Quickcheck.laws_exn
"(Q.pair (Q.list Q.int) (Q.list Q.int)) (fun (l,m) -> reverse (l @ m) = reverse m @ reverse l)"
(Q.pair (Q.list Q.int) (Q.list Q.int)) (fun (l,m) -> reverse (l @ m) = reverse m @ reverse l);()


let suite = "Reverse unit tests" >:::
[
"test_reverse_line_6" >:: test_reverse_line_6
]

let () = Tests.register suite
Post a Comment

Blog Archive

About Me

My photo

Built art installations, web sites, graphics libraries, web browsers, mobile apps, desktop apps, media player themes, many nutty prototypes