stedolan / crowbar

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

Better range function #35

Open FooB4r opened 6 years ago

FooB4r commented 6 years ago

When generating characters from their ASCII code if we want every char except the controls chars

Controls char: from 0 to 31 and 127

map [choose [range 32; const 127]] Char.chr

Non controls char:

choose [
  map [range 94] (fun n -> n + 32); (* 32 -> 126 *)
  map [range 127] (fun n -> n + 128)] (*128 -> 255*)
]

I suggest instead:

choose [range 32 127; range 128 256]

Same for alphanumeric, and all sets of contiguous values in ASCII.

stedolan commented 6 years ago

OK, sounds like the right thing to do is to replace:

val range : int -> int gen

with

val range : ?min:int -> lim:int -> int gen

so that

range 10
range ~lim:10
range ~min:5 ~lim:10

all work.

I'm not quite sure what the second argument should be called. range uses half-open intervals (exclusive of the upper bound), so max is the wrong word.

FooB4r commented 6 years ago

You can either name it ubound (which is the mathematical term for [min; ubound[) Another solution is to have n as in "n elements" which is more computer science and range min n would give [min; min+n[ val range : ?min:int -> n:int -> int gen