-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.rb
292 lines (242 loc) · 6.99 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
require "test/unit"
require 'test/unit/ui/console/testrunner'
require_relative "calc.tab.rb"
class MacroParserTest < Test::Unit::TestCase
def test_not
parser = MacroParser.new
parser.split_parse("define A (!0);define B(x) !A ? 10*x : 100+x")
ret = parser.exe("A")
assert_equal(ret, true)
ret = parser.exe("B(1)")
assert_equal(ret, 101)
end
def test_uminus
parser = MacroParser.new
parser.split_parse("define A -1;define B(x) -A+1-1-A+x+(-x)")
ret = parser.exe("A")
assert_equal(ret, -1)
ret = parser.exe("B(10)")
assert_equal(ret, 2)
end
def test_reverse
parser = MacroParser.new
parser.split_parse("define A ~0;define B(x) -A+1+((~0xFFFF0000)&x)")
ret = parser.exe("A")
assert_equal(ret, -1)
ret = parser.exe("B(0x12345678)")
assert_equal(ret, 0x567A)
end
def test_leftshift
parser = MacroParser.new
parser.split_parse("define A 0x12;define B(x) -((~A)<<x)")
ret = parser.exe("B(8)")
assert_equal(ret, 4864)
end
def test_rightshift
parser = MacroParser.new
parser.split_parse("define A(n) 0x124&n;define B(x) ((A(0xff)<<x)>>x)<<x")
ret = parser.exe("B(8)")
assert_equal(ret, 0x2400)
end
def test_no_para
parser = MacroParser.new
parser.parse("define A (((1+1)+2)+3)/2*2")
ret = parser.exe("A")
assert_equal(ret, 6)
parser.parse("define A 1+2*3+(20+15)/5 -1")
ret = parser.exe("A")
assert_equal(ret, 13)
parser.parse("define A 5%(2+1)")
ret = parser.exe("A")
assert_equal(ret,2)
parser.parse("define A 2|1")
ret = parser.exe("A")
assert_equal(ret,3)
parser.parse("define A 1&2")
ret = parser.exe("A")
assert_equal(ret,0)
parser.parse("define A 0xFFFFFFFF&&0x1234")
ret = parser.exe("A")
assert_equal(ret,0x1234)
end
def test_hex_numer
parser = MacroParser.new
parser.parse("define A (0x11+0x1+10)*0x2")
parser.parse("define B(x0) A+0x0*0x2+0x2*x0")
ret = parser.exe("A")
assert_equal(ret, 56)
ret = parser.exe("B((0x1+0x5)/2)")
assert_equal(ret, 62)
end
def test_1_para
parser = MacroParser.new
parser.parse("define A(x) 3*((x+1)/2)*x*x")
ret = parser.exe("A(4)")
assert_equal(ret, 96)
parser.parse("define z(x) x")
ret = parser.exe("z(4)")
assert_equal(ret, 4)
parser.parse("define B(x) A(x)+(A(4)/4)+1")
ret = parser.exe("B(4)")
assert_equal(ret, 121)
parser.split_parse("define A(x) x*2;define B(a1) a1+4;define C(_x) A(B(_x)+B(1)+1)+1")
ret = parser.exe("C(2)")
assert_equal(ret, 25)
end
def test_logic_op
parser = MacroParser.new
parser.parse("define A(x,y) x>y")
ret = parser.exe("A(4,5)")
assert_equal(ret, false)
ret = parser.exe("A(5,4)")
assert_equal(ret, true)
ret = parser.exe("A(4,4)")
assert_equal(ret, false)
parser.parse("define A(x,y) x>=y")
ret = parser.exe("A(4,5)")
assert_equal(ret, false)
ret = parser.exe("A(5,4)")
assert_equal(ret, true)
ret = parser.exe("A(4,4)")
assert_equal(ret, true)
parser.parse("define A(x,y) x<y")
ret = parser.exe("A(4,5)")
assert_equal(ret, true)
ret = parser.exe("A(5,4)")
assert_equal(ret, false)
ret = parser.exe("A(4,4)")
assert_equal(ret, false)
parser.parse("define A(x,y) x<=y")
ret = parser.exe("A(4,5)")
assert_equal(ret, true)
ret = parser.exe("A(5,4)")
assert_equal(ret, false)
ret = parser.exe("A(4,4)")
assert_equal(ret, true)
parser.parse("define A(x,y) x==y")
ret = parser.exe("A(4,5)")
assert_equal(ret, false)
ret = parser.exe("A(4,4)")
assert_equal(ret, true)
end
def test_2_para
parser = MacroParser.new
parser.split_parse("define A(x,y) (x*x+y/y+1);define B(x) A(4,(1+4))+A(x,x+1)")
ret = parser.exe("A(4,(1+4))")
assert_equal(ret, 18)
ret = parser.exe("B(4)")
assert_equal(ret, 36)
parser.split_parse("define A1(x) x*x;define A2(x,y) 1*A1(x)+1*A1(y)+0/5*8")
ret = parser.exe("A2(2,3)")
assert_equal(ret, 13)
parser.split_parse("define B1(x) x+1;define B2(x,y) x+y;define B3(x,y) B2(x,x)+B2(B1(x),B1(y+x)+1)")
ret = parser.exe("B3(2,3)")
assert_equal(ret, 14)
end
def test_3_para
parser = MacroParser.new
parser.parse("define A(x,y,z) (x+(y+z)*1)")
ret = parser.exe("A(4,5,1)")
assert_equal(ret, 10)
parser.split_parse("define A1(x) x*x;define A2(x,y) x/y + x*y")
parser.split_parse("define B(a,b,c) A2(a,b)+A1(c);define B2(x,a,b) B(a,b,x)")
ret = parser.exe("B(1,2,3)")
assert_equal(ret, 11)
ret = parser.exe("B2(1,2,3)")
assert_equal(ret, 7)
end
def test_4_para
parser = MacroParser.new
parser.parse("define A(x,y,z,m) x*y+z/m")
ret = parser.exe("A(4,5,4,2)")
assert_equal(ret, 22)
parser.split_parse("define B(x) x;define C(x,y) x;define D(x,y,z) x;define AA(a,b,c,d) D(B(A(4,5,4,2)),C(b,c),1)+1+C(b,c)+D(a,b,c)")
ret = parser.exe("AA(4,5,4,2)")
assert_equal(ret, 22+1+5+4)
end
def test_5_para
parser = MacroParser.new
parser.parse("define A(x,y,z,m,kk) x*y+z/m+kk")
ret = parser.exe("A(4,5,4,2,10)")
assert_equal(ret, 32)
end
def test_6_para
parser = MacroParser.new
parser.parse("define A(a1,a2,a3,a4,a5,a6) a1+a2+a3+a4+a5+a6")
ret = parser.exe("A(1,2,3,4,5,6)")
assert_equal(ret, 21)
end
def test_7_para
parser = MacroParser.new
parser.parse("define A(a1,a2,a3,a4,a5,a6,a7) a1+a2+a3+a4+a5+a6+a7")
ret = parser.exe("A(1,2,3,4,5,6,7)")
assert_equal(ret, 28)
end
def test_deep_call
parser = MacroParser.new
parser.split_parse("define A(x) x+1;define B(b) b+1;define C(a) a+1;define D(mmmm) mmmm+1;
define E(av) av+1;define H(ca0) ca0+1;define I(_1a) _1a+1;define Z(z) I(H(E(D(C(A(1+B(z)+1))))))")
ret = parser.exe("Z(2)")
assert_equal(ret, 11)
end
def test_3operator
parser = MacroParser.new
parser.split_parse("define A 1?0:2;define B 1>2 ? 100 : 1000;define C 0xFFFFFFF ? 1 : 0")
ret = parser.exe("A")
assert_equal(ret, 0)
ret = parser.exe("B")
assert_equal(ret, 1000)
ret = parser.exe("C")
assert_equal(ret, 1)
parser.split_parse("define max(a,b) (a > b) ? a : b;define max3(a,b,c) max(max(a,b),c)")
ret = parser.exe("max(2,5)")
assert_equal(ret, 5)
ret = parser.exe("max3(200,50,400)")
assert_equal(ret, 400)
end
def test_0
parser = MacroParser.new
parser.split_parse("define A 1;define B ((A)+A)")
ret = parser.exe("B")
assert_equal(ret, 2)
end
def test_1
parser = MacroParser.new
parser.split_parse("define A 1;define B (A);define C ((A)+B)")
ret = parser.exe("B")
assert_equal(ret, 1)
ret = parser.exe("C")
assert_equal(ret, 2)
end
def test_no_define
parser = MacroParser.new
parser.split_parse("define A 1")
begin
ret = parser.exe("BBBB")
rescue => info
assert_equal(info.to_s, "BBBB Not Found!")
end
end
def test_devide_0
parser = MacroParser.new
parser.split_parse("define A(x,y) x/y")
begin
ret = parser.exe("A(8,0)")
rescue => info
assert_equal(info.to_s, "divided by 0")
end
end
def test_wrong_input
parser = MacroParser.new
begin
parser.split_parse("666")
rescue => info
p info.to_s
end
begin
parser.exe("def AAA(()444")
rescue => info
assert_equal(info.to_s,"\nparse error on value :AAA (NAME)")
end
end
end