-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht.rb
89 lines (79 loc) · 2.95 KB
/
t.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
require "minitest/autorun"
require "stringio"
require_relative "./honestb.rb"
class TestHonestB < Minitest::Test
def setup
@hb = HonestB.new
[:cn2i, :car, :cdr, :ast2expr].each{|s| self.class.define_method(s){|*a| @hb.send(s, *a) }}
@cons = ast2expr([[:S, [[:S, [:K, :S]], [[:S, [:K, :K]], [[:S, [:K, :S]], [[:S, [:K, [:S, :I]]], :K]]]]], [:K, :K]])
@cnums = @hb.instance_variable_get("@cnums")
end
def test_parse
assert_equal "[[:S, :K], :K]", @hb.parse("クイッククイックスロー❤❤").to_s
assert_equal "[:S, [:K, :K]]", @hb.parse("クイッククイック❤スロー❤").to_s
["\u2764", "\u2764\ufe0e", "\u2764\ufe0f"].each {|c|
assert_equal "[:S, :K]", @hb.parse("クイックスロー" + c).to_s
}
end
def test_primitive_cnum
[0, 1, 2, 128, 256].each{|i|
assert_equal i, cn2i(@cnums[i])
}
end
def test_composed_cnum
assert_equal 0, cn2i(ast2expr([:K, :I]))
assert_equal 1, cn2i(ast2expr(:I))
assert_equal 0, cn2i(ast2expr([:S, :K]))
assert_equal 1, cn2i(ast2expr([[:S, :K], :K]))
cn2 = [[:S, [[:S, [:K, :S]], :K]], :I] # ``s``s`kski
cn3 = [[:S, [[:S, [:K, :S]], :K]], [[:S, [[:S, [:K, :S]], :K]], :I]]
assert_equal 2, cn2i(ast2expr(cn2))
assert_equal 8, cn2i(ast2expr([cn3, cn2]))
assert_equal 64, cn2i(ast2expr([cn2, [cn3, cn2]]))
assert_equal 256, cn2i(ast2expr([[cn3, cn2], cn2]))
end
def test_comb
s = HonestB::Expr.new(:S)
k = HonestB::Expr.new(:K)
n1 = HonestB::Expr.new(:NUM, 1)
n2 = HonestB::Expr.new(:NUM, 2)
n3 = HonestB::Expr.new(:NUM, 3)
assert_equal 1, k.apply(n1).apply(n2).eval.to_i
assert_equal 2, k.apply(k).apply(n1).apply(n2).apply(n3).eval.to_i
assert_equal 1, s.apply(k).apply(k).apply(n1).eval.to_i
end
def test_list
l = [256, 255, 128, 0].map{|i| @cnums[i] }.reduce{|a, c|
@cons.apply(c).apply(a)
}
assert_equal 0, cn2i(car(l)).to_i
assert_equal 128, cn2i(car(cdr(l))).to_i
assert_equal 255, cn2i(car(cdr(cdr(l)))).to_i
assert_equal 256, cn2i(cdr(cdr(cdr(l)))).to_i
end
def test_output
n0 = HonestB::Expr.new(:Num, 0)
l = [0, 256, 67, 66, 65].map{|i| @cnums[i] }.reduce{|a, c| @cons.apply(c).apply(a) }
stdin = StringIO.new("")
stdout = StringIO.new
HonestB.new(stdin, stdout).send(:print_list, l)
assert_equal "ABC", stdout.string
end
def test_input
stdin = StringIO.new("foo")
stdout = StringIO.new
hb = HonestB.new(stdin, stdout)
input = HonestB::Expr.new(:READ, hb.method(:readc), 0)
assert_equal 102, cn2i(car(input))
assert_equal 111, cn2i(car(cdr(input)))
assert_equal 111, cn2i(car(cdr(cdr(input))))
assert_equal 256, cn2i(car(cdr(cdr(cdr(input)))))
end
def test_echo
stdin = StringIO.new("ご友人\u2764\u2764")
stdout = StringIO.new
hb = HonestB.new(stdin, stdout)
hb.run(StringIO.new("クイッククイックスロー\u2764\u2764"))
assert_equal "ご友人\u2764\u2764", stdout.string
end
end