hash-table-aux.lsp 3.24 KB
Newer Older
1 2 3 4 5
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sat Oct  4 09:24:24 2003
;;;; Contains: Aux. functions for testing hash tables

6

7 8

(eval-when (:load-toplevel :compile-toplevel :execute)
9
  (compile-and-load "ANSI-TESTS:AUX;random-aux.lsp"))
10 11 12 13 14

(defparameter *hash-table-test-iters* 1000)

(defun test-hash-table-1 (&rest args)
  (let ((table (apply #'make-hash-table args))
sds's avatar
sds committed
15
        (test (or (getf args :test) 'eql)))
16 17 18 19 20 21 22
    (assert (member test '(eq eql equal equalp)))
    (assert (hash-table-p table))
    (assert (typep table 'hash-table))
    ;; Build a hash table using the arguments in ARGS.
    ;; Perform *hash-table-test-iters* iterations of
    ;; random hash table operations
    (let* ((universe-vec (coerce *universe* 'vector))
sds's avatar
sds committed
23 24 25
           ;; (universe-size (length universe-vec))
           (mapping nil)
           (count 0))
26 27 28 29 30

      (loop
       for i from 0 below *hash-table-test-iters*
       do (assert (eql (hash-table-count table) count))
       do (assert (let ((size (hash-table-size table)))
sds's avatar
sds committed
31
                    (and (integerp size) (>= size 0))))
32 33
       do
       (flet ((%remove-pair
sds's avatar
sds committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
               (rpair)
               (decf count)
               (let ((key (car rpair))
                     (expected-value (cdr rpair)))
                 (multiple-value-bind (value present-p)
                     (gethash key table)
                   (assert present-p)
                   (assert (eql expected-value value))
                   (setf mapping
                         (remove rpair mapping :count 1 :test 'eq)))
                 (assert (remhash key table))
                 (multiple-value-bind (value present-p)
                     (gethash key table)
                   (assert (not present-p))
                   (assert (null value))
                   ))))

         (rcase
          (1 ;; Insert
           (let* ((new-elem (random-from-seq universe-vec))
                  (pair (assoc new-elem mapping :test test)))
             (cond
              (pair
               (multiple-value-bind
                   (value present-p)
                   (gethash new-elem table)
                 (assert present-p)
                 (assert (eql (cdr pair) value))
                 (setf (cdr pair) i
                       (gethash new-elem table) i)))
              (t
               (assert
                (equal (multiple-value-list (gethash new-elem table))
                       '(nil nil)))
               (incf count)
               (push (cons new-elem i) mapping)
               (setf (gethash new-elem table) i)))))
          (1 ;; Delete element in the set
           (when mapping
             (%remove-pair (random-from-seq mapping))))
          (1 ;; Delete random element from universe
           (let* ((key (random-from-seq universe-vec))
                  (pair (assoc key mapping :test test)))
             (cond
              (pair (%remove-pair pair))
              (t
               ;; Not present -- check that this is true
               (assert (equal (multiple-value-list (gethash key table))
                              '(nil nil)))
               (assert (not (remhash key table)))
               (assert (equal (multiple-value-list (gethash key table))
                              '(nil nil)))))
             ))
          ))))))





93