PageRenderTime 18ms CodeModel.GetById 1ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/extra/variants/variants-tests.factor

http://github.com/abeaumont/factor
Unknown | 40 lines | 31 code | 9 blank | 0 comment | 0 complexity | 0d7371b2ede3bfba27a8a5ce289ff169 MD5 | raw file
 1! (c)2009 Joe Groff bsd license
 2USING: kernel math tools.test variants slots ;
 3IN: variants.tests
 4
 5VARIANT: list
 6    nil
 7    cons: { { first object } { rest list } }
 8    ;
 9
10[ t ] [ nil list? ] unit-test
11[ t ] [ 1 nil <cons> list? ] unit-test
12[ f ] [ 1 list? ] unit-test
13
14: list-length ( list -- length )
15    {
16        { nil  [ 0 ] }
17        { cons [ nip list-length 1 + ] }
18    } match ;
19
20[ 4 ]
21[ 5 6 7 8 nil <cons> <cons> <cons> <cons> list-length ] unit-test
22
23[ nil t ] [ list initial-value ] unit-test
24
25VARIANT: list2 ;
26VARIANT-MEMBER: list2 nil2
27VARIANT-MEMBER: list2 cons2: { { first object } { rest list2 } }
28
29[ t ] [ nil2 list2? ] unit-test
30[ t ] [ 1 nil2 <cons2> list2? ] unit-test
31[ f ] [ 1 list2? ] unit-test
32
33: list2-length ( list2 -- length )
34    {
35        { nil2  [ 0 ] }
36        { cons2 [ nip list2-length 1 + ] }
37    } match ;
38
39[ 4 ]
40[ 5 6 7 8 nil2 <cons2> <cons2> <cons2> <cons2> list2-length ] unit-test