| 1 |
import py |
| 2 |
import sys, operator |
| 3 |
from rpython.translator.translator import TranslationContext |
| 4 |
from rpython.rtyper.test import snippet |
| 5 |
from rpython.rlib.rarithmetic import r_int, r_uint, r_longlong, r_ulonglong |
| 6 |
from rpython.rlib.rarithmetic import ovfcheck, r_int64, intmask, int_between |
| 7 |
from rpython.rlib import objectmodel |
| 8 |
from rpython.rtyper.test.tool import BaseRtypingTest |
| 9 |
from rpython.flowspace.model import summary |
| 10 |
|
| 11 |
|
| 12 |
class TestSnippet(object): |
| 13 |
|
| 14 |
def _test(self, func, types): |
| 15 |
t = TranslationContext() |
| 16 |
t.buildannotator().build_types(func, types) |
| 17 |
t.buildrtyper().specialize() |
| 18 |
t.checkgraphs() |
| 19 |
|
| 20 |
def test_not1(self): |
| 21 |
self._test(snippet.not1, [int]) |
| 22 |
|
| 23 |
def test_not2(self): |
| 24 |
self._test(snippet.not2, [int]) |
| 25 |
|
| 26 |
def test_int1(self): |
| 27 |
self._test(snippet.int1, [int]) |
| 28 |
|
| 29 |
def test_int_cast1(self): |
| 30 |
self._test(snippet.int_cast1, [int]) |
| 31 |
|
| 32 |
|
| 33 |
class TestRint(BaseRtypingTest): |
| 34 |
|
| 35 |
def test_char_constant(self): |
| 36 |
def dummyfn(i): |
| 37 |
return chr(i) |
| 38 |
res = self.interpret(dummyfn, [ord(' ')]) |
| 39 |
assert res == ' ' |
| 40 |
res = self.interpret(dummyfn, [0]) |
| 41 |
assert res == '\0' |
| 42 |
res = self.interpret(dummyfn, [ord('a')]) |
| 43 |
assert res == 'a' |
| 44 |
|
| 45 |
def test_str_of_int(self): |
| 46 |
def dummy(i): |
| 47 |
return str(i) |
| 48 |
|
| 49 |
res = self.interpret(dummy, [0]) |
| 50 |
assert self.ll_to_string(res) == '0' |
| 51 |
|
| 52 |
res = self.interpret(dummy, [1034]) |
| 53 |
assert self.ll_to_string(res) == '1034' |
| 54 |
|
| 55 |
res = self.interpret(dummy, [-123]) |
| 56 |
assert self.ll_to_string(res) == '-123' |
| 57 |
|
| 58 |
res = self.interpret(dummy, [-sys.maxint-1]) |
| 59 |
assert self.ll_to_string(res) == str(-sys.maxint-1) |
| 60 |
|
| 61 |
def test_hex_of_int(self): |
| 62 |
def dummy(i): |
| 63 |
return hex(i) |
| 64 |
|
| 65 |
res = self.interpret(dummy, [0]) |
| 66 |
assert self.ll_to_string(res) == '0x0' |
| 67 |
|
| 68 |
res = self.interpret(dummy, [1034]) |
| 69 |
assert self.ll_to_string(res) == '0x40a' |
| 70 |
|
| 71 |
res = self.interpret(dummy, [-123]) |
| 72 |
assert self.ll_to_string(res) == '-0x7b' |
| 73 |
|
| 74 |
res = self.interpret(dummy, [-sys.maxint-1]) |
| 75 |
res = self.ll_to_string(res) |
| 76 |
assert res == '-0x8' + '0' * (len(res)-4) |
| 77 |
|
| 78 |
def test_hex_of_uint(self): |
| 79 |
def dummy(i): |
| 80 |
return hex(r_uint(i)) |
| 81 |
|
| 82 |
res = self.interpret(dummy, [-5]) |
| 83 |
res = self.ll_to_string(res) |
| 84 |
assert res == '0x' + 'f' * (len(res)-3) + 'b' |
| 85 |
|
| 86 |
def test_oct_of_int(self): |
| 87 |
def dummy(i): |
| 88 |
return oct(i) |
| 89 |
|
| 90 |
res = self.interpret(dummy, [0]) |
| 91 |
assert self.ll_to_string(res) == '0' |
| 92 |
|
| 93 |
res = self.interpret(dummy, [1034]) |
| 94 |
assert self.ll_to_string(res) == '02012' |
| 95 |
|
| 96 |
res = self.interpret(dummy, [-123]) |
| 97 |
assert self.ll_to_string(res) == '-0173' |
| 98 |
|
| 99 |
res = self.interpret(dummy, [-sys.maxint-1]) |
| 100 |
res = self.ll_to_string(res) |
| 101 |
assert res == '-' + oct(sys.maxint+1).replace('L', '').replace('l', '') |
| 102 |
|
| 103 |
def test_bin_of_int(self): |
| 104 |
from rpython.rtyper.lltypesystem.ll_str import ll_int2bin |
| 105 |
|
| 106 |
def dummy(i): |
| 107 |
return bin(i) |
| 108 |
|
| 109 |
res = self.interpret(dummy, [0]) |
| 110 |
assert self.ll_to_string(res) == '0b0' |
| 111 |
|
| 112 |
res = self.interpret(dummy, [1034]) |
| 113 |
assert self.ll_to_string(res) == '0b10000001010' |
| 114 |
|
| 115 |
res = self.interpret(dummy, [-123]) |
| 116 |
assert self.ll_to_string(res) == '-0b1111011' |
| 117 |
|
| 118 |
res = self.interpret(dummy, [-sys.maxint-1]) |
| 119 |
res = self.ll_to_string(res) |
| 120 |
assert res == '-0b1' + '0' * (len(res)-4) |
| 121 |
|
| 122 |
def test_bin_of_uint(self): |
| 123 |
def dummy(i): |
| 124 |
return bin(r_uint(i)) |
| 125 |
|
| 126 |
res = self.interpret(dummy, [-5]) |
| 127 |
res = self.ll_to_string(res) |
| 128 |
assert res == '0b' + '1' * (len(res)-5) + '011' |
| 129 |
|
| 130 |
def test_str_of_longlong(self): |
| 131 |
def f(i): |
| 132 |
return str(i) |
| 133 |
|
| 134 |
res = self.interpret(f, [r_int64(0)]) |
| 135 |
assert self.ll_to_string(res) == '0' |
| 136 |
|
| 137 |
res = self.interpret(f, [r_int64(413974738222117)]) |
| 138 |
assert self.ll_to_string(res) == '413974738222117' |
| 139 |
|
| 140 |
def test_str_of_uint(self): |
| 141 |
def f(i): |
| 142 |
return str(i) |
| 143 |
|
| 144 |
res = self.interpret(f, [r_uint(0)]) |
| 145 |
assert self.ll_to_string(res) == '0' |
| 146 |
|
| 147 |
res = self.interpret(f, [r_uint(sys.maxint)]) |
| 148 |
assert self.ll_to_string(res) == str(sys.maxint) |
| 149 |
|
| 150 |
res = self.interpret(f, [r_uint(sys.maxint+1)]) |
| 151 |
assert self.ll_to_string(res) == str(sys.maxint+1) |
| 152 |
|
| 153 |
res = self.interpret(f, [r_uint(-1)]) |
| 154 |
assert self.ll_to_string(res) == str(2*sys.maxint+1) |
| 155 |
|
| 156 |
def test_unsigned(self): |
| 157 |
bigvalue = r_uint(sys.maxint + 17) |
| 158 |
def dummy(i): |
| 159 |
i = r_uint(i) |
| 160 |
j = bigvalue |
| 161 |
return i < j |
| 162 |
|
| 163 |
res = self.interpret(dummy,[0]) |
| 164 |
assert res is True |
| 165 |
|
| 166 |
res = self.interpret(dummy, [-1]) |
| 167 |
assert res is False |
| 168 |
|
| 169 |
def test_specializing_int_functions(self): |
| 170 |
def f(i): |
| 171 |
return i + 1 |
| 172 |
f._annspecialcase_ = "specialize:argtype(0)" |
| 173 |
def g(n): |
| 174 |
if n > 0: |
| 175 |
return f(r_int64(0)) |
| 176 |
else: |
| 177 |
return f(0) |
| 178 |
res = self.interpret(g, [0]) |
| 179 |
assert res == 1 |
| 180 |
|
| 181 |
res = self.interpret(g, [1]) |
| 182 |
assert res == 1 |
| 183 |
|
| 184 |
def test_downcast_int(self): |
| 185 |
def f(i): |
| 186 |
return int(i) |
| 187 |
res = self.interpret(f, [r_int64(0)]) |
| 188 |
assert res == 0 |
| 189 |
|
| 190 |
def test_isinstance_vs_int_types(self): |
| 191 |
class FakeSpace(object): |
| 192 |
def wrap(self, x): |
| 193 |
if x is None: |
| 194 |
return [None] |
| 195 |
if isinstance(x, str): |
| 196 |
return x |
| 197 |
if isinstance(x, r_int64): |
| 198 |
return int(x) |
| 199 |
return "XXX" |
| 200 |
wrap._annspecialcase_ = 'specialize:argtype(0)' |
| 201 |
|
| 202 |
space = FakeSpace() |
| 203 |
def wrap(x): |
| 204 |
return space.wrap(x) |
| 205 |
res = self.interpret(wrap, [r_int64(0)]) |
| 206 |
assert res == 0 |
| 207 |
|
| 208 |
def test_truediv(self): |
| 209 |
def f(n, m): |
| 210 |
return operator.truediv(n, m) |
| 211 |
res = self.interpret(f, [20, 4]) |
| 212 |
assert type(res) is float |
| 213 |
assert res == 5.0 |
| 214 |
|
| 215 |
def test_float_conversion(self): |
| 216 |
def f(ii): |
| 217 |
return float(ii) |
| 218 |
res = self.interpret(f, [r_int64(100000000)]) |
| 219 |
assert type(res) is float |
| 220 |
assert res == 100000000. |
| 221 |
res = self.interpret(f, [r_int64(1234567890123456789)]) |
| 222 |
assert type(res) is float |
| 223 |
assert self.float_eq(res, 1.2345678901234568e+18) |
| 224 |
|
| 225 |
def test_float_conversion_implicit(self): |
| 226 |
def f(ii): |
| 227 |
return 1.0 + ii |
| 228 |
res = self.interpret(f, [r_int64(100000000)]) |
| 229 |
assert type(res) is float |
| 230 |
assert res == 100000001. |
| 231 |
res = self.interpret(f, [r_int64(1234567890123456789)]) |
| 232 |
assert type(res) is float |
| 233 |
assert self.float_eq(res, 1.2345678901234568e+18) |
| 234 |
|
| 235 |
def test_rarithmetic(self): |
| 236 |
inttypes = [int, r_uint, r_int64, r_ulonglong] |
| 237 |
for inttype in inttypes: |
| 238 |
c = inttype() |
| 239 |
def f(): |
| 240 |
return c |
| 241 |
res = self.interpret(f, []) |
| 242 |
assert res == f() |
| 243 |
assert type(res) == inttype |
| 244 |
|
| 245 |
for inttype in inttypes: |
| 246 |
def f(): |
| 247 |
return inttype(0) |
| 248 |
res = self.interpret(f, []) |
| 249 |
assert res == f() |
| 250 |
assert type(res) == inttype |
| 251 |
|
| 252 |
for inttype in inttypes: |
| 253 |
def f(x): |
| 254 |
return x |
| 255 |
res = self.interpret(f, [inttype(0)]) |
| 256 |
assert res == f(inttype(0)) |
| 257 |
assert type(res) == inttype |
| 258 |
|
| 259 |
def test_and_or(self): |
| 260 |
inttypes = [int, r_uint, r_int64, r_ulonglong] |
| 261 |
for inttype in inttypes: |
| 262 |
def f(a, b, c): |
| 263 |
return a&b|c |
| 264 |
res = self.interpret(f, [inttype(0x1234), inttype(0x00FF), inttype(0x5600)]) |
| 265 |
assert res == f(0x1234, 0x00FF, 0x5600) |
| 266 |
|
| 267 |
def test_neg_abs_ovf(self): |
| 268 |
for op in (operator.neg, abs): |
| 269 |
def f(x): |
| 270 |
try: |
| 271 |
return ovfcheck(op(x)) |
| 272 |
except OverflowError: |
| 273 |
return 0 |
| 274 |
res = self.interpret(f, [-1]) |
| 275 |
assert res == 1 |
| 276 |
res = self.interpret(f, [int(-1<<(r_int.BITS-1))]) |
| 277 |
assert res == 0 |
| 278 |
|
| 279 |
def test_lshift_rshift(self): |
| 280 |
for name, f in [('_lshift', lambda x, y: x << y), |
| 281 |
('_rshift', lambda x, y: x >> y)]: |
| 282 |
for inttype in (int, r_uint, r_int64, r_ulonglong): |
| 283 |
res = self.interpret(f, [inttype(2147483647), 12]) |
| 284 |
if inttype is int: |
| 285 |
assert res == intmask(f(2147483647, 12)) |
| 286 |
else: |
| 287 |
assert res == inttype(f(2147483647, 12)) |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
_, _, graph = self.gengraph(f, [inttype, int]) |
| 293 |
block = graph.startblock |
| 294 |
assert len(block.operations) == 1 |
| 295 |
assert block.operations[0].opname.endswith(name) |
| 296 |
|
| 297 |
def test_cast_uint_to_longlong(self): |
| 298 |
if r_uint.BITS == r_longlong.BITS: |
| 299 |
py.test.skip("only on 32-bits") |
| 300 |
def f(x): |
| 301 |
return r_longlong(r_uint(x)) |
| 302 |
res = self.interpret(f, [-42]) |
| 303 |
assert res == (sys.maxint+1) * 2 - 42 |
| 304 |
|
| 305 |
div_mod_iteration_count = 1000 |
| 306 |
def test_div_mod(self): |
| 307 |
import random |
| 308 |
|
| 309 |
for inttype in (int, r_int64): |
| 310 |
|
| 311 |
def d(x, y): |
| 312 |
return x/y |
| 313 |
|
| 314 |
for i in range(self.div_mod_iteration_count): |
| 315 |
x = inttype(random.randint(-100000, 100000)) |
| 316 |
y = inttype(random.randint(-100000, 100000)) |
| 317 |
if not y: continue |
| 318 |
if (i & 31) == 0: |
| 319 |
x = (x//y) * y |
| 320 |
res = self.interpret(d, [x, y]) |
| 321 |
assert res == d(x, y) |
| 322 |
|
| 323 |
def m(x, y): |
| 324 |
return x%y |
| 325 |
|
| 326 |
for i in range(self.div_mod_iteration_count): |
| 327 |
x = inttype(random.randint(-100000, 100000)) |
| 328 |
y = inttype(random.randint(-100000, 100000)) |
| 329 |
if not y: continue |
| 330 |
if (i & 31) == 0: |
| 331 |
x = (x//y) * y |
| 332 |
res = self.interpret(m, [x, y]) |
| 333 |
assert res == m(x, y) |
| 334 |
|
| 335 |
def test_protected_div_mod(self): |
| 336 |
def div_unpro(x, y): |
| 337 |
return x//y |
| 338 |
def div_ovf(x, y): |
| 339 |
try: |
| 340 |
return ovfcheck(x//y) |
| 341 |
except OverflowError: |
| 342 |
return 42 |
| 343 |
def div_zer(x, y): |
| 344 |
try: |
| 345 |
return x//y |
| 346 |
except ZeroDivisionError: |
| 347 |
return 84 |
| 348 |
def div_ovf_zer(x, y): |
| 349 |
try: |
| 350 |
return ovfcheck(x//y) |
| 351 |
except OverflowError: |
| 352 |
return 42 |
| 353 |
except ZeroDivisionError: |
| 354 |
return 84 |
| 355 |
|
| 356 |
def mod_unpro(x, y): |
| 357 |
return x%y |
| 358 |
def mod_ovf(x, y): |
| 359 |
try: |
| 360 |
return ovfcheck(x%y) |
| 361 |
except OverflowError: |
| 362 |
return 42 |
| 363 |
def mod_zer(x, y): |
| 364 |
try: |
| 365 |
return x%y |
| 366 |
except ZeroDivisionError: |
| 367 |
return 84 |
| 368 |
def mod_ovf_zer(x, y): |
| 369 |
try: |
| 370 |
return ovfcheck(x%y) |
| 371 |
except OverflowError: |
| 372 |
return 42 |
| 373 |
except ZeroDivisionError: |
| 374 |
return 84 |
| 375 |
|
| 376 |
for inttype in (int, r_int64): |
| 377 |
|
| 378 |
args = [( 5, 2), (-5, 2), ( 5,-2), (-5,-2), |
| 379 |
( 6, 2), (-6, 2), ( 6,-2), (-6,-2), |
| 380 |
(-sys.maxint, -1), (4, 0)] |
| 381 |
|
| 382 |
funcs = [div_unpro, div_ovf, div_zer, div_ovf_zer, |
| 383 |
mod_unpro, mod_ovf, mod_zer, mod_ovf_zer] |
| 384 |
|
| 385 |
for func in funcs: |
| 386 |
print func |
| 387 |
if 'ovf' in func.__name__ and inttype is r_longlong: |
| 388 |
continue |
| 389 |
for x, y in args: |
| 390 |
x, y = inttype(x), inttype(y) |
| 391 |
try: |
| 392 |
res1 = func(x, y) |
| 393 |
if isinstance(res1, int): |
| 394 |
res1 = ovfcheck(res1) |
| 395 |
except (OverflowError, ZeroDivisionError): |
| 396 |
continue |
| 397 |
res2 = self.interpret(func, [x, y]) |
| 398 |
assert res1 == res2 |
| 399 |
|
| 400 |
def test_int_add_nonneg_ovf(self): |
| 401 |
def f(x): |
| 402 |
try: |
| 403 |
a = ovfcheck(x + 50) |
| 404 |
except OverflowError: |
| 405 |
return 0 |
| 406 |
try: |
| 407 |
a += ovfcheck(100 + x) |
| 408 |
except OverflowError: |
| 409 |
return 1 |
| 410 |
return a |
| 411 |
t, rtyper, graph = self.gengraph(f, [int]) |
| 412 |
assert summary(graph).get('int_add_nonneg_ovf') == 2 |
| 413 |
res = self.interpret(f, [-3]) |
| 414 |
assert res == 144 |
| 415 |
res = self.interpret(f, [sys.maxint-50]) |
| 416 |
assert res == 1 |
| 417 |
res = self.interpret(f, [sys.maxint]) |
| 418 |
assert res == 0 |
| 419 |
|
| 420 |
def test_int_py_div_nonnegargs(self): |
| 421 |
def f(x, y): |
| 422 |
assert x >= 0 |
| 423 |
assert y >= 0 |
| 424 |
return x // y |
| 425 |
res = self.interpret(f, [1234567, 123]) |
| 426 |
assert res == 1234567 // 123 |
| 427 |
|
| 428 |
def test_int_py_mod_nonnegargs(self): |
| 429 |
def f(x, y): |
| 430 |
assert x >= 0 |
| 431 |
assert y >= 0 |
| 432 |
return x % y |
| 433 |
res = self.interpret(f, [1234567, 123]) |
| 434 |
assert res == 1234567 % 123 |
| 435 |
|
| 436 |
def test_cast_to_float_exc_check(self): |
| 437 |
def f(x): |
| 438 |
try: |
| 439 |
return float(x) |
| 440 |
except ValueError: |
| 441 |
return 3.0 |
| 442 |
|
| 443 |
res = self.interpret(f, [3]) |
| 444 |
assert res == 3 |
| 445 |
|
| 446 |
def test_hash(self): |
| 447 |
def f(x): |
| 448 |
return objectmodel.compute_hash(x) |
| 449 |
res = self.interpret(f, [123456789]) |
| 450 |
assert res == 123456789 |
| 451 |
res = self.interpret(f, [r_int64(123456789012345678)]) |
| 452 |
if sys.maxint == 2147483647: |
| 453 |
|
| 454 |
assert res == -1506741426 + 9 * 28744523 |
| 455 |
else: |
| 456 |
assert res == 123456789012345678 |
| 457 |
|
| 458 |
def test_int_between(self): |
| 459 |
def fn(a, b, c): |
| 460 |
return int_between(a, b, c) |
| 461 |
assert self.interpret(fn, [1, 1, 3]) |
| 462 |
assert self.interpret(fn, [1, 2, 3]) |
| 463 |
assert not self.interpret(fn, [1, 0, 2]) |
| 464 |
assert not self.interpret(fn, [1, 5, 2]) |
| 465 |
assert not self.interpret(fn, [1, 2, 2]) |
| 466 |
assert not self.interpret(fn, [1, 1, 1]) |