Debian Codegraph @

pypy/module/micronumpy/test/test_casting.py

raw symbols SCIP
1 from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest
2 from pypy.module.micronumpy.descriptor import get_dtype_cache, num2dtype
3 from pypy.module.micronumpy.casting import (
4 promote_types, can_cast_type, _promote_types_su)
5 import pypy.module.micronumpy.constants as NPY
6
7
8 class AppTestNumSupport(BaseNumpyAppTest):
9 def test_result_type(self):
10 import numpy as np
11 exc = raises(ValueError, np.result_type)
12 assert str(exc.value) == "at least one array or dtype is required"
13 exc = raises(TypeError, np.result_type, a=2)
14 assert str(exc.value) == "result_type() takes no keyword arguments"
15 assert np.result_type(True) is np.dtype('bool')
16 assert np.result_type(1) is np.dtype('int')
17 assert np.result_type(1.) is np.dtype('float64')
18 assert np.result_type(1+2j) is np.dtype('complex128')
19 assert np.result_type(1, 1.) is np.dtype('float64')
20 assert np.result_type(np.array([1, 2])) is np.dtype('int')
21 assert np.result_type(np.array([1, 2]), 1, 1+2j) is np.dtype('complex128')
22 assert np.result_type(np.array([1, 2]), 1, 'float64') is np.dtype('float64')
23 assert np.result_type(np.array([1, 2]), 1, None) is np.dtype('float64')
24
25 def test_can_cast(self):
26 import numpy as np
27
28 assert np.can_cast(np.int32, np.int64)
29 assert np.can_cast(np.float64, complex)
30 assert not np.can_cast(np.complex64, float)
31 assert np.can_cast(np.bool_, np.bool_)
32
33 assert np.can_cast('i8', 'f8')
34 assert not np.can_cast('i8', 'f4')
35 assert np.can_cast('i4', 'S11')
36
37 assert np.can_cast('i8', 'i8', 'no')
38 assert not np.can_cast('<i8', '>i8', 'no')
39
40 assert np.can_cast('<i8', '>i8', 'equiv')
41 assert not np.can_cast('<i4', '>i8', 'equiv')
42
43 assert np.can_cast('<i4', '>i8', 'safe')
44 assert not np.can_cast('<i8', '>i4', 'safe')
45
46 assert np.can_cast('<i8', '>i4', 'same_kind')
47 assert not np.can_cast('<i8', '>u4', 'same_kind')
48
49 assert np.can_cast('<i8', '>u4', 'unsafe')
50
51 assert np.can_cast('bool', 'S5')
52 assert not np.can_cast('bool', 'S4')
53
54 assert np.can_cast('b', 'S4')
55 assert not np.can_cast('b', 'S3')
56
57 assert np.can_cast('u1', 'S3')
58 assert not np.can_cast('u1', 'S2')
59 assert np.can_cast('u2', 'S5')
60 assert not np.can_cast('u2', 'S4')
61 assert np.can_cast('u4', 'S10')
62 assert not np.can_cast('u4', 'S9')
63 assert np.can_cast('u8', 'S20')
64 assert not np.can_cast('u8', 'S19')
65
66 assert np.can_cast('i1', 'S4')
67 assert not np.can_cast('i1', 'S3')
68 assert np.can_cast('i2', 'S6')
69 assert not np.can_cast('i2', 'S5')
70 assert np.can_cast('i4', 'S11')
71 assert not np.can_cast('i4', 'S10')
72 assert np.can_cast('i8', 'S21')
73 assert not np.can_cast('i8', 'S20')
74
75 assert np.can_cast('bool', 'S5')
76 assert not np.can_cast('bool', 'S4')
77
78 assert np.can_cast('b', 'U4')
79 assert not np.can_cast('b', 'U3')
80
81 assert np.can_cast('u1', 'U3')
82 assert not np.can_cast('u1', 'U2')
83 assert np.can_cast('u2', 'U5')
84 assert not np.can_cast('u2', 'U4')
85 assert np.can_cast('u4', 'U10')
86 assert not np.can_cast('u4', 'U9')
87 assert np.can_cast('u8', 'U20')
88 assert not np.can_cast('u8', 'U19')
89
90 assert np.can_cast('i1', 'U4')
91 assert not np.can_cast('i1', 'U3')
92 assert np.can_cast('i2', 'U6')
93 assert not np.can_cast('i2', 'U5')
94 assert np.can_cast('i4', 'U11')
95 assert not np.can_cast('i4', 'U10')
96 assert np.can_cast('i8', 'U21')
97 assert not np.can_cast('i8', 'U20')
98
99 raises(TypeError, np.can_cast, 'i4', None)
100 raises(TypeError, np.can_cast, None, 'i4')
101
102 def test_can_cast_scalar(self):
103 import numpy as np
104 assert np.can_cast(True, np.bool_)
105 assert np.can_cast(True, np.int8)
106 assert not np.can_cast(0, np.bool_)
107 assert np.can_cast(127, np.int8)
108 assert not np.can_cast(128, np.int8)
109 assert np.can_cast(128, np.int16)
110
111 assert np.can_cast(np.float32('inf'), np.float32)
112 assert np.can_cast(float('inf'), np.float32) # XXX: False in CNumPy?!
113 assert np.can_cast(3.3e38, np.float32)
114 assert not np.can_cast(3.4e38, np.float32)
115
116 assert np.can_cast(1 + 2j, np.complex64)
117 assert not np.can_cast(1 + 1e50j, np.complex64)
118 assert np.can_cast(1., np.complex64)
119 assert not np.can_cast(1e50, np.complex64)
120
121 def test_can_cast_record(self):
122 import numpy as np
123 rec1 = np.dtype([('x', int), ('y', float)])
124 rec2 = np.dtype([('x', float), ('y', float)])
125 rec3 = np.dtype([('y', np.float64), ('x', float)])
126 assert not np.can_cast(rec1, rec2, 'equiv')
127 assert np.can_cast(rec2, rec3, 'equiv')
128 assert np.can_cast(rec1, rec2)
129 assert np.can_cast(int, rec1)
130
131 def test_min_scalar_type(self):
132 import numpy as np
133 assert np.min_scalar_type(2**8 - 1) == np.dtype('uint8')
134 assert np.min_scalar_type(2**64 - 1) == np.dtype('uint64')
135 # XXX: np.asarray(2**64) fails with OverflowError
136 # assert np.min_scalar_type(2**64) == np.dtype('O')
137
138 def test_promote_types(self):
139 import numpy as np
140 assert np.promote_types('f4', 'f8') == np.dtype('float64')
141 assert np.promote_types('i8', 'f4') == np.dtype('float64')
142 assert np.promote_types('>i8', '<c8') == np.dtype('complex128')
143 assert np.promote_types('i4', 'S8') == np.dtype('S11')
144 assert np.promote_types('f4', 'S8') == np.dtype('S32')
145 assert np.promote_types('f4', 'U8') == np.dtype('U32')
146 assert np.promote_types('?', '?') is np.dtype('?')
147 assert np.promote_types('?', 'float64') is np.dtype('float64')
148 assert np.promote_types('float64', '?') is np.dtype('float64')
149 assert np.promote_types('i', 'b') is np.dtype('i')
150 assert np.promote_types('i', '?') is np.dtype('i')
151 assert np.promote_types('c8', 'f8') is np.dtype('c16')
152 assert np.promote_types('c8', 'longdouble') == np.dtype('clongdouble')
153 assert np.promote_types('c16', 'longdouble') == np.dtype('clongdouble')
154
155 def test_result_type(self):
156 import numpy as np
157 assert np.result_type(np.uint8, np.int8) == np.int16
158 assert np.result_type(np.uint16(1), np.int8(0)) == np.int32
159 assert np.result_type(np.uint16(1), np.int8(0), np.uint8) == np.uint8
160 assert np.result_type(-1, np.uint8, 1) == np.int16
161
162 def test_can_cast_same_type(space):
163 dt_bool = get_dtype_cache(space).w_booldtype
164 assert can_cast_type(space, dt_bool, dt_bool, 'no')
165 assert can_cast_type(space, dt_bool, dt_bool, 'equiv')
166 assert can_cast_type(space, dt_bool, dt_bool, 'safe')
167 assert can_cast_type(space, dt_bool, dt_bool, 'same_kind')
168 assert can_cast_type(space, dt_bool, dt_bool, 'unsafe')
169
170 def test_promote_types_su(space):
171 dt_int8 = num2dtype(space, NPY.BYTE)
172 dt_uint8 = num2dtype(space, NPY.UBYTE)
173 dt_int16 = num2dtype(space, NPY.SHORT)
174 dt_uint16 = num2dtype(space, NPY.USHORT)
175 # The results must be signed
176 assert _promote_types_su(space, dt_int8, dt_int16, False, False) == (dt_int16, False)
177 assert _promote_types_su(space, dt_int8, dt_int16, True, False) == (dt_int16, False)
178 assert _promote_types_su(space, dt_int8, dt_int16, False, True) == (dt_int16, False)
179
180 # The results may be unsigned
181 assert _promote_types_su(space, dt_int8, dt_int16, True, True) == (dt_int16, True)
182 assert _promote_types_su(space, dt_uint8, dt_int16, False, True) == (dt_uint16, True)