| 1 |
"""Test cases for various RType operations.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import unittest |
| 6 |
|
| 7 |
from mypyc.ir.rtypes import ( |
| 8 |
RUnion, |
| 9 |
bit_rprimitive, |
| 10 |
bool_rprimitive, |
| 11 |
int16_rprimitive, |
| 12 |
int32_rprimitive, |
| 13 |
int64_rprimitive, |
| 14 |
int_rprimitive, |
| 15 |
object_rprimitive, |
| 16 |
short_int_rprimitive, |
| 17 |
str_rprimitive, |
| 18 |
) |
| 19 |
from mypyc.rt_subtype import is_runtime_subtype |
| 20 |
from mypyc.subtype import is_subtype |
| 21 |
|
| 22 |
native_int_types = [int64_rprimitive, int32_rprimitive, int16_rprimitive] |
| 23 |
|
| 24 |
|
| 25 |
class TestSubtype(unittest.TestCase): |
| 26 |
def test_bit(self) -> None: |
| 27 |
assert is_subtype(bit_rprimitive, bool_rprimitive) |
| 28 |
assert is_subtype(bit_rprimitive, int_rprimitive) |
| 29 |
assert is_subtype(bit_rprimitive, short_int_rprimitive) |
| 30 |
for rt in native_int_types: |
| 31 |
assert is_subtype(bit_rprimitive, rt) |
| 32 |
|
| 33 |
def test_bool(self) -> None: |
| 34 |
assert not is_subtype(bool_rprimitive, bit_rprimitive) |
| 35 |
assert is_subtype(bool_rprimitive, int_rprimitive) |
| 36 |
assert is_subtype(bool_rprimitive, short_int_rprimitive) |
| 37 |
for rt in native_int_types: |
| 38 |
assert is_subtype(bool_rprimitive, rt) |
| 39 |
|
| 40 |
def test_int64(self) -> None: |
| 41 |
assert is_subtype(int64_rprimitive, int64_rprimitive) |
| 42 |
assert is_subtype(int64_rprimitive, int_rprimitive) |
| 43 |
assert not is_subtype(int64_rprimitive, short_int_rprimitive) |
| 44 |
assert not is_subtype(int64_rprimitive, int32_rprimitive) |
| 45 |
assert not is_subtype(int64_rprimitive, int16_rprimitive) |
| 46 |
|
| 47 |
def test_int32(self) -> None: |
| 48 |
assert is_subtype(int32_rprimitive, int32_rprimitive) |
| 49 |
assert is_subtype(int32_rprimitive, int_rprimitive) |
| 50 |
assert not is_subtype(int32_rprimitive, short_int_rprimitive) |
| 51 |
assert not is_subtype(int32_rprimitive, int64_rprimitive) |
| 52 |
assert not is_subtype(int32_rprimitive, int16_rprimitive) |
| 53 |
|
| 54 |
def test_int16(self) -> None: |
| 55 |
assert is_subtype(int16_rprimitive, int16_rprimitive) |
| 56 |
assert is_subtype(int16_rprimitive, int_rprimitive) |
| 57 |
assert not is_subtype(int16_rprimitive, short_int_rprimitive) |
| 58 |
assert not is_subtype(int16_rprimitive, int64_rprimitive) |
| 59 |
assert not is_subtype(int16_rprimitive, int32_rprimitive) |
| 60 |
|
| 61 |
|
| 62 |
class TestRuntimeSubtype(unittest.TestCase): |
| 63 |
def test_bit(self) -> None: |
| 64 |
assert is_runtime_subtype(bit_rprimitive, bool_rprimitive) |
| 65 |
assert not is_runtime_subtype(bit_rprimitive, int_rprimitive) |
| 66 |
|
| 67 |
def test_bool(self) -> None: |
| 68 |
assert not is_runtime_subtype(bool_rprimitive, bit_rprimitive) |
| 69 |
assert not is_runtime_subtype(bool_rprimitive, int_rprimitive) |
| 70 |
|
| 71 |
def test_union(self) -> None: |
| 72 |
bool_int_mix = RUnion([bool_rprimitive, int_rprimitive]) |
| 73 |
assert not is_runtime_subtype(bool_int_mix, short_int_rprimitive) |
| 74 |
assert not is_runtime_subtype(bool_int_mix, int_rprimitive) |
| 75 |
assert not is_runtime_subtype(short_int_rprimitive, bool_int_mix) |
| 76 |
assert not is_runtime_subtype(int_rprimitive, bool_int_mix) |
| 77 |
|
| 78 |
|
| 79 |
class TestUnionSimplification(unittest.TestCase): |
| 80 |
def test_simple_type_result(self) -> None: |
| 81 |
assert RUnion.make_simplified_union([int_rprimitive]) == int_rprimitive |
| 82 |
|
| 83 |
def test_remove_duplicate(self) -> None: |
| 84 |
assert RUnion.make_simplified_union([int_rprimitive, int_rprimitive]) == int_rprimitive |
| 85 |
|
| 86 |
def test_cannot_simplify(self) -> None: |
| 87 |
assert RUnion.make_simplified_union( |
| 88 |
[int_rprimitive, str_rprimitive, object_rprimitive] |
| 89 |
) == RUnion([int_rprimitive, str_rprimitive, object_rprimitive]) |
| 90 |
|
| 91 |
def test_nested(self) -> None: |
| 92 |
assert RUnion.make_simplified_union( |
| 93 |
[int_rprimitive, RUnion([str_rprimitive, int_rprimitive])] |
| 94 |
) == RUnion([int_rprimitive, str_rprimitive]) |
| 95 |
assert RUnion.make_simplified_union( |
| 96 |
[int_rprimitive, RUnion([str_rprimitive, RUnion([int_rprimitive])])] |
| 97 |
) == RUnion([int_rprimitive, str_rprimitive]) |