srfi-69.test 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. (define-module (test-srfi-69)
  20. #:use-module (test-suite lib)
  21. #:use-module (srfi srfi-69)
  22. #:use-module (srfi srfi-1)
  23. #:use-module (srfi srfi-26))
  24. (define (string-ci-assoc-equal? left right)
  25. "Answer whether LEFT and RIGHT are equal, being associations of
  26. case-insensitive strings to `equal?'-tested values."
  27. (and (string-ci=? (car left) (car right))
  28. (equal? (cdr left) (cdr right))))
  29. (with-test-prefix "SRFI-69"
  30. (pass-if "small alist<->hash tables round-trip"
  31. (let* ((start-alist '((a . 1) (b . 2) (c . 3) (a . 42)))
  32. (ht (alist->hash-table start-alist eq?))
  33. (end-alist (hash-table->alist ht)))
  34. (and (= 3 (hash-table-size ht))
  35. (lset= equal? end-alist (take start-alist 3))
  36. (= 1 (hash-table-ref ht 'a))
  37. (= 2 (hash-table-ref ht 'b))
  38. (= 3 (hash-table-ref ht 'c)))))
  39. (pass-if "string-ci=? tables work by default"
  40. (let ((ht (alist->hash-table '(("xY" . 2) ("abc" . 54)) string-ci=?)))
  41. (hash-table-set! ht "XY" 42)
  42. (hash-table-set! ht "qqq" 100)
  43. (and (= 54 (hash-table-ref ht "ABc"))
  44. (= 42 (hash-table-ref ht "xy"))
  45. (= 3 (hash-table-size ht))
  46. (lset= string-ci-assoc-equal?
  47. '(("xy" . 42) ("abc" . 54) ("qqq" . 100))
  48. (hash-table->alist ht)))))
  49. (pass-if-exception "Bad weakness arg to mht signals an error"
  50. '(misc-error . "^Invalid weak hash table type")
  51. (make-hash-table equal? hash #:weak 'key-and-value))
  52. (pass-if "empty hash tables are empty"
  53. (null? (hash-table->alist (make-hash-table eq?))))
  54. (pass-if "hash-table-ref uses default"
  55. (equal? '(4)
  56. (hash-table-ref (alist->hash-table '((a . 1)) eq?)
  57. 'b (cut list (+ 2 2)))))
  58. (pass-if "hash-table-delete! deletes present assocs, ignores others"
  59. (let ((ht (alist->hash-table '((a . 1) (b . 2)) eq?)))
  60. (hash-table-delete! ht 'c)
  61. (and (= 2 (hash-table-size ht))
  62. (begin
  63. (hash-table-delete! ht 'a)
  64. (= 1 (hash-table-size ht)))
  65. (lset= equal? '((b . 2)) (hash-table->alist ht)))))
  66. (pass-if "alist->hash-table does not require linear stack space"
  67. (eqv? 99999
  68. (hash-table-ref (alist->hash-table
  69. (unfold-right (cut >= <> 100000)
  70. (lambda (s) `(x . ,s)) 1+ 0)
  71. eq?)
  72. 'x)))
  73. (pass-if "hash-table-walk ignores return values"
  74. (let ((ht (alist->hash-table '((a . 1) (b . 2) (c . 3)) eq?)))
  75. (for-each (cut hash-table-walk ht <>)
  76. (list (lambda (k v) (values))
  77. (lambda (k v) (values 1 2 3))))
  78. #t))
  79. (pass-if "hash-table-update! modifies existing binding"
  80. (let ((ht (alist->hash-table '((a . 1)) eq?)))
  81. (hash-table-update! ht 'a 1+)
  82. (hash-table-update! ht 'a (cut + 4 <>) (lambda () 42))
  83. (and (= 1 (hash-table-size ht))
  84. (lset= equal? '((a . 6)) (hash-table->alist ht)))))
  85. (pass-if "hash-table-update! creates new binding when appropriate"
  86. (let ((ht (make-hash-table eq?)))
  87. (hash-table-update! ht 'b 1+ (lambda () 42))
  88. (hash-table-update! ht 'b (cut + 10 <>))
  89. (and (= 1 (hash-table-size ht))
  90. (lset= equal? '((b . 53)) (hash-table->alist ht)))))
  91. (pass-if "can use all arguments, including size"
  92. (hash-table? (make-hash-table equal? hash #:weak 'key 31)))
  93. )