1"""Unit tests for RArray types."""
2
3import unittest
4
5from mypyc.common import PLATFORM_SIZE
6from mypyc.ir.rtypes import (
7    RArray, int_rprimitive, bool_rprimitive, compute_rtype_alignment, compute_rtype_size
8)
9
10
11class TestRArray(unittest.TestCase):
12    def test_basics(self) -> None:
13        a = RArray(int_rprimitive, 10)
14        assert a.item_type == int_rprimitive
15        assert a.length == 10
16
17    def test_str_conversion(self) -> None:
18        a = RArray(int_rprimitive, 10)
19        assert str(a) == 'int[10]'
20        assert repr(a) == '<RArray <RPrimitive builtins.int>[10]>'
21
22    def test_eq(self) -> None:
23        a = RArray(int_rprimitive, 10)
24        assert a == RArray(int_rprimitive, 10)
25        assert a != RArray(bool_rprimitive, 10)
26        assert a != RArray(int_rprimitive, 9)
27
28    def test_hash(self) -> None:
29        assert hash(RArray(int_rprimitive, 10)) == hash(RArray(int_rprimitive, 10))
30        assert hash(RArray(bool_rprimitive, 5)) == hash(RArray(bool_rprimitive, 5))
31
32    def test_alignment(self) -> None:
33        a = RArray(int_rprimitive, 10)
34        assert compute_rtype_alignment(a) == PLATFORM_SIZE
35        b = RArray(bool_rprimitive, 55)
36        assert compute_rtype_alignment(b) == 1
37
38    def test_size(self) -> None:
39        a = RArray(int_rprimitive, 9)
40        assert compute_rtype_size(a) == 9 * PLATFORM_SIZE
41        b = RArray(bool_rprimitive, 3)
42        assert compute_rtype_size(b) == 3
43