[racket] QuickCheck-like testing library?

From: Michael Sperber (sperber at deinprogramm.de)
Date: Fri Aug 17 02:25:06 EDT 2012

Ismael Figueroa Palet <ifigueroap at gmail.com> writes:

> I copied the quickcheck folder to my project, but I don't know how to use
> it :-(
> do you have any examples of how to use it? do I need to copy other
> collects, or do I have to use the full deinprogramm language?

No, you don't.  It can be used as a library.

Here's a few examples I use for testing (which can be run using
`(quickcheck <property>)'):

(define rev-app/
  (property ((xs (arbitrary-list arbitrary-integer))
	     (ys (arbitrary-list arbitrary-integer)))
	    (equal? (reverse (append xs ys)) (append (reverse ys) (reverse xs)))))

(define broken-rev-app/
  (property ((xs (arbitrary-list arbitrary-integer))
	     (ys (arbitrary-list arbitrary-integer)))
	    (equal? (reverse (append ys xs)) (append (reverse ys) (reverse xs)))))

(define char->integer/
  (property ((ch arbitrary-char))
	    (char=? ch (integer->char (char->integer ch)))))

(define string->list/
  (property ((str arbitrary-string))
	    (string=? str (list->string (string->list str)))))

(define vector->list/
  (property ((vec (arbitrary-vector arbitrary-integer)))
	    (equal? vec (list->vector (vector->list vec)))))

(define map-append/
  (property ((proc (arbitrary-procedure arbitrary-integer arbitrary-char))
	     (xs (arbitrary-list arbitrary-char))
	     (ys (arbitrary-list arbitrary-char)))
	    (equal? (map proc (append xs ys))
		    (append (map proc xs) (map proc ys)))))

(define ttt/
  (property ((a (arbitrary-record cons (list car cdr) arbitrary-integer arbitrary-integer)))
	    (write (list 'aaa a)) (newline)
	    #t))

(define max-le/
  (property ((x arbitrary-integer)
	     (y arbitrary-integer))
    (==> (<= x y)
	 (= (max x y) y))))

(define insert1/
  (property ((x arbitrary-integer)
	     (xs (arbitrary-list arbitrary-integer)))
	    (==> (list-sorted? xs <)
		 (trivial (null? xs) 
			  (list-sorted? (insert < x xs) <)))))

(define insert2/
  (property ((x arbitrary-integer)
	     (xs (arbitrary-list arbitrary-integer)))
	    (==> (list-sorted? xs <)
		 (collect (length xs) 
			  (list-sorted? (insert < x xs) <)))))

(define (insert <= x xs)
  (cond
   ((null? xs) (list x))
   ((<= x (car xs))
    (cons x xs))
   (else
    (cons (car xs) (insert <= x (cdr xs))))))

; This only tests whether the coarbitrary of arbitrary-real works in
; acceptable time.
(define xxx/
  (property ((x arbitrary-real)
	     (p (arbitrary-procedure arbitrary-boolean arbitrary-real)))
	    (or (p x)
		(not (p x)))))

(define non-assoc/
  (property ((a arbitrary-real)
	     (b arbitrary-real)
	     (c arbitrary-real))
	    (= (+ (+ a b) c)
	       (+ a (+ b c)))))

-- 
Cheers =8-} Mike
Friede, Völkerverständigung und überhaupt blabla

Posted on the users mailing list.