| 1 |
;; Tests for property-test library. |
| 2 |
;; SPDX-License-Identifier: MIT |
| 3 |
;; Copyright 2024 Antero Mejr <[email protected]> |
| 4 |
|
| 5 |
;; Permission is hereby granted, free of charge, to any person |
| 6 |
;; obtaining a copy of this software and associated documentation |
| 7 |
;; files (the "Software"), to deal in the Software without |
| 8 |
;; restriction, including without limitation the rights to use, copy, |
| 9 |
;; modify, merge, publish, distribute, sublicense, and/or sell copies |
| 10 |
;; of the Software, and to permit persons to whom the Software is |
| 11 |
;; furnished to do so, subject to the following conditions: |
| 12 |
|
| 13 |
;; The above copyright notice and this permission notice shall be |
| 14 |
;; included in all copies or substantial portions of the Software. |
| 15 |
|
| 16 |
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 |
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 |
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 |
;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 20 |
;; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 21 |
;; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 |
;; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 |
;; SOFTWARE. |
| 24 |
|
| 25 |
(import (scheme base) |
| 26 |
(scheme complex) |
| 27 |
(scheme read) |
| 28 |
(srfi 1) |
| 29 |
(srfi 27) |
| 30 |
(srfi 64) |
| 31 |
(srfi 158) |
| 32 |
(srfi 194) |
| 33 |
(property-test)) |
| 34 |
(cond-expand ((library (srfi 36)) (import (srfi 36))) (else)) |
| 35 |
|
| 36 |
(test-begin "property-test") |
| 37 |
|
| 38 |
(define (three x) 3) |
| 39 |
(define (wrong-three x) x) |
| 40 |
(define (three-property x) (= (three x) 3)) |
| 41 |
(define (wrong-three-property x) (= (wrong-three x) 3)) |
| 42 |
(define (error-three-property x) (string-append 1 2)) |
| 43 |
(define (make-read-error x) (read (open-input-string (string-append ")" x)))) |
| 44 |
(define (make-read-error-property x) (symbol? (make-read-error x))) |
| 45 |
(define (bad-generator) (gmap (lambda (x) |
| 46 |
(string-append 1 2) |
| 47 |
x) |
| 48 |
(boolean-generator))) |
| 49 |
|
| 50 |
(test-group "test-property" |
| 51 |
(test-property three-property (list (integer-generator))) |
| 52 |
(test-property three-property (list (real-generator))) |
| 53 |
(test-property three-property (list (integer-generator)) 10)) |
| 54 |
|
| 55 |
(test-group "test-property-expect-fail" |
| 56 |
(test-property-expect-fail wrong-three-property (list (integer-generator))) |
| 57 |
(test-property-expect-fail wrong-three-property (list (integer-generator)) 10)) |
| 58 |
|
| 59 |
(test-group "test-property-skip" ; shouldn't run |
| 60 |
(test-property-skip three-property (list (bad-generator))) |
| 61 |
(test-property-skip three-property (list (bad-generator)) 10)) |
| 62 |
|
| 63 |
(test-group "test-property-error" |
| 64 |
(test-property-error error-three-property (list (integer-generator))) |
| 65 |
(test-property-error error-three-property (list (integer-generator)) 10)) |
| 66 |
|
| 67 |
(cond-expand |
| 68 |
((library (srfi 36)) |
| 69 |
(test-group "test-property-error-type" |
| 70 |
(test-property-error-type &read-error make-read-error-property |
| 71 |
(list (string-generator))))) |
| 72 |
(else)) |
| 73 |
|
| 74 |
(test-group "test-property/with-2-arguments" |
| 75 |
(test-property (lambda (x y) |
| 76 |
(and (boolean? x) (integer? y))) |
| 77 |
(list (boolean-generator) (integer-generator)))) |
| 78 |
|
| 79 |
;; Testing basic generators |
| 80 |
|
| 81 |
(test-group "boolean-generator" |
| 82 |
(test-property boolean? (list (boolean-generator)))) |
| 83 |
|
| 84 |
(test-group "bytevector-generator" |
| 85 |
(test-property bytevector? (list (bytevector-generator)))) |
| 86 |
|
| 87 |
(test-group "char-generator" |
| 88 |
(test-property char? (list (char-generator)))) |
| 89 |
|
| 90 |
(test-group "string-generator" |
| 91 |
(test-property string? (list (string-generator)))) |
| 92 |
|
| 93 |
(test-group "symbol-generator" |
| 94 |
(test-property symbol? (list (symbol-generator)))) |
| 95 |
|
| 96 |
;; Testing exact generators |
| 97 |
|
| 98 |
(cond-expand |
| 99 |
(exact-complex |
| 100 |
(test-group "exact-complex-generator" |
| 101 |
(test-property (lambda (x) |
| 102 |
(and (complex? x) |
| 103 |
(exact? (real-part x)) |
| 104 |
(exact? (imag-part x)))) |
| 105 |
(list (exact-complex-generator))))) |
| 106 |
(else)) |
| 107 |
|
| 108 |
(test-group "exact-integer-generator" |
| 109 |
(test-property (lambda (x) |
| 110 |
(and (integer? x) (exact? x))) |
| 111 |
(list (exact-integer-generator)))) |
| 112 |
|
| 113 |
(test-group "exact-number-generator" |
| 114 |
(test-property exact? (list (exact-number-generator)))) |
| 115 |
|
| 116 |
(test-group "exact-rational-generator" |
| 117 |
(test-property (lambda (x) |
| 118 |
(and (exact? x) (rational? x))) |
| 119 |
(list (exact-rational-generator)))) |
| 120 |
|
| 121 |
(test-group "exact-real-generator" |
| 122 |
(test-property (lambda (x) |
| 123 |
(and (exact? x) (real? x))) |
| 124 |
(list (exact-real-generator)))) |
| 125 |
|
| 126 |
(test-group "exact-integer-complex-generator" |
| 127 |
(cond-expand |
| 128 |
(exact-complex |
| 129 |
(test-property (lambda (x) |
| 130 |
(and (complex? x) |
| 131 |
(exact? (real-part x)) |
| 132 |
(exact? (imag-part x)) |
| 133 |
(integer? (real-part x)) |
| 134 |
(integer? (imag-part x)))) |
| 135 |
(list (exact-integer-complex-generator)))) |
| 136 |
(else))) |
| 137 |
|
| 138 |
;; Testing inexact generators |
| 139 |
|
| 140 |
(test-group "inexact-complex-generator" |
| 141 |
(test-property (lambda (x) |
| 142 |
(and (complex? x) |
| 143 |
(inexact? (real-part x)) |
| 144 |
(inexact? (imag-part x)))) |
| 145 |
(list (inexact-complex-generator)))) |
| 146 |
|
| 147 |
(test-group "inexact-integer-generator" |
| 148 |
(test-property (lambda (x) |
| 149 |
(and (inexact? x) (integer? x))) |
| 150 |
(list (inexact-integer-generator)))) |
| 151 |
|
| 152 |
(test-group "inexact-number-generator" |
| 153 |
(test-property inexact? (list (inexact-number-generator)))) |
| 154 |
|
| 155 |
(test-group "inexact-rational-generator" |
| 156 |
(test-property (lambda (x) |
| 157 |
(and (inexact? x) (rational? x))) |
| 158 |
(list (inexact-rational-generator)))) |
| 159 |
|
| 160 |
(test-group "inexact-integer-generator" |
| 161 |
(test-property (lambda (x) |
| 162 |
(and (inexact? x) (real? x))) |
| 163 |
(list (inexact-real-generator)))) |
| 164 |
|
| 165 |
;; Testing union generators |
| 166 |
|
| 167 |
(test-group "complex-generator" |
| 168 |
(test-property complex? (list (complex-generator)))) |
| 169 |
|
| 170 |
(test-group "integer-generator" |
| 171 |
(test-property integer? (list (integer-generator)))) |
| 172 |
|
| 173 |
(test-group "number-generator" |
| 174 |
(test-property number? (list (number-generator)))) |
| 175 |
|
| 176 |
(test-group "rational-generator" |
| 177 |
(test-property rational? (list (rational-generator)))) |
| 178 |
|
| 179 |
(test-group "real-generator" |
| 180 |
(test-property real? (list (real-generator)))) |
| 181 |
|
| 182 |
;; Testing special generators |
| 183 |
|
| 184 |
(test-group "list-generator-of" |
| 185 |
(test-property (lambda (x) |
| 186 |
(and (list? x) (<= (length x) 1001) |
| 187 |
(every integer? x))) |
| 188 |
(list (list-generator-of (integer-generator))))) |
| 189 |
|
| 190 |
(test-group "pair-generator-of" |
| 191 |
(test-property (lambda (x) |
| 192 |
(pair? x) (integer? (car x)) (boolean? (cdr x))) |
| 193 |
(list (pair-generator-of (integer-generator) |
| 194 |
(boolean-generator))))) |
| 195 |
|
| 196 |
(test-group "procedure-generator-of" |
| 197 |
(test-property (lambda (x) |
| 198 |
(procedure? x) (integer? (x))) |
| 199 |
(list (procedure-generator-of (integer-generator))))) |
| 200 |
|
| 201 |
(test-group "vector-generator-of" |
| 202 |
(test-property (lambda (x) |
| 203 |
(vector? x) |
| 204 |
(<= (vector-length x) 1001) |
| 205 |
(every integer? (vector->list x))) |
| 206 |
(list (vector-generator-of (integer-generator))))) |
| 207 |
|
| 208 |
(test-group "non-determinism" |
| 209 |
(let ((gen1 (gdrop (exact-number-generator) 30)) ;skip the initial sequence |
| 210 |
(gen2 (gdrop (exact-number-generator) 30))) |
| 211 |
(test-property (lambda (x y) |
| 212 |
(not (= x y))) |
| 213 |
(list gen1 gen2)))) |
| 214 |
|
| 215 |
(test-group "determinism" |
| 216 |
(parameterize ((current-random-source (make-random-source))) |
| 217 |
(let ((gen1 (gdrop (exact-number-generator) 30))) |
| 218 |
(parameterize ((current-random-source (make-random-source))) |
| 219 |
(let ((gen2 (gdrop (exact-number-generator) 30))) |
| 220 |
(test-property = (list gen1 gen2))))))) |
| 221 |
|
| 222 |
(test-end) |