srfi-69-test.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. ;; From Larceny
  2. #|
  3. Copyright 1991, 1994, 1998 William D Clinger
  4. Copyright 1998 Lars T Hansen
  5. Copyright 1984 - 1993 Lightship Software, Incorporated
  6. Permission to copy this software, in whole or in part, to use this
  7. software for any lawful purpose, and to redistribute this software
  8. is granted subject to the following restriction: Any publication
  9. or redistribution of this software, whether on its own or
  10. incorporated into other software, must bear the above copyright
  11. notices and the following legend:
  12. The Twobit compiler and the Larceny runtime system were
  13. developed by William Clinger and Lars Hansen with the
  14. assistance of Lightship Software and the College of Computer
  15. Science of Northeastern University. This acknowledges that
  16. Clinger et al remain the sole copyright holders to Twobit
  17. and Larceny and that no rights pursuant to that status are
  18. waived or conveyed.
  19. Twobit and Larceny are provided as is. The user specifically
  20. acknowledges that Northeastern University, William Clinger, Lars
  21. Hansen, and Lightship Software have not made any representations
  22. or warranty with regard to performance of Twobit and Larceny,
  23. their merchantability, or fitness for a particular purpose. Users
  24. further acknowledge that they have had the opportunity to inspect
  25. Twobit and Larceny and will hold harmless Northeastern University,
  26. William Clinger, Lars Hansen, and Lightship Software from any cost,
  27. liability, or expense arising from, or in any way related to the
  28. use of this software.
  29. |#
  30. (require 'srfi-69)
  31. (require 'srfi-95)
  32. (define (list-sort p l) (sort l p))
  33. (define (writeln . xs)
  34. (for-each display xs)
  35. (newline))
  36. (define (fail token . more)
  37. (writeln "Error: test failed: " token)
  38. #f)
  39. (define ht1equal (make-hash-table))
  40. (define ht2equal (make-hash-table equal?))
  41. (define ht3equal (make-hash-table equal? hash))
  42. (define ht2eqv (make-hash-table eqv?))
  43. (define ht3eqv (make-hash-table eqv? hash))
  44. (define ht2eq (make-hash-table eq?))
  45. (define ht3eq (make-hash-table eq? hash))
  46. (define ht3string= (make-hash-table string=? string-hash))
  47. (define ht3string-ci= (make-hash-table string-ci=? string-ci-hash))
  48. (define fx=? =)
  49. (define ht3fx= (make-hash-table fx=? values))
  50. (define ht4equal (alist->hash-table '()))
  51. (define ht5equal (alist->hash-table '() equal?))
  52. (define ht6equal (alist->hash-table '() equal? hash))
  53. (define ht5eqv (alist->hash-table '() eqv?))
  54. (define ht6eqv (alist->hash-table '() eqv? hash))
  55. (define ht5eq (alist->hash-table '() eq?))
  56. (define ht6eq (alist->hash-table '() eq? hash))
  57. (define ht6string= (alist->hash-table '() string=? string-hash))
  58. (define ht6string-ci= (alist->hash-table '() string-ci=? string-ci-hash))
  59. (define ht6fx= (alist->hash-table '() fx=? values))
  60. (define (test-tables)
  61. (list ht1equal ht2equal ht3equal
  62. ht2eqv ht3eqv
  63. ht2eq ht3eq
  64. ht3string= ht3string-ci= ht3fx=
  65. ht4equal ht5equal ht6equal
  66. ht5eqv ht6eqv
  67. ht5eq ht6eq
  68. ht6string= ht6string-ci= ht6fx=))
  69. (define (test-tables-general&nonempty)
  70. (list ht4equal ht5equal ht6equal
  71. ht5eqv ht6eqv
  72. ht5eq ht6eq))
  73. (or (equal? (map hash-table? (test-tables))
  74. (map (lambda (x) #t) (test-tables)))
  75. (fail 'hash-table?))
  76. (or (equal? (map hash-table-size (test-tables))
  77. (map (lambda (x) 0) (test-tables)))
  78. (fail 'alist->hash-table:1))
  79. (set! ht4equal (alist->hash-table '((a 11) ("b" 12) (cee 13) (47.8 14))))
  80. (set! ht5equal (alist->hash-table '((a 11) ("b" 12) (cee 13) (47.8 14))
  81. equal?))
  82. (set! ht6equal (alist->hash-table '((a 11) ("b" 12) (cee 13) (47.8 14))
  83. equal? hash))
  84. (set! ht5eqv (alist->hash-table '((a 11) ("b" 12) (cee 13) (47.8 14))
  85. eqv?))
  86. (set! ht6eqv (alist->hash-table '((a 11) ("b" 12) (cee 13) (47.8 14))
  87. eqv? hash))
  88. (set! ht5eq (alist->hash-table '((a 11) ("b" 12) (cee 13) (47.8 14))
  89. eq?))
  90. (set! ht6eq (alist->hash-table '((a 11) ("b" 12) (cee 13) (47.8 14))
  91. eq? hash))
  92. (set! ht6string=
  93. (alist->hash-table '(("a" 11) ("b" 12) ("cee" 13) ("d" 14))
  94. string=? string-hash))
  95. (set! ht6string-ci=
  96. (alist->hash-table '(("a" 11) ("b" 12) ("CeE" 13) ("d" 14))
  97. string-ci=? string-ci-hash))
  98. (set! ht6fx= (alist->hash-table '((101 201) (102 202) (103 203) (104 204))
  99. fx=? values))
  100. (or (equal? (map hash-table-size (test-tables))
  101. '(0 0 0 0 0 0 0 0 0 0 4 4 4 4 4 4 4 4 4 4))
  102. (fail 'alist->hash-table:2))
  103. (or (equal? (map hash-table-equivalence-function (test-tables))
  104. (list equal? equal? equal? eqv? eqv? eq? eq?
  105. string=? string-ci=? fx=?
  106. equal? equal? equal? eqv? eqv? eq? eq?
  107. string=? string-ci=? fx=?))
  108. (fail 'hash-table-equivalence-function:1))
  109. (or (equal? (map hash-table-hash-function
  110. (list ht1equal ht2equal ht3equal
  111. ht3eqv ht3eq ht3string= ht3string-ci= ht3fx=))
  112. (list hash hash hash hash hash
  113. string-hash string-ci-hash values))
  114. (fail 'hash-table-hash-function:1))
  115. (or (equal? (map (lambda (ht)
  116. (hash-table-ref ht 'cee))
  117. (test-tables-general&nonempty))
  118. '((13) (13) (13) (13) (13) (13) (13)))
  119. (fail 'hash-table-ref:1))
  120. (or (equal? (map (lambda (ht)
  121. (hash-table-ref ht 47.8))
  122. (list ht4equal ht5equal ht6equal ht5eqv ht6eqv))
  123. '((14) (14) (14) (14) (14)))
  124. (fail 'hash-table-ref:2))
  125. (or (equal? (map (lambda (ht)
  126. (hash-table-ref ht "cee" (lambda () #f)))
  127. (append (test-tables-general&nonempty)
  128. (list ht6string= ht6string-ci=)))
  129. '(#f #f #f #f #f #f #f (13) (13)))
  130. (fail 'hash-table-ref:3))
  131. (or (equal? (map (lambda (ht)
  132. (hash-table-ref ht "CeE" (lambda () 99)))
  133. (append (test-tables-general&nonempty)
  134. (list ht6string= ht6string-ci=)))
  135. '(99 99 99 99 99 99 99 99 (13)))
  136. (fail 'hash-table-ref:4))
  137. (or (equal? (map (lambda (ht)
  138. (hash-table-ref/default ht "CeE" 97))
  139. (append (test-tables-general&nonempty)
  140. (list ht6string= ht6string-ci=)))
  141. '(97 97 97 97 97 97 97 97 (13)))
  142. (fail 'hash-table-ref:5))
  143. (for-each (lambda (ht) (hash-table-set! ht "cee" 'see))
  144. (append (test-tables-general&nonempty)
  145. (list ht6string= ht6string-ci=)))
  146. (or (equal? (map hash-table-size
  147. (append (test-tables-general&nonempty)
  148. (list ht6string= ht6string-ci=)))
  149. '(5 5 5 5 5 5 5 4 4))
  150. (fail 'hash-table-set!:1))
  151. (for-each (lambda (ht) (hash-table-delete! ht (string #\b)))
  152. (append (test-tables-general&nonempty)
  153. (list ht6string= ht6string-ci=)))
  154. (or (equal? (map hash-table-size
  155. (append (test-tables-general&nonempty)
  156. (list ht6string= ht6string-ci=)))
  157. '(4 4 4 5 5 5 5 3 3))
  158. (fail 'hash-table-delete!:1))
  159. (or (equal? (map (lambda (ht) (hash-table-exists? ht "om"))
  160. (append (test-tables-general&nonempty)
  161. (list ht6string= ht6string-ci=)))
  162. '(#f #f #f #f #f #f #f #f #f))
  163. (fail 'hash-table-exists?:1))
  164. (or (equal? (map (lambda (ht) (hash-table-exists? ht (string-copy "cee")))
  165. (append (test-tables-general&nonempty)
  166. (list ht6string= ht6string-ci=)))
  167. '(#t #t #t #f #f #f #f #t #t))
  168. (fail 'hash-table-exists?:2))
  169. (for-each (lambda (ht) (hash-table-update! ht 'a car))
  170. (test-tables-general&nonempty))
  171. (or (equal? (map (lambda (ht) (hash-table-ref/default ht 'a #f))
  172. (test-tables-general&nonempty))
  173. '(11 11 11 11 11 11 11))
  174. (fail 'hash-table-update!:1))
  175. (or (equal? (map hash-table-size (test-tables))
  176. '(0 0 0 0 0 0 0 0 0 0 4 4 4 5 5 5 5 3 3 4))
  177. (fail 'hash-table-size:1))
  178. ;;; This is slightly flaky, because hash might hash two keys
  179. ;;; to the same value. In particular, a symbol might be hashed
  180. ;;; the same as its print string.
  181. (define (canonical-order? x y)
  182. (let ((i (hash x))
  183. (j (hash y)))
  184. (or (< i j)
  185. (and (= i j) (symbol? x) (string? y)))))
  186. (define (canonical-order lis)
  187. (list-sort canonical-order? lis))
  188. (or (equal? (map canonical-order
  189. (map hash-table-keys (test-tables)))
  190. (map canonical-order
  191. '(() () () () () () () () () ()
  192. (a cee 47.8 "cee")
  193. (a cee 47.8 "cee")
  194. (a cee 47.8 "cee")
  195. (a "b" cee 47.8 "cee")
  196. (a "b" cee 47.8 "cee")
  197. (a "b" cee 47.8 "cee")
  198. (a "b" cee 47.8 "cee")
  199. ("a" "cee" "d")
  200. ("a" "CeE" "d")
  201. (101 102 103 104))))
  202. (fail 'hash-table-keys:1))
  203. (or (equal? (map canonical-order
  204. (map hash-table-values (test-tables)))
  205. (map canonical-order
  206. '(() () () () () () () () () ()
  207. (see 11 (13) (14))
  208. (see 11 (13) (14))
  209. (see 11 (13) (14))
  210. (see 11 (12) (13) (14))
  211. (see 11 (12) (13) (14))
  212. (see 11 (12) (13) (14))
  213. (see 11 (12) (13) (14))
  214. (see (11) (14))
  215. (see (11) (14))
  216. ((201) (202) (203) (204)))))
  217. (fail 'hash-table-values:1))
  218. (let ((keys '())
  219. (vals '()))
  220. (hash-table-walk ht4equal
  221. (lambda (key val)
  222. (set! keys (cons key keys))
  223. (set! vals (cons val vals))))
  224. (or (and (equal? (canonical-order keys)
  225. (canonical-order (hash-table-keys ht4equal)))
  226. (equal? (canonical-order vals)
  227. (canonical-order (hash-table-values ht4equal))))
  228. (fail 'hash-table-walk:1)))
  229. (or (and (equal? (canonical-order
  230. (hash-table-fold ht4equal
  231. (lambda (key val x) (cons key x))
  232. '()))
  233. (canonical-order (hash-table-keys ht4equal)))
  234. (equal? (canonical-order
  235. (hash-table-fold ht4equal
  236. (lambda (key val x) (cons val x))
  237. '()))
  238. (canonical-order (hash-table-values ht4equal))))
  239. (fail 'hash-table-fold:1))
  240. (writeln "---done--")
  241. ; Not yet tested:
  242. ;
  243. ; hash-table->alist
  244. ; hash-table-copy
  245. ; hash-table-merge!
  246. ;
  247. ; hash
  248. ; string-hash
  249. ; string-ci-hash
  250. ; hash-by-identity