-
Notifications
You must be signed in to change notification settings - Fork 46
/
test.rb
236 lines (184 loc) · 6.68 KB
/
test.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#coding:utf-8
require 'test/unit'
RUN = "python can_source/cantonese.py "
puts "OK sir! Ready to test!!!"
puts %q{
______ __
/ ________ _____ / /_____ ____ ___ ________
/ / / __ `/ __ \/ __/ __ \/ __ \/ _ \/ ___/ _ \
/ /___/ /_/ / / / / /_/ /_/ / / / / __(__ / __/
\____/\__,_/_/ /_/\__/\____/_/ /_/\___/____/\___/
}
class BasicTest < Test::Unit::TestCase
def test_hello_world
res = %x(#{RUN} examples/basic/helloworld.cantonese).encode("UTF-8")
assert res == "Hello World!\n"
end
def test_assign
res = %x(#{RUN} examples/basic/assign.cantonese).encode("UTF-8")
assert res == "1\n3\n"
end
def test_comment
res = %x(#{RUN} examples/basic/comment.cantonese).encode("UTF-8")
assert res == "Run OK\n"
end
def test_assert
res = %x(#{RUN} examples/basic/assert.cantonese).encode("UTF-8")
assert res.include?("AssertionError")
end
def test_class
res = %x(#{RUN} examples/basic/class.cantonese).encode("UTF-8")
assert res == "Duck is swimming\nDuck is sleeping\n公\n"
end
def test_callpython
res = %x(#{RUN} examples/basic/call_python.cantonese).encode("UTF-8")
assert res == "10\n"
end
def test_exit
res = %x(#{RUN} examples/basic/exit.cantonese).encode("UTF-8")
assert res == "执行exit\n"
end
def test_for
res = %x(#{RUN} examples/basic/for.cantonese).encode("UTF-8")
assert res == "1\n2\n3\n4\n1\n2\n3\n"
end
def test_function
res = %x(#{RUN} examples/basic/function.cantonese).encode("UTF-8")
assert res == "Hello\nHello World1\n"
end
def test_if
res = %x(#{RUN} examples/basic/if.cantonese).encode("UTF-8")
assert res == "A 係 3\nB 係 1\n"
end
def test_import
res = %x(#{RUN} examples/basic/import.cantonese).encode("UTF-8")
assert res == "1\n3\n5.0\n1\n测试成功\n"
end
def test_lambda
res = %x(#{RUN} examples/basic/lambda.cantonese).encode("UTF-8")
assert res == "4\n"
end
def test_list
res = %x(#{RUN} examples/basic/list.cantonese).encode("UTF-8")
assert res == "[2, 3, 3]\n3\n2\n3\n3\n[]\n"
end
def test_match
res = %x(#{RUN} examples/basic/match.cantonese).encode("UTF-8")
assert res == "Not found\n"
end
def test_raise
res = %x(#{RUN} examples/basic/raise.cantonese).encode("UTF-8")
assert res.include?("濑嘢!")
end
def test_set
res = %x(#{RUN} examples/basic/set.cantonese).encode("UTF-8")
assert (not res.include?("濑嘢!")) and res != ""
end
def test_try_finally
res = %x(#{RUN} examples/basic/try_finally.cantonese).encode("UTF-8")
assert res == "揾到NameError\n执手尾: \n1 1\n"
end
def test_type
res = %x(#{RUN} examples/basic/type.cantonese).encode("UTF-8")
assert res == "<class 'int'>\n<class 'can_source.can_libs.std.impl.Str'>\n"
end
def test_while
res = %x(#{RUN} examples/basic/while.cantonese).encode("UTF-8")
assert res == "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n"
end
end
class AlgoTest < Test::Unit::TestCase
def test_binary_search
res = %x(#{RUN} examples/algorithms/binary_search.cantonese).encode("UTF-8")
assert res == "揾到啦!!!\n揾唔到: (\n"
end
def test_bubble_sort
res = %x(#{RUN} examples/algorithms/bubble_sort.cantonese).encode("UTF-8")
assert res == "[11, 12, 22, 25, 34, 64, 90]\n[12, 21, 22, 55, 77, 90, 97]\n"
end
def test_fib
res = %x(#{RUN} examples/algorithms/fib.cantonese).encode("UTF-8")
assert res == "55\n1\n"
end
def test_factorial
res = %x(#{RUN} examples/algorithms/factorial.cantonese).encode("UTF-8")
assert res == "2\n720\n"
end
def test_fizzbuzz
res = %x(#{RUN} examples/algorithms/fizzbuzz.cantonese).encode("UTF-8")
assert (not res.include?("濑嘢!"))
end
def test_insert_sort
res = %x(#{RUN} examples/algorithms/insert_sort.cantonese).encode("UTF-8")
assert res == "[11, 12, 22, 25, 34, 64, 90]\n[12, 21, 22, 55, 77, 90, 97]\n"
end
def test_linear_search
res = %x(#{RUN} examples/algorithms/linear_search.cantonese).encode("UTF-8")
assert res == "揾到啦:)\n揾唔到:(\n"
end
def test_max
res = %x(#{RUN} examples/algorithms/max.cantonese).encode("UTF-8")
assert res == "34\n27\n"
end
def test_Tower_of_Hanoi
res = %x(#{RUN} examples/algorithms/Tower_of_Hanoi.cantonese).encode("UTF-8")
assert (not res.include?("濑嘢!")) and res != ""
end
def test_climbStairs
res = %x(#{RUN} examples/leetcode/climbStairs.cantonese).encode("UTF-8")
assert (not res.include?("濑嘢!")) and res != ""
end
def test_getSum
res = %x(#{RUN} examples/leetcode/getSum.cantonese).encode("UTF-8")
assert (not res.include?("濑嘢!")) and res != ""
end
def test_numIdenticalPairs
res = %x(#{RUN} examples/leetcode/numIdenticalPairs.cantonese).encode("UTF-8")
assert (not res.include?("濑嘢!")) and res != ""
end
def test_rotateString
res = %x(#{RUN} examples/leetcode/rotateString.cantonese).encode("UTF-8")
assert (not res.include?("濑嘢!")) and res != ""
end
def test_singleNumber
res = %x(#{RUN} examples/leetcode/singleNumber.cantonese).encode("UTF-8")
assert (not res.include?("濑嘢!")) and res != ""
end
end
class MiscTest < Test::Unit::TestCase
def test_calc_corr
res = %x(#{RUN} examples/numerical/calc_corr.cantonese).encode("UTF-8")
assert res == "0.8066499427138474\n"
end
def test_Matrix
res = %x(#{RUN} examples/numerical/Matrix.cantonese).encode("UTF-8")
assert res == "Matrix: [[1, 1], [2, 2]]\nMatrix: [[2, 2], [3, 3]]\nMatrix: [[3, 3], [5, 5]]\nMatrix: [[3, 3, 3], [6, 6, 6]]\n"
end
def test_knn
res = %x(#{RUN} examples/machine_learning/KNN.cantonese).encode("UTF-8")
assert res == "动作片\n"
end
def test_linear_regression
res = %x(#{RUN} examples/machine_learning/linear_regression.cantonese).encode("UTF-8")
assert res == "Linear function is:\ny=0.530960991635149x+189.75347155122432\n667.6183640228585\n"
end
end
class LibTest < Test::Unit::TestCase
def test_csv_parse
res = %x(#{RUN} examples/lib_sample/csv_parse.cantonese).encode("UTF-8")
assert res == "['id', 'name', ' age', 'gender', 'class_num']\n['1001', '张三', '18', 'male', '01']\n['1002', '李四', '19', 'male', '01']\n['1003', '王五', '19', 'famale', '01']\n['1004', '李华', '18', 'male', '01']\n"
end
def test_file
res = %x(#{RUN} examples/lib_sample/file.cantonese).encode("UTF-8")
assert (not res.include?("濑嘢!")) and res != ""
end
def test_random
res = %x(#{RUN} examples/lib_sample/random.cantonese).encode("UTF-8")
assert (not res.include?("濑嘢!")) and res != ""
end
def test_re
res = %x(#{RUN} examples/lib_sample/re.cantonese).encode("UTF-8")
assert res == "(0, 3)\nNone\n"
end
end
# AppGUITest