concatenative 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,44 +0,0 @@
1
- #!usr/bin/env ruby
2
-
3
- dir = File.dirname(File.expand_path(__FILE__))+'/../lib/'
4
-
5
- require dir+"concatenative"
6
-
7
- describe Concatenative do
8
-
9
- it "should define SWONS" do
10
- [[2], 1, :SWAP, :CONS].execute.should == [1,2]
11
- [[2],1, :SWONS].execute.should == [[2],1, :SWAP, :CONS].execute
12
- [[2],1, :SWONS].execute.should == [1,2]
13
- end
14
-
15
- it "should define POPD" do
16
- [1,2,3, :POPD].execute.should == [1,3]
17
- end
18
-
19
- it "should define DUPD" do
20
- [1,2,3, :DUPD].execute.should == [1,2,2,3]
21
- end
22
-
23
- it "should define SWAPD" do
24
- [1,2,3, :SWAPD].execute.should == [2,1,3]
25
- end
26
-
27
- it "should define SIP" do
28
- [[1,2],[3,4],:SIP].execute.should == [[1,2],3,4,[1,2]]
29
- end
30
-
31
- it "should define REP" do
32
- [[2,3, :*], :REP, 2].execute.should == [6,6,2]
33
- end
34
-
35
- it "should define ROLLUP, ROLLDOWN and ROTATE" do
36
- a = [3,2,1]
37
- (a.dup << :ROLLUP).execute.should == [1,3,2]
38
- (a.dup << :ROLLDOWN).execute.should == [2,1,3]
39
- (a.dup << :ROTATE).execute.should == [1,2,3]
40
- end
41
-
42
- end
43
-
44
-
@@ -1,107 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- dir = File.dirname(File.expand_path(__FILE__))+'/../lib/'
4
-
5
- require dir+"concatenative"
6
-
7
- describe Concatenative::System do
8
-
9
- it "should expose CLEAR" do
10
- [1,2,3,4,5, :CLEAR].execute.should == []
11
- end
12
-
13
- it "should expose POP " do
14
- lambda { concatenate :POP }.should raise_error(EmptyStackError)
15
- concatenate(1,2,3,4,:POP, :POP, :POP).should == 1
16
- end
17
-
18
- it "should expose DUP" do
19
- lambda { concatenate :DUP }.should raise_error(EmptyStackError)
20
- concatenate(1,2,:DUP).should == [1,2,2]
21
- end
22
-
23
- it "should expose SWAP" do
24
- lambda { concatenate :SWAP }.should raise_error(EmptyStackError)
25
- [1,3,2, :SWAP].execute.should == [1,2,3]
26
- end
27
-
28
- it "should expose CONS, FIRST and REST" do
29
- [1, [2], :CONS].execute.should == [1,2]
30
- [4, [3], [2, 1], :CONS, :CONS, 5, :SWAP, :CONS].execute.should == [5,4,[3],2,1]
31
- [[1,2,3,4], :REST].execute.should == [2,3,4]
32
- [[1,2,3,4], :FIRST].execute.should == 1
33
- lambda { [1,2,3, :CONS].execute}.should raise_error
34
- end
35
-
36
- it "should expose CAT" do
37
- [[1,2],[3,4], :CAT].execute.should == [1,2,3,4]
38
- end
39
-
40
- it "should handle method arities" do
41
- # Fixnum#>: arity = 1
42
- [2, 20, :>].execute.should == false
43
- ["Test", /T/, 'F', :sub|2].execute.should == "Fest"
44
- [[1,2,3],:join].execute.should == "123"
45
- [[1,2,3],'|',:join|1].execute.should == "1|2|3"
46
- end
47
-
48
- it "should expose I" do
49
- [2, 5, [:*, 6,:+], :I].execute.should == 16
50
- # Check other definitions of :I according to http://tunes.org/~iepos/joy.html
51
- [2, 5, [:*, 6,:+], :DUP, :DIP, :ZAP].execute.should == 16
52
- [2, 5, [:*, 6,:+], [[]], :DIP, :DIP, :ZAP].execute.should == 16
53
- [2, 5, [:*, 6,:+], [[]], :DIP, :DIP, :DIP].execute.should == 16
54
- end
55
-
56
- it "should expose DIP" do
57
- [2, 3, 4, [:+], :DIP].execute.should == [5, 4]
58
- end
59
-
60
- it "should expose UNIT" do
61
- [2, 3, :UNIT].execute.should == [2, [3]]
62
- end
63
-
64
- it "should expose IFTE" do
65
- t = [1000, :>], [2, :/], [3, :*], :IFTE
66
- [1200, *t].execute.should == 600
67
- [800, *t].execute.should == 2400
68
- # Test factorial with explicit recursion
69
- :FACTORIAL.define [0, :==], [:POP, 1], [:DUP, 1, :- , :FACTORIAL, :*], :IFTE
70
- [5, :FACTORIAL].execute.should == 120
71
- end
72
-
73
- it "should expose MAP" do
74
- [[1,2,3,4], [:DUP, :*], :MAP, 1].execute.should == [[1,4,9,16], 1]
75
- end
76
-
77
- it "should expose STEP" do
78
- [[1,2,3,4], [:DUP, :*], :STEP, 1].execute.should == [1,4,9,16, 1]
79
- end
80
-
81
- it "should expose LINREC" do
82
- # factorial
83
- [5, [0, :==], [1, :+], [:DUP, 1, :-], [:*], :LINREC].execute.should == 120
84
- end
85
-
86
- it "should expose PRIMREC" do
87
- # factorial
88
- [5, [1], [:*], :PRIMREC].execute.should == 120
89
- end
90
-
91
- it "should expose TIMES" do
92
- [4, [5, 2, :*], :TIMES].execute.should == [10, 10, 10, 10]
93
- # factorial
94
- [5, 1, 1, :ROLLDOWN, [:DUP, [:*], :DIP, :succ], :TIMES, :POP].execute.should == 120
95
- x1,x2 = 0, 1
96
- res = []
97
- 0.upto(50){ res << x1; x1+=x2; x1,x2= x2,x1}
98
- # Fibonacci number
99
- [50, 0, 1, :ROLLDOWN, [:DUP, [:+], :DIP, :SWAP], :TIMES, :POP].execute.should == res[res.length-1]
100
- end
101
-
102
- it "should expose WHILE" do
103
- # gcd
104
- [40, 25, [0, :>], [:DUP, :ROLLUP, :remainder|1], :WHILE, :POP].execute.should == 5
105
- end
106
-
107
- end