Debian Codegraph @

mypyc/test/test_struct.py

raw
1 from __future__ import annotations
2
3 import unittest
4
5 from mypyc.ir.rtypes import (
6 RStruct,
7 bool_rprimitive,
8 int32_rprimitive,
9 int64_rprimitive,
10 int_rprimitive,
11 object_rprimitive,
12 )
13 from mypyc.rt_subtype import is_runtime_subtype
14
15
16 class TestStruct(unittest.TestCase):
17 def test_struct_offsets(self) -> None:
18 # test per-member alignment
19 r = RStruct("", [], [bool_rprimitive, int32_rprimitive, int64_rprimitive])
20 assert r.size == 16
21 assert r.offsets == [0, 4, 8]
22
23 # test final alignment
24 r1 = RStruct("", [], [bool_rprimitive, bool_rprimitive])
25 assert r1.size == 2
26 assert r1.offsets == [0, 1]
27 r2 = RStruct("", [], [int32_rprimitive, bool_rprimitive])
28 r3 = RStruct("", [], [int64_rprimitive, bool_rprimitive])
29 assert r2.offsets == [0, 4]
30 assert r3.offsets == [0, 8]
31 assert r2.size == 8
32 assert r3.size == 16
33
34 r4 = RStruct("", [], [bool_rprimitive, bool_rprimitive, bool_rprimitive, int32_rprimitive])
35 assert r4.size == 8
36 assert r4.offsets == [0, 1, 2, 4]
37
38 # test nested struct
39 r5 = RStruct("", [], [bool_rprimitive, r])
40 assert r5.offsets == [0, 8]
41 assert r5.size == 24
42 r6 = RStruct("", [], [int32_rprimitive, r5])
43 assert r6.offsets == [0, 8]
44 assert r6.size == 32
45 # test nested struct with alignment less than 8
46 r7 = RStruct("", [], [bool_rprimitive, r4])
47 assert r7.offsets == [0, 4]
48 assert r7.size == 12
49
50 def test_struct_str(self) -> None:
51 r = RStruct("Foo", ["a", "b"], [bool_rprimitive, object_rprimitive])
52 assert str(r) == "Foo{a:bool, b:object}"
53 assert (
54 repr(r) == "<RStruct Foo{a:<RPrimitive builtins.bool>, "
55 "b:<RPrimitive builtins.object>}>"
56 )
57 r1 = RStruct("Bar", ["c"], [int32_rprimitive])
58 assert str(r1) == "Bar{c:i32}"
59 assert repr(r1) == "<RStruct Bar{c:<RPrimitive i32>}>"
60 r2 = RStruct("Baz", [], [])
61 assert str(r2) == "Baz{}"
62 assert repr(r2) == "<RStruct Baz{}>"
63
64 def test_runtime_subtype(self) -> None:
65 # right type to check with
66 r = RStruct("Foo", ["a", "b"], [bool_rprimitive, int_rprimitive])
67
68 # using the exact same fields
69 r1 = RStruct("Foo", ["a", "b"], [bool_rprimitive, int_rprimitive])
70
71 # names different
72 r2 = RStruct("Bar", ["c", "b"], [bool_rprimitive, int_rprimitive])
73
74 # name different
75 r3 = RStruct("Baz", ["a", "b"], [bool_rprimitive, int_rprimitive])
76
77 # type different
78 r4 = RStruct("FooBar", ["a", "b"], [bool_rprimitive, int32_rprimitive])
79
80 # number of types different
81 r5 = RStruct(
82 "FooBarBaz", ["a", "b", "c"], [bool_rprimitive, int_rprimitive, bool_rprimitive]
83 )
84
85 assert is_runtime_subtype(r1, r) is True
86 assert is_runtime_subtype(r2, r) is False
87 assert is_runtime_subtype(r3, r) is False
88 assert is_runtime_subtype(r4, r) is False
89 assert is_runtime_subtype(r5, r) is False
90
91 def test_eq_and_hash(self) -> None:
92 r = RStruct("Foo", ["a", "b"], [bool_rprimitive, int_rprimitive])
93
94 # using the exact same fields
95 r1 = RStruct("Foo", ["a", "b"], [bool_rprimitive, int_rprimitive])
96 assert hash(r) == hash(r1)
97 assert r == r1
98
99 # different name
100 r2 = RStruct("Foq", ["a", "b"], [bool_rprimitive, int_rprimitive])
101 assert hash(r) != hash(r2)
102 assert r != r2
103
104 # different names
105 r3 = RStruct("Foo", ["a", "c"], [bool_rprimitive, int_rprimitive])
106 assert hash(r) != hash(r3)
107 assert r != r3
108
109 # different type
110 r4 = RStruct("Foo", ["a", "b"], [bool_rprimitive, int_rprimitive, bool_rprimitive])
111 assert hash(r) != hash(r4)
112 assert r != r4