2008-02-21 18:07:26 -05:00
! Copyright 2007, 2008 Ryan Murphy, Slava Pestov
2007-11-02 15:41:19 -04:00
! See http://factorcode.org/license.txt for BSD license.
2007-12-20 00:34:30 -05:00
USING: arrays kernel math namespaces tools.test
2008-04-04 01:33:06 -04:00
heaps heaps.private math.parser random assocs sequences sorting
2008-04-26 03:01:06 -04:00
accessors math.order ;
2008-03-01 17:00:45 -05:00
IN: heaps.tests
2007-11-02 15:41:19 -04:00
2008-02-06 14:47:19 -05:00
[ <min-heap> heap-pop ] must-fail
[ <max-heap> heap-pop ] must-fail
2007-11-02 15:41:19 -04:00
[ t ] [ <min-heap> heap-empty? ] unit-test
2007-11-05 12:35:44 -05:00
[ f ] [ <min-heap> 1 t pick heap-push heap-empty? ] unit-test
2007-11-02 15:41:19 -04:00
[ t ] [ <max-heap> heap-empty? ] unit-test
2007-11-05 12:35:44 -05:00
[ f ] [ <max-heap> 1 t pick heap-push heap-empty? ] unit-test
2007-11-02 15:41:19 -04:00
! Binary Min Heap
{ 1 2 3 4 5 6 } [ 0 left 0 right 1 left 1 right 2 left 2 right ] unit-test
2008-02-22 17:16:00 -05:00
{ t } [ t 5 f <entry> t 3 f <entry> T{ min-heap } heap-compare ] unit-test
{ f } [ t 5 f <entry> t 3 f <entry> T{ max-heap } heap-compare ] unit-test
2007-11-02 15:41:19 -04:00
2007-11-05 12:48:22 -05:00
[ t 2 ] [ <min-heap> t 300 pick heap-push t 200 pick heap-push t 400 pick heap-push t 3 pick heap-push t 2 pick heap-push heap-pop ] unit-test
2007-11-02 15:41:19 -04:00
2007-11-05 12:48:22 -05:00
[ t 1 ] [ <min-heap> t 300 pick heap-push t 200 pick heap-push t 400 pick heap-push t 3 pick heap-push t 2 pick heap-push t 1 pick heap-push heap-pop ] unit-test
2007-11-02 15:41:19 -04:00
2007-11-05 12:48:22 -05:00
[ t 400 ] [ <max-heap> t 300 pick heap-push t 200 pick heap-push t 400 pick heap-push t 3 pick heap-push t 2 pick heap-push t 1 pick heap-push heap-pop ] unit-test
2007-11-05 12:10:26 -05:00
2008-02-21 18:07:26 -05:00
[ 0 ] [ <max-heap> heap-size ] unit-test
[ 1 ] [ <max-heap> t 1 pick heap-push heap-size ] unit-test
: heap-sort ( alist -- keys )
<min-heap> [ heap-push-all ] keep heap-pop-all ;
: random-alist ( n -- alist )
[
2009-01-21 20:55:47 -05:00
drop 32 random-bits dup number>string
] H{ } map>assoc ;
2008-02-21 18:07:26 -05:00
: test-heap-sort ( n -- ? )
random-alist dup >alist sort-keys swap heap-sort = ;
14 [
[ t ] swap [ 2^ test-heap-sort ] curry unit-test
] each
: test-entry-indices ( n -- ? )
random-alist
<min-heap> [ heap-push-all ] keep
2008-08-29 18:07:30 -04:00
data>> dup length swap [ index>> ] map sequence= ;
2008-02-21 18:07:26 -05:00
14 [
[ t ] swap [ 2^ test-entry-indices ] curry unit-test
] each
: sort-entries ( entries -- entries' )
2008-08-29 17:49:41 -04:00
[ [ key>> ] compare ] sort ;
2008-02-21 18:07:26 -05:00
: delete-test ( n -- ? )
[
random-alist
<min-heap> [ heap-push-all ] keep
2008-04-04 01:33:06 -04:00
dup data>> clone swap
2008-12-17 20:17:37 -05:00
] keep 3 /i [ 2dup [ delete-random ] dip heap-delete ] times
2008-04-04 01:33:06 -04:00
data>>
2008-08-29 17:49:41 -04:00
[ [ key>> ] map ] bi@
2008-03-29 21:36:58 -04:00
[ natural-sort ] bi@ ;
2008-02-21 18:07:26 -05:00
11 [
[ t ] swap [ 2^ delete-test sequence= ] curry unit-test
] each