| 1 | ;;;; srfi-69.test --- Test suite for SRFI 69 -*- scheme -*- |
| 2 | ;;;; |
| 3 | ;;;; Copyright (C) 2007 Free Software Foundation, Inc. |
| 4 | ;;;; |
| 5 | ;;;; This program is free software; you can redistribute it and/or modify |
| 6 | ;;;; it under the terms of the GNU General Public License as published by |
| 7 | ;;;; the Free Software Foundation; either version 2, or (at your option) |
| 8 | ;;;; any later version. |
| 9 | ;;;; |
| 10 | ;;;; This program is distributed in the hope that it will be useful, |
| 11 | ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | ;;;; GNU General Public License for more details. |
| 14 | ;;;; |
| 15 | ;;;; You should have received a copy of the GNU General Public License |
| 16 | ;;;; along with this software; see the file COPYING. If not, write to |
| 17 | ;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 18 | ;;;; Boston, MA 02110-1301 USA |
| 19 | |
| 20 | (define-module (test-srfi-69) |
| 21 | #:use-module (test-suite lib) |
| 22 | #:use-module (srfi srfi-69) |
| 23 | #:use-module (srfi srfi-1) |
| 24 | #:use-module (srfi srfi-26)) |
| 25 | |
| 26 | (define (string-ci-assoc-equal? left right) |
| 27 | "Answer whether LEFT and RIGHT are equal, being associations of |
| 28 | case-insensitive strings to `equal?'-tested values." |
| 29 | (and (string-ci=? (car left) (car right)) |
| 30 | (equal? (cdr left) (cdr right)))) |
| 31 | |
| 32 | (with-test-prefix "SRFI-69" |
| 33 | |
| 34 | (pass-if "small alist<->hash tables round-trip" |
| 35 | (let* ((start-alist '((a . 1) (b . 2) (c . 3) (a . 42))) |
| 36 | (ht (alist->hash-table start-alist eq?)) |
| 37 | (end-alist (hash-table->alist ht))) |
| 38 | (and (= 3 (hash-table-size ht)) |
| 39 | (lset= equal? end-alist (take start-alist 3)) |
| 40 | (= 1 (hash-table-ref ht 'a)) |
| 41 | (= 2 (hash-table-ref ht 'b)) |
| 42 | (= 3 (hash-table-ref ht 'c))))) |
| 43 | |
| 44 | (pass-if "string-ci=? tables work by default" |
| 45 | (let ((ht (alist->hash-table '(("xY" . 2) ("abc" . 54)) string-ci=?))) |
| 46 | (hash-table-set! ht "XY" 42) |
| 47 | (hash-table-set! ht "qqq" 100) |
| 48 | (and (= 54 (hash-table-ref ht "ABc")) |
| 49 | (= 42 (hash-table-ref ht "xy")) |
| 50 | (= 3 (hash-table-size ht)) |
| 51 | (lset= string-ci-assoc-equal? |
| 52 | '(("xy" . 42) ("abc" . 54) ("qqq" . 100)) |
| 53 | (hash-table->alist ht))))) |
| 54 | |
| 55 | (pass-if-exception "Bad weakness arg to mht signals an error" |
| 56 | '(misc-error . "^Invalid weak hash table type") |
| 57 | (make-hash-table equal? hash #:weak 'key-and-value)) |
| 58 | |
| 59 | (pass-if "empty hash tables are empty" |
| 60 | (null? (hash-table->alist (make-hash-table eq?)))) |
| 61 | |
| 62 | (pass-if "hash-table-ref uses default" |
| 63 | (equal? '(4) |
| 64 | (hash-table-ref (alist->hash-table '((a . 1)) eq?) |
| 65 | 'b (cut list (+ 2 2))))) |
| 66 | |
| 67 | (pass-if "hash-table-delete! deletes present assocs, ignores others" |
| 68 | (let ((ht (alist->hash-table '((a . 1) (b . 2)) eq?))) |
| 69 | (hash-table-delete! ht 'c) |
| 70 | (and (= 2 (hash-table-size ht)) |
| 71 | (begin |
| 72 | (hash-table-delete! ht 'a) |
| 73 | (= 1 (hash-table-size ht))) |
| 74 | (lset= equal? '((b . 2)) (hash-table->alist ht))))) |
| 75 | |
| 76 | (pass-if "alist->hash-table does not require linear stack space" |
| 77 | (eqv? 99999 |
| 78 | (hash-table-ref (alist->hash-table |
| 79 | (unfold-right (cut >= <> 100000) |
| 80 | (lambda (s) `(x . ,s)) 1+ 0) |
| 81 | eq?) |
| 82 | 'x))) |
| 83 | |
| 84 | (pass-if "hash-table-walk ignores return values" |
| 85 | (let ((ht (alist->hash-table '((a . 1) (b . 2) (c . 3)) eq?))) |
| 86 | (for-each (cut hash-table-walk ht <>) |
| 87 | (list (lambda (k v) (values)) |
| 88 | (lambda (k v) (values 1 2 3)))) |
| 89 | #t)) |
| 90 | |
| 91 | (pass-if "hash-table-update! modifies existing binding" |
| 92 | (let ((ht (alist->hash-table '((a . 1)) eq?))) |
| 93 | (hash-table-update! ht 'a 1+) |
| 94 | (hash-table-update! ht 'a (cut + 4 <>) (lambda () 42)) |
| 95 | (and (= 1 (hash-table-size ht)) |
| 96 | (lset= equal? '((a . 6)) (hash-table->alist ht))))) |
| 97 | |
| 98 | (pass-if "hash-table-update! creates new binding when appropriate" |
| 99 | (let ((ht (make-hash-table eq?))) |
| 100 | (hash-table-update! ht 'b 1+ (lambda () 42)) |
| 101 | (hash-table-update! ht 'b (cut + 10 <>)) |
| 102 | (and (= 1 (hash-table-size ht)) |
| 103 | (lset= equal? '((b . 53)) (hash-table->alist ht))))) |
| 104 | |
| 105 | (pass-if "can use all arguments, including size" |
| 106 | (hash-table? (make-hash-table equal? hash #:weak 'key 31))) |
| 107 | |
| 108 | ) |