-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtest_array_basics.py
159 lines (146 loc) · 6.79 KB
/
test_array_basics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import unittest
from dynd import nd, ndt
import math
class TestBasics(unittest.TestCase):
def test_null_array(self):
a = nd.array()
self.assertEqual(str(a), "nd.array()")
self.assertEqual(repr(a), "nd.array()")
self.assertRaises(AttributeError, lambda: a.real)
self.assertRaises(AttributeError, lambda: a.access_flags)
self.assertRaises(AttributeError, lambda: a.shape)
self.assertRaises(AttributeError, lambda: a.strides)
self.assertRaises(AttributeError, lambda: a.is_scalar)
def test_index(self):
# Test that the __index__ method/nb_index slot
# in dynd arrays is working
a = [1, 2, 3, 4, 5, 6]
self.assertEqual(a[nd.array(0)], 1)
self.assertEqual(a[nd.array(1):nd.array(3)], [2, 3])
self.assertEqual(a[nd.array(-1, type=ndt.int8)], 6)
def test_index_errors(self):
a = [1, 2, 3, 4, 5, 6]
self.assertRaises(TypeError, lambda x : a[x], nd.array(True, type = ndt.bool))
self.assertRaises(TypeError, lambda x : a[x], nd.array(3.5, type = ndt.float64))
self.assertRaises(IndexError, lambda x : a[x], nd.array(10, type = ndt.int32))
def test_nonzero(self):
# boolean values
self.assertFalse(bool(nd.array(False)))
self.assertTrue(bool(nd.array(True)))
# integer values
self.assertFalse(bool(nd.array(0, type=ndt.int8)))
self.assertFalse(bool(nd.array(0, type=ndt.uint8)))
self.assertFalse(bool(nd.array(0, type=ndt.int64)))
self.assertFalse(bool(nd.array(0, type=ndt.uint64)))
self.assertTrue(bool(nd.array(100, type=ndt.int8)))
self.assertTrue(bool(nd.array(100, type=ndt.uint8)))
self.assertTrue(bool(nd.array(100, type=ndt.int64)))
self.assertTrue(bool(nd.array(100, type=ndt.uint64)))
# float values
self.assertFalse(bool(nd.array(0.0, type=ndt.float32)))
self.assertFalse(bool(nd.array(0.0, type=ndt.float64)))
self.assertTrue(bool(nd.array(100.0, type=ndt.float32)))
self.assertTrue(bool(nd.array(100.0, type=ndt.float64)))
# complex values
self.assertFalse(bool(nd.array(0.0, type=ndt.complex_float32)))
self.assertFalse(bool(nd.array(0.0, type=ndt.complex_float64)))
self.assertTrue(bool(nd.array(100.0+10.0j, type=ndt.complex_float32)))
self.assertTrue(bool(nd.array(100.0+10.0j, type=ndt.complex_float64)))
# strings
self.assertFalse(bool(nd.array('')))
self.assertFalse(bool(nd.array('', type = ndt.string)))
self.assertTrue(bool(nd.array(' ')))
self.assertTrue(bool(nd.array('test', type = ndt.string)))
def test_nonzero_errors(self):
# Non-scalars raise errors like NumPy, because their
# truth value is ambiguous
self.assertRaises(ValueError, bool, nd.array([0]))
self.assertRaises(ValueError, bool, nd.array([1, 2, 3]))
self.assertRaises(ValueError, bool, nd.array(['abc', 3], type='{x:string, y:int32}'))
def test_iter(self):
# Iteration of a 1D array
a = nd.array([1, 2, 3])
self.assertEqual([nd.as_py(x) for x in a], [1, 2, 3])
# Iteration of a 2D array
a = nd.array([[1, 2, 3], [4,5,6]])
self.assertEqual([nd.as_py(x) for x in a], [[1, 2, 3], [4,5,6]])
def test_iter_fixed_dim(self):
# Iteration of a 1D array
a = nd.array([1, 2, 3], type='3 * int64')
self.assertEqual(len(a), 3)
self.assertEqual([nd.as_py(x) for x in a], [1, 2, 3])
# Iteration of a 2D array
a = nd.array([[1, 2, 3], [4,5,6]], type='2 * 3 * int64')
self.assertEqual(len(a), 2)
self.assertEqual([nd.as_py(x) for x in a], [[1, 2, 3], [4,5,6]])
def test_contiguous(self):
# Scalar is contiguous
a = nd.array(3)
self.assertTrue(nd.is_c_contiguous(a))
self.assertTrue(nd.is_f_contiguous(a))
# Default-constructed 1D array
a = nd.array([1, 3, 5, 2])
self.assertTrue(nd.is_c_contiguous(a))
self.assertTrue(nd.is_f_contiguous(a))
# Sliced 1D array with a step
a = nd.array([1, 3, 5, 2])[::2]
self.assertFalse(nd.is_c_contiguous(a))
self.assertFalse(nd.is_f_contiguous(a))
a = nd.array([1, 3, 5, 2])[::-1]
self.assertFalse(nd.is_c_contiguous(a))
self.assertFalse(nd.is_f_contiguous(a))
# Default-constructed 2D array
a = nd.array([[1, 3, 5, 2], [1, 2, 3, 4]])
self.assertTrue(nd.is_c_contiguous(a))
self.assertFalse(nd.is_f_contiguous(a))
# Sliced 2D array with a step
a = nd.array([[1, 3, 5, 2], [1, 2, 3, 4], [3,4,5,6]])[::2]
self.assertFalse(nd.is_c_contiguous(a))
self.assertFalse(nd.is_f_contiguous(a))
a = nd.array([[1, 3, 5, 2], [1, 2, 3, 4]])[:, ::2]
self.assertFalse(nd.is_c_contiguous(a))
self.assertFalse(nd.is_f_contiguous(a))
a = nd.array([[1, 3, 5, 2], [1, 2, 3, 4]])[::-1, ::-1]
self.assertFalse(nd.is_c_contiguous(a))
self.assertFalse(nd.is_f_contiguous(a))
def test_uint_value_limits(self):
# Valid maximums
a = nd.array(0xff, type = ndt.uint8)
self.assertEqual(nd.as_py(a), 0xff)
a = nd.array(0xffff, type = ndt.uint16)
self.assertEqual(nd.as_py(a), 0xffff)
a = nd.array(0xffffffff, type = ndt.uint32)
self.assertEqual(nd.as_py(a), 0xffffffff)
a = nd.array(0xffffffffffffffff, type = ndt.uint64)
self.assertEqual(nd.as_py(a), 0xffffffffffffffff)
# Out of bounds
self.assertRaises(OverflowError, nd.array, 0x100, type = ndt.uint8)
self.assertRaises(OverflowError, nd.array, 0x10000, type = ndt.uint16)
self.assertRaises(OverflowError, nd.array, 0x100000000, type = ndt.uint32)
self.assertRaises(OverflowError, nd.array, 0x10000000000000000, type = ndt.uint64)
def test_inf(self):
# Validate nd.inf
self.assertEqual(nd.inf * 2, nd.inf)
self.assertTrue(nd.inf > 0)
self.assertTrue(-nd.inf < 0)
# as an array
a = nd.array(nd.inf)
self.assertEqual(nd.as_py(a), nd.inf)
self.assertEqual(nd.type_of(a), ndt.float64)
a = nd.array(nd.inf, type = ndt.float32)
self.assertEqual(nd.as_py(a), nd.inf)
def test_nan(self):
# Validate nd.nan
self.assertTrue(math.isnan(nd.nan))
self.assertFalse(nd.nan > 0)
self.assertFalse(nd.nan < 0)
self.assertFalse(nd.nan == 0)
self.assertFalse(nd.nan == nd.nan)
# as an array
a = nd.array(nd.nan, type = ndt.float64)
self.assertTrue(math.isnan(nd.as_py(a)))
self.assertEqual(nd.type_of(a), ndt.float64)
a = nd.array(nd.nan, type = ndt.float32)
self.assertTrue(math.isnan(nd.as_py(a)))
if __name__ == '__main__':
unittest.main(verbosity=2)