-USING: accessors assocs continuations hashtables io kernel make
-math namespaces prettyprint sequences sequences.private
+USING: accessors assocs continuations fry hashtables io kernel
+make math namespaces prettyprint sequences sequences.private
tools.test vectors ;
IN: hashtables.tests
H{ } "x" set
100 [ drop "x" get clear-assoc ] each-integer
-! Crash discovered by erg
-{ t } [ 0.75 <hashtable> dup clone = ] unit-test
+! non-integer capacity not allowed
+[ 0.75 <hashtable> ] must-fail
! Another crash discovered by erg
{ } [
[ [ neg ] dip sq ] assoc-map
] unit-test
+! make sure growth and capacity use same load-factor
+{ t } [
+ 100 iota
+ [ [ <hashtable> ] map ]
+ [ [ H{ } clone [ '[ dup _ set-at ] each-integer ] keep ] map ] bi
+ [ [ array>> length ] bi@ = ] 2all?
+] unit-test
+
! Bug discovered by littledan
{ { 5 5 5 5 } } [
[
[ no-key ] [ 2dup hash@ 0 (key@) ] if ; inline
: <hash-array> ( n -- array )
- 1 + next-power-of-2 4 * ((empty)) <array> ; inline
+ 3 * 1 + 2/ next-power-of-2 2 * ((empty)) <array> ; inline
: init-hash ( hash -- )
0 >>count 0 >>deleted drop ; inline
[ array>> 2dup hash@ 0 f (new-key@) ] keep swap
[ over [ hash-deleted- ] [ hash-count+ ] if swap or ] [ 2drop ] if ; inline
-: set-nth-pair ( value key seq n -- )
+: set-nth-pair ( value key array n -- )
2 fixnum+fast [ set-slot ] 2keep
1 fixnum+fast set-slot ; inline
[ swapd (set-at) ] curry assoc-each ; inline
: hash-large? ( hash -- ? )
- [ count>> 3 fixnum*fast ]
+ [ count>> 1 fixnum+fast 3 fixnum*fast ]
[ array>> length>> ] bi fixnum>= ; inline
: each-pair ( ... array quot: ( ... key value -- ... ) -- ... )
PRIVATE>
: <hashtable> ( n -- hash )
+ integer>fixnum-strict
[ 0 0 ] dip <hash-array> hashtable boa ; inline
M: hashtable at*