Debian Codegraph @

src/tests/normal-tests/hash-tables.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;; Author: Daniel Kochmański
5 ;;;; Created: 2017-04-28
6 ;;;; Contains: hash-table tests
7
8 (in-package :cl-test)
9
10 (suite 'hash-tables)
11
12
13 ;;; weak hash tables interface
14 (test hash-tables.fill-content
15 (let ((ht (make-hash-table)))
16 (setf (gethash :qux ht) 3)
17 (ext:hash-table-fill ht '((:foo . 1) (:bar . 2)))
18 (= (length (ext:hash-table-content ht)) 3)))
19
20 (test hash-tables.weak-key
21 (let ((ht (make-hash-table :weakness :key))
22 (key (make-random-state))
23 (val (make-random-state)))
24 (setf (gethash key ht) val)
25 (destructuring-bind ((k . v)) (ext:hash-table-content ht)
26 (is k 'ext:weak-pointer)
27 (is v 'random-state))))
28
29 (test hash-tables.weak-value
30 (let ((ht (make-hash-table :weakness :value))
31 (key (make-random-state))
32 (val (make-random-state)))
33 (setf (gethash key ht) val)
34 (destructuring-bind ((k . v)) (ext:hash-table-content ht)
35 (is v 'ext:weak-pointer)
36 (is k 'random-state))))
37
38 (test hash-tables.weak-key-and-value
39 (let ((ht (make-hash-table :weakness :key-and-value))
40 (key (make-random-state))
41 (val (make-random-state)))
42 (setf (gethash key ht) val)
43 (destructuring-bind ((k . v)) (ext:hash-table-content ht)
44 (is k 'ext:weak-pointer)
45 (is v 'ext:weak-pointer))))
46
47 (test hash-tables.weak-key-or-value
48 (let ((ht (make-hash-table :weakness :key-or-value))
49 (key (make-random-state))
50 (val (make-random-state)))
51 (setf (gethash key ht) val)
52 (destructuring-bind ((k . v)) (ext:hash-table-content ht)
53 (is k 'ext:weak-pointer)
54 (is v 'ext:weak-pointer))))
55
56 (test hash-tables.weak-nil
57 (let ((ht (make-hash-table :weakness nil))
58 (key (make-random-state))
59 (val (make-random-state)))
60 (setf (gethash key ht) val)
61 (destructuring-bind ((k . v)) (ext:hash-table-content ht)
62 (is k 'random-state)
63 (is v 'random-state))))
64
65 ;;; For some corner cases weak hash-tables returned unnormalized
66 ;;; values. Then we had a regression in gethash which tried to
67 ;;; normalize already normalized values. We clearly need a simple test
68 ;;; whenever hash tables work correctly.
69 (test hash-tables.weak-faithful
70 (let ((hts (list (make-hash-table :weakness nil)
71 (make-hash-table :weakness :key)
72 (make-hash-table :weakness :value)
73 (make-hash-table :weakness :key-or-value)
74 (make-hash-table :weakness :key-and-value)))
75 (keys (list :a :b :c))
76 (vals (list :x :y :z)))
77 ;; ensure basic set/get on null entries
78 (mapc (lambda (ht)
79 (mapc (lambda (k v)
80 (is (null (gethash k ht)))
81 (setf (gethash k ht) v)
82 (is (eql (gethash k ht) v)
83 "gethash ~s = ~s, should be ~s, weakness is ~s"
84 k (gethash k ht) v (ext:hash-table-weakness ht)))
85 keys vals))
86 hts)
87 ;; ensure that values get updated for a known key
88 (mapc (lambda (ht)
89 (setf (gethash :c ht) :z-prim)
90 (is (eql (gethash :c ht) :z-prim)))
91 hts)
92 ;; ensure maphash
93 (mapc (lambda (ht)
94 (let ((i 0))
95 (maphash (lambda (k v)
96 (incf i)
97 (is (eql v (ecase k
98 (:a :x)
99 (:b :y)
100 (:c :z-prim)))))
101 ht)
102 (is (= i 3))))
103 hts)))
104
105 (test hash-tables.weak-err
106 (signals simple-type-error (make-hash-table :weakness :whatever)))
107
108
109 ;;; Synchronization
110 (test hash-tables.sync
111 (let ((ht (make-hash-table :synchronized t)))
112 (is-true (ext:hash-table-synchronized-p ht))
113 (setf (gethash :foo ht) 3)
114 (setf (gethash :bar ht) 4)
115 (is (= 2 (hash-table-count ht)))
116 (is (= 3 (gethash :foo ht)))
117 (is-true (remhash :bar ht))
118 (is (= 1 (hash-table-count ht)))))
119
120
121 ;;; generic test and hash functions
122
123 ;;; In this test we provide an equality predicate which distinguishes
124 ;;; only two types of numbers: odd and even. HT is synchronized
125 ;;; because we want also to check, if lock is not hogged by errors
126 ;;; inside our function (we pass string for that purpose).
127 (test hash-tables.custom
128 (flet ((not-so-fancy-equals (x y)
129 (if (zerop x)
130 (= x y)
131 (eql (evenp x) (evenp y))))
132 (not-so-fancy-hash (x)
133 (cond ((zerop x) 0)
134 ((evenp x) 1)
135 (T 2))))
136 (signals error (make-hash-table :test #'not-so-fancy-equals))
137 (let ((ht (make-hash-table :test #'not-so-fancy-equals
138 :hash-function #'not-so-fancy-hash
139 :synchronized t)))
140 (finishes
141 (setf (gethash 13 ht) 42
142 (gethash 12 ht) 33
143 (gethash 10 ht) 55))
144 (is (= (gethash 12 ht) 55))
145 (is (= (gethash 1 ht) 42))
146 (is (null (gethash 0 ht)))
147 (signals error (gethash "foobar" ht))
148 (signals error (setf (gethash "foobar" ht) 15))
149 (finishes (remhash 3 ht))
150 (finishes (hash-table-test ht))
151 (is (null (gethash 1 ht)))
152 (finishes (setf (gethash 55 ht) 0))
153 (is (= (gethash 13 ht) 0)))))
154
155
156 ;;; More weak hash table tests. See #761.
157
158 ;;; Weak hash tables autonormalized GC-ed entries creating holes, which (unlike
159 ;;; remhash) were not filled. That leads to invalid results for GETHASH.
160 (test hash-tables.weak-collision
161 (let ((ht (make-hash-table :weakness :key :size 2 :test #'eq)))
162 (loop repeat 15 do (setf (gethash (gensym) ht) 42))
163 (setf (gethash :sm ht) t)
164 (ext:gc t)
165 (is (not (null (gethash :sm ht))))))
166
167 ;;; MAPHASH mapped also garbage-collected entries leading to dud calls to the
168 ;;; continuation.
169 (test hash-tables.weak-maphash
170 (let ((ht (make-hash-table :weakness :key :size 2 :test #'eq)))
171 (loop repeat 4 do (setf (gethash (gensym) ht) 42))
172 (setf (gethash :sm ht) t)
173 (ext:gc t)
174 (maphash (lambda (k v)
175 (is (not (null k)))
176 (is (not (null v))))
177 ht)))
178
179 ;;;
180 ;;; This test leads to a segfault while running tests, hence it is disabled.
181 ;;; So what's going on?
182 ;;;
183 ;;; 1. si_gc disables interrupts for the env for the time GC_collect is running.
184 ;;; 2. GC_collect calls GC_finalize > GC_make_disappearing_links_disappear
185 ;;; 3. Finally GC_is_marked calls mark_bit_from_hdr that marks env
186 ;;;
187 ;;; Note that this failure happens sometimes with two tests above, but it is
188 ;;; much rarer -- the test below more or less deterministically crashes ECL.
189 ;;;
190 #+ (or)
191 (test hash-tables.gc-shakedown
192 (dolist (sk '(nil :key :value :key-or-value :key-and-value))
193 (let ((ht (make-hash-table :weakness sk :size 2 :test #'eq)))
194 (progn
195 (progn
196 (loop repeat 4 do (setf (gethash (gensym) ht) 42))
197 (setf (gethash :sm ht) t)
198 (ext:gc t))
199 "Crashed on ~a." sk))))