]> gitweb.factorcode.org Git - factor.git/blob - basis/heaps/heaps-tests.factor
7fa0d5fae0ca16a41e1bd84185d33ccf2e5c0079
[factor.git] / basis / heaps / heaps-tests.factor
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 ;
6 IN: heaps.tests
7
8 [ <min-heap> heap-pop ] must-fail
9 [ <max-heap> heap-pop ] must-fail
10
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
15
16 ! Binary Min Heap
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
20
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
22
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
24
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
26
27 { 0 } [ <max-heap> heap-size ] unit-test
28 { 1 } [ <max-heap> t 1 pick heap-push heap-size ] unit-test
29
30 : heap-sort ( alist heap -- keys )
31     [ heap-push-all ] keep heap-pop-all ;
32
33 : random-alist ( n -- alist )
34     iota [
35         drop 32 random-bits dup number>string
36     ] H{ } map>assoc >alist ;
37
38 :: test-heap-sort ( n heap reverse? -- ? )
39     n random-alist
40     [ sort-keys reverse? [ reverse ] when ] keep
41     heap heap-sort = ;
42
43 : test-minheap-sort ( n -- ? )
44     <min-heap> f test-heap-sort ;
45
46 : test-maxheap-sort ( n -- ? )
47     <max-heap> t test-heap-sort ;
48
49 14 [
50     [ t ] swap [ 2^ <min-heap> f test-heap-sort ] curry unit-test
51 ] each-integer
52
53 14 [
54     [ t ] swap [ 2^ <max-heap> t test-heap-sort ] curry unit-test
55 ] each-integer
56
57 : test-entry-indices ( n -- ? )
58     random-alist
59     <min-heap> [ heap-push-all ] keep
60     data>> dup length iota swap [ index>> ] map sequence= ;
61
62 14 [
63     [ t ] swap [ 2^ test-entry-indices ] curry unit-test
64 ] each-integer
65
66 : sort-entries ( entries -- entries' )
67     [ key>> ] sort-with ;
68
69 : delete-test ( n -- obj1 obj2 )
70     [
71         random-alist
72         <min-heap> [ heap-push-all ] keep
73         dup data>> clone swap
74     ] keep 3 /i [ 2dup [ delete-random ] dip heap-delete ] times
75     data>>
76     [ [ key>> ] map ] bi@
77     [ natural-sort ] bi@ ;
78
79 11 [
80     [ t ] swap [ 2^ delete-test sequence= ] curry unit-test
81 ] each-integer