Skip to content

Instantly share code, notes, and snippets.

@takikawa
Last active August 29, 2015 13:57
Show Gist options
  • Save takikawa/9414362 to your computer and use it in GitHub Desktop.
Save takikawa/9414362 to your computer and use it in GitHub Desktop.
#lang racket/base
(require quickcheck
typed-racket/base-env/base-types
typed-racket/base-env/base-types-extra
typed-racket/base-env/case-lambda
(for-syntax racket/base
racket/lazy-require
racket/match
racket/syntax
syntax/parse
typed-racket/base-env/annotate-classes
typed-racket/private/parse-type
typed-racket/private/syntax-properties
typed-racket/rep/type-rep
typed-racket/standard-inits))
(provide tr-quickcheck)
(begin-for-syntax
;; not sure why this is necessary
(do-standard-inits)
(define (type->generator type)
(match type
[(Base: 'String _ _ _) #'arbitrary-string]
[(Base: 'Char _ _ _) #'arbitrary-char]
[(Base: 'Integer _ _ _) #'arbitrary-integer]
[(Base: 'Symbol _ _ _) #'arbitrary-symbol]
[(Vector: elem) #`(arbitrary-vector #,(type->generator elem))]
[_ (raise-syntax-error
#f
(format "could not construct a generator for ~a" type))])))
(define-syntax (tr-quickcheck stx)
(syntax-parse stx
[(_ (f:optionally-annotated-formal ...) body ...)
(define/with-syntax (generator ...)
(for/list ([type (in-list (attribute f.ty))])
(type->generator (and type (parse-type type)))))
(ignore #`(quickcheck (property ([f.ann-name generator] ...)
body ...)))]))
#lang typed/racket
(require "quickcheck.rkt")
(tr-quickcheck ([x : String]) (< (string-length x) 10))
(tr-quickcheck ([x : (Vectorof String)]) (< (vector-length x) 10))
Falsifiable, after 28 tests:
x = "\u0001\u000E\u0002\u0006\t\f\u0002\u0005\u0005\u0005\f\v"
Falsifiable, after 28 tests:
x = #("\u000F\u0005\u0004\u0003\u0006\t\u000E\n\u0005\u0003" "\u0004\u0003\b\u000E\b\u0002\a\u0002\b\u0004\r\b" "\u0005\u0001\u0006\u0000\u0000\u0004\f\u0005\u000F\u0004\u0005\u0003\a\u0003\u000F" "\t\u000F\u0004\b\t\u0000\u000F" "\u0004\u0004\r\u0001\u000E\u0002\a\u0006\u0000\t\t" "\u0002\f\b\u0006\u0001\n\u0000\u0001\u0001\u0002\t\r\n\u0005" "\u000E\u0002\v\t\b\u0003\b\f\n\u0000\f\u0002" "\a\n\u000E\u0000\u0002\v\f\u0004" "\f\r\u0005\n\t\t\u0001\u0002\u0005\u0003\v\u000E\u0002\f\u0004" "\u000F\u000E\u000E\u0001\r\u0002\f\u000F\u000E\f" "\u0003\u000E" "\r\b\u0004\v\u000E\u0002\u0005\u000E\t\u0004\r")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment