srfi-69.test 4.0 KB

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