| 1 |
;; Copyright © Marc Nieper-Wißkirchen (2020). |
| 2 |
|
| 3 |
;; Permission is hereby granted, free of charge, to any person |
| 4 |
;; obtaining a copy of this software and associated documentation |
| 5 |
;; files (the "Software"), to deal in the Software without |
| 6 |
;; restriction, including without limitation the rights to use, copy, |
| 7 |
;; modify, merge, publish, distribute, sublicense, and/or sell copies |
| 8 |
;; of the Software, and to permit persons to whom the Software is |
| 9 |
;; furnished to do so, subject to the following conditions: |
| 10 |
|
| 11 |
;; The above copyright notice and this permission notice shall be |
| 12 |
;; included in all copies or substantial portions of the Software. |
| 13 |
|
| 14 |
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 15 |
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 |
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 17 |
;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 18 |
;; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 19 |
;; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 20 |
;; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 |
;; SOFTWARE. |
| 22 |
|
| 23 |
(define-library (srfi 210 test) |
| 24 |
(export run-tests) |
| 25 |
(import (scheme base) |
| 26 |
(srfi 210) |
| 27 |
(srfi 64) |
| 28 |
(srfi 195)) |
| 29 |
(begin |
| 30 |
(define-syntax test-values |
| 31 |
(syntax-rules () |
| 32 |
((test-values (expected ...) test-expr) |
| 33 |
(test-equal (list expected ...) (list/mv test-expr))) |
| 34 |
((test-values test-name (expected ...) test-expr) |
| 35 |
(test-equal test-name (list expected ...) (list/mv test-expr))))) |
| 36 |
|
| 37 |
(define (run-tests) |
| 38 |
(test-begin "SRFI 210") |
| 39 |
|
| 40 |
(test-equal "ab" (apply/mv string (values #\a #\b))) |
| 41 |
(test-equal "abc" (apply/mv string #\a (values #\b #\c))) |
| 42 |
(test-equal "abcd" (apply/mv string #\a #\b (values #\c #\d))) |
| 43 |
|
| 44 |
(test-equal "" (call/mv string)) |
| 45 |
(test-equal "abc" (call/mv string (values #\a #\b #\c))) |
| 46 |
(test-equal "abcd" (call/mv string (values #\a #\b) (values #\c #\d))) |
| 47 |
(test-equal "abcde" (call/mv string (values #\a #\b) #\c (values #\d #\e))) |
| 48 |
|
| 49 |
(test-equal '(a) (list/mv 'a)) |
| 50 |
(test-equal '(a b) (list/mv (values 'a 'b))) |
| 51 |
(test-equal '(a b c) (list/mv 'a (values 'b 'c))) |
| 52 |
(test-equal '(a b c d) (list/mv 'a 'b (values 'c 'd))) |
| 53 |
|
| 54 |
(test-equal '#(a) (vector/mv 'a)) |
| 55 |
(test-equal '#(a b) (vector/mv (values 'a 'b))) |
| 56 |
(test-equal '#(a b c) (vector/mv 'a (values 'b 'c))) |
| 57 |
(test-equal '#(a b c d) (vector/mv 'a 'b (values 'c 'd))) |
| 58 |
|
| 59 |
(test-values () (unbox (box/mv (values)))) |
| 60 |
(test-values ('a) (unbox (box/mv 'a))) |
| 61 |
(test-values ('a 'b) (unbox (box/mv (values 'a 'b)))) |
| 62 |
(test-values ('a 'b 'c) (unbox (box/mv 'a (values 'b 'c)))) |
| 63 |
(test-values ('a 'b 'c 'd) (unbox (box/mv 'a 'b (values 'c 'd)))) |
| 64 |
|
| 65 |
(test-equal 'a (value/mv 0 (values 'a 'b))) |
| 66 |
(test-equal 'b (value/mv 1 (values 'a 'b))) |
| 67 |
(test-equal 'a (value/mv 0 'a (values 'b 'c))) |
| 68 |
(test-equal 'b (value/mv 1 'a (values 'b 'c))) |
| 69 |
(test-equal 'c (value/mv 2 'a (values 'b 'c))) |
| 70 |
|
| 71 |
(test-equal 0 (coarity (values))) |
| 72 |
(test-equal 1 (coarity 'a)) |
| 73 |
(test-equal 3 (coarity (values 'a 'b 'c))) |
| 74 |
|
| 75 |
(test-equal '(a (b)) |
| 76 |
(let ((x #f) (y #f)) |
| 77 |
(set!-values (x . y) (values 'a 'b)) |
| 78 |
(list x y))) |
| 79 |
(test-equal '(a b) |
| 80 |
(let ((x #f)) |
| 81 |
(set!-values x (values 'a 'b)) |
| 82 |
x)) |
| 83 |
(test-equal '(a b) |
| 84 |
(let ((x #f) (y #f)) |
| 85 |
(set!-values (x y) (values 'a 'b)) |
| 86 |
(list x y))) |
| 87 |
(test-equal '(a b ()) |
| 88 |
(let ((x #f) (y #f) (z #f)) |
| 89 |
(set!-values (x y . z) (values 'a 'b)) |
| 90 |
(list x y z))) |
| 91 |
|
| 92 |
(test-equal 5 |
| 93 |
(with-values (values 4 5) |
| 94 |
(lambda (a b) b))) |
| 95 |
|
| 96 |
(test-equal '(a (b)) |
| 97 |
(case-receive (values 'a 'b) |
| 98 |
((x) #f) |
| 99 |
((x . y) (list x y)))) |
| 100 |
|
| 101 |
(test-values (3 5 7) (bind/mv (values 1 2 3) |
| 102 |
(map-values (lambda (x) (* 2 x))) |
| 103 |
(map-values (lambda (x) (+ 1 x))))) |
| 104 |
|
| 105 |
(test-values ('a 'b 'c) |
| 106 |
(list-values '(a b c))) |
| 107 |
|
| 108 |
(test-values ('a 'b 'c) |
| 109 |
(vector-values #(a b c))) |
| 110 |
|
| 111 |
(test-values ('a 'b 'c) |
| 112 |
(box-values (box 'a 'b 'c))) |
| 113 |
|
| 114 |
(test-equal 'b (value 1 'a 'b 'c)) |
| 115 |
|
| 116 |
(test-values (1 2 3) (identity 1 2 3)) |
| 117 |
|
| 118 |
(test-values (3 5 7) |
| 119 |
(let ((f (map-values (lambda (x) (* 2 x)))) |
| 120 |
(g (map-values (lambda (x) (+ x 1))))) |
| 121 |
((compose-left f g) 1 2 3))) |
| 122 |
(test-values (-3 -5 -7) |
| 123 |
(let ((f (map-values (lambda (x) (* 2 x)))) |
| 124 |
(g (map-values (lambda (x) (+ x 1)))) |
| 125 |
(h (map-values (lambda (x) (- x))))) |
| 126 |
((compose-left f g h) 1 2 3))) |
| 127 |
|
| 128 |
(test-values (4 6 8) |
| 129 |
(let ((f (map-values (lambda (x) (* 2 x)))) |
| 130 |
(g (map-values (lambda (x) (+ x 1))))) |
| 131 |
((compose-right f g) 1 2 3))) |
| 132 |
(test-values (0 -2 -4) |
| 133 |
(let ((f (map-values (lambda (x) (* 2 x)))) |
| 134 |
(g (map-values (lambda (x) (+ x 1)))) |
| 135 |
(h (map-values (lambda (x) (- x))))) |
| 136 |
((compose-right f g h) 1 2 3))) |
| 137 |
|
| 138 |
(test-values (#t #f #t) ((map-values odd?) 1 2 3)) |
| 139 |
|
| 140 |
(test-values (3 6 9) |
| 141 |
(bind/list '(1 2 3) (map-values (lambda (x) (* 3 x))))) |
| 142 |
(test-values (4 7 10) |
| 143 |
(bind/list '(1 2 3) |
| 144 |
(map-values (lambda (x) (* 3 x))) |
| 145 |
(map-values (lambda (x) (+ x 1))))) |
| 146 |
|
| 147 |
(test-values (3 6 9) |
| 148 |
(bind/box (box 1 2 3) (map-values (lambda (x) (* 3 x))))) |
| 149 |
|
| 150 |
(test-values (3 2) |
| 151 |
(bind 1 (lambda (x) (values (* 3 x) (+ 1 x))))) |
| 152 |
|
| 153 |
(test-end)))) |