ifigueroap / racket-quickcheck

Quickcheck Clone implemented in Racket
Other
31 stars 16 forks source link

choose-with-frequencies only chooses the first generator (or runs forever) #33

Open default-kramer opened 5 years ago

default-kramer commented 5 years ago

I might be misunderstanding what choose-with-frequencies does. I was trying to make a generator that produces integers or symbols at random. I did this:

#lang racket
(require quickcheck)

(define (gen-datum)
  (choose-with-frequencies
   (list
    (cons 1 (choose-integer 100 200))
    (cons 99 (choose-symbol choose-printable-ascii-char 10)))))

(quickcheck
 (property ([x (gen-datum)])
           (println x)
           #t))

But I get 100 integers, followed by "OK, passed 100 tests."

Also, if I reduce the frequency of the first option to 0, it runs forever.

Is this expected? Is it possible to make a generator that produces different kinds of values randomly?

ausimian commented 4 years ago

I just ran into this problem, the issue is a bug in the pick function where the recursive call fails to pass the cdr of the generator list, so ultimately always picks the first one. It also explains why passing 0 as the first frequency causes an infinite loop.

[Edit - I'll submit a PR]

ausimian commented 4 years ago

https://github.com/ifigueroap/racket-quickcheck/pull/37