| 1 |
;; Copyright (C) 2020 Free Software Foundation, Inc. |
| 2 |
;; |
| 3 |
;; This library is free software; you can redistribute it and/or |
| 4 |
;; modify it under the terms of the GNU Lesser General Public |
| 5 |
;; License as published by the Free Software Foundation; either |
| 6 |
;; version 3 of the License, or (at your option) any later version. |
| 7 |
;; |
| 8 |
;; This library is distributed in the hope that it will be useful, |
| 9 |
;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
;; Lesser General Public License for more details. |
| 12 |
;; |
| 13 |
;; You should have received a copy of the GNU Lesser General Public |
| 14 |
;; License along with this library; if not, write to the Free Software |
| 15 |
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 |
|
| 17 |
(define-module (test-srfi-171) |
| 18 |
#:use-module (test-suite lib) |
| 19 |
#:use-module (ice-9 hash-table) |
| 20 |
#:use-module (srfi srfi-171) |
| 21 |
#:use-module (srfi srfi-171 gnu) |
| 22 |
#:use-module (rnrs bytevectors) |
| 23 |
#:use-module ((rnrs hashtables) #:prefix rnrs:) |
| 24 |
#:use-module ((srfi srfi-69) #:prefix srfi:)) |
| 25 |
|
| 26 |
(define (add1 x) (+ x 1)) |
| 27 |
|
| 28 |
(define numeric-list (iota 5)) |
| 29 |
(define numeric-vec (list->vector numeric-list)) |
| 30 |
(define bv (list->u8vector numeric-list)) |
| 31 |
(define test-string "0123456789abcdef") |
| 32 |
(define list-of-chars (string->list test-string)) |
| 33 |
|
| 34 |
;; for testing all treplace variations |
| 35 |
(define replace-alist '((1 . s) (2 . c) (3 . h) (4 . e) (5 . m))) |
| 36 |
(define guile-hashtable (alist->hash-table replace-alist)) |
| 37 |
(define srfi69-hashtable (srfi:alist->hash-table replace-alist)) |
| 38 |
(define rnrs-hashtable (rnrs:make-eq-hashtable)) |
| 39 |
(rnrs:hashtable-set! rnrs-hashtable 1 's) |
| 40 |
(rnrs:hashtable-set! rnrs-hashtable 2 'c) |
| 41 |
(rnrs:hashtable-set! rnrs-hashtable 3 'h) |
| 42 |
(rnrs:hashtable-set! rnrs-hashtable 4 'e) |
| 43 |
(rnrs:hashtable-set! rnrs-hashtable 5 'm) |
| 44 |
(define (replace-function val) |
| 45 |
(case val |
| 46 |
((1) 's) |
| 47 |
((2) 'c) |
| 48 |
((3) 'h) |
| 49 |
((4) 'e) |
| 50 |
((5) 'm) |
| 51 |
(else val))) |
| 52 |
|
| 53 |
;; Test procedures for port-transduce |
| 54 |
;; broken out to properly close port |
| 55 |
(define (port-transduce-test) |
| 56 |
(let* ((port (open-input-string "0 1 2 3 4")) |
| 57 |
(res (equal? 15 (port-transduce (tmap add1) + read |
| 58 |
(open-input-string "0 1 2 3 4"))))) |
| 59 |
(close-port port) |
| 60 |
res)) |
| 61 |
(define (port-transduce-with-identity-test) |
| 62 |
(let* ((port (open-input-string "0 1 2 3 4")) |
| 63 |
(res (equal? 15 (port-transduce (tmap add1) |
| 64 |
+ |
| 65 |
0 |
| 66 |
read |
| 67 |
(open-input-string "0 1 2 3 4"))))) |
| 68 |
(close-port port) |
| 69 |
res)) |
| 70 |
|
| 71 |
(with-test-prefix "transducers" |
| 72 |
(pass-if "tmap" (equal? '(1 2 3 4 5) (list-transduce (tmap add1) |
| 73 |
rcons |
| 74 |
numeric-list))) |
| 75 |
|
| 76 |
(pass-if "tfilter" (equal? '(0 2 4) (list-transduce (tfilter even?) |
| 77 |
rcons |
| 78 |
numeric-list))) |
| 79 |
|
| 80 |
(pass-if "tfilter+tmap" (equal? |
| 81 |
'(1 3 5) |
| 82 |
(list-transduce (compose (tfilter even?) (tmap add1)) |
| 83 |
rcons |
| 84 |
numeric-list))) |
| 85 |
|
| 86 |
(pass-if "tfilter-map" |
| 87 |
(equal? '(1 3 5) |
| 88 |
(list-transduce (tfilter-map |
| 89 |
(lambda (x) |
| 90 |
(if (even? x) |
| 91 |
(+ x 1) |
| 92 |
#f))) |
| 93 |
rcons numeric-list))) |
| 94 |
|
| 95 |
(pass-if "tremove" |
| 96 |
(equal? (list-transduce (tremove char-alphabetic?) |
| 97 |
rcount |
| 98 |
list-of-chars) |
| 99 |
(string-transduce (tremove char-alphabetic?) |
| 100 |
rcount |
| 101 |
test-string))) |
| 102 |
|
| 103 |
(pass-if "treplace with alist" |
| 104 |
(equal? '(s c h e m e r o c k s) |
| 105 |
(list-transduce (treplace replace-alist) |
| 106 |
rcons |
| 107 |
'(1 2 3 4 5 4 r o c k s) ))) |
| 108 |
|
| 109 |
(pass-if "treplace with replace-function" |
| 110 |
(equal? '(s c h e m e r o c k s) |
| 111 |
(list-transduce (treplace replace-function) |
| 112 |
rcons |
| 113 |
'(1 2 3 4 5 4 r o c k s)))) |
| 114 |
|
| 115 |
|
| 116 |
(pass-if "treplace with guile hash-table" |
| 117 |
(equal? '(s c h e m e r o c k s) |
| 118 |
(list-transduce (treplace guile-hashtable) |
| 119 |
rcons |
| 120 |
'(1 2 3 4 5 4 r o c k s)))) |
| 121 |
|
| 122 |
(pass-if "treplace with srfi-69 hash-table" |
| 123 |
(equal? '(s c h e m e r o c k s) |
| 124 |
(list-transduce (treplace srfi69-hashtable) |
| 125 |
rcons |
| 126 |
'(1 2 3 4 5 4 r o c k s)))) |
| 127 |
|
| 128 |
(pass-if "treplace with rnrs hash-table" |
| 129 |
(equal? '(s c h e m e r o c k s) |
| 130 |
(list-transduce (treplace rnrs-hashtable) |
| 131 |
rcons |
| 132 |
'(1 2 3 4 5 4 r o c k s)))) |
| 133 |
|
| 134 |
(pass-if "ttake" |
| 135 |
(equal? 6 (list-transduce (ttake 4) + numeric-list))) |
| 136 |
|
| 137 |
(pass-if "tdrop" |
| 138 |
(equal? 7 (list-transduce (tdrop 3) + numeric-list))) |
| 139 |
|
| 140 |
(pass-if "tdrop-while" |
| 141 |
(equal? '(3 4) |
| 142 |
(list-transduce (tdrop-while (lambda (x) (< x 3))) |
| 143 |
rcons |
| 144 |
numeric-list))) |
| 145 |
|
| 146 |
(pass-if "ttake-while" |
| 147 |
(equal? '(0 1 2) |
| 148 |
(list-transduce (ttake-while (lambda (x) (< x 3))) |
| 149 |
rcons |
| 150 |
numeric-list))) |
| 151 |
|
| 152 |
(pass-if "tconcatenate" |
| 153 |
(equal? '(0 1 2 3 4) (list-transduce tconcatenate |
| 154 |
rcons |
| 155 |
'((0 1) (2 3) (4))))) |
| 156 |
|
| 157 |
(pass-if "tappend-map" |
| 158 |
(equal? '(1 2 2 4 3 6) |
| 159 |
(list-transduce (tappend-map (lambda (x) (list x (* x 2)))) |
| 160 |
rcons |
| 161 |
'(1 2 3)))) |
| 162 |
|
| 163 |
(pass-if "tdelete-neighbor-duplicates" |
| 164 |
(equal? '(1 2 1 2 3) |
| 165 |
(list-transduce (tdelete-neighbor-duplicates) |
| 166 |
rcons |
| 167 |
'(1 1 1 2 2 1 2 3 3)))) |
| 168 |
|
| 169 |
(pass-if "tdelete-neighbor-duplicates with equality predicate" |
| 170 |
(equal? '(a b c "hej" "hej") |
| 171 |
(list-transduce (tdelete-neighbor-duplicates eq?) |
| 172 |
rcons |
| 173 |
(list 'a 'a 'b 'c 'c "hej" (string #\h #\e #\j))))) |
| 174 |
|
| 175 |
(pass-if "tdelete-duplicates" |
| 176 |
(equal? '(1 2 3 4) |
| 177 |
(list-transduce (tdelete-duplicates) |
| 178 |
rcons |
| 179 |
'(1 1 2 1 2 3 3 1 2 3 4)))) |
| 180 |
|
| 181 |
(pass-if "tdelete-duplicates with predicate" |
| 182 |
(equal? '("hej" "hopp") |
| 183 |
(list-transduce (tdelete-duplicates string-ci=?) |
| 184 |
rcons |
| 185 |
(list "hej" "HEJ" "hopp" "HOPP" "heJ")))) |
| 186 |
|
| 187 |
(pass-if "tflatten" |
| 188 |
(equal? '(1 2 3 4 5 6 7 8 9) |
| 189 |
(list-transduce tflatten rcons '((1 2) 3 (4 (5 6) 7) 8 (9))))) |
| 190 |
|
| 191 |
(pass-if "tpartition" |
| 192 |
(equal? '((1 1 1 1) (2 2 2 2) (3 3 3) (4 4 4 4)) |
| 193 |
(list-transduce (tpartition even?) |
| 194 |
rcons |
| 195 |
'(1 1 1 1 2 2 2 2 3 3 3 4 4 4 4)))) |
| 196 |
|
| 197 |
(pass-if "tsegment" |
| 198 |
(equal? '((0 1) (2 3) (4)) |
| 199 |
(vector-transduce (tsegment 2) rcons numeric-vec))) |
| 200 |
|
| 201 |
(pass-if "tadd-between" |
| 202 |
(equal? '(0 and 1 and 2 and 3 and 4) |
| 203 |
(list-transduce (tadd-between 'and) rcons numeric-list))) |
| 204 |
|
| 205 |
(pass-if "tenumerate" |
| 206 |
(equal? '((-1 . 0) (0 . 1) (1 . 2) (2 . 3) (3 . 4)) |
| 207 |
(list-transduce (tenumerate (- 1)) rcons numeric-list))) |
| 208 |
|
| 209 |
(pass-if "tbatch" |
| 210 |
(equal? |
| 211 |
'((0 1) (2 3) (4)) |
| 212 |
(list-transduce (tbatch (ttake 2) rcons) rcons numeric-list))) |
| 213 |
|
| 214 |
(pass-if "tfold" |
| 215 |
(equal? |
| 216 |
'(0 1 3 6 10) |
| 217 |
(list-transduce (tfold +) rcons numeric-list)))) |
| 218 |
|
| 219 |
|
| 220 |
(with-test-prefix "x-transduce" |
| 221 |
(pass-if "list-transduce" |
| 222 |
(equal? 15 (list-transduce (tmap add1) + numeric-list))) |
| 223 |
|
| 224 |
(pass-if "list-transduce with identity" |
| 225 |
(equal? 15 (list-transduce (tmap add1) + 0 numeric-list))) |
| 226 |
|
| 227 |
(pass-if "vector-transduce" |
| 228 |
(equal? 15 (vector-transduce (tmap add1) + numeric-vec))) |
| 229 |
|
| 230 |
(pass-if "vector-transduce with identity" |
| 231 |
(equal? 15 |
| 232 |
(vector-transduce (tmap add1) + 0 numeric-vec))) |
| 233 |
|
| 234 |
(pass-if "port-transduce" (port-transduce-test)) |
| 235 |
(pass-if "port-transduce with identity" (port-transduce-with-identity-test)) |
| 236 |
|
| 237 |
;; Converts each numeric char to it's corresponding integer and sums them. |
| 238 |
(pass-if "string-transduce" |
| 239 |
(equal? |
| 240 |
15 |
| 241 |
(string-transduce (tmap (lambda (x) (- (char->integer x) 47))) + "01234"))) |
| 242 |
|
| 243 |
(pass-if "string-transduce with identity" |
| 244 |
(equal? |
| 245 |
15 |
| 246 |
(string-transduce (tmap (lambda (x) (- (char->integer x) 47))) |
| 247 |
+ |
| 248 |
0 |
| 249 |
"01234"))) |
| 250 |
|
| 251 |
(pass-if "generator-transduce" |
| 252 |
(equal? |
| 253 |
'(1 2 3) |
| 254 |
(parameterize ((current-input-port (open-input-string "1 2 3"))) |
| 255 |
(generator-transduce (tmap (lambda (x) x)) rcons read)))) |
| 256 |
|
| 257 |
(pass-if "generator-transduce with identity" |
| 258 |
(equal? |
| 259 |
'(1 2 3) |
| 260 |
(parameterize ((current-input-port (open-input-string "1 2 3"))) |
| 261 |
(generator-transduce (tmap (lambda (x) x)) rcons '() read)))) |
| 262 |
|
| 263 |
(pass-if "bytevector-u8-transduce" |
| 264 |
(equal? 15 (bytevector-u8-transduce (tmap add1) + bv))) |
| 265 |
|
| 266 |
(pass-if "bytevector-u8-transduce with identity" |
| 267 |
(equal? 15 (bytevector-u8-transduce (tmap add1) + 0 bv)))) |