-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
171 lines (125 loc) · 3.41 KB
/
test.py
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
import unittest
import compiler as cp
import random
import numpy as np
def add(a, b):
return a + b
def sub(a, b):
return a - b
def mul(a, b):
return a * b
def div(a, b):
return a / b
def rem(a, b):
return a % b
def mul(a, b):
return a * b
def add2(a, b):
x = a
return x + b
def add3(a, b):
x = 100
y = 100
return x + y
def add4(a, b, c):
x = a + b
return x + c
def add5(a, b, c):
return a + b + c
def add6(a, b, c, d):
return a + b + c + d
def add7(a, b):
x = a + 1
b = 200
return x + b
def mod(a, b, c):
return a * b + c
class UnitTester(unittest.TestCase):
def setUp(self):
self.compiler = cp.Compiler()
def test_add(self):
for _ in range(100):
x = random.randint(-100, 100)
y = random.randint(-100, 100)
ret = self.compiler.exe(add, x, y)
self.assertEqual(x + y, ret)
def test_sub(self):
for _ in range(100):
x = random.randint(-100, 100)
y = random.randint(-100, 100)
ret = self.compiler.exe(sub, x, y)
self.assertEqual(x - y, ret)
def test_mul(self):
for _ in range(100):
x = random.randint(-100, 100)
y = random.randint(-100, 100)
ret = self.compiler.exe(mul, x, y)
self.assertEqual(x * y, ret)
def test_div(self):
for _ in range(100):
x = random.randint(-100, 100)
y = random.randint(1, 100)
ret = self.compiler.exe(div, x, y)
self.assertEqual(int(x / y), ret)
def test_rem(self):
for _ in range(100):
x = random.randint(0, 100)
y = random.randint(1, 100)
ret = self.compiler.exe(rem, x, y)
self.assertEqual(x % y, ret)
def test_add_float(self):
x = 1.1
y = 2.1
ret = self.compiler.exe(add, x, y)
self.assertEqual(np.float32(x) + np.float32(y), ret)
def test_mul_float(self):
x = 1.1
y = 2.1
ret = self.compiler.exe(mul, x, y)
self.assertEqual(np.float32(x) * np.float32(y), ret)
def test_add2(self):
x = 10
y = 20
ret = self.compiler.exe(add2, x, y)
self.assertEqual(x + y, ret)
def test_add3(self):
ret = self.compiler.exe(add3, 1, 1)
self.assertEqual(200, ret)
def test_add4(self):
x = 100
y = 200
z = -1
ret = self.compiler.exe(add4, x, y, z)
self.assertEqual(add4(x, y, z), ret)
def test_add5(self):
x = 100
y = 200
z = -1
ret = self.compiler.exe(add5, x, y, z)
self.assertEqual(add5(x, y, z), ret)
def test_add6(self):
x = 100
y = 200
z = -1
w = 999
ret = self.compiler.exe(add6, x, y, z, w)
self.assertEqual(add6(x, y, z, w), ret)
def test_add7(self):
x = 1
y = 1
ret = self.compiler.exe(add7, x, y)
self.assertEqual(add7(x, y), ret)
def test_mod(self):
x = 1
y = 1
z = 1
ret = self.compiler.exe(mod, x, y, z)
self.assertEqual(mod(x, y, z), ret)
def test_mod_float(self):
x = 2.0
y = 1.1
z = 9.0
ret = self.compiler.exe(mod, x, y, z)
self.assertEqual(mod(np.float32(x), np.float32(y), np.float32(z)), ret)
if __name__ == '__main__':
unittest.main()