On this page:
test-random

2 RackUnit Integration

 (require (planet cce/fasttest:4:2/rackunit))

This module provides the test-random macro for generating RackUnit test suites that generate and record random inputs.

(test-random opt ...
  ([lhs rhs bind-opt ...]
   ...)
  body ...+)
 
opt = #:name name
  | #:repeat repeat
  | #:limit limit
     
bind-opt = #:where bind-where
  | #:limit bind-limit
The test-random form constructs a RackUnit test-suite that conducts multiple trials of body ...+. During a trial, each variable lhs is bound to the value of the corresponding expression rhs. Bindings proceed left-to-right with let*-scope, and each rhs is re-run to permit new random values.

The suite conducts repeat trials (default 100).

After each rhs is run, its corresponding where expression (if any) is run; if where evaluates to #t, the rhs is re-run. Each rhs is run at most the minimum of limit and the corresponding bind-limit, defaulting to 10000.

Examples:

> (require rackunit rackunit/text-ui)
> (random-seed 1)
> (run-tests
   (test-random
    ([x (random-integer)]
     [y (random-integer)])
    (check-pred integer? (+ x y))))

100 success(es) 0 failure(s) 0 error(s) 100 test(s) run

0

> (random-seed 1)
> (run-tests
   (test-random #:name "test w/ options"
                #:limit 4
                #:repeat 5
    ([x (random-integer)]
     [y (random-integer)])
    (check-pred positive? (+ x y))))

--------------------

test w/ options > Trial #1

Trial #1

FAILURE

name:       check-pred

location:   eval:119:0

params:     #<procedure:positive?>

-158

x:          -165

y:          7

Check failure

 === context ===

/Users/cce/git/racket/collects/rackunit/private/check.rkt:122:29

/Users/cce/git/racket/collects/rackunit/private/check.rkt:108:21: core50

/Users/cce/git/racket/collects/rackunit/private/result.rkt:99:3

/Users/cce/git/racket/collects/rackunit/private/test-suite.rkt:28:2

/Users/cce/git/planet/fasttest/rackunit.rkt:74:8: for-loop

the-tests

/Users/cce/git/racket/collects/rackunit/private/test-suite.rkt:60:0: apply-test-suite

/Users/cce/git/racket/collects/rackunit/text-ui.rkt:218:0: core14

/Users/cce/git/racket/collects/racket/private/more-scheme.rkt:151:2: call-with-break-parameterization

/Users/cce/git/racket/collects/racket/sandbox.rkt:746:5: loop

--------------------

--------------------

test w/ options > Trial #3

Trial #3

FAILURE

name:       check-pred

location:   eval:119:0

params:     #<procedure:positive?>

-651

x:          -688

y:          37

Check failure

 === context ===

/Users/cce/git/racket/collects/rackunit/private/check.rkt:122:29

/Users/cce/git/racket/collects/rackunit/private/check.rkt:108:21: core50

/Users/cce/git/racket/collects/rackunit/private/result.rkt:99:3

/Users/cce/git/racket/collects/rackunit/private/test-suite.rkt:28:2

/Users/cce/git/planet/fasttest/rackunit.rkt:74:8: for-loop

the-tests

/Users/cce/git/racket/collects/rackunit/private/test-suite.rkt:60:0: apply-test-suite

/Users/cce/git/racket/collects/rackunit/text-ui.rkt:218:0: core14

/Users/cce/git/racket/collects/racket/private/more-scheme.rkt:151:2: call-with-break-parameterization

/Users/cce/git/racket/collects/racket/sandbox.rkt:746:5: loop

--------------------

--------------------

test w/ options > Trials #5 to #5 (unsatisfied preconditions)

Trials #5 to #5 (unsatisfied preconditions)

FAILURE

name:       fail

location:   rackunit.rkt:78:14

params:     

message:    "Out of 4 attempts to generate input data for 5 trials, only 4 satisfied the preconditions.\n\nFailed input data sets include:\n\n"

Check failure

 === context ===

/Users/cce/git/racket/collects/rackunit/private/check.rkt:122:29

/Users/cce/git/racket/collects/rackunit/private/check.rkt:108:21: core185

/Users/cce/git/racket/collects/rackunit/private/result.rkt:99:3

/Users/cce/git/racket/collects/rackunit/private/test-suite.rkt:28:2

/Users/cce/git/planet/fasttest/rackunit.rkt:74:8: for-loop

the-tests

/Users/cce/git/racket/collects/rackunit/private/test-suite.rkt:60:0: apply-test-suite

/Users/cce/git/racket/collects/rackunit/text-ui.rkt:218:0: core14

/Users/cce/git/racket/collects/racket/private/more-scheme.rkt:151:2: call-with-break-parameterization

/Users/cce/git/racket/collects/racket/sandbox.rkt:746:5: loop

--------------------

2 success(es) 3 failure(s) 0 error(s) 5 test(s) run

3

> (random-seed 1)
> (run-tests
   (test-random #:name "test w/ bind options"
                #:repeat 4
    ([x (random-integer)
        #:where (odd? x)
        #:limit 4]
     [y (random-integer)
        #:where (odd? y)
        #:limit 4])
    (check-pred even? (+ x y))))

--------------------

test w/ bind options > Trials #3 to #4 (unsatisfied preconditions)

Trials #3 to #4 (unsatisfied preconditions)

FAILURE

name:       fail

location:   rackunit.rkt:78:14

params:     

message:    "Out of 1000 attempts to generate input data for 4 trials, only 2 satisfied the preconditions.\n\nFailed input data sets include:\n\nx = -123\ny = 2716\n\nx = -123\ny = 886\n\nx = -688\n"

Check failure

 === context ===

/Users/cce/git/racket/collects/rackunit/private/check.rkt:122:29

/Users/cce/git/racket/collects/rackunit/private/check.rkt:108:21: core185

/Users/cce/git/racket/collects/rackunit/private/result.rkt:99:3

/Users/cce/git/racket/collects/rackunit/private/test-suite.rkt:28:2

/Users/cce/git/planet/fasttest/rackunit.rkt:74:8: for-loop

the-tests

/Users/cce/git/racket/collects/rackunit/private/test-suite.rkt:60:0: apply-test-suite

/Users/cce/git/racket/collects/rackunit/text-ui.rkt:218:0: core14

/Users/cce/git/racket/collects/racket/private/more-scheme.rkt:151:2: call-with-break-parameterization

/Users/cce/git/racket/collects/racket/sandbox.rkt:746:5: loop

--------------------

2 success(es) 1 failure(s) 0 error(s) 3 test(s) run

1