forked from Distributive-Network/PythonMonkey
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pythonmonkey_eval.py
More file actions
342 lines (274 loc) · 11.5 KB
/
Copy pathtest_pythonmonkey_eval.py
File metadata and controls
342 lines (274 loc) · 11.5 KB
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import pythonmonkey as pm
import gc
import random
from datetime import datetime, timedelta
def test_passes():
assert True
def test_eval_ascii_string_matches_evaluated_string():
py_ascii_string = "abc"
js_ascii_string = pm.eval(repr(py_ascii_string))
assert py_ascii_string == js_ascii_string
def test_eval_latin1_string_matches_evaluated_string():
py_latin1_string = "a©Ð"
js_latin1_string = pm.eval(repr(py_latin1_string))
assert py_latin1_string == js_latin1_string
def test_eval_null_character_string_matches_evaluated_string():
py_null_character_string = "a\x00©"
js_null_character_string = pm.eval(repr(py_null_character_string))
assert py_null_character_string == js_null_character_string
def test_eval_ucs2_string_matches_evaluated_string():
py_ucs2_string = "ՄԸՋ"
js_ucs2_string = pm.eval(repr(py_ucs2_string))
assert py_ucs2_string == js_ucs2_string
def test_eval_unpaired_surrogate_string_matches_evaluated_string():
py_unpaired_surrogate_string = "Ջ©\ud8fe"
js_unpaired_surrogate_string = pm.eval(repr(py_unpaired_surrogate_string))
assert py_unpaired_surrogate_string == js_unpaired_surrogate_string
def test_eval_ucs4_string_matches_evaluated_string():
py_ucs4_string = "🀄🀛🜢"
js_utf16_string = pm.eval(repr(py_ucs4_string))
js_ucs4_string = pm.asUCS4(js_utf16_string)
assert py_ucs4_string == js_ucs4_string
def test_eval_latin1_string_fuzztest():
n = 10
for i in range(n):
length = random.randint(0x0000, 0xFFFF)
string1 = ''
for i in range(length):
codepoint = random.randint(0x00, 0xFF)
string1 += chr(codepoint) # add random chr in latin1 range
INITIAL_STRING = string1
m = 10
for j in range(m):
string2 = pm.eval(repr(string1))
assert len(string1) == length
assert len(string2) == length
assert len(string1) == len(string2)
assert string1 == string2
gc.collect()
pm.collect()
#garbage collection should not collect variables still in scope
assert len(string1) == length
assert len(string2) == length
assert len(string1) == len(string2)
assert string1 == string2
string1 = string2
assert INITIAL_STRING == string1 #strings should still match after a bunch of iterations through JS
def test_eval_ucs2_string_fuzztest():
n = 10
for i in range(n):
length = random.randint(0x0000, 0xFFFF)
string1 = ''
for i in range(length):
codepoint = random.randint(0x00, 0xFFFF)
string1 += chr(codepoint) # add random chr in ucs2 range
INITIAL_STRING = string1
m = 10
for j in range(m):
string2 = pm.eval(repr(string1))
assert len(string1) == length
assert len(string2) == length
assert len(string1) == len(string2)
assert string1 == string2
gc.collect()
pm.collect()
#garbage collection should not collect variables still in scope
assert len(string1) == length
assert len(string2) == length
assert len(string1) == len(string2)
assert string1 == string2
string1 = string2
assert INITIAL_STRING == string1 #strings should still match after a bunch of iterations through JS
def test_eval_ucs4_string_fuzztest():
n = 10
for i in range(n):
length = random.randint(0x0000, 0xFFFF)
string1 = ''
for i in range(length):
codepoint = random.randint(0x010000, 0x10FFFF)
string1 += chr(codepoint) # add random chr outside BMP
INITIAL_STRING = string1
m = 10
for j in range(m):
utf16_string2 = pm.eval("'" + string1 + "'")
string2 = pm.asUCS4(utf16_string2)
assert len(string1) == length
assert len(string2) == length
assert len(string1) == len(string2)
assert string1 == string2
gc.collect()
pm.collect()
#garbage collection should not collect variables still in scope
assert len(string1) == length
assert len(string2) == length
assert len(string1) == len(string2)
assert string1 == string2
string1 = string2
assert INITIAL_STRING == string1 #strings should still match after a bunch of iterations through JS
def test_eval_numbers_floats():
for i in range(10):
py_number = random.uniform(-1000000,1000000)
js_number = pm.eval(repr(py_number))
assert py_number == js_number
def test_eval_numbers_integers():
for i in range(10):
py_number = random.randint(-1000000,1000000)
js_number = pm.eval(repr(py_number))
assert py_number == js_number
def test_eval_booleans():
py_bool = True
js_bool = pm.eval('true')
assert py_bool == js_bool
py_bool = False
js_bool = pm.eval('false')
assert py_bool == js_bool
def test_eval_dates():
MIN_YEAR = 100
MAX_YEAR = 2023
start = datetime(MIN_YEAR, 1, 1, 00, 00, 00)
years = MAX_YEAR - MIN_YEAR + 1
end = start + timedelta(days=365 * years)
for i in range(10):
py_date = start + (end - start) * random.random()
py_date = py_date.replace(microsecond=0)
js_date = pm.eval(f'new Date({py_date.year}, {py_date.month - 1}, {py_date.day}, {py_date.hour}, {py_date.minute}, {py_date.second}, {py_date.microsecond / 1000})')
assert py_date == js_date
def test_eval_boxed_booleans():
py_bool = True
js_bool = pm.eval('new Boolean(true)')
assert py_bool == js_bool
py_bool = False
js_bool = pm.eval('new Boolean(false)')
assert py_bool == js_bool
def test_eval_boxed_numbers_floats():
for i in range(10):
py_number = random.uniform(-1000000,1000000)
js_number = pm.eval(f'new Number({repr(py_number)})')
assert py_number == js_number
def test_eval_boxed_numbers_integers():
for i in range(10):
py_number = random.randint(-1000000,1000000)
js_number = pm.eval(f'new Number({repr(py_number)})')
assert py_number == js_number
def test_eval_boxed_ascii_string_matches_evaluated_string():
py_ascii_string = "abc"
js_ascii_string = pm.eval(f'new String({repr(py_ascii_string)})')
assert py_ascii_string == js_ascii_string
def test_eval_boxed_latin1_string_matches_evaluated_string():
py_latin1_string = "a©Ð"
js_latin1_string = pm.eval(f'new String({repr(py_latin1_string)})')
assert py_latin1_string == js_latin1_string
def test_eval_boxed_null_character_string_matches_evaluated_string():
py_null_character_string = "a\x00©"
js_null_character_string = pm.eval(f'new String({repr(py_null_character_string)})')
assert py_null_character_string == js_null_character_string
def test_eval_boxed_ucs2_string_matches_evaluated_string():
py_ucs2_string = "ՄԸՋ"
js_ucs2_string = pm.eval(f'new String({repr(py_ucs2_string)})')
assert py_ucs2_string == js_ucs2_string
def test_eval_boxed_unpaired_surrogate_string_matches_evaluated_string():
py_unpaired_surrogate_string = "Ջ©\ud8fe"
js_unpaired_surrogate_string = pm.eval(f'new String({repr(py_unpaired_surrogate_string)})')
assert py_unpaired_surrogate_string == js_unpaired_surrogate_string
def test_eval_boxed_ucs4_string_matches_evaluated_string():
py_ucs4_string = "🀄🀛🜢"
js_utf16_string = pm.eval(f'new String({repr(py_ucs4_string)})')
js_ucs4_string = pm.asUCS4(js_utf16_string)
assert py_ucs4_string == js_ucs4_string
def test_eval_boxed_latin1_string_fuzztest():
n = 10
for i in range(n):
length = random.randint(0x0000, 0xFFFF)
string1 = ''
for i in range(length):
codepoint = random.randint(0x00, 0xFF)
string1 += chr(codepoint) # add random chr in latin1 range
INITIAL_STRING = string1
m = 10
for j in range(m):
string2 = pm.eval(f'new String({repr(string1)})')
assert len(string1) == length
assert len(string2) == length
assert len(string1) == len(string2)
assert string1 == string2
gc.collect()
pm.collect()
#garbage collection should not collect variables still in scope
assert len(string1) == length
assert len(string2) == length
assert len(string1) == len(string2)
assert string1 == string2
string1 = string2
assert INITIAL_STRING == string1 #strings should still match after a bunch of iterations through JS
def test_eval_boxed_ucs2_string_fuzztest():
n = 10
for i in range(n):
length = random.randint(0x0000, 0xFFFF)
string1 = ''
for i in range(length):
codepoint = random.randint(0x00, 0xFFFF)
string1 += chr(codepoint) # add random chr in ucs2 range
INITIAL_STRING = string1
m = 10
for j in range(m):
string2 = pm.eval(f'new String({repr(string1)})')
assert len(string1) == length
assert len(string2) == length
assert len(string1) == len(string2)
assert string1 == string2
gc.collect()
pm.collect()
#garbage collection should not collect variables still in scope
assert len(string1) == length
assert len(string2) == length
assert len(string1) == len(string2)
assert string1 == string2
string1 = string2
assert INITIAL_STRING == string1 #strings should still match after a bunch of iterations through JS
def test_eval_boxed_ucs4_string_fuzztest():
n = 10
for i in range(n):
length = random.randint(0x0000, 0xFFFF)
string1 = ''
for i in range(length):
codepoint = random.randint(0x010000, 0x10FFFF)
string1 += chr(codepoint) # add random chr outside BMP
INITIAL_STRING = string1
m = 10
for j in range(m):
utf16_string2 = pm.eval(f'new String("{string1}")')
string2 = pm.asUCS4(utf16_string2)
assert len(string1) == length
assert len(string2) == length
assert len(string1) == len(string2)
assert string1 == string2
gc.collect()
pm.collect()
#garbage collection should not collect variables still in scope
assert len(string1) == length
assert len(string2) == length
assert len(string1) == len(string2)
assert string1 == string2
string1 = string2
assert INITIAL_STRING == string1 #strings should still match after a bunch of iterations through JS
def test_eval_undefined():
x = pm.eval("undefined")
assert x == None
def test_eval_null():
x = pm.eval("null")
assert x == pm.null
def test_eval_functions():
f = pm.eval("() => { return undefined }")
assert f() == None
g = pm.eval("() => { return null}")
assert g() == pm.null
h = pm.eval("(a, b) => {return a + b}")
n = 10
for i in range(n):
a = random.randint(0, 1000)
b = random.randint(0, 1000)
assert h(a, b) == (a + b)
for i in range (n):
a = random.uniform(-1000.0, 1000.0)
b = random.uniform(-1000.0, 1000.0)
assert h(a, b) == (a + b)