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