1 ! Copyright 2007, 2008 Ryan Murphy, Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays kernel math namespaces tools.test
4 heaps heaps.private math.parser random assocs sequences sorting
5 accessors math.order locals ;
8 [ <min-heap> heap-pop ] must-fail
9 [ <max-heap> heap-pop ] must-fail
11 { t } [ <min-heap> heap-empty? ] unit-test
12 { f } [ <min-heap> 1 t pick heap-push heap-empty? ] unit-test
13 { t } [ <max-heap> heap-empty? ] unit-test
14 { f } [ <max-heap> 1 t pick heap-push heap-empty? ] unit-test
17 { 1 2 3 4 5 6 } [ 0 left 0 right 1 left 1 right 2 left 2 right ] unit-test
18 { t } [ t 5 f <entry> t 3 f <entry> T{ min-heap } heap-compare ] unit-test
19 { f } [ t 5 f <entry> t 3 f <entry> T{ max-heap } heap-compare ] unit-test
21 { 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
23 { 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
25 { 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
27 { 0 } [ <max-heap> heap-size ] unit-test
28 { 1 } [ <max-heap> t 1 pick heap-push heap-size ] unit-test
30 DEFER: (assert-heap-invariant)
32 : ((assert-heap-invariant)) ( parent child heap heap-size -- )
34 [ [ heapdata-compare f assert= ] 2keep ] dip
35 (assert-heap-invariant)
38 : (assert-heap-invariant) ( n heap heap-size -- )
39 [ dup left dup 1 + ] 2dip
40 [ ((assert-heap-invariant)) ] 2curry bi-curry@ bi ;
42 : assert-heap-invariant ( heap -- )
43 dup heap-empty? [ drop ]
44 [ 0 swap dup heap-size (assert-heap-invariant) ] if ;
46 : heap-sort ( alist heap -- keys )
47 [ heap-push-all ] keep dup assert-heap-invariant heap-pop-all ;
49 : random-alist ( n -- alist )
51 drop 32 random-bits dup number>string
52 ] H{ } map>assoc >alist ;
54 :: test-heap-sort ( n heap reverse? -- ? )
56 [ sort-keys reverse? [ reverse ] when ] keep
59 : test-minheap-sort ( n -- ? )
60 <min-heap> f test-heap-sort ;
62 : test-maxheap-sort ( n -- ? )
63 <max-heap> t test-heap-sort ;
66 [ t ] swap [ 2^ <min-heap> f test-heap-sort ] curry unit-test
70 [ t ] swap [ 2^ <max-heap> t test-heap-sort ] curry unit-test
73 : test-entry-indices ( n -- ? )
75 <min-heap> [ heap-push-all ] keep
76 dup assert-heap-invariant
77 data>> dup length <iota> swap [ index>> ] map sequence= ;
80 [ t ] swap [ 2^ test-entry-indices ] curry unit-test
83 : delete-test ( n -- obj1 obj2 )
86 <min-heap> [ heap-push-all ] keep
88 ] keep 3 /i [ 2dup [ delete-random ] dip heap-delete ] times
89 dup assert-heap-invariant
92 [ natural-sort ] bi@ ;
95 [ t ] swap [ 2^ delete-test sequence= ] curry unit-test
100 t 1 heap heap-push* :> entry
103 entry heap heap-delete ] [ bad-heap-delete? ] must-fail-with
107 t 1 heap heap-push* :> entry
110 entry heap heap-delete ] [ bad-heap-delete? ] must-fail-with
114 t 1 heap heap-push* :> entry
116 entry heap heap-delete
117 entry heap heap-delete ] [ bad-heap-delete? ] must-fail-with
122 t 1 heap heap-push* :> entry
123 entry heap heap-delete
124 entry heap heap-delete ] [ bad-heap-delete? ] must-fail-with