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
2011-04-10 13:57:39 -04:00
accessors math.order locals ;
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
2011-04-10 13:57:39 -04:00
: heap-sort ( alist heap -- keys )
[ heap-push-all ] keep heap-pop-all ;
2008-02-21 18:07:26 -05:00
: random-alist ( n -- alist )
2010-01-14 10:10:13 -05:00
iota [
2009-01-21 20:55:47 -05:00
drop 32 random-bits dup number>string
2011-04-10 13:57:39 -04:00
] H{ } map>assoc >alist ;
2008-02-21 18:07:26 -05:00
2011-04-10 13:57:39 -04:00
:: test-heap-sort ( n heap reverse? -- ? )
n random-alist
[ sort-keys reverse? [ reverse ] when ] keep
heap heap-sort = ;
: test-minheap-sort ( n -- ? )
<min-heap> f test-heap-sort ;
: test-maxheap-sort ( n -- ? )
<max-heap> t test-heap-sort ;
14 [
[ t ] swap [ 2^ <min-heap> f test-heap-sort ] curry unit-test
] each-integer
2008-02-21 18:07:26 -05:00
14 [
2011-04-10 13:57:39 -04:00
[ t ] swap [ 2^ <max-heap> t test-heap-sort ] curry unit-test
2010-01-14 10:10:13 -05:00
] each-integer
2008-02-21 18:07:26 -05:00
: test-entry-indices ( n -- ? )
random-alist
<min-heap> [ heap-push-all ] keep
2010-01-14 10:10:13 -05:00
data>> dup length iota swap [ index>> ] map sequence= ;
2008-02-21 18:07:26 -05:00
14 [
[ t ] swap [ 2^ test-entry-indices ] curry unit-test
2010-01-14 10:10:13 -05:00
] each-integer
2008-02-21 18:07:26 -05:00
: sort-entries ( entries -- entries' )
2009-08-02 21:09:23 -04:00
[ key>> ] sort-with ;
2008-02-21 18:07:26 -05:00
2009-04-17 15:44:08 -04:00
: delete-test ( n -- obj1 obj2 )
2008-02-21 18:07:26 -05:00
[
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
2010-01-14 10:10:13 -05:00
] each-integer