| 1 | module scheme |
| 2 | |
| 3 | fn table_text(mut vm VM, source string) string { |
| 4 | return write(vm.eval(source) or { panic('${source}: ${err}') }) or { panic(err) } |
| 5 | } |
| 6 | |
| 7 | fn test_hashtable_basic_operations_inspection_and_binding_collections() { |
| 8 | for mode in ['make-eq-hashtable', 'make-eqv-hashtable', |
| 9 | '(lambda () (make-hashtable (lambda (x) 0) =))'] { |
| 10 | mut vm := new() |
| 11 | assert table_text(mut vm, '(define h (${mode})) |
| 12 | (list (hashtable? h) (hashtable-mutable? h) (hashtable-size h) |
| 13 | (hashtable-ref h 1 99) (hashtable-contains? h 1))') == '(#t #t 0 99 #f)' |
| 14 | assert table_text(mut vm, '(hashtable-set! h 1 10) (hashtable-set! h 2 20) |
| 15 | (hashtable-set! h 1 11) (hashtable-update! h 2 (lambda (v) (+ v 1)) 100) |
| 16 | (hashtable-update! h 3 (lambda (v) (+ v 1)) 100) |
| 17 | (list (hashtable-size h) (hashtable-ref h 1 #f) (hashtable-ref h 2 #f) (hashtable-ref h 3 #f))') == '(3 11 21 101)' |
| 18 | assert table_text(mut vm, '(hashtable-delete! h 2) (hashtable-delete! h 999) |
| 19 | (list (hashtable-size h) (hashtable-contains? h 2) (hashtable-ref h 2 42))') == '(2 #f 42)' |
| 20 | assert table_text(mut vm, '(call-with-values (lambda () (hashtable-entries h)) |
| 21 | (lambda (keys vals) (for-all (lambda (i) (= (hashtable-ref h (vector-ref keys i) #f) (vector-ref vals i))) |
| 22 | (list 0 1))))') == '#t' |
| 23 | assert table_text(mut vm, '(hashtable-clear! h 8) (list (hashtable-size h) (hashtable-keys h))') == '(0 #())' |
| 24 | } |
| 25 | mut vm := new() |
| 26 | assert table_text(mut vm, '(define e (make-eq-hashtable)) (define v (make-eqv-hashtable)) |
| 27 | (list (eq? (hashtable-equivalence-function e) eq?) (eq? (hashtable-equivalence-function v) eqv?) |
| 28 | (hashtable-hash-function e) (hashtable-hash-function v))') == '(#t #t #f #f)' |
| 29 | assert table_text(mut vm, '(define hf (lambda (x) 0)) (define ef =) |
| 30 | (define h (make-hashtable hf ef 32)) |
| 31 | (list (eq? hf (hashtable-hash-function h)) (eq? ef (hashtable-equivalence-function h)))') == '(#t #t)' |
| 32 | for origin in ['vile', 'rnrs'] { |
| 33 | assert table_text(mut vm, '(import (prefix (${origin} hashtables) ${origin}:)) |
| 34 | (eq? ${origin}:hashtable-ref hashtable-ref)') == '#t' |
| 35 | } |
| 36 | assert write(vm.eval_alt('h:=make-eqv-hashtable()\nhashtable-set!(h,1,42)\nhashtable-ref(h,1,0)') or { panic(err) }) or { panic(err) } == '42' |
| 37 | } |
| 38 | |
| 39 | fn test_hashtable_copies_are_shallow_independent_and_immutable_by_default() { |
| 40 | mut vm := new() |
| 41 | assert table_text(mut vm, '(define h (make-hashtable string-hash string=?)) (define key (string-copy "key")) |
| 42 | (define value (vector 42)) (hashtable-set! h key value) |
| 43 | (define frozen (hashtable-copy h)) (define writable (hashtable-copy frozen 42)) |
| 44 | (hashtable-set! writable "other" 99) (hashtable-delete! h key) |
| 45 | (list (hashtable-size h) (hashtable-size frozen) (hashtable-size writable) |
| 46 | (hashtable-mutable? frozen) (hashtable-mutable? writable) |
| 47 | (eq? (vector-ref (hashtable-keys frozen) 0) key) (eq? (hashtable-ref frozen "key" #f) value))') == '(0 1 2 #f #t #t #t)' |
| 48 | for source in ['(hashtable-set! frozen "x" 1)', '(hashtable-delete! frozen "key")', |
| 49 | '(hashtable-clear! frozen)', '(hashtable-update! frozen "key" (lambda (x) (/ 1 0)) 0)'] { |
| 50 | if _ := vm.eval(source) { |
| 51 | assert false, source |
| 52 | } else { |
| 53 | assert err.msg().contains('immutable') |
| 54 | } |
| 55 | } |
| 56 | assert table_text(mut vm, '(define snapshot (hashtable-keys frozen)) (vector-set! snapshot 0 "changed") |
| 57 | (list (hashtable-contains? frozen "key") (hashtable-contains? frozen "changed"))') == '(#t #f)' |
| 58 | assert table_text(mut vm, '(vector-set! value 0 100) (hashtable-ref frozen "key" #f)') == '#(100)' |
| 59 | } |
| 60 | |
| 61 | fn test_hashtable_deleted_slots_release_handles_and_storage_is_reused() { |
| 62 | mut vm := new_with_options(Options{ max_steps: 0 }) or { panic(err) } |
| 63 | vm.eval('(define h (make-eqv-hashtable)) |
| 64 | (let loop ((n 2000)) (if (= n 0) 0 (begin |
| 65 | (hashtable-set! h n (vector n)) (hashtable-delete! h n) (loop (- n 1)))))') or { panic(err) } |
| 66 | table := vm.lookup('h') or { panic(err) }.hashtable_pointer() |
| 67 | assert table.size == 0 && table.entries.len == 1 && table.buckets.len == 0 |
| 68 | assert !table.entries[0].live && table.entries[0].key.tag == .unspecified && table.entries[0].value.tag == .unspecified |
| 69 | vm.eval('(hashtable-set! h 1 (vector 1)) (hashtable-clear! h)') or { panic(err) } |
| 70 | assert table.entries.len == 0 && table.size == 0 |
| 71 | } |
| 72 | |
| 73 | fn test_hashtable_type_arity_capacity_and_callback_failures_are_recoverable() { |
| 74 | mut vm := new() |
| 75 | for source in ['(make-eq-hashtable -1)', '(make-eqv-hashtable 1.0)', '(make-hashtable 1 =)', |
| 76 | '(make-hashtable (lambda (x) 0) 1)', '(make-hashtable (lambda (x) 0) = 9223372036854775807)', |
| 77 | '(hashtable-ref 1 2 3)', '(hashtable-set! (make-eq-hashtable) 1)', '(hashtable-size)', |
| 78 | '(hashtable-update! (make-eq-hashtable) 1 2 3)', '(hashtable-clear! (make-eq-hashtable) -1)', |
| 79 | '(hashtable-copy (make-eq-hashtable) #t #f)', '(string-hash 1)', '(string-ci-hash 1)', |
| 80 | '(symbol-hash "x")', '(hashtable-ref (make-hashtable (lambda (x) -1) =) 1 #f)', |
| 81 | '(hashtable-ref (make-hashtable (lambda (x) 1.0) =) 1 #f)', |
| 82 | '(hashtable-ref (make-hashtable (lambda (x) (values)) =) 1 #f)', |
| 83 | '(hashtable-ref (make-hashtable (lambda () 0) =) 1 #f)', |
| 84 | '(define bad (make-hashtable (lambda (x) 0) (lambda (x y) (values 1 2)))) |
| 85 | (hashtable-set! bad 1 10) (hashtable-ref bad 1 #f)', |
| 86 | '(hashtable-update! (make-eqv-hashtable) 1 (lambda (x) (values)) 0)'] { |
| 87 | if _ := vm.eval('\n' + source) { |
| 88 | assert false, source |
| 89 | } else { |
| 90 | assert err is EvalError, '${source}: ${err}' |
| 91 | } |
| 92 | assert vm.active.len == 0 |
| 93 | assert table_text(mut vm, '42') == '42' |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | fn test_builtin_table_predicate_inspection_uses_pristine_bindings_after_redefinition() { |
| 98 | mut vm := new() |
| 99 | assert table_text(mut vm, '(define original eq?) (set! eq? (lambda (a b) #f)) |
| 100 | (define h (make-eq-hashtable)) (hashtable-set! h 1 42) |
| 101 | (list (original original (hashtable-equivalence-function h)) (hashtable-ref h 1 0))') == '(#t 42)' |
| 102 | } |
| 103 | |