Bogdanp / rackcheck

A property-based testing library for Racket.
28 stars 7 forks source link

Introduce a gen:hash alternative for mappings #13

Open priime0 opened 4 months ago

priime0 commented 4 months ago

Would it be worth adding an alternative to the gen:hash, gen:hasheq, and gen:hasheqv forms that doesn't require hardcoding the keys?

Motivation: One alternative use of a hashtable involves mapping from arbitrary keys to values. However, the current gen:hash implementation expects the hardcoding of keys, such that the generated hashtables all have the same keys. This current implementation is useful for generating hashtables that represent jsexprs of some constant structure but is quite limiting for the other use/purpose.

An example of this alternative use would be a mapping of string IDs to naturals (maybe representing a score of some type). However, generating something like this is cumbersome (lifting+modifying this from the examples):

(define gen:mapping
  (gen:let ([keys&vals (gen:list (gen:tuple (gen:string gen:char-alphanumeric) gen:natural) #:max-length 5)])
    (make-immutable-hasheq keys&vals)))

Introducing such a generator for the mappings, however, would make it considerably more concise (assume gen:hash* represents that generator):

(define gen:mapping (gen:hash* (gen:string gen:char-alphanumeric) gen:natural))

Could this just be implemented by the users? Yes, but currently my stance is that the library itself should support this.

Bogdanp commented 4 months ago

I agree that this would be nice to include. Would you like to make a PR?

priime0 commented 4 months ago

Yes, I can make a PR. Are the gen:hash*, gen:hasheq*, gen:hasheqv* names agreeable?

Bogdanp commented 4 months ago

Yep, those names are good.