Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
numba
GitHub Repository: numba/llvmlite
Path: blob/main/llvmlite/tests/test_valuerepr.py
1154 views
1
import math
2
import sys
3
import unittest
4
5
from llvmlite.ir import (
6
Constant, FloatType, DoubleType, LiteralStructType, IntType,
7
ArrayType, HalfType)
8
from llvmlite.tests import TestCase
9
10
11
int8 = IntType(8)
12
int16 = IntType(16)
13
14
15
PY36_OR_LATER = sys.version_info[:2] >= (3, 6)
16
17
18
class TestValueRepr(TestCase):
19
20
def test_double_repr(self):
21
def check_repr(val, expected):
22
c = Constant(DoubleType(), val)
23
self.assertEqual(str(c), expected)
24
check_repr(math.pi, "double 0x400921fb54442d18")
25
check_repr(float('inf'), "double 0x7ff0000000000000")
26
check_repr(float('-inf'), "double 0xfff0000000000000")
27
28
def test_float_repr(self):
29
def check_repr(val, expected):
30
c = Constant(FloatType(), val)
31
self.assertEqual(str(c), expected)
32
check_repr(math.pi, "float 0x400921fb60000000")
33
check_repr(float('inf'), "float 0x7ff0000000000000")
34
check_repr(float('-inf'), "float 0xfff0000000000000")
35
36
@unittest.skipUnless(PY36_OR_LATER, 'py36+ only')
37
def test_half_repr(self):
38
def check_repr(val, expected):
39
c = Constant(HalfType(), val)
40
self.assertEqual(str(c), expected)
41
check_repr(math.pi, "half 0x4009200000000000")
42
check_repr(float('inf'), "half 0x7ff0000000000000")
43
check_repr(float('-inf'), "half 0xfff0000000000000")
44
45
def test_struct_repr(self):
46
tp = LiteralStructType([int8, int16])
47
c = Constant(tp, (Constant(int8, 100), Constant(int16, 1000)))
48
self.assertEqual(str(c), "{i8, i16} {i8 100, i16 1000}")
49
50
def test_array_repr(self):
51
tp = ArrayType(int8, 3)
52
values = [Constant(int8, x) for x in (5, 10, -15)]
53
c = Constant(tp, values)
54
self.assertEqual(str(c), "[3 x i8] [i8 5, i8 10, i8 -15]")
55
c = Constant(tp, bytearray(b"\x01\x02\x03"))
56
self.assertEqual(str(c), '[3 x i8] c"\\01\\02\\03"')
57
58
59
if __name__ == "__main__":
60
unittest.main()
61
62