| 1 |
import pytest |
| 2 |
from rpython.rlib.runicode import code_to_unichr, MAXUNICODE |
| 3 |
from rpython.rlib.unicodedata import unicodedb_5_2_0, unicodedb_11_0_0 |
| 4 |
from rpython.rtyper.test.tool import BaseRtypingTest |
| 5 |
from rpython.translator.c.test.test_genc import compile |
| 6 |
|
| 7 |
|
| 8 |
class TestTranslated(BaseRtypingTest): |
| 9 |
def test_translated(self): |
| 10 |
def f(n): |
| 11 |
if n == 0: |
| 12 |
return -1 |
| 13 |
else: |
| 14 |
u = unicodedb_5_2_0.lookup("GOTHIC LETTER FAIHU") |
| 15 |
return u |
| 16 |
res = self.interpret(f, [1]) |
| 17 |
print hex(res) |
| 18 |
assert res == f(1) |
| 19 |
|
| 20 |
def test_translated_crash(self): |
| 21 |
def f(n): |
| 22 |
try: |
| 23 |
return unicodedb_5_2_0.tolower(n) |
| 24 |
except KeyError: |
| 25 |
return -9 |
| 26 |
res = self.interpret(f, [1]) |
| 27 |
assert res == f(1) |
| 28 |
res = self.interpret(f, [2 ** 30]) |
| 29 |
assert res == f(2 ** 30) == -9 |
| 30 |
|
| 31 |
def test_code_to_unichr(): |
| 32 |
def f(c): |
| 33 |
return ord(code_to_unichr(c)[0]) |
| 34 |
f1 = compile(f, [int]) |
| 35 |
got = f1(0x12346) |
| 36 |
if MAXUNICODE == 65535: |
| 37 |
assert got == 0xd808 |
| 38 |
else: |
| 39 |
assert got == 0x12346 |
| 40 |
|
| 41 |
def test_cjk(): |
| 42 |
cases = [ |
| 43 |
('3400', '4DB5'), |
| 44 |
('4E00', '9FEF'), |
| 45 |
('20000', '2A6D6'), |
| 46 |
('2A700', '2B734'), |
| 47 |
('2B740', '2B81D'), |
| 48 |
('2B820', '2CEA1'), |
| 49 |
] |
| 50 |
for first, last in cases: |
| 51 |
first = int(first, 16) |
| 52 |
last = int(last, 16) |
| 53 |
|
| 54 |
for i in (first, first + 1, last - 1, last): |
| 55 |
charname = 'CJK UNIFIED IDEOGRAPH-%X'%i |
| 56 |
assert unicodedb_11_0_0.lookup(charname) == i |
| 57 |
|
| 58 |
for i in first - 1, last + 1: |
| 59 |
charname = 'CJK UNIFIED IDEOGRAPH-%X'%i |
| 60 |
with pytest.raises(KeyError): |
| 61 |
unicodedb_11_0_0.lookup(charname) |
| 62 |
|