| 1 |
from rpython.translator.translator import TranslationContext |
| 2 |
from rpython.rtyper.test import snippet |
| 3 |
from rpython.rtyper.test.tool import BaseRtypingTest |
| 4 |
|
| 5 |
|
| 6 |
class TestSnippet(object): |
| 7 |
def _test(self, func, types): |
| 8 |
t = TranslationContext() |
| 9 |
t.buildannotator().build_types(func, types) |
| 10 |
t.buildrtyper().specialize() |
| 11 |
t.checkgraphs() |
| 12 |
|
| 13 |
def test_not1(self): |
| 14 |
self._test(snippet.not1, [bool]) |
| 15 |
|
| 16 |
def test_not2(self): |
| 17 |
self._test(snippet.not2, [bool]) |
| 18 |
|
| 19 |
def test_bool1(self): |
| 20 |
self._test(snippet.bool1, [bool]) |
| 21 |
|
| 22 |
def test_bool_cast1(self): |
| 23 |
self._test(snippet.bool_cast1, [bool]) |
| 24 |
|
| 25 |
|
| 26 |
class TestRbool(BaseRtypingTest): |
| 27 |
|
| 28 |
def test_bool2int(self): |
| 29 |
def f(n): |
| 30 |
if n: |
| 31 |
n = 2 |
| 32 |
return n |
| 33 |
res = self.interpret(f, [False]) |
| 34 |
assert res == 0 and res is not False |
| 35 |
res = self.interpret(f, [True]) |
| 36 |
assert res == 2 |
| 37 |
|
| 38 |
def test_arithmetic_with_bool_inputs(self): |
| 39 |
def f(n): |
| 40 |
a = n * ((n>2) + (n>=2)) |
| 41 |
a -= (a != n) > False |
| 42 |
return a + (-(n<0)) |
| 43 |
for i in [-1, 1, 2, 42]: |
| 44 |
res = self.interpret(f, [i]) |
| 45 |
assert res == f(i) |
| 46 |
|
| 47 |
def test_bool2str(self): |
| 48 |
def f(n, m): |
| 49 |
if m == 1: |
| 50 |
return hex(n > 5) |
| 51 |
elif m == 2: |
| 52 |
return oct(n > 5) |
| 53 |
else: |
| 54 |
return str(n > 5) |
| 55 |
res = self.interpret(f, [2, 0]) |
| 56 |
assert self.ll_to_string(res) in ('0', 'False') |
| 57 |
res = self.interpret(f, [9, 0]) |
| 58 |
assert self.ll_to_string(res) in ('1', 'True') |
| 59 |
res = self.interpret(f, [2, 1]) |
| 60 |
assert self.ll_to_string(res) == '0x0' |
| 61 |
res = self.interpret(f, [9, 1]) |
| 62 |
assert self.ll_to_string(res) == '0x1' |
| 63 |
res = self.interpret(f, [2, 2]) |
| 64 |
assert self.ll_to_string(res) == '0' |
| 65 |
res = self.interpret(f, [9, 2]) |
| 66 |
assert self.ll_to_string(res) == '01' |
| 67 |
|
| 68 |
def test_bool_int_mixture(self): |
| 69 |
def f(x, y): |
| 70 |
return x/y |
| 71 |
res = self.interpret(f, [True, 1]) |
| 72 |
assert res == 1 |
| 73 |
res = self.interpret(f, [1, True]) |
| 74 |
assert res == 1 |