stedolan / crowbar

Property fuzzing for OCaml
MIT License
180 stars 31 forks source link

Add char generator #53

Closed pascutto closed 4 years ago

pascutto commented 5 years ago

This PR adds a char : char gen generator. While one can easily get it out of a string gen using map, I think it's nice to have basic types directly available.

cfcs commented 5 years ago

Here's one for Uchar.t

let uchar : Uchar.t gen =
  let c_gen : int gen =
     choose [ range 0xD7FF
        (* skip 0xD800..DFFF since they're reserved for UTF-16 encoding: *)
         ; map [range (*~min:0xE000 *) (0x10FFFF-0xE000)]
             (fun x -> x+0xE000)]
  in map [c_gen] Uchar.of_int
  (* whole point of c_gen above was to able to use
      Uchar.unsafe_of_int to avoid the bounds checking *)

(note that range ?min would have avoided the extra map, but that was not available in my opam repo.)

stedolan commented 4 years ago

Merged (eventually), thanks. I've added both the char and uchar generators (although with a slightly different implementation for uchar)