]> gitweb.factorcode.org Git - factor.git/blob - extra/match/match-tests.factor
Initial import
[factor.git] / extra / match / match-tests.factor
1 ! Copyright (C) 2006 Chris Double.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: tools.test match namespaces arrays ;
4 IN: temporary
5
6 MATCH-VARS: ?a ?b ;
7
8 [ H{ { ?a 1 } { ?b 2 } } ] [
9  { ?a ?b } { 1 2 } match
10 ] unit-test
11
12 [ { 1 2 } ] [ 
13   { 1 2 } 
14   {
15     { { ?a ?b } [ ?a ?b 2array ] }
16   } match-cond
17 ] unit-test
18
19 [ t ] [ 
20   { 1 2 } 
21   {
22     { { 1 2 } [ t ] }
23     { f [ f ] }
24   } match-cond
25 ] unit-test
26
27 [ t ] [ 
28   { 1 3 } 
29   {
30     { { 1 2 } [ t ] }
31     { { 1 3 } [ t ] }
32   } match-cond
33 ] unit-test
34
35 [ f ] [ 
36   { 1 5 } 
37   {
38     { { 1 2 } [ t ] }
39     { { 1 3 } [ t ] }
40     { _       [ f ] }
41   } match-cond
42 ] unit-test
43
44 TUPLE: foo a b ;
45
46 C: <foo> foo
47
48 { 1 2 } [
49   1 2 <foo> T{ foo f ?a ?b } match [
50     ?a ?b
51   ] bind
52 ] unit-test
53
54 { 1 2 } [
55   1 2 <foo> \ ?a \ ?b <foo> match [
56     ?a ?b
57   ] bind
58 ] unit-test
59
60 { H{ { ?a ?a } } } [ 
61   \ ?a \ ?a match 
62 ] unit-test
63
64 [ "match" ] [ 
65   "abcd" {
66     { ?a [ "match" ] }
67   } match-cond
68 ] unit-test
69
70 [
71   { 2 1 }
72 ] [
73   { "a" 1 2 "b" } { _ ?a ?b _ } { ?b ?a } match-replace
74 ] unit-test
75
76 TUPLE: match-replace-test a b ;
77
78 [
79     T{ match-replace-test f 2 1 }
80 ] [
81   T{ match-replace-test f 1 2 }
82   T{ match-replace-test f ?a ?b }
83   T{ match-replace-test f ?b ?a }
84   match-replace
85 ] unit-test