mes/tests/hash.test

111 lines
3.3 KiB
Scheme
Executable File

#! /bin/sh
# -*-scheme-*-
exec ${MES-bin/mes} --no-auto-compile -L ${0%/*} -L module -C module -e '(tests hash)' -s "$0" "$@"
!#
;;; -*-scheme-*-
;;; GNU Mes --- Maxwell Equations of Software
;;; Copyright © 2022 Timothy Sample <samplet@ngyro.com>
;;;
;;; This file is part of GNU Mes.
;;;
;;; GNU Mes is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Mes is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Mes. If not, see <https://www.gnu.org/licenses/>.
(define-module (tests hash)
#:use-module (mes test))
(cond-expand
(mes
(mes-use-module (mes test)))
(else))
(pass-if "make-hash-table" (make-hash-table))
(pass-if "make-hash-table with size" (make-hash-table 10))
(pass-if "symbol key round trip with ref"
(let ((t (make-hash-table)))
(hashq-set! t 'key 'value)
(eq? (hashq-ref t 'key) 'value)))
(pass-if "string key round trip with ref"
(let ((t (make-hash-table)))
(hash-set! t "key" 'value)
(eq? (hash-ref t "key") 'value)))
(pass-if "round trip with handle"
(let ((t (make-hash-table)))
(hashq-set! t 'key 'value)
(equal? (hashq-get-handle t 'key) '(key . value))))
(pass-if "hashq-set! leaves other values alone"
(let ((t (make-hash-table)))
(hashq-set! t 'k1 'v1)
(hashq-set! t 'k2 'v2)
(hashq-set! t 'k1 'v3)
(and (eq? (hashq-ref t 'k1) 'v3)
(eq? (hashq-ref t 'k2) 'v2))))
(pass-if "hashq-ref with default"
(let ((t (make-hash-table)))
(eq? (hashq-ref t 'key 'value) 'value)))
(pass-if "hash-ref with default"
(let ((t (make-hash-table 10)))
;; Currently, Mes only looks at the first two characters when
;; hashing. Taking advantage of this, we can try and test with and
;; without hash collisions.
(hash-set! t "ke" 'bar)
(and (eq? (hash-ref t "key" 'foo) 'foo)
(eq? (hash-ref t "k2" 'foo) 'foo))))
(pass-if "hash-map->list"
(let ((t (make-hash-table)))
(hash-set! t "first" 1)
(hash-set! t "second" 2)
(hash-set! t "third" 3)
(let ((a (hash-map->list cons t)))
(and (= (assoc-ref a "first") 1)
(= (assoc-ref a "second") 2)
(= (assoc-ref a "third") 3)))))
(pass-if "integer keys"
(let ((t (make-hash-table)))
(hash-set! t 21 'a)
(hash-set! t 42 'b)
(and (eq? (hash-ref t 42) 'b)
(eq? (hash-ref t 21) 'a))))
(pass-if "hash-table?"
(hash-table? (make-hash-table)))
(pass-if "hash-clear!"
(let ((t (make-hash-table)))
(hashq-set! t 'k1 'v1)
(hashq-set! t 'k2 'v2)
(hash-clear! t)
(not (or (hashq-ref t 'k1)
(hashq-ref t 'k2)))))
(pass-if "hashq-create-handle!"
(let ((t (make-hash-table)))
(hashq-set! t 'k1 'v1)
(let ((h1 (hashq-get-handle t 'k1))
(h2 (hashq-create-handle! t 'k2 'v2)))
(hashq-create-handle! t 'k1 #f)
(and (eq? h1 (hashq-get-handle t 'k1))
(eq? h2 (hashq-get-handle t 'k2))))))
(result 'report)