ndtypes 0.2.0dev4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CONTRIBUTING.md +50 -0
- data/Gemfile +2 -0
- data/History.md +0 -0
- data/README.md +19 -0
- data/Rakefile +125 -0
- data/ext/ruby_ndtypes/extconf.rb +55 -0
- data/ext/ruby_ndtypes/gc_guard.c +36 -0
- data/ext/ruby_ndtypes/gc_guard.h +12 -0
- data/ext/ruby_ndtypes/ndtypes/AUTHORS.txt +5 -0
- data/ext/ruby_ndtypes/ndtypes/INSTALL.txt +101 -0
- data/ext/ruby_ndtypes/ndtypes/LICENSE.txt +29 -0
- data/ext/ruby_ndtypes/ndtypes/MANIFEST.in +3 -0
- data/ext/ruby_ndtypes/ndtypes/Makefile.in +87 -0
- data/ext/ruby_ndtypes/ndtypes/README.rst +47 -0
- data/ext/ruby_ndtypes/ndtypes/config.guess +1530 -0
- data/ext/ruby_ndtypes/ndtypes/config.h.in +67 -0
- data/ext/ruby_ndtypes/ndtypes/config.sub +1782 -0
- data/ext/ruby_ndtypes/ndtypes/configure +5260 -0
- data/ext/ruby_ndtypes/ndtypes/configure.ac +161 -0
- data/ext/ruby_ndtypes/ndtypes/doc/Makefile +14 -0
- data/ext/ruby_ndtypes/ndtypes/doc/_static/copybutton.js +66 -0
- data/ext/ruby_ndtypes/ndtypes/doc/conf.py +26 -0
- data/ext/ruby_ndtypes/ndtypes/doc/grammar/grammar.rst +27 -0
- data/ext/ruby_ndtypes/ndtypes/doc/index.rst +56 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/context.rst +131 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/encodings.rst +68 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/fields-values.rst +175 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/functions.rst +72 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/index.rst +43 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/init.rst +48 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/io.rst +100 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/memory.rst +124 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/predicates.rst +110 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/typedef.rst +31 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/types.rst +594 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/util.rst +166 -0
- data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/buffer-protocol.rst +27 -0
- data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/index.rst +21 -0
- data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/pattern-matching.rst +330 -0
- data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/quickstart.rst +144 -0
- data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/types.rst +544 -0
- data/ext/ruby_ndtypes/ndtypes/doc/releases/index.rst +35 -0
- data/ext/ruby_ndtypes/ndtypes/install-sh +527 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/Makefile.in +271 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/Makefile.vc +269 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/alloc.c +230 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/attr.c +268 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/attr.h +109 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/Makefile.in +73 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/Makefile.vc +70 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/README.txt +16 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bpgrammar.c +2179 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bpgrammar.h +134 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bpgrammar.y +428 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bplexer.c +2543 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bplexer.h +735 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bplexer.l +176 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/export.c +543 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/import.c +110 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/context.c +228 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/copy.c +634 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/encodings.c +116 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/equal.c +288 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/grammar.c +3067 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/grammar.h +180 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/grammar.y +417 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/io.c +1658 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/lexer.c +2773 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/lexer.h +734 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/lexer.l +222 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/match.c +1132 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/ndtypes.c +2323 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/ndtypes.h.in +893 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/overflow.h +161 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/parsefuncs.c +473 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/parsefuncs.h +92 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/parser.c +246 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/seq.c +269 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/seq.h +197 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/Makefile.in +48 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/Makefile.vc +46 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/deserialize.c +1007 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/serialize.c +442 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/slice.h +42 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/substitute.c +238 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/substitute.h +50 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/symtable.c +371 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/symtable.h +100 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/Makefile.in +55 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/Makefile.vc +45 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/alloc_fail.c +82 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/alloc_fail.h +49 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/runtest.c +1657 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test.h +85 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_array.c +115 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_buffer.c +137 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_indent.c +201 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_match.c +2397 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_numba.c +57 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_parse.c +349 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_parse_error.c +27839 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_parse_roundtrip.c +350 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_record.c +231 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_typecheck.c +375 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_typedef.c +65 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/valgrind.supp +30 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tools/bench.c +79 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tools/indent.c +94 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tools/print_ast.c +96 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/util.c +474 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/values.c +228 -0
- data/ext/ruby_ndtypes/ndtypes/python/bench.py +49 -0
- data/ext/ruby_ndtypes/ndtypes/python/ndt_randtype.py +409 -0
- data/ext/ruby_ndtypes/ndtypes/python/ndt_support.py +14 -0
- data/ext/ruby_ndtypes/ndtypes/python/ndtypes/__init__.py +70 -0
- data/ext/ruby_ndtypes/ndtypes/python/ndtypes/_ndtypes.c +1332 -0
- data/ext/ruby_ndtypes/ndtypes/python/ndtypes/docstrings.h +319 -0
- data/ext/ruby_ndtypes/ndtypes/python/ndtypes/pyndtypes.h +154 -0
- data/ext/ruby_ndtypes/ndtypes/python/test_ndtypes.py +1977 -0
- data/ext/ruby_ndtypes/ndtypes/setup.py +288 -0
- data/ext/ruby_ndtypes/ndtypes/vcbuild/INSTALL.txt +41 -0
- data/ext/ruby_ndtypes/ndtypes/vcbuild/runtest32.bat +15 -0
- data/ext/ruby_ndtypes/ndtypes/vcbuild/runtest64.bat +13 -0
- data/ext/ruby_ndtypes/ndtypes/vcbuild/vcbuild32.bat +38 -0
- data/ext/ruby_ndtypes/ndtypes/vcbuild/vcbuild64.bat +38 -0
- data/ext/ruby_ndtypes/ndtypes/vcbuild/vcclean.bat +13 -0
- data/ext/ruby_ndtypes/ndtypes/vcbuild/vcdistclean.bat +14 -0
- data/ext/ruby_ndtypes/ruby_ndtypes.c +1003 -0
- data/ext/ruby_ndtypes/ruby_ndtypes.h +37 -0
- data/ext/ruby_ndtypes/ruby_ndtypes_internal.h +28 -0
- data/lib/ndtypes.rb +45 -0
- data/lib/ndtypes/errors.rb +2 -0
- data/lib/ndtypes/version.rb +6 -0
- data/ndtypes.gemspec +47 -0
- data/spec/gc_table_spec.rb +10 -0
- data/spec/ndtypes_spec.rb +289 -0
- data/spec/spec_helper.rb +241 -0
- metadata +242 -0
@@ -0,0 +1,1977 @@
|
|
1
|
+
#
|
2
|
+
# BSD 3-Clause License
|
3
|
+
#
|
4
|
+
# Copyright (c) 2017-2018, plures
|
5
|
+
# All rights reserved.
|
6
|
+
#
|
7
|
+
# Redistribution and use in source and binary forms, with or without
|
8
|
+
# modification, are permitted provided that the following conditions are met:
|
9
|
+
#
|
10
|
+
# 1. Redistributions of source code must retain the above copyright notice,
|
11
|
+
# this list of conditions and the following disclaimer.
|
12
|
+
#
|
13
|
+
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
14
|
+
# this list of conditions and the following disclaimer in the documentation
|
15
|
+
# and/or other materials provided with the distribution.
|
16
|
+
#
|
17
|
+
# 3. Neither the name of the copyright holder nor the names of its
|
18
|
+
# contributors may be used to endorse or promote products derived from
|
19
|
+
# this software without specific prior written permission.
|
20
|
+
#
|
21
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
22
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
23
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
24
|
+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
25
|
+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
26
|
+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
27
|
+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
28
|
+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
29
|
+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
30
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
31
|
+
#
|
32
|
+
|
33
|
+
import sys, argparse
|
34
|
+
import unittest, gc
|
35
|
+
import weakref, struct
|
36
|
+
from copy import copy
|
37
|
+
from ndtypes import ndt, typedef, instantiate, MAX_DIM, ApplySpec
|
38
|
+
from ndt_support import *
|
39
|
+
from ndt_randtype import *
|
40
|
+
from random import random
|
41
|
+
import pickle
|
42
|
+
|
43
|
+
|
44
|
+
try:
|
45
|
+
import numpy as np
|
46
|
+
except ImportError:
|
47
|
+
np = None
|
48
|
+
|
49
|
+
|
50
|
+
SKIP_LONG = True
|
51
|
+
SKIP_BRUTE_FORCE = True
|
52
|
+
|
53
|
+
|
54
|
+
def check_serialize(self, t):
|
55
|
+
b = t.serialize()
|
56
|
+
u = ndt.deserialize(b)
|
57
|
+
self.assertEqual(u, t)
|
58
|
+
|
59
|
+
s = pickle.dumps(t)
|
60
|
+
u = pickle.loads(s)
|
61
|
+
self.assertEqual(u, t)
|
62
|
+
|
63
|
+
|
64
|
+
class TestModule(unittest.TestCase):
|
65
|
+
|
66
|
+
def test_module_predicates(self):
|
67
|
+
# Namespaces are not yet supported in xnd. One can construct the
|
68
|
+
# types, however. Modules are for pattern matching only, so they
|
69
|
+
# are abstract.
|
70
|
+
t = ndt("SomeNamespace:: 2 * 3 * float64")
|
71
|
+
check_serialize(self, t)
|
72
|
+
|
73
|
+
self.assertTrue(t.isabstract())
|
74
|
+
self.assertFalse(t.iscomplex())
|
75
|
+
self.assertFalse(t.isconcrete())
|
76
|
+
self.assertFalse(t.isfloat())
|
77
|
+
self.assertFalse(t.isoptional())
|
78
|
+
self.assertFalse(t.isscalar())
|
79
|
+
self.assertFalse(t.issigned())
|
80
|
+
self.assertFalse(t.isunsigned())
|
81
|
+
|
82
|
+
self.assertFalse(t.is_f_contiguous())
|
83
|
+
self.assertFalse(t.is_c_contiguous())
|
84
|
+
|
85
|
+
def test_module_common_fields(self):
|
86
|
+
t = ndt("SomeNamespace:: 2 * 3 * float64")
|
87
|
+
check_serialize(self, t)
|
88
|
+
|
89
|
+
# Common type fields are undefined.
|
90
|
+
self.assertRaises(TypeError, getattr, t, 'ndim')
|
91
|
+
self.assertRaises(TypeError, getattr, t, 'itemsize')
|
92
|
+
self.assertRaises(TypeError, getattr, t, 'align')
|
93
|
+
|
94
|
+
# Cannot be represented as an ndarray.
|
95
|
+
self.assertRaises(TypeError, getattr, t, 'shape')
|
96
|
+
self.assertRaises(TypeError, getattr, t, 'strides')
|
97
|
+
|
98
|
+
|
99
|
+
class TestFunction(unittest.TestCase):
|
100
|
+
|
101
|
+
def test_function_predicates(self):
|
102
|
+
t = ndt("(10 * float64, string) -> float64")
|
103
|
+
check_serialize(self, t)
|
104
|
+
|
105
|
+
self.assertTrue(t.isabstract())
|
106
|
+
self.assertFalse(t.iscomplex())
|
107
|
+
self.assertFalse(t.isconcrete())
|
108
|
+
self.assertFalse(t.isfloat())
|
109
|
+
self.assertFalse(t.isoptional())
|
110
|
+
self.assertFalse(t.isscalar())
|
111
|
+
self.assertFalse(t.issigned())
|
112
|
+
self.assertFalse(t.isunsigned())
|
113
|
+
|
114
|
+
self.assertFalse(t.is_c_contiguous())
|
115
|
+
self.assertFalse(t.is_f_contiguous())
|
116
|
+
|
117
|
+
def test_function_common_fields(self):
|
118
|
+
t = ndt("(10 * float64, string) -> float64")
|
119
|
+
check_serialize(self, t)
|
120
|
+
|
121
|
+
# Common type fields are undefined.
|
122
|
+
self.assertRaises(TypeError, getattr, t, 'ndim')
|
123
|
+
self.assertRaises(TypeError, getattr, t, 'itemsize')
|
124
|
+
self.assertRaises(TypeError, getattr, t, 'align')
|
125
|
+
|
126
|
+
# Cannot be represented as an ndarray.
|
127
|
+
self.assertRaises(TypeError, getattr, t, 'shape')
|
128
|
+
self.assertRaises(TypeError, getattr, t, 'strides')
|
129
|
+
|
130
|
+
|
131
|
+
class TestVoid(unittest.TestCase):
|
132
|
+
|
133
|
+
def test_void_as_return_value(self):
|
134
|
+
t = ndt("() -> void")
|
135
|
+
check_serialize(self, t)
|
136
|
+
|
137
|
+
self.assertTrue(t.isabstract())
|
138
|
+
self.assertFalse(t.iscomplex())
|
139
|
+
self.assertFalse(t.isconcrete())
|
140
|
+
self.assertFalse(t.isfloat())
|
141
|
+
self.assertFalse(t.isoptional())
|
142
|
+
self.assertFalse(t.isscalar())
|
143
|
+
self.assertFalse(t.issigned())
|
144
|
+
self.assertFalse(t.isunsigned())
|
145
|
+
|
146
|
+
self.assertFalse(t.is_c_contiguous())
|
147
|
+
self.assertFalse(t.is_f_contiguous())
|
148
|
+
|
149
|
+
def test_void_exceptions(self):
|
150
|
+
# Void can only be used as a function return type.
|
151
|
+
self.assertRaises(ValueError, ndt, "void")
|
152
|
+
|
153
|
+
|
154
|
+
class TestAny(unittest.TestCase):
|
155
|
+
|
156
|
+
def test_any_predicates(self):
|
157
|
+
t = ndt("Any")
|
158
|
+
|
159
|
+
self.assertTrue(t.isabstract())
|
160
|
+
self.assertFalse(t.iscomplex())
|
161
|
+
self.assertFalse(t.isconcrete())
|
162
|
+
self.assertFalse(t.isfloat())
|
163
|
+
self.assertFalse(t.isoptional())
|
164
|
+
self.assertFalse(t.isscalar())
|
165
|
+
self.assertFalse(t.issigned())
|
166
|
+
self.assertFalse(t.isunsigned())
|
167
|
+
|
168
|
+
self.assertFalse(t.is_c_contiguous())
|
169
|
+
self.assertFalse(t.is_f_contiguous())
|
170
|
+
|
171
|
+
def test_any_common_fields(self):
|
172
|
+
t = ndt("Any")
|
173
|
+
|
174
|
+
# Common type fields are undefined.
|
175
|
+
self.assertRaises(TypeError, getattr, t, 'ndim')
|
176
|
+
self.assertRaises(TypeError, getattr, t, 'itemsize')
|
177
|
+
self.assertRaises(TypeError, getattr, t, 'align')
|
178
|
+
|
179
|
+
# Cannot be represented as an ndarray.
|
180
|
+
self.assertRaises(TypeError, getattr, t, 'shape')
|
181
|
+
self.assertRaises(TypeError, getattr, t, 'strides')
|
182
|
+
|
183
|
+
|
184
|
+
class TestFixedDim(unittest.TestCase):
|
185
|
+
|
186
|
+
def test_fixed_dim_predicates(self):
|
187
|
+
t = ndt("10 * 20 * complex128")
|
188
|
+
check_serialize(self, t)
|
189
|
+
|
190
|
+
self.assertFalse(t.isabstract())
|
191
|
+
self.assertFalse(t.iscomplex())
|
192
|
+
self.assertTrue(t.isconcrete())
|
193
|
+
self.assertFalse(t.isfloat())
|
194
|
+
self.assertFalse(t.isoptional())
|
195
|
+
self.assertFalse(t.isscalar())
|
196
|
+
self.assertFalse(t.issigned())
|
197
|
+
self.assertFalse(t.isunsigned())
|
198
|
+
|
199
|
+
self.assertTrue(t.is_c_contiguous())
|
200
|
+
self.assertFalse(t.is_f_contiguous())
|
201
|
+
|
202
|
+
t = ndt("20 * complex128")
|
203
|
+
self.assertTrue(t.is_c_contiguous())
|
204
|
+
self.assertTrue(t.is_f_contiguous())
|
205
|
+
|
206
|
+
t = ndt("1 * 10 * complex128")
|
207
|
+
self.assertTrue(t.is_c_contiguous())
|
208
|
+
self.assertTrue(t.is_f_contiguous())
|
209
|
+
|
210
|
+
def test_fixed_dim_common_fields(self):
|
211
|
+
dt = "{a: complex64, b: string}"
|
212
|
+
t = ndt("2 * 3 * %s" % dt)
|
213
|
+
check_serialize(self, t)
|
214
|
+
|
215
|
+
dtype = ndt(dt)
|
216
|
+
|
217
|
+
self.assertEqual(t.ndim, 2)
|
218
|
+
if not HAVE_32_BIT_LINUX:
|
219
|
+
self.assertEqual(t.itemsize, dtype.itemsize)
|
220
|
+
self.assertEqual(t.align, dtype.align)
|
221
|
+
|
222
|
+
self.assertEqual(t.shape, (2, 3))
|
223
|
+
self.assertEqual(t.strides, (3 * dtype.itemsize, dtype.itemsize))
|
224
|
+
|
225
|
+
def test_fixed_dim_invariants(self):
|
226
|
+
# Mixing var and fixed is disallowed.
|
227
|
+
self.assertRaises(TypeError, ndt, "10 * var * int8")
|
228
|
+
self.assertRaises(TypeError, ndt, "var * 10 * int16")
|
229
|
+
self.assertRaises(TypeError, ndt, "10 * var * 10 * int32")
|
230
|
+
self.assertRaises(TypeError, ndt, "var * 10 * var * int64")
|
231
|
+
|
232
|
+
def test_fixed_dim_dtypes(self):
|
233
|
+
for dtype, mem in DTYPE_TEST_CASES:
|
234
|
+
t = ndt(dtype)
|
235
|
+
self.assertEqual(t.ndim, 0)
|
236
|
+
if not HAVE_32_BIT_LINUX:
|
237
|
+
self.assertEqual(t.itemsize, mem.itemsize)
|
238
|
+
self.assertEqual(t.align, mem.align)
|
239
|
+
|
240
|
+
self.assertEqual(t.shape, ())
|
241
|
+
self.assertEqual(t.strides, ())
|
242
|
+
|
243
|
+
for i in range(10):
|
244
|
+
t = ndt("%d * %s" % (i, dtype))
|
245
|
+
check_serialize(self, t)
|
246
|
+
shape = (i,)
|
247
|
+
strides = (mem.itemsize,)
|
248
|
+
|
249
|
+
self.assertEqual(t.ndim, 1)
|
250
|
+
if not HAVE_32_BIT_LINUX:
|
251
|
+
self.assertEqual(t.itemsize, mem.itemsize)
|
252
|
+
self.assertEqual(t.align, mem.align)
|
253
|
+
self.assertEqual(t.strides, strides)
|
254
|
+
|
255
|
+
self.assertEqual(t.shape, shape)
|
256
|
+
self.assertEqual(t.datasize, c_datasize(t))
|
257
|
+
self.assertTrue(verify_datasize(t))
|
258
|
+
|
259
|
+
for i in range(10):
|
260
|
+
for j in range(10):
|
261
|
+
t = ndt("%d * %d * %s" % (i, j, dtype))
|
262
|
+
check_serialize(self, t)
|
263
|
+
shape = (i, j)
|
264
|
+
strides = (j * mem.itemsize, mem.itemsize)
|
265
|
+
|
266
|
+
self.assertEqual(t.ndim, 2)
|
267
|
+
if not HAVE_32_BIT_LINUX:
|
268
|
+
self.assertEqual(t.itemsize, mem.itemsize)
|
269
|
+
self.assertEqual(t.align, mem.align)
|
270
|
+
self.assertEqual(t.strides, strides)
|
271
|
+
|
272
|
+
self.assertEqual(t.shape, shape)
|
273
|
+
self.assertEqual(t.datasize, c_datasize(t))
|
274
|
+
self.assertTrue(verify_datasize(t))
|
275
|
+
|
276
|
+
for i in range(5):
|
277
|
+
for j in range(5):
|
278
|
+
for k in range(5):
|
279
|
+
t = ndt("%d * %d * %d * %s" % (i, j, k, dtype))
|
280
|
+
check_serialize(self, t)
|
281
|
+
shape = (i, j, k)
|
282
|
+
strides = (j * k * mem.itemsize, k * mem.itemsize, mem.itemsize)
|
283
|
+
|
284
|
+
self.assertEqual(t.ndim, 3)
|
285
|
+
if not HAVE_32_BIT_LINUX:
|
286
|
+
self.assertEqual(t.itemsize, mem.itemsize)
|
287
|
+
self.assertEqual(t.align, mem.align)
|
288
|
+
self.assertEqual(t.strides, strides)
|
289
|
+
|
290
|
+
self.assertEqual(t.shape, shape)
|
291
|
+
self.assertEqual(t.datasize, c_datasize(t))
|
292
|
+
self.assertTrue(verify_datasize(t))
|
293
|
+
|
294
|
+
|
295
|
+
class TestFortran(unittest.TestCase):
|
296
|
+
|
297
|
+
def test_fortran_predicates(self):
|
298
|
+
t = ndt("!10 * 20 * complex128")
|
299
|
+
|
300
|
+
self.assertFalse(t.isabstract())
|
301
|
+
self.assertFalse(t.iscomplex())
|
302
|
+
self.assertTrue(t.isconcrete())
|
303
|
+
self.assertFalse(t.isfloat())
|
304
|
+
self.assertFalse(t.isoptional())
|
305
|
+
self.assertFalse(t.isscalar())
|
306
|
+
self.assertFalse(t.issigned())
|
307
|
+
self.assertFalse(t.isunsigned())
|
308
|
+
|
309
|
+
self.assertFalse(t.is_c_contiguous())
|
310
|
+
self.assertTrue(t.is_f_contiguous())
|
311
|
+
|
312
|
+
t = ndt("!20 * complex128")
|
313
|
+
self.assertTrue(t.is_c_contiguous())
|
314
|
+
self.assertTrue(t.is_f_contiguous())
|
315
|
+
|
316
|
+
t = ndt("!1 * 10 * uint8")
|
317
|
+
self.assertTrue(t.is_c_contiguous())
|
318
|
+
self.assertTrue(t.is_f_contiguous())
|
319
|
+
|
320
|
+
def test_fortran_common_fields(self):
|
321
|
+
dt = "{a: complex64, b: string}"
|
322
|
+
t = ndt("!2 * 3 * %s" % dt)
|
323
|
+
dtype = ndt(dt)
|
324
|
+
|
325
|
+
self.assertEqual(t.ndim, 2)
|
326
|
+
if not HAVE_32_BIT_LINUX:
|
327
|
+
self.assertEqual(t.itemsize, dtype.itemsize)
|
328
|
+
self.assertEqual(t.align, dtype.align)
|
329
|
+
|
330
|
+
self.assertEqual(t.shape, (2, 3))
|
331
|
+
self.assertEqual(t.strides, (dtype.itemsize, 2 * dtype.itemsize))
|
332
|
+
self.assertTrue(verify_datasize(t))
|
333
|
+
|
334
|
+
def test_fortran_dtypes(self):
|
335
|
+
for dtype, mem in DTYPE_TEST_CASES:
|
336
|
+
t = ndt(dtype)
|
337
|
+
self.assertEqual(t.ndim, 0)
|
338
|
+
if not HAVE_32_BIT_LINUX:
|
339
|
+
self.assertEqual(t.itemsize, mem.itemsize)
|
340
|
+
self.assertEqual(t.align, mem.align)
|
341
|
+
|
342
|
+
self.assertEqual(t.shape, ())
|
343
|
+
self.assertEqual(t.strides, ())
|
344
|
+
|
345
|
+
for i in range(10):
|
346
|
+
t = ndt("!%d * %s" % (i, dtype))
|
347
|
+
check_serialize(self, t)
|
348
|
+
shape = (i,)
|
349
|
+
strides = (mem.itemsize,)
|
350
|
+
|
351
|
+
self.assertEqual(t.ndim, 1)
|
352
|
+
if not HAVE_32_BIT_LINUX:
|
353
|
+
self.assertEqual(t.itemsize, mem.itemsize)
|
354
|
+
self.assertEqual(t.align, mem.align)
|
355
|
+
self.assertEqual(t.strides, strides)
|
356
|
+
|
357
|
+
self.assertEqual(t.shape, shape)
|
358
|
+
self.assertEqual(t.datasize, c_datasize(t))
|
359
|
+
self.assertTrue(verify_datasize(t))
|
360
|
+
|
361
|
+
for i in range(10):
|
362
|
+
for j in range(10):
|
363
|
+
t = ndt("!%d * %d * %s" % (i, j, dtype))
|
364
|
+
check_serialize(self, t)
|
365
|
+
shape = (i, j)
|
366
|
+
strides = (mem.itemsize, i * mem.itemsize)
|
367
|
+
|
368
|
+
self.assertEqual(t.ndim, 2)
|
369
|
+
if not HAVE_32_BIT_LINUX:
|
370
|
+
self.assertEqual(t.itemsize, mem.itemsize)
|
371
|
+
self.assertEqual(t.align, mem.align)
|
372
|
+
self.assertEqual(t.strides, strides)
|
373
|
+
|
374
|
+
self.assertEqual(t.shape, shape)
|
375
|
+
self.assertEqual(t.datasize, c_datasize(t))
|
376
|
+
self.assertTrue(verify_datasize(t))
|
377
|
+
|
378
|
+
for i in range(5):
|
379
|
+
for j in range(5):
|
380
|
+
for k in range(5):
|
381
|
+
t = ndt("!%d * %d * %d * %s" % (i, j, k, dtype))
|
382
|
+
check_serialize(self, t)
|
383
|
+
shape = (i, j, k)
|
384
|
+
strides = (mem.itemsize, i * mem.itemsize, i * j * mem.itemsize)
|
385
|
+
|
386
|
+
self.assertEqual(t.ndim, 3)
|
387
|
+
if not HAVE_32_BIT_LINUX:
|
388
|
+
self.assertEqual(t.itemsize, mem.itemsize)
|
389
|
+
self.assertEqual(t.align, mem.align)
|
390
|
+
self.assertEqual(t.strides, strides)
|
391
|
+
|
392
|
+
self.assertEqual(t.shape, shape)
|
393
|
+
self.assertEqual(t.datasize, c_datasize(t))
|
394
|
+
self.assertTrue(verify_datasize(t))
|
395
|
+
|
396
|
+
def test_fortran_conversion(self):
|
397
|
+
# Conversion should be from C-contiguous array.
|
398
|
+
s = "!fixed(shape=2, step=-20) * uint8"
|
399
|
+
self.assertRaises(TypeError, ndt, s)
|
400
|
+
|
401
|
+
|
402
|
+
class TestVarDim(unittest.TestCase):
|
403
|
+
|
404
|
+
def test_var_dim_predicates(self):
|
405
|
+
t = ndt("var(offsets=[0,2]) * var(offsets=[0,3,10]) * complex128")
|
406
|
+
check_serialize(self, t)
|
407
|
+
|
408
|
+
self.assertFalse(t.isabstract())
|
409
|
+
self.assertFalse(t.iscomplex())
|
410
|
+
self.assertTrue(t.isconcrete())
|
411
|
+
self.assertFalse(t.isfloat())
|
412
|
+
self.assertFalse(t.isoptional())
|
413
|
+
self.assertFalse(t.isscalar())
|
414
|
+
self.assertFalse(t.issigned())
|
415
|
+
self.assertFalse(t.isunsigned())
|
416
|
+
|
417
|
+
self.assertFalse(t.is_c_contiguous())
|
418
|
+
self.assertFalse(t.is_f_contiguous())
|
419
|
+
|
420
|
+
def test_var_dim_common_fields(self):
|
421
|
+
dt = "{a: complex64, b: string}"
|
422
|
+
t = ndt("var(offsets=[0,2]) * var(offsets=[0,3,10]) * %s" % dt)
|
423
|
+
check_serialize(self, t)
|
424
|
+
dtype = ndt(dt)
|
425
|
+
|
426
|
+
self.assertEqual(t.ndim, 2)
|
427
|
+
if not HAVE_32_BIT_LINUX:
|
428
|
+
self.assertEqual(t.itemsize, dtype.itemsize)
|
429
|
+
self.assertEqual(t.align, dtype.align)
|
430
|
+
|
431
|
+
self.assertRaises(TypeError, getattr, t, 'shape')
|
432
|
+
self.assertRaises(TypeError, getattr, t, 'strides')
|
433
|
+
|
434
|
+
def test_var_dim_invariants(self):
|
435
|
+
# Mixing var and fixed is disallowed.
|
436
|
+
self.assertRaises(TypeError, ndt, "10 * var * int64")
|
437
|
+
self.assertRaises(TypeError, ndt, "var * 10 * int64")
|
438
|
+
self.assertRaises(TypeError, ndt, "10 * var * 10 * int64")
|
439
|
+
self.assertRaises(TypeError, ndt, "var * 10 * var * int64")
|
440
|
+
self.assertRaises(TypeError, ndt, "N * var * int64")
|
441
|
+
self.assertRaises(TypeError, ndt, "var * N * int64")
|
442
|
+
self.assertRaises(TypeError, ndt, "N * var * N * int64")
|
443
|
+
self.assertRaises(TypeError, ndt, "var * N * var * int64")
|
444
|
+
|
445
|
+
# Too many dimensions.
|
446
|
+
self.assertRaises(TypeError, ndt, "var * " * (MAX_DIM + 1) + "float64")
|
447
|
+
|
448
|
+
# Nested var is disallowed.
|
449
|
+
self.assertRaises(TypeError, ndt, "2 * {a: var * complex128}")
|
450
|
+
self.assertRaises(TypeError, ndt, "var * {a: var * complex128}")
|
451
|
+
self.assertRaises(TypeError, ndt, "var * ref(var * string)")
|
452
|
+
self.assertRaises(TypeError, ndt, "var * SomeConstr(var * string)")
|
453
|
+
|
454
|
+
def test_var_dim_external_offsets(self):
|
455
|
+
# Invalid offsets.
|
456
|
+
self.assertRaises(TypeError, ndt, "int8", [""])
|
457
|
+
self.assertRaises(TypeError, ndt, "int8", [0])
|
458
|
+
self.assertRaises(TypeError, ndt, "int8", [0, 2])
|
459
|
+
self.assertRaises(TypeError, ndt, "int8", {})
|
460
|
+
self.assertRaises(TypeError, ndt, "int8", ())
|
461
|
+
self.assertRaises(TypeError, ndt, "int8", [(), ()])
|
462
|
+
|
463
|
+
self.assertRaises(ValueError, ndt, "int8", [])
|
464
|
+
self.assertRaises(ValueError, ndt, "int8", [[0]])
|
465
|
+
self.assertRaises(ValueError, ndt, "int8", [[0], [0]])
|
466
|
+
|
467
|
+
self.assertRaises(ValueError, ndt, "int8", [[-1, 2]])
|
468
|
+
self.assertRaises(ValueError, ndt, "int8", [[0, 2147483648]])
|
469
|
+
|
470
|
+
# Invalid combinations.
|
471
|
+
self.assertRaises(ValueError, ndt, "int8", [[0, 2], [0, 10]])
|
472
|
+
self.assertRaises(ValueError, ndt, "int8", [[0, 2], [0, 10, 30, 40]])
|
473
|
+
|
474
|
+
# Implicit mixing of var and fixed.
|
475
|
+
self.assertRaises(TypeError, ndt, "10 * int8", [[0, 2], [0, 10, 20]])
|
476
|
+
|
477
|
+
# Abstract dtype.
|
478
|
+
self.assertRaises(ValueError, ndt, "N * int8", [[0, 2], [0, 10, 20]])
|
479
|
+
self.assertRaises(ValueError, ndt, "var * int8", [[0, 2], [0, 10, 20]])
|
480
|
+
|
481
|
+
# Mixing external and internal offsets.
|
482
|
+
self.assertRaises(TypeError, ndt, "var(offsets=[0,2,10]) * int8", [[0, 1], [0, 2]])
|
483
|
+
|
484
|
+
|
485
|
+
class TestSymbolicDim(unittest.TestCase):
|
486
|
+
|
487
|
+
def test_symbolic_dim_predicates(self):
|
488
|
+
t = ndt("N * M * complex128")
|
489
|
+
check_serialize(self, t)
|
490
|
+
|
491
|
+
self.assertTrue(t.isabstract())
|
492
|
+
self.assertFalse(t.iscomplex())
|
493
|
+
self.assertFalse(t.isconcrete())
|
494
|
+
self.assertFalse(t.isfloat())
|
495
|
+
self.assertFalse(t.isoptional())
|
496
|
+
self.assertFalse(t.isscalar())
|
497
|
+
self.assertFalse(t.issigned())
|
498
|
+
self.assertFalse(t.isunsigned())
|
499
|
+
|
500
|
+
self.assertFalse(t.is_c_contiguous())
|
501
|
+
self.assertFalse(t.is_f_contiguous())
|
502
|
+
|
503
|
+
def test_symbolic_dim_common_fields(self):
|
504
|
+
dt = "{a: complex64, b: string}"
|
505
|
+
t = ndt("N * M * %s" % dt)
|
506
|
+
check_serialize(self, t)
|
507
|
+
dtype = ndt(dt)
|
508
|
+
|
509
|
+
self.assertRaises(TypeError, t, 'ndim')
|
510
|
+
self.assertRaises(TypeError, t, 'itemsize')
|
511
|
+
self.assertRaises(TypeError, t, 'align')
|
512
|
+
|
513
|
+
self.assertRaises(TypeError, t, 'shape')
|
514
|
+
self.assertRaises(TypeError, t, 'strides')
|
515
|
+
|
516
|
+
|
517
|
+
class TestEllipsisDim(unittest.TestCase):
|
518
|
+
|
519
|
+
def test_ellipsis_dim_predicates(self):
|
520
|
+
t = ndt("... * 2 * complex128")
|
521
|
+
check_serialize(self, t)
|
522
|
+
|
523
|
+
self.assertTrue(t.isabstract())
|
524
|
+
self.assertFalse(t.iscomplex())
|
525
|
+
self.assertFalse(t.isconcrete())
|
526
|
+
self.assertFalse(t.isfloat())
|
527
|
+
self.assertFalse(t.isoptional())
|
528
|
+
self.assertFalse(t.isscalar())
|
529
|
+
self.assertFalse(t.issigned())
|
530
|
+
self.assertFalse(t.isunsigned())
|
531
|
+
|
532
|
+
self.assertFalse(t.is_c_contiguous())
|
533
|
+
self.assertFalse(t.is_f_contiguous())
|
534
|
+
|
535
|
+
def test_ellipsis_dim_common_fields(self):
|
536
|
+
dt = "{a: complex64, b: string}"
|
537
|
+
t = ndt("... * 2 * %s" % dt)
|
538
|
+
check_serialize(self, t)
|
539
|
+
dtype = ndt(dt)
|
540
|
+
|
541
|
+
self.assertRaises(TypeError, t, 'ndim')
|
542
|
+
self.assertRaises(TypeError, t, 'itemsize')
|
543
|
+
self.assertRaises(TypeError, t, 'align')
|
544
|
+
|
545
|
+
self.assertRaises(TypeError, t, 'shape')
|
546
|
+
self.assertRaises(TypeError, t, 'strides')
|
547
|
+
|
548
|
+
|
549
|
+
class TestTuple(unittest.TestCase):
|
550
|
+
|
551
|
+
def test_tuple_predicates(self):
|
552
|
+
for s in ["()", "(int64)", "(string, bytes, pack=1)"]:
|
553
|
+
t = ndt(s)
|
554
|
+
check_serialize(self, t)
|
555
|
+
|
556
|
+
self.assertFalse(t.isabstract())
|
557
|
+
self.assertFalse(t.iscomplex())
|
558
|
+
self.assertTrue(t.isconcrete())
|
559
|
+
self.assertFalse(t.isfloat())
|
560
|
+
self.assertFalse(t.isoptional())
|
561
|
+
self.assertFalse(t.isscalar())
|
562
|
+
self.assertFalse(t.issigned())
|
563
|
+
self.assertFalse(t.isunsigned())
|
564
|
+
|
565
|
+
self.assertTrue(t.is_c_contiguous())
|
566
|
+
self.assertTrue(t.is_f_contiguous())
|
567
|
+
|
568
|
+
for s in ["(Any)", "(int64, N * M * uint8)", "(string, Float)"]:
|
569
|
+
t = ndt(s)
|
570
|
+
check_serialize(self, t)
|
571
|
+
|
572
|
+
self.assertTrue(t.isabstract())
|
573
|
+
self.assertFalse(t.iscomplex())
|
574
|
+
self.assertFalse(t.isconcrete())
|
575
|
+
self.assertFalse(t.isfloat())
|
576
|
+
self.assertFalse(t.isoptional())
|
577
|
+
self.assertFalse(t.isscalar())
|
578
|
+
self.assertFalse(t.issigned())
|
579
|
+
self.assertFalse(t.isunsigned())
|
580
|
+
|
581
|
+
self.assertFalse(t.is_c_contiguous())
|
582
|
+
self.assertFalse(t.is_f_contiguous())
|
583
|
+
|
584
|
+
def test_tuple_common_fields(self):
|
585
|
+
f = "{a: complex64, b: string}"
|
586
|
+
t = ndt("(%s, %s)" % (f, f))
|
587
|
+
check_serialize(self, t)
|
588
|
+
field = ndt(f)
|
589
|
+
|
590
|
+
self.assertEqual(t.ndim, 0)
|
591
|
+
if not HAVE_32_BIT_LINUX:
|
592
|
+
self.assertEqual(t.itemsize, 2 * field.itemsize)
|
593
|
+
self.assertEqual(t.align, field.align)
|
594
|
+
|
595
|
+
self.assertRaises(TypeError, t, 'shape')
|
596
|
+
self.assertRaises(TypeError, t, 'strides')
|
597
|
+
|
598
|
+
|
599
|
+
class TestRecord(unittest.TestCase):
|
600
|
+
|
601
|
+
def test_record_predicates(self):
|
602
|
+
for s in ["{}", "{a: int64, b: bytes}", "{x: string, y: uint8, pack=1}"]:
|
603
|
+
t = ndt(s)
|
604
|
+
check_serialize(self, t)
|
605
|
+
|
606
|
+
self.assertFalse(t.isabstract())
|
607
|
+
self.assertFalse(t.iscomplex())
|
608
|
+
self.assertTrue(t.isconcrete())
|
609
|
+
self.assertFalse(t.isfloat())
|
610
|
+
self.assertFalse(t.isoptional())
|
611
|
+
self.assertFalse(t.isscalar())
|
612
|
+
self.assertFalse(t.issigned())
|
613
|
+
self.assertFalse(t.isunsigned())
|
614
|
+
|
615
|
+
self.assertTrue(t.is_c_contiguous())
|
616
|
+
self.assertTrue(t.is_f_contiguous())
|
617
|
+
|
618
|
+
for s in ["{a: Any, b: Complex}", "{x: N * M * T}"]:
|
619
|
+
t = ndt(s)
|
620
|
+
check_serialize(self, t)
|
621
|
+
|
622
|
+
self.assertTrue(t.isabstract())
|
623
|
+
self.assertFalse(t.iscomplex())
|
624
|
+
self.assertFalse(t.isconcrete())
|
625
|
+
self.assertFalse(t.isfloat())
|
626
|
+
self.assertFalse(t.isoptional())
|
627
|
+
self.assertFalse(t.isscalar())
|
628
|
+
self.assertFalse(t.issigned())
|
629
|
+
self.assertFalse(t.isunsigned())
|
630
|
+
|
631
|
+
self.assertFalse(t.is_c_contiguous())
|
632
|
+
self.assertFalse(t.is_f_contiguous())
|
633
|
+
|
634
|
+
def test_record_common_fields(self):
|
635
|
+
f = "{a: complex64, b: string}"
|
636
|
+
t = ndt("{x: %s, y: %s, z: %s}" % (f, f, f))
|
637
|
+
check_serialize(self, t)
|
638
|
+
field = ndt(f)
|
639
|
+
|
640
|
+
self.assertEqual(t.ndim, 0)
|
641
|
+
if not HAVE_32_BIT_LINUX:
|
642
|
+
self.assertEqual(t.itemsize, 3 * field.itemsize)
|
643
|
+
self.assertEqual(t.align, field.align)
|
644
|
+
|
645
|
+
self.assertRaises(TypeError, t, 'shape')
|
646
|
+
self.assertRaises(TypeError, t, 'strides')
|
647
|
+
|
648
|
+
|
649
|
+
class TestRef(unittest.TestCase):
|
650
|
+
|
651
|
+
def test_ref_predicates(self):
|
652
|
+
for s in ["&2 * 3 * float64", "&bool", "&(uint8, int32)"]:
|
653
|
+
t = ndt(s)
|
654
|
+
check_serialize(self, t)
|
655
|
+
|
656
|
+
self.assertFalse(t.isabstract())
|
657
|
+
self.assertFalse(t.iscomplex())
|
658
|
+
self.assertTrue(t.isconcrete())
|
659
|
+
self.assertFalse(t.isfloat())
|
660
|
+
self.assertFalse(t.isoptional())
|
661
|
+
self.assertFalse(t.isscalar())
|
662
|
+
self.assertFalse(t.issigned())
|
663
|
+
self.assertFalse(t.isunsigned())
|
664
|
+
|
665
|
+
self.assertTrue(t.is_c_contiguous())
|
666
|
+
self.assertTrue(t.is_f_contiguous())
|
667
|
+
|
668
|
+
for s in ["&Any", "&(int64, N * M * uint8)"]:
|
669
|
+
t = ndt(s)
|
670
|
+
check_serialize(self, t)
|
671
|
+
|
672
|
+
self.assertTrue(t.isabstract())
|
673
|
+
self.assertFalse(t.iscomplex())
|
674
|
+
self.assertFalse(t.isconcrete())
|
675
|
+
self.assertFalse(t.isfloat())
|
676
|
+
self.assertFalse(t.isoptional())
|
677
|
+
self.assertFalse(t.isscalar())
|
678
|
+
self.assertFalse(t.issigned())
|
679
|
+
self.assertFalse(t.isunsigned())
|
680
|
+
|
681
|
+
self.assertFalse(t.is_c_contiguous())
|
682
|
+
self.assertFalse(t.is_f_contiguous())
|
683
|
+
|
684
|
+
def test_ref_common_fields(self):
|
685
|
+
a = "{a: complex64, b: string}"
|
686
|
+
t = ndt("ref(%s)" % a)
|
687
|
+
check_serialize(self, t)
|
688
|
+
arg = ndt(a)
|
689
|
+
|
690
|
+
self.assertEqual(t.ndim, 0)
|
691
|
+
self.assertEqual(t.itemsize, SIZEOF_PTR)
|
692
|
+
self.assertEqual(t.align, SIZEOF_PTR)
|
693
|
+
|
694
|
+
self.assertRaises(TypeError, t, 'shape')
|
695
|
+
self.assertRaises(TypeError, t, 'strides')
|
696
|
+
|
697
|
+
|
698
|
+
class TestConstr(unittest.TestCase):
|
699
|
+
|
700
|
+
def test_constr_predicates(self):
|
701
|
+
for s in ["Some(int16)", "Maybe(int64)", "Just((string, bytes))"]:
|
702
|
+
t = ndt(s)
|
703
|
+
check_serialize(self, t)
|
704
|
+
|
705
|
+
self.assertFalse(t.isabstract())
|
706
|
+
self.assertFalse(t.iscomplex())
|
707
|
+
self.assertTrue(t.isconcrete())
|
708
|
+
self.assertFalse(t.isfloat())
|
709
|
+
self.assertFalse(t.isoptional())
|
710
|
+
self.assertFalse(t.isscalar())
|
711
|
+
self.assertFalse(t.issigned())
|
712
|
+
self.assertFalse(t.isunsigned())
|
713
|
+
|
714
|
+
self.assertTrue(t.is_c_contiguous())
|
715
|
+
self.assertTrue(t.is_f_contiguous())
|
716
|
+
|
717
|
+
def test_constr_common_fields(self):
|
718
|
+
a = "{a: complex64, b: string}"
|
719
|
+
t = ndt("Just(%s)" % a)
|
720
|
+
check_serialize(self, t)
|
721
|
+
arg = ndt(a)
|
722
|
+
|
723
|
+
self.assertEqual(t.ndim, 0)
|
724
|
+
if not HAVE_32_BIT_LINUX:
|
725
|
+
self.assertEqual(t.itemsize, arg.itemsize)
|
726
|
+
self.assertEqual(t.align, arg.align)
|
727
|
+
|
728
|
+
self.assertRaises(TypeError, t, 'shape')
|
729
|
+
self.assertRaises(TypeError, t, 'strides')
|
730
|
+
|
731
|
+
|
732
|
+
class TestNominal(unittest.TestCase):
|
733
|
+
|
734
|
+
# The regrtest.py -R option causes setUpClass() to be called several times.
|
735
|
+
initialized = False
|
736
|
+
|
737
|
+
@classmethod
|
738
|
+
def setUpClass(cls):
|
739
|
+
if not cls.initialized:
|
740
|
+
typedef("some_t", "2 * 10 * complex128")
|
741
|
+
cls.initialized = True
|
742
|
+
|
743
|
+
def test_nominal_predicates(self):
|
744
|
+
t = ndt("some_t")
|
745
|
+
check_serialize(self, t)
|
746
|
+
|
747
|
+
# The nominal type is opaque. The only thing known is that
|
748
|
+
# the typedef is concrete.
|
749
|
+
self.assertFalse(t.isabstract())
|
750
|
+
self.assertFalse(t.iscomplex())
|
751
|
+
self.assertTrue(t.isconcrete())
|
752
|
+
self.assertFalse(t.isfloat())
|
753
|
+
self.assertFalse(t.isoptional())
|
754
|
+
self.assertFalse(t.isscalar())
|
755
|
+
self.assertFalse(t.issigned())
|
756
|
+
self.assertFalse(t.isunsigned())
|
757
|
+
|
758
|
+
self.assertTrue(t.is_c_contiguous())
|
759
|
+
self.assertTrue(t.is_f_contiguous())
|
760
|
+
|
761
|
+
def test_nominal_common_fields(self):
|
762
|
+
t = ndt("some_t")
|
763
|
+
dtype = ndt("complex128")
|
764
|
+
|
765
|
+
# The opaque type is treated as a dtype with ndim==0, same as
|
766
|
+
# for constructor types.
|
767
|
+
self.assertEqual(t.ndim, 0)
|
768
|
+
if not HAVE_32_BIT_LINUX:
|
769
|
+
self.assertEqual(t.itemsize, 2 * 10 * dtype.itemsize)
|
770
|
+
self.assertEqual(t.align, dtype.align)
|
771
|
+
|
772
|
+
self.assertRaises(TypeError, t, 'shape')
|
773
|
+
self.assertRaises(TypeError, t, 'strides')
|
774
|
+
|
775
|
+
def test_nominal_exceptions(self):
|
776
|
+
# not in the typedef table
|
777
|
+
self.assertRaises(ValueError, ndt, "undefined_t")
|
778
|
+
|
779
|
+
# duplicate typedef
|
780
|
+
self.assertRaises(ValueError, typedef, "some_t", "int64")
|
781
|
+
|
782
|
+
|
783
|
+
class TestScalarKind(unittest.TestCase):
|
784
|
+
|
785
|
+
def test_scalar_kind_predicates(self):
|
786
|
+
t = ndt("Scalar")
|
787
|
+
check_serialize(self, t)
|
788
|
+
|
789
|
+
self.assertTrue(t.isabstract())
|
790
|
+
self.assertFalse(t.iscomplex())
|
791
|
+
self.assertFalse(t.isconcrete())
|
792
|
+
self.assertFalse(t.isfloat())
|
793
|
+
self.assertFalse(t.isoptional())
|
794
|
+
self.assertFalse(t.isscalar())
|
795
|
+
self.assertFalse(t.issigned())
|
796
|
+
self.assertFalse(t.isunsigned())
|
797
|
+
|
798
|
+
self.assertFalse(t.is_c_contiguous())
|
799
|
+
self.assertFalse(t.is_f_contiguous())
|
800
|
+
|
801
|
+
def test_scalar_kind_common_fields(self):
|
802
|
+
t = ndt("Scalar")
|
803
|
+
check_serialize(self, t)
|
804
|
+
|
805
|
+
self.assertRaises(TypeError, t, 'ndim')
|
806
|
+
self.assertRaises(TypeError, t, 'itemsize')
|
807
|
+
self.assertRaises(TypeError, t, 'align')
|
808
|
+
|
809
|
+
self.assertRaises(TypeError, t, 'shape')
|
810
|
+
self.assertRaises(TypeError, t, 'strides')
|
811
|
+
|
812
|
+
|
813
|
+
class TestCategorical(unittest.TestCase):
|
814
|
+
|
815
|
+
def test_categorical_predicates(self):
|
816
|
+
for s in [
|
817
|
+
"categorical(NA, 1, 100, -29999)",
|
818
|
+
"categorical(NA, 1.2, -200.25)",
|
819
|
+
"categorical('foo', 'bar')",
|
820
|
+
"categorical('foo', NA, 100)"]:
|
821
|
+
t = ndt(s)
|
822
|
+
check_serialize(self, t)
|
823
|
+
|
824
|
+
self.assertFalse(t.isabstract())
|
825
|
+
self.assertFalse(t.iscomplex())
|
826
|
+
self.assertTrue(t.isconcrete())
|
827
|
+
self.assertFalse(t.isfloat())
|
828
|
+
self.assertFalse(t.isoptional())
|
829
|
+
self.assertFalse(t.isscalar())
|
830
|
+
self.assertFalse(t.issigned())
|
831
|
+
self.assertFalse(t.isunsigned())
|
832
|
+
|
833
|
+
self.assertTrue(t.is_c_contiguous())
|
834
|
+
self.assertTrue(t.is_f_contiguous())
|
835
|
+
|
836
|
+
def test_categorical_common_fields(self):
|
837
|
+
t = ndt("categorical(NA, 'something', 'must', 'be', 'done')")
|
838
|
+
check_serialize(self, t)
|
839
|
+
|
840
|
+
self.assertEqual(t.ndim, 0)
|
841
|
+
if not HAVE_32_BIT_LINUX:
|
842
|
+
self.assertEqual(t.itemsize, 8)
|
843
|
+
self.assertEqual(t.align, 8)
|
844
|
+
|
845
|
+
self.assertRaises(TypeError, t, 'shape')
|
846
|
+
self.assertRaises(TypeError, t, 'strides')
|
847
|
+
|
848
|
+
|
849
|
+
class TestFixedStringKind(unittest.TestCase):
|
850
|
+
|
851
|
+
def test_fixed_string_kind_predicates(self):
|
852
|
+
t = ndt("FixedString")
|
853
|
+
check_serialize(self, t)
|
854
|
+
|
855
|
+
self.assertTrue(t.isabstract())
|
856
|
+
self.assertFalse(t.iscomplex())
|
857
|
+
self.assertFalse(t.isconcrete())
|
858
|
+
self.assertFalse(t.isfloat())
|
859
|
+
self.assertFalse(t.isoptional())
|
860
|
+
self.assertFalse(t.isscalar())
|
861
|
+
self.assertFalse(t.issigned())
|
862
|
+
self.assertFalse(t.isunsigned())
|
863
|
+
|
864
|
+
self.assertFalse(t.is_c_contiguous())
|
865
|
+
self.assertFalse(t.is_f_contiguous())
|
866
|
+
|
867
|
+
def test_fixed_string_kind_common_fields(self):
|
868
|
+
t = ndt("FixedString")
|
869
|
+
|
870
|
+
self.assertRaises(TypeError, t, 'ndim')
|
871
|
+
self.assertRaises(TypeError, t, 'itemsize')
|
872
|
+
self.assertRaises(TypeError, t, 'align')
|
873
|
+
|
874
|
+
self.assertRaises(TypeError, t, 'shape')
|
875
|
+
self.assertRaises(TypeError, t, 'strides')
|
876
|
+
|
877
|
+
|
878
|
+
class TestFixedString(unittest.TestCase):
|
879
|
+
|
880
|
+
def test_fixed_string_predicates(self):
|
881
|
+
t = ndt("fixed_string(380, 'utf16')")
|
882
|
+
check_serialize(self, t)
|
883
|
+
|
884
|
+
self.assertFalse(t.isabstract())
|
885
|
+
self.assertFalse(t.iscomplex())
|
886
|
+
self.assertTrue(t.isconcrete())
|
887
|
+
self.assertFalse(t.isfloat())
|
888
|
+
self.assertFalse(t.isoptional())
|
889
|
+
self.assertTrue(t.isscalar())
|
890
|
+
self.assertFalse(t.issigned())
|
891
|
+
self.assertFalse(t.isunsigned())
|
892
|
+
|
893
|
+
self.assertTrue(t.is_c_contiguous())
|
894
|
+
self.assertTrue(t.is_f_contiguous())
|
895
|
+
|
896
|
+
def test_fixed_string_common_fields(self):
|
897
|
+
for encoding, codepoint_size in [
|
898
|
+
('ascii', 1),
|
899
|
+
('utf8', 1),
|
900
|
+
('utf16', 2),
|
901
|
+
('utf32', 4)]:
|
902
|
+
|
903
|
+
t = ndt("fixed_string(20, '%s')" % encoding)
|
904
|
+
check_serialize(self, t)
|
905
|
+
|
906
|
+
self.assertEqual(t.ndim, 0)
|
907
|
+
if not HAVE_32_BIT_LINUX:
|
908
|
+
self.assertEqual(t.itemsize, 20 * codepoint_size)
|
909
|
+
self.assertEqual(t.align, codepoint_size)
|
910
|
+
|
911
|
+
self.assertRaises(TypeError, t, 'shape')
|
912
|
+
self.assertRaises(TypeError, t, 'strides')
|
913
|
+
|
914
|
+
|
915
|
+
class TestFixedBytesKind(unittest.TestCase):
|
916
|
+
|
917
|
+
def test_fixed_bytes_kind_predicates(self):
|
918
|
+
t = ndt("FixedBytes")
|
919
|
+
check_serialize(self, t)
|
920
|
+
|
921
|
+
self.assertTrue(t.isabstract())
|
922
|
+
self.assertFalse(t.iscomplex())
|
923
|
+
self.assertFalse(t.isconcrete())
|
924
|
+
self.assertFalse(t.isfloat())
|
925
|
+
self.assertFalse(t.isoptional())
|
926
|
+
self.assertFalse(t.isscalar())
|
927
|
+
self.assertFalse(t.issigned())
|
928
|
+
self.assertFalse(t.isunsigned())
|
929
|
+
|
930
|
+
self.assertFalse(t.is_c_contiguous())
|
931
|
+
self.assertFalse(t.is_f_contiguous())
|
932
|
+
|
933
|
+
def test_fixed_string_kind_common_fields(self):
|
934
|
+
t = ndt("FixedBytes")
|
935
|
+
|
936
|
+
self.assertRaises(TypeError, t, 'ndim')
|
937
|
+
self.assertRaises(TypeError, t, 'itemsize')
|
938
|
+
self.assertRaises(TypeError, t, 'align')
|
939
|
+
|
940
|
+
self.assertRaises(TypeError, t, 'shape')
|
941
|
+
self.assertRaises(TypeError, t, 'strides')
|
942
|
+
|
943
|
+
|
944
|
+
class TestFixedBytes(unittest.TestCase):
|
945
|
+
|
946
|
+
def test_fixed_bytes_predicates(self):
|
947
|
+
t = ndt("fixed_bytes(size=1020)")
|
948
|
+
check_serialize(self, t)
|
949
|
+
|
950
|
+
self.assertFalse(t.isabstract())
|
951
|
+
self.assertFalse(t.iscomplex())
|
952
|
+
self.assertTrue(t.isconcrete())
|
953
|
+
self.assertFalse(t.isfloat())
|
954
|
+
self.assertFalse(t.isoptional())
|
955
|
+
self.assertTrue(t.isscalar())
|
956
|
+
self.assertFalse(t.issigned())
|
957
|
+
self.assertFalse(t.isunsigned())
|
958
|
+
|
959
|
+
self.assertTrue(t.is_c_contiguous())
|
960
|
+
self.assertTrue(t.is_f_contiguous())
|
961
|
+
|
962
|
+
def test_fixed_bytes_common_fields(self):
|
963
|
+
for align in [1,2,4,8,16,32,64]:
|
964
|
+
|
965
|
+
t = ndt("fixed_bytes(size=1024, align=%d)" % align)
|
966
|
+
check_serialize(self, t)
|
967
|
+
|
968
|
+
self.assertEqual(t.ndim, 0)
|
969
|
+
if not HAVE_32_BIT_LINUX:
|
970
|
+
self.assertEqual(t.itemsize, 1024)
|
971
|
+
self.assertEqual(t.align, align)
|
972
|
+
|
973
|
+
self.assertRaises(TypeError, t, 'shape')
|
974
|
+
self.assertRaises(TypeError, t, 'strides')
|
975
|
+
|
976
|
+
def test_fixed_bytes_exceptions(self):
|
977
|
+
# Data size must be a multiple of align.
|
978
|
+
self.assertRaises(ValueError, ndt, "fixed_bytes(size=20, align=8)")
|
979
|
+
|
980
|
+
|
981
|
+
class TestString(unittest.TestCase):
|
982
|
+
|
983
|
+
def test_string_predicates(self):
|
984
|
+
t = ndt("string")
|
985
|
+
check_serialize(self, t)
|
986
|
+
|
987
|
+
self.assertFalse(t.isabstract())
|
988
|
+
self.assertFalse(t.iscomplex())
|
989
|
+
self.assertTrue(t.isconcrete())
|
990
|
+
self.assertFalse(t.isfloat())
|
991
|
+
self.assertFalse(t.isoptional())
|
992
|
+
self.assertTrue(t.isscalar())
|
993
|
+
self.assertFalse(t.issigned())
|
994
|
+
self.assertFalse(t.isunsigned())
|
995
|
+
|
996
|
+
self.assertTrue(t.is_c_contiguous())
|
997
|
+
self.assertTrue(t.is_f_contiguous())
|
998
|
+
|
999
|
+
def test_string_common_fields(self):
|
1000
|
+
t = ndt("string")
|
1001
|
+
|
1002
|
+
self.assertEqual(t.ndim, 0)
|
1003
|
+
self.assertEqual(t.itemsize, SIZEOF_PTR)
|
1004
|
+
self.assertEqual(t.align, SIZEOF_PTR)
|
1005
|
+
|
1006
|
+
self.assertRaises(TypeError, t, 'shape')
|
1007
|
+
self.assertRaises(TypeError, t, 'strides')
|
1008
|
+
|
1009
|
+
|
1010
|
+
class TestBytes(unittest.TestCase):
|
1011
|
+
|
1012
|
+
def test_bytes_predicates(self):
|
1013
|
+
t = ndt("bytes")
|
1014
|
+
check_serialize(self, t)
|
1015
|
+
|
1016
|
+
self.assertFalse(t.isabstract())
|
1017
|
+
self.assertFalse(t.iscomplex())
|
1018
|
+
self.assertTrue(t.isconcrete())
|
1019
|
+
self.assertFalse(t.isfloat())
|
1020
|
+
self.assertFalse(t.isoptional())
|
1021
|
+
self.assertTrue(t.isscalar())
|
1022
|
+
self.assertFalse(t.issigned())
|
1023
|
+
self.assertFalse(t.isunsigned())
|
1024
|
+
|
1025
|
+
self.assertTrue(t.is_c_contiguous())
|
1026
|
+
self.assertTrue(t.is_f_contiguous())
|
1027
|
+
|
1028
|
+
def test_bytes_common_fields(self):
|
1029
|
+
t = ndt("bytes")
|
1030
|
+
|
1031
|
+
self.assertEqual(t.ndim, 0)
|
1032
|
+
if not HAVE_32_BIT_LINUX:
|
1033
|
+
self.assertEqual(t.itemsize, 16)
|
1034
|
+
self.assertEqual(t.align, 8)
|
1035
|
+
|
1036
|
+
self.assertRaises(TypeError, t, 'shape')
|
1037
|
+
self.assertRaises(TypeError, t, 'strides')
|
1038
|
+
|
1039
|
+
|
1040
|
+
class TestChar(unittest.TestCase):
|
1041
|
+
|
1042
|
+
def test_char_predicates(self):
|
1043
|
+
t = ndt("char")
|
1044
|
+
check_serialize(self, t)
|
1045
|
+
|
1046
|
+
self.assertFalse(t.isabstract())
|
1047
|
+
self.assertFalse(t.iscomplex())
|
1048
|
+
self.assertTrue(t.isconcrete())
|
1049
|
+
self.assertFalse(t.isfloat())
|
1050
|
+
self.assertFalse(t.isoptional())
|
1051
|
+
self.assertTrue(t.isscalar())
|
1052
|
+
self.assertFalse(t.issigned())
|
1053
|
+
self.assertFalse(t.isunsigned())
|
1054
|
+
|
1055
|
+
self.assertTrue(t.is_c_contiguous())
|
1056
|
+
self.assertTrue(t.is_f_contiguous())
|
1057
|
+
|
1058
|
+
def test_char_common_fields(self):
|
1059
|
+
t = ndt("char('utf32')")
|
1060
|
+
check_serialize(self, t)
|
1061
|
+
|
1062
|
+
self.assertEqual(t.ndim, 0)
|
1063
|
+
self.assertEqual(t.itemsize, 4)
|
1064
|
+
self.assertEqual(t.align, 4)
|
1065
|
+
|
1066
|
+
self.assertRaises(TypeError, t, 'shape')
|
1067
|
+
self.assertRaises(TypeError, t, 'strides')
|
1068
|
+
|
1069
|
+
|
1070
|
+
class TestBool(unittest.TestCase):
|
1071
|
+
|
1072
|
+
def test_bool_predicates(self):
|
1073
|
+
t = ndt("bool")
|
1074
|
+
check_serialize(self, t)
|
1075
|
+
|
1076
|
+
self.assertFalse(t.isabstract())
|
1077
|
+
self.assertFalse(t.iscomplex())
|
1078
|
+
self.assertTrue(t.isconcrete())
|
1079
|
+
self.assertFalse(t.isfloat())
|
1080
|
+
self.assertFalse(t.isoptional())
|
1081
|
+
self.assertTrue(t.isscalar())
|
1082
|
+
self.assertFalse(t.issigned())
|
1083
|
+
self.assertFalse(t.isunsigned())
|
1084
|
+
|
1085
|
+
self.assertTrue(t.is_c_contiguous())
|
1086
|
+
self.assertTrue(t.is_f_contiguous())
|
1087
|
+
|
1088
|
+
def test_bool_common_fields(self):
|
1089
|
+
t = ndt("bool")
|
1090
|
+
|
1091
|
+
self.assertEqual(t.ndim, 0)
|
1092
|
+
self.assertEqual(t.itemsize, 1)
|
1093
|
+
self.assertEqual(t.align, 1)
|
1094
|
+
|
1095
|
+
self.assertRaises(TypeError, t, 'shape')
|
1096
|
+
self.assertRaises(TypeError, t, 'strides')
|
1097
|
+
|
1098
|
+
|
1099
|
+
class TestSignedKind(unittest.TestCase):
|
1100
|
+
|
1101
|
+
def test_signed_kind_predicates(self):
|
1102
|
+
t = ndt("Signed")
|
1103
|
+
check_serialize(self, t)
|
1104
|
+
|
1105
|
+
self.assertTrue(t.isabstract())
|
1106
|
+
self.assertFalse(t.iscomplex())
|
1107
|
+
self.assertFalse(t.isconcrete())
|
1108
|
+
self.assertFalse(t.isfloat())
|
1109
|
+
self.assertFalse(t.isoptional())
|
1110
|
+
self.assertFalse(t.isscalar())
|
1111
|
+
self.assertFalse(t.issigned())
|
1112
|
+
self.assertFalse(t.isunsigned())
|
1113
|
+
|
1114
|
+
self.assertFalse(t.is_c_contiguous())
|
1115
|
+
self.assertFalse(t.is_f_contiguous())
|
1116
|
+
|
1117
|
+
def test_signed_kind_common_fields(self):
|
1118
|
+
t = ndt("Signed")
|
1119
|
+
|
1120
|
+
self.assertRaises(TypeError, t, 'ndim')
|
1121
|
+
self.assertRaises(TypeError, t, 'itemsize')
|
1122
|
+
self.assertRaises(TypeError, t, 'align')
|
1123
|
+
|
1124
|
+
self.assertRaises(TypeError, t, 'shape')
|
1125
|
+
self.assertRaises(TypeError, t, 'strides')
|
1126
|
+
|
1127
|
+
|
1128
|
+
class TestSigned(unittest.TestCase):
|
1129
|
+
|
1130
|
+
def test_signed_predicates(self):
|
1131
|
+
signed = ['int8', 'int16', 'int32', 'int64']
|
1132
|
+
|
1133
|
+
for s in signed:
|
1134
|
+
t = ndt(s)
|
1135
|
+
check_serialize(self, t)
|
1136
|
+
|
1137
|
+
self.assertFalse(t.isabstract())
|
1138
|
+
self.assertFalse(t.iscomplex())
|
1139
|
+
self.assertTrue(t.isconcrete())
|
1140
|
+
self.assertFalse(t.isfloat())
|
1141
|
+
self.assertFalse(t.isoptional())
|
1142
|
+
self.assertTrue(t.isscalar())
|
1143
|
+
self.assertTrue(t.issigned())
|
1144
|
+
self.assertFalse(t.isunsigned())
|
1145
|
+
|
1146
|
+
self.assertTrue(t.is_c_contiguous())
|
1147
|
+
self.assertTrue(t.is_f_contiguous())
|
1148
|
+
|
1149
|
+
def test_signed_common_fields(self):
|
1150
|
+
for s, itemsize in [
|
1151
|
+
('int8', 1),
|
1152
|
+
('int16', 2),
|
1153
|
+
('int32', 4),
|
1154
|
+
('int64', 8)]:
|
1155
|
+
|
1156
|
+
t = ndt(s)
|
1157
|
+
|
1158
|
+
self.assertEqual(t.ndim, 0)
|
1159
|
+
if not HAVE_32_BIT_LINUX:
|
1160
|
+
self.assertEqual(t.itemsize, itemsize)
|
1161
|
+
self.assertEqual(t.align, itemsize)
|
1162
|
+
|
1163
|
+
self.assertRaises(TypeError, t, 'shape')
|
1164
|
+
self.assertRaises(TypeError, t, 'shape')
|
1165
|
+
|
1166
|
+
|
1167
|
+
class TestUnsignedKind(unittest.TestCase):
|
1168
|
+
|
1169
|
+
def test_unsigned_kind_predicates(self):
|
1170
|
+
t = ndt("Unsigned")
|
1171
|
+
check_serialize(self, t)
|
1172
|
+
|
1173
|
+
self.assertTrue(t.isabstract())
|
1174
|
+
self.assertFalse(t.iscomplex())
|
1175
|
+
self.assertFalse(t.isconcrete())
|
1176
|
+
self.assertFalse(t.isfloat())
|
1177
|
+
self.assertFalse(t.isoptional())
|
1178
|
+
self.assertFalse(t.isscalar())
|
1179
|
+
self.assertFalse(t.issigned())
|
1180
|
+
self.assertFalse(t.isunsigned())
|
1181
|
+
|
1182
|
+
self.assertFalse(t.is_c_contiguous())
|
1183
|
+
self.assertFalse(t.is_f_contiguous())
|
1184
|
+
|
1185
|
+
def test_unsigned_kind_common_fields(self):
|
1186
|
+
t = ndt("Unsigned")
|
1187
|
+
|
1188
|
+
self.assertRaises(TypeError, t, 'ndim')
|
1189
|
+
self.assertRaises(TypeError, t, 'itemsize')
|
1190
|
+
self.assertRaises(TypeError, t, 'align')
|
1191
|
+
|
1192
|
+
self.assertRaises(TypeError, t, 'shape')
|
1193
|
+
self.assertRaises(TypeError, t, 'strides')
|
1194
|
+
|
1195
|
+
|
1196
|
+
class TestUnsigned(unittest.TestCase):
|
1197
|
+
|
1198
|
+
def test_unsigned_predicates(self):
|
1199
|
+
unsigned = ['uint8', 'uint16', 'uint32', 'uint64']
|
1200
|
+
|
1201
|
+
for s in unsigned:
|
1202
|
+
t = ndt(s)
|
1203
|
+
check_serialize(self, t)
|
1204
|
+
|
1205
|
+
self.assertFalse(t.isabstract())
|
1206
|
+
self.assertFalse(t.iscomplex())
|
1207
|
+
self.assertTrue(t.isconcrete())
|
1208
|
+
self.assertFalse(t.isfloat())
|
1209
|
+
self.assertFalse(t.isoptional())
|
1210
|
+
self.assertTrue(t.isscalar())
|
1211
|
+
self.assertFalse(t.issigned())
|
1212
|
+
self.assertTrue(t.isunsigned())
|
1213
|
+
|
1214
|
+
self.assertTrue(t.is_c_contiguous())
|
1215
|
+
self.assertTrue(t.is_f_contiguous())
|
1216
|
+
|
1217
|
+
def test_unsigned_common_fields(self):
|
1218
|
+
for s, itemsize in [
|
1219
|
+
('uint8', 1),
|
1220
|
+
('uint16', 2),
|
1221
|
+
('uint32', 4),
|
1222
|
+
('uint64', 8)]:
|
1223
|
+
|
1224
|
+
t = ndt(s)
|
1225
|
+
|
1226
|
+
self.assertEqual(t.ndim, 0)
|
1227
|
+
if not HAVE_32_BIT_LINUX:
|
1228
|
+
self.assertEqual(t.itemsize, itemsize)
|
1229
|
+
self.assertEqual(t.align, itemsize)
|
1230
|
+
|
1231
|
+
self.assertRaises(TypeError, t, 'shape')
|
1232
|
+
self.assertRaises(TypeError, t, 'shape')
|
1233
|
+
|
1234
|
+
|
1235
|
+
class TestFloatKind(unittest.TestCase):
|
1236
|
+
|
1237
|
+
def test_float_kind_predicates(self):
|
1238
|
+
t = ndt("Float")
|
1239
|
+
check_serialize(self, t)
|
1240
|
+
|
1241
|
+
self.assertTrue(t.isabstract())
|
1242
|
+
self.assertFalse(t.iscomplex())
|
1243
|
+
self.assertFalse(t.isconcrete())
|
1244
|
+
self.assertFalse(t.isfloat())
|
1245
|
+
self.assertFalse(t.isoptional())
|
1246
|
+
self.assertFalse(t.isscalar())
|
1247
|
+
self.assertFalse(t.issigned())
|
1248
|
+
self.assertFalse(t.isunsigned())
|
1249
|
+
|
1250
|
+
self.assertFalse(t.is_c_contiguous())
|
1251
|
+
self.assertFalse(t.is_f_contiguous())
|
1252
|
+
|
1253
|
+
def test_float_kind_common_fields(self):
|
1254
|
+
t = ndt("Float")
|
1255
|
+
check_serialize(self, t)
|
1256
|
+
|
1257
|
+
self.assertRaises(TypeError, t, 'ndim')
|
1258
|
+
self.assertRaises(TypeError, t, 'itemsize')
|
1259
|
+
self.assertRaises(TypeError, t, 'align')
|
1260
|
+
|
1261
|
+
self.assertRaises(TypeError, t, 'shape')
|
1262
|
+
self.assertRaises(TypeError, t, 'strides')
|
1263
|
+
|
1264
|
+
|
1265
|
+
class TestFloat(unittest.TestCase):
|
1266
|
+
|
1267
|
+
def test_float_predicates(self):
|
1268
|
+
_float = ['float32', 'float64']
|
1269
|
+
if HAVE_PYTHON_36:
|
1270
|
+
_float.insert(0, 'float16')
|
1271
|
+
|
1272
|
+
for s in _float:
|
1273
|
+
t = ndt(s)
|
1274
|
+
check_serialize(self, t)
|
1275
|
+
|
1276
|
+
self.assertFalse(t.isabstract())
|
1277
|
+
self.assertFalse(t.iscomplex())
|
1278
|
+
self.assertTrue(t.isconcrete())
|
1279
|
+
self.assertTrue(t.isfloat())
|
1280
|
+
self.assertFalse(t.isoptional())
|
1281
|
+
self.assertTrue(t.isscalar())
|
1282
|
+
self.assertFalse(t.issigned())
|
1283
|
+
self.assertFalse(t.isunsigned())
|
1284
|
+
|
1285
|
+
self.assertTrue(t.is_c_contiguous())
|
1286
|
+
self.assertTrue(t.is_f_contiguous())
|
1287
|
+
|
1288
|
+
def test_float_common_fields(self):
|
1289
|
+
_float = [('float32', 4), ('float64', 8)]
|
1290
|
+
if HAVE_PYTHON_36:
|
1291
|
+
_float.insert(0, ('float16', 2))
|
1292
|
+
|
1293
|
+
for s, itemsize in _float:
|
1294
|
+
t = ndt(s)
|
1295
|
+
|
1296
|
+
self.assertEqual(t.ndim, 0)
|
1297
|
+
if not HAVE_32_BIT_LINUX:
|
1298
|
+
self.assertEqual(t.itemsize, itemsize)
|
1299
|
+
self.assertEqual(t.align, itemsize)
|
1300
|
+
|
1301
|
+
self.assertRaises(TypeError, t, 'shape')
|
1302
|
+
self.assertRaises(TypeError, t, 'shape')
|
1303
|
+
|
1304
|
+
|
1305
|
+
class TestComplexKind(unittest.TestCase):
|
1306
|
+
|
1307
|
+
def test_complex_kind_predicates(self):
|
1308
|
+
t = ndt("Complex")
|
1309
|
+
check_serialize(self, t)
|
1310
|
+
|
1311
|
+
self.assertTrue(t.isabstract())
|
1312
|
+
self.assertFalse(t.iscomplex())
|
1313
|
+
self.assertFalse(t.isconcrete())
|
1314
|
+
self.assertFalse(t.isfloat())
|
1315
|
+
self.assertFalse(t.isoptional())
|
1316
|
+
self.assertFalse(t.isscalar())
|
1317
|
+
self.assertFalse(t.issigned())
|
1318
|
+
self.assertFalse(t.isunsigned())
|
1319
|
+
|
1320
|
+
self.assertFalse(t.is_c_contiguous())
|
1321
|
+
self.assertFalse(t.is_f_contiguous())
|
1322
|
+
|
1323
|
+
def test_complex_kind_common_fields(self):
|
1324
|
+
t = ndt("Complex")
|
1325
|
+
|
1326
|
+
self.assertRaises(TypeError, t, 'ndim')
|
1327
|
+
self.assertRaises(TypeError, t, 'itemsize')
|
1328
|
+
self.assertRaises(TypeError, t, 'align')
|
1329
|
+
|
1330
|
+
self.assertRaises(TypeError, t, 'shape')
|
1331
|
+
self.assertRaises(TypeError, t, 'strides')
|
1332
|
+
|
1333
|
+
|
1334
|
+
class TestComplex(unittest.TestCase):
|
1335
|
+
|
1336
|
+
def test_complex_predicates(self):
|
1337
|
+
_complex = ['complex64', 'complex128']
|
1338
|
+
if HAVE_PYTHON_36:
|
1339
|
+
_complex.insert(0, 'complex32')
|
1340
|
+
|
1341
|
+
for s in _complex:
|
1342
|
+
t = ndt(s)
|
1343
|
+
check_serialize(self, t)
|
1344
|
+
|
1345
|
+
self.assertFalse(t.isabstract())
|
1346
|
+
self.assertTrue(t.iscomplex())
|
1347
|
+
self.assertTrue(t.isconcrete())
|
1348
|
+
self.assertFalse(t.isfloat())
|
1349
|
+
self.assertFalse(t.isoptional())
|
1350
|
+
self.assertTrue(t.isscalar())
|
1351
|
+
self.assertFalse(t.issigned())
|
1352
|
+
self.assertFalse(t.isunsigned())
|
1353
|
+
|
1354
|
+
self.assertTrue(t.is_c_contiguous())
|
1355
|
+
self.assertTrue(t.is_f_contiguous())
|
1356
|
+
|
1357
|
+
def test_complex_common_fields(self):
|
1358
|
+
_complex = [('complex64', 8), ('complex128', 16)]
|
1359
|
+
if HAVE_PYTHON_36:
|
1360
|
+
_complex.insert(0, ('complex32', 4))
|
1361
|
+
|
1362
|
+
for s, itemsize in _complex:
|
1363
|
+
t = ndt(s)
|
1364
|
+
|
1365
|
+
self.assertEqual(t.ndim, 0)
|
1366
|
+
if not HAVE_32_BIT_LINUX:
|
1367
|
+
self.assertEqual(t.itemsize, itemsize)
|
1368
|
+
self.assertEqual(t.align, itemsize // 2)
|
1369
|
+
|
1370
|
+
self.assertRaises(TypeError, t, 'shape')
|
1371
|
+
self.assertRaises(TypeError, t, 'shape')
|
1372
|
+
|
1373
|
+
|
1374
|
+
class TestTypevar(unittest.TestCase):
|
1375
|
+
|
1376
|
+
def test_typevar_predicates(self):
|
1377
|
+
t = ndt("T")
|
1378
|
+
check_serialize(self, t)
|
1379
|
+
|
1380
|
+
self.assertTrue(t.isabstract())
|
1381
|
+
self.assertFalse(t.iscomplex())
|
1382
|
+
self.assertFalse(t.isconcrete())
|
1383
|
+
self.assertFalse(t.isfloat())
|
1384
|
+
self.assertFalse(t.isoptional())
|
1385
|
+
self.assertFalse(t.isscalar())
|
1386
|
+
self.assertFalse(t.issigned())
|
1387
|
+
self.assertFalse(t.isunsigned())
|
1388
|
+
|
1389
|
+
self.assertFalse(t.is_c_contiguous())
|
1390
|
+
self.assertFalse(t.is_f_contiguous())
|
1391
|
+
|
1392
|
+
def test_typevar_common_fields(self):
|
1393
|
+
t = ndt("T")
|
1394
|
+
|
1395
|
+
self.assertRaises(TypeError, t, 'ndim')
|
1396
|
+
self.assertRaises(TypeError, t, 'itemsize')
|
1397
|
+
self.assertRaises(TypeError, t, 'align')
|
1398
|
+
|
1399
|
+
self.assertRaises(TypeError, t, 'shape')
|
1400
|
+
self.assertRaises(TypeError, t, 'strides')
|
1401
|
+
|
1402
|
+
|
1403
|
+
class TestCopy(unittest.TestCase):
|
1404
|
+
|
1405
|
+
def test_copy(self):
|
1406
|
+
for dtype, mem in DTYPE_TEST_CASES:
|
1407
|
+
t = ndt(dtype)
|
1408
|
+
u = copy(t)
|
1409
|
+
self.assertEqual(u, t)
|
1410
|
+
self.assertEqual(u.ast_repr(), t.ast_repr())
|
1411
|
+
|
1412
|
+
def test_copy_gc(self):
|
1413
|
+
class MyType(ndt):
|
1414
|
+
self.attr = None
|
1415
|
+
class MyObject():
|
1416
|
+
self.attr = None
|
1417
|
+
|
1418
|
+
x = ndt("var(offsets=[0,2]) * var(offsets=[0,3,10]) * int8")
|
1419
|
+
y = copy(x)
|
1420
|
+
x = y = None
|
1421
|
+
gc.collect()
|
1422
|
+
|
1423
|
+
x = ndt("{z: 10 * int8}", [[0, 2], [0, 10, 20]])
|
1424
|
+
y = copy(x)
|
1425
|
+
x = y = None
|
1426
|
+
gc.collect()
|
1427
|
+
|
1428
|
+
x = MyType("var(offsets=[0,2]) * var(offsets=[0,3,10]) * int8")
|
1429
|
+
y = copy(x)
|
1430
|
+
o = MyObject()
|
1431
|
+
o.attr = x
|
1432
|
+
x.attr = y
|
1433
|
+
y.attr = o
|
1434
|
+
wr = weakref.ref(o)
|
1435
|
+
x = y = o = None
|
1436
|
+
gc.collect()
|
1437
|
+
self.assertTrue(wr() is None, wr())
|
1438
|
+
|
1439
|
+
|
1440
|
+
class TestBufferProtocol(unittest.TestCase):
|
1441
|
+
|
1442
|
+
def test_array(self):
|
1443
|
+
test_cases = [
|
1444
|
+
# format, itemsize, alignment
|
1445
|
+
("(0)f", 4, 4),
|
1446
|
+
("(1)d", 8, 8),
|
1447
|
+
("(125)d", 8, 8),
|
1448
|
+
("(2,3)d", 8, 8),
|
1449
|
+
("(2,10)T{<b:a:Q:b:}", 9, 1),
|
1450
|
+
("(2,19)T{<b:a:xxxQ:b:}", 12, 4),
|
1451
|
+
("(31,221)T{<b:a:xxxxxxxQ:b:}", 16, 8),
|
1452
|
+
("(2,3,10)T{<b:a:xxxxxxxxxxxxxxxQ:b:xxxxxxxx}", 32, 16),
|
1453
|
+
("(2,10)T{=L:a:(2,3)D:b:}", 100, 1)]
|
1454
|
+
|
1455
|
+
test_error_cases = [
|
1456
|
+
# empty shape (scalars are not arrays in datashape)
|
1457
|
+
"()Q",
|
1458
|
+
# Ambiguous (tuple of 10 or array of 10)?
|
1459
|
+
"10Q"]
|
1460
|
+
|
1461
|
+
for fmt, itemsize, align in test_cases:
|
1462
|
+
t = ndt.from_format(fmt)
|
1463
|
+
if not HAVE_32_BIT_LINUX:
|
1464
|
+
self.assertEqual(t.itemsize, itemsize)
|
1465
|
+
self.assertEqual(t.align, align)
|
1466
|
+
|
1467
|
+
for fmt in test_error_cases:
|
1468
|
+
self.assertRaises(ValueError, ndt.from_format, fmt)
|
1469
|
+
|
1470
|
+
def test_record(self):
|
1471
|
+
test_cases = [
|
1472
|
+
# format, itemsize, alignment
|
1473
|
+
("T{<b:a:Q:b:}", 9, 1),
|
1474
|
+
("T{<b:a:xQ:b:}", 10, 2),
|
1475
|
+
("T{<b:a:xxxQ:b:}", 12, 4),
|
1476
|
+
("T{<b:a:xxxxxxxQ:b:}", 16, 8),
|
1477
|
+
("T{<b:a:xxxxxxxxxxxxxxxQ:b:xxxxxxxx}", 32, 16),
|
1478
|
+
("T{=i:foo:f:bar:10s:baz:}", 18, 1)]
|
1479
|
+
|
1480
|
+
test_error_cases = [
|
1481
|
+
# sizeof(signed char) + padding is not a power of two.
|
1482
|
+
"T{<b:a:xxQ:b:}",
|
1483
|
+
# Missing padding bytes at the end of the struct. The only
|
1484
|
+
# reason a compiler would add three padding bytes to field
|
1485
|
+
# zero is an artificial forced alignment of four for the short
|
1486
|
+
# in field one. This in turn requires that the entire struct
|
1487
|
+
# has alignment four, which necessitates two padding bytes at
|
1488
|
+
# the end of the struct.
|
1489
|
+
"T{<b:a:xxxh:b:}",
|
1490
|
+
# Unnatural padding at the end of the struct (expect two padding
|
1491
|
+
# bytes instead of four).
|
1492
|
+
"T{<b:a:xxxh:b:xxxx}"]
|
1493
|
+
|
1494
|
+
for fmt, itemsize, align in test_cases:
|
1495
|
+
t = ndt.from_format(fmt)
|
1496
|
+
if not HAVE_32_BIT_LINUX:
|
1497
|
+
self.assertEqual(t.itemsize, itemsize)
|
1498
|
+
self.assertEqual(t.align, align)
|
1499
|
+
|
1500
|
+
for fmt in test_error_cases:
|
1501
|
+
self.assertRaises(ValueError, ndt.from_format, fmt)
|
1502
|
+
|
1503
|
+
def test_fixed_bytes(self):
|
1504
|
+
for fmt in ['s', '100s']:
|
1505
|
+
t = ndt.from_format(fmt)
|
1506
|
+
s = struct.Struct(fmt)
|
1507
|
+
self.assertEqual(t.itemsize, s.size)
|
1508
|
+
|
1509
|
+
# For consistency (it would be easy to allow, but other dtypes
|
1510
|
+
# cannot have size 0).
|
1511
|
+
self.assertRaises(ValueError, ndt.from_format, "0s")
|
1512
|
+
|
1513
|
+
for fmt in ['0s', 's', '100s']:
|
1514
|
+
for modifier in ['@', '=', '<', '>', '!']:
|
1515
|
+
f = modifier + fmt
|
1516
|
+
self.assertRaises(ValueError, ndt.from_format, f)
|
1517
|
+
|
1518
|
+
t = ndt.from_format('c')
|
1519
|
+
self.assertEqual(t, ndt("char('ascii')"))
|
1520
|
+
|
1521
|
+
def test_primitive(self):
|
1522
|
+
standard = [
|
1523
|
+
'?',
|
1524
|
+
'c', 'b', 'B',
|
1525
|
+
'h', 'i', 'l', 'q',
|
1526
|
+
'H', 'I', 'L', 'Q',
|
1527
|
+
'f', 'd']
|
1528
|
+
|
1529
|
+
native = ['n', 'N']
|
1530
|
+
|
1531
|
+
if HAVE_PYTHON_36:
|
1532
|
+
standard += ['e']
|
1533
|
+
|
1534
|
+
for fmt in standard:
|
1535
|
+
for modifier in ['', '@', '=', '<', '>', '!']:
|
1536
|
+
f = modifier + fmt
|
1537
|
+
t = ndt.from_format(f)
|
1538
|
+
s = struct.Struct(f)
|
1539
|
+
if not HAVE_32_BIT_LINUX:
|
1540
|
+
self.assertEqual(t.itemsize, s.size)
|
1541
|
+
|
1542
|
+
for fmt in native:
|
1543
|
+
for modifier in ['', '@']:
|
1544
|
+
f = modifier + fmt
|
1545
|
+
t = ndt.from_format(f)
|
1546
|
+
s = struct.Struct(f)
|
1547
|
+
if not HAVE_32_BIT_LINUX:
|
1548
|
+
self.assertEqual(t.itemsize, s.size)
|
1549
|
+
|
1550
|
+
for fmt in native:
|
1551
|
+
for modifier in ['=', '<', '>', '!']:
|
1552
|
+
f = modifier + fmt
|
1553
|
+
self.assertRaises(ValueError, ndt.from_format, f)
|
1554
|
+
self.assertRaises(struct.error, struct.Struct, f)
|
1555
|
+
|
1556
|
+
if HAVE_PYTHON_36:
|
1557
|
+
# complex32
|
1558
|
+
fmt = 'E'
|
1559
|
+
for modifier in ['', '@', '=', '<', '>', '!']:
|
1560
|
+
f = modifier + fmt
|
1561
|
+
t = ndt.from_format(f)
|
1562
|
+
if not HAVE_32_BIT_LINUX:
|
1563
|
+
self.assertEqual(t.itemsize, 4)
|
1564
|
+
|
1565
|
+
# complex64
|
1566
|
+
fmt = 'F'
|
1567
|
+
for modifier in ['', '@', '=', '<', '>', '!']:
|
1568
|
+
f = modifier + fmt
|
1569
|
+
t = ndt.from_format(f)
|
1570
|
+
if not HAVE_32_BIT_LINUX:
|
1571
|
+
self.assertEqual(t.itemsize, 8)
|
1572
|
+
|
1573
|
+
# complex128
|
1574
|
+
fmt = 'D'
|
1575
|
+
for modifier in ['', '@', '=', '<', '>', '!']:
|
1576
|
+
f = modifier + fmt
|
1577
|
+
t = ndt.from_format(f)
|
1578
|
+
if not HAVE_32_BIT_LINUX:
|
1579
|
+
self.assertEqual(t.itemsize, 16)
|
1580
|
+
|
1581
|
+
|
1582
|
+
class TestError(unittest.TestCase):
|
1583
|
+
|
1584
|
+
def test_exceptions(self):
|
1585
|
+
self.assertRaises(TypeError, ndt, None)
|
1586
|
+
self.assertRaises(ValueError, ndt, "")
|
1587
|
+
self.assertRaises(ValueError, ndt, "xyz")
|
1588
|
+
self.assertRaises(ValueError, ndt, "var() * int64")
|
1589
|
+
|
1590
|
+
|
1591
|
+
class TestConstruction(unittest.TestCase):
|
1592
|
+
|
1593
|
+
def test_roundtrip(self):
|
1594
|
+
test_cases = [
|
1595
|
+
"2 * 3 * float64",
|
1596
|
+
"2 * 3 * {a : uint8, b : complex64}",
|
1597
|
+
]
|
1598
|
+
for s in test_cases:
|
1599
|
+
t = ndt(s)
|
1600
|
+
self.assertEqual(str(t), s)
|
1601
|
+
|
1602
|
+
def test_from_ndt(self):
|
1603
|
+
test_cases = [
|
1604
|
+
"2 * 3 * {a : 10 * bytes, b : 20 * string}",
|
1605
|
+
"var(offsets=[0,2]) * var(offsets=[0,3,10]) * complex128"]
|
1606
|
+
|
1607
|
+
for s in test_cases:
|
1608
|
+
t = ndt(s)
|
1609
|
+
u = ndt(t)
|
1610
|
+
self.assertEqual(u, t)
|
1611
|
+
t = None
|
1612
|
+
gc.collect()
|
1613
|
+
|
1614
|
+
t = ndt("{x: complex128, y: float64}", [[0, 2], [0, 3, 5]])
|
1615
|
+
u = ndt(t)
|
1616
|
+
self.assertEqual(u, t)
|
1617
|
+
t = None
|
1618
|
+
gc.collect()
|
1619
|
+
|
1620
|
+
|
1621
|
+
class TestMatch(unittest.TestCase):
|
1622
|
+
|
1623
|
+
def test_match(self):
|
1624
|
+
|
1625
|
+
##### No contiguity requirements.
|
1626
|
+
p = ndt("... * 2 * 3 * float32")
|
1627
|
+
c = ndt("2 * 3 * float32")
|
1628
|
+
self.assertTrue(p.match(c))
|
1629
|
+
|
1630
|
+
##### C-contiguity required for inner dimensions.
|
1631
|
+
p = ndt("... * C[2 * 3 * float32]")
|
1632
|
+
|
1633
|
+
# C input.
|
1634
|
+
c = ndt("2 * 3 * float32")
|
1635
|
+
self.assertTrue(p.match(c))
|
1636
|
+
|
1637
|
+
# Inner dimensions C-contiguous.
|
1638
|
+
c = ndt("4 * 2 * 3 * float32")
|
1639
|
+
self.assertTrue(p.match(c))
|
1640
|
+
|
1641
|
+
# Inner dimensions C-contiguous.
|
1642
|
+
c = ndt("fixed(shape=4, step=-6) * 2 * 3 * float32")
|
1643
|
+
self.assertTrue(p.match(c))
|
1644
|
+
|
1645
|
+
# F input.
|
1646
|
+
c = ndt("!2 * 3 * float32")
|
1647
|
+
self.assertFalse(p.match(c))
|
1648
|
+
|
1649
|
+
##### F-contiguity required for inner dimensions.
|
1650
|
+
p = ndt("... * F[2 * 3 * float32]")
|
1651
|
+
|
1652
|
+
# F input.
|
1653
|
+
c = ndt("!2 * 3 * float32")
|
1654
|
+
self.assertTrue(p.match(c))
|
1655
|
+
|
1656
|
+
# Inner dimensions F-contiguous.
|
1657
|
+
c = ndt("fixed(shape=10, step=6) * fixed(shape=2, step=1) * fixed(shape=3, step=2) * float32")
|
1658
|
+
self.assertTrue(p.match(c))
|
1659
|
+
|
1660
|
+
# C input.
|
1661
|
+
c = ndt("2 * 3 * float32")
|
1662
|
+
self.assertFalse(p.match(c))
|
1663
|
+
|
1664
|
+
##### C-contiguity required for all dimensions after broadcast.
|
1665
|
+
p = ndt("C[... * 2 * 3 * float32]")
|
1666
|
+
|
1667
|
+
# C input.
|
1668
|
+
c = ndt("2 * 3 * float32")
|
1669
|
+
self.assertTrue(p.match(c))
|
1670
|
+
|
1671
|
+
# All dimensions C-contiguous.
|
1672
|
+
c = ndt("4 * 2 * 3 * float32")
|
1673
|
+
self.assertTrue(p.match(c))
|
1674
|
+
|
1675
|
+
# Inner dimensions C-contiguous.
|
1676
|
+
c = ndt("fixed(shape=4, step=-6) * 2 * 3 * float32")
|
1677
|
+
self.assertFalse(p.match(c))
|
1678
|
+
|
1679
|
+
# F input.
|
1680
|
+
c = ndt("!2 * 3 * float32")
|
1681
|
+
self.assertFalse(p.match(c))
|
1682
|
+
|
1683
|
+
##### F-contiguity required for all dimensions after broadcast.
|
1684
|
+
p = ndt("F[... * 2 * 3 * float32]")
|
1685
|
+
|
1686
|
+
# C input.
|
1687
|
+
c = ndt("2 * 3 * float32")
|
1688
|
+
self.assertFalse(p.match(c))
|
1689
|
+
|
1690
|
+
c = ndt("4 * 2 * 3 * float32")
|
1691
|
+
self.assertFalse(p.match(c))
|
1692
|
+
|
1693
|
+
# F input.
|
1694
|
+
c = ndt("!2 * 3 * float32")
|
1695
|
+
self.assertTrue(p.match(c))
|
1696
|
+
|
1697
|
+
# All dimensions F-contiguous.
|
1698
|
+
c = ndt("!4 * 2 * 3 * float32")
|
1699
|
+
self.assertTrue(p.match(c))
|
1700
|
+
|
1701
|
+
# Inner dimensions C-contiguous.
|
1702
|
+
c = ndt("fixed(shape=10, step=6) * fixed(shape=2, step=1) * fixed(shape=3, step=2) * float32")
|
1703
|
+
self.assertFalse(p.match(c))
|
1704
|
+
|
1705
|
+
|
1706
|
+
class TestApply(unittest.TestCase):
|
1707
|
+
|
1708
|
+
def test_apply(self):
|
1709
|
+
# Type checking and return type inference for function applications.
|
1710
|
+
|
1711
|
+
# Function type:
|
1712
|
+
sig = ndt("Dims... * N * M * int64, Dims... * M * P * int64 -> Dims... * N * P * float64")
|
1713
|
+
check_serialize(self, sig)
|
1714
|
+
|
1715
|
+
|
1716
|
+
# Argument types:
|
1717
|
+
in_types = [ndt("20 * 2 * 3 * int64"), ndt("20 * 3 * 4 * int64")]
|
1718
|
+
|
1719
|
+
spec = sig.apply(in_types)
|
1720
|
+
self.assertEqual(spec.sig, sig)
|
1721
|
+
self.assertEqual(spec.in_types, in_types)
|
1722
|
+
self.assertSequenceEqual(spec.out_types, [ndt("20 * 2 * 4 * float64")])
|
1723
|
+
self.assertEqual(spec.outer_dims, 1)
|
1724
|
+
|
1725
|
+
def test_apply_error(self):
|
1726
|
+
|
1727
|
+
sig = ndt("Dims... * N * M * int64, Dims... * M * P * int64 -> Dims... * N * P * float64")
|
1728
|
+
|
1729
|
+
lst = [["20 * 2 * 3 * int8", "20 * 3 * 4 * int64"],
|
1730
|
+
["10 * 2 * 3 * int64", "20 * 3 * 4 * int64"],
|
1731
|
+
["20 * 2 * 100 * int64", "20 * 3 * 4 * int64"]]
|
1732
|
+
|
1733
|
+
for l in lst:
|
1734
|
+
in_types = [ndt(x) for x in l]
|
1735
|
+
self.assertRaises(TypeError, sig.apply, in_types)
|
1736
|
+
|
1737
|
+
|
1738
|
+
def from_shape_strides(shape, strides):
|
1739
|
+
s = "float64"
|
1740
|
+
for n, m in zip(reversed(shape), reversed(strides)):
|
1741
|
+
s = "fixed(shape=%s, step=%s) * %s" % (n, m//8, s)
|
1742
|
+
return ndt(s)
|
1743
|
+
|
1744
|
+
class TestBroadcast(unittest.TestCase):
|
1745
|
+
|
1746
|
+
def test_broadcast(self):
|
1747
|
+
|
1748
|
+
for c in BROADCAST_TEST_CASES:
|
1749
|
+
spec = c.sig.apply(c.in_types)
|
1750
|
+
|
1751
|
+
self.assertEqual(len(spec), len(c))
|
1752
|
+
for v, u in zip(spec, c):
|
1753
|
+
self.assertEqual(v, u)
|
1754
|
+
|
1755
|
+
def test_against_numpy(self):
|
1756
|
+
|
1757
|
+
skip_if(np is None, "test requires numpy")
|
1758
|
+
skip_if(SKIP_LONG, "use --long argument to enable these tests")
|
1759
|
+
|
1760
|
+
sig = ndt("... * float64, ... * float64 -> ... * float64")
|
1761
|
+
check_serialize(self, sig)
|
1762
|
+
|
1763
|
+
for xshape in genindices(1):
|
1764
|
+
for yshape in genindices(1):
|
1765
|
+
for xstrides in genindices(8):
|
1766
|
+
for ystrides in genindices(8):
|
1767
|
+
|
1768
|
+
if len(xshape) != len(xstrides) or len(yshape) != len(ystrides):
|
1769
|
+
continue
|
1770
|
+
|
1771
|
+
if (random() < 0.99):
|
1772
|
+
continue
|
1773
|
+
|
1774
|
+
try:
|
1775
|
+
x = np.ndarray(shape=xshape, strides=xstrides)
|
1776
|
+
except ValueError:
|
1777
|
+
continue
|
1778
|
+
|
1779
|
+
try:
|
1780
|
+
y = np.ndarray(shape=yshape, strides=ystrides)
|
1781
|
+
except ValueError:
|
1782
|
+
continue
|
1783
|
+
|
1784
|
+
try:
|
1785
|
+
xx, yy = np.broadcast_arrays(x, y)
|
1786
|
+
except ValueError:
|
1787
|
+
continue
|
1788
|
+
|
1789
|
+
t = from_shape_strides(xshape, xstrides)
|
1790
|
+
u = from_shape_strides(yshape, ystrides)
|
1791
|
+
spec = sig.apply([t, u])
|
1792
|
+
tt, uu = spec.in_broadcast
|
1793
|
+
|
1794
|
+
self.assertEqual(tt.shape, xx.shape)
|
1795
|
+
self.assertEqual(uu.shape, yy.shape)
|
1796
|
+
|
1797
|
+
self.assertEqual(len(tt.strides), len(xx.strides))
|
1798
|
+
for i in range(len(tt.strides)):
|
1799
|
+
if tt.shape[i] <= 1:
|
1800
|
+
# numpy has strange rules for strides in this case.
|
1801
|
+
self.assertEqual(tt.strides[i], 0)
|
1802
|
+
else:
|
1803
|
+
self.assertEqual(tt.strides[i], xx.strides[i])
|
1804
|
+
|
1805
|
+
self.assertEqual(len(uu.strides), len(yy.strides))
|
1806
|
+
for i in range(len(uu.strides)):
|
1807
|
+
if uu.shape[i] <= 1:
|
1808
|
+
# numpy has strange rules for strides in this case.
|
1809
|
+
self.assertEqual(uu.strides[i], 0)
|
1810
|
+
else:
|
1811
|
+
self.assertEqual(uu.strides[i], yy.strides[i])
|
1812
|
+
|
1813
|
+
|
1814
|
+
class TestTypedef(unittest.TestCase):
|
1815
|
+
|
1816
|
+
def test_instantiate(self):
|
1817
|
+
typedef("node", "int32")
|
1818
|
+
typedef("cost", "int32")
|
1819
|
+
typedef("graph", "var * var * (node, cost)")
|
1820
|
+
|
1821
|
+
t = ndt("var(offsets=[0,2]) * var(offsets=[0,3,10]) * (node, cost)")
|
1822
|
+
u = instantiate("graph", t)
|
1823
|
+
del t
|
1824
|
+
gc.collect()
|
1825
|
+
self.assertTrue(u.isconcrete())
|
1826
|
+
|
1827
|
+
t = ndt("var(offsets=[0,2]) * var(offsets=[0,2,3]) * var(offsets=[0,1,2,3]) * (node, cost)")
|
1828
|
+
self.assertRaises(ValueError, instantiate, "graph", t)
|
1829
|
+
|
1830
|
+
|
1831
|
+
class TestSerialize(unittest.TestCase):
|
1832
|
+
|
1833
|
+
def test_serialize(self):
|
1834
|
+
typedef("xnode", "int32")
|
1835
|
+
typedef("xcost", "int32")
|
1836
|
+
|
1837
|
+
t = ndt("var(offsets=[0,2]) * var(offsets=[0,3,10]) * (xnode, xcost)")
|
1838
|
+
b = t.serialize()
|
1839
|
+
u = ndt.deserialize(b)
|
1840
|
+
self.assertEqual(u, t)
|
1841
|
+
|
1842
|
+
|
1843
|
+
class TestPickle(unittest.TestCase):
|
1844
|
+
|
1845
|
+
def test_pickle(self):
|
1846
|
+
typedef("ynode", "int32")
|
1847
|
+
typedef("ycost", "int32")
|
1848
|
+
|
1849
|
+
t = ndt("var(offsets=[0,2]) * var(offsets=[0,3,10]) * (ynode, ycost)")
|
1850
|
+
s = pickle.dumps(t)
|
1851
|
+
u = pickle.loads(s)
|
1852
|
+
self.assertEqual(u, t)
|
1853
|
+
|
1854
|
+
|
1855
|
+
class LongFixedDimTests(unittest.TestCase):
|
1856
|
+
|
1857
|
+
def test_steps_random(self):
|
1858
|
+
# Test random steps.
|
1859
|
+
skip_if(SKIP_LONG, "use --long argument to enable these tests")
|
1860
|
+
|
1861
|
+
for dtype, _ in DTYPE_TEST_CASES:
|
1862
|
+
for _ in range(1000):
|
1863
|
+
s = dtype
|
1864
|
+
ndim = randrange(1, 10)
|
1865
|
+
|
1866
|
+
for n in range(ndim):
|
1867
|
+
shape = randrange(1, 100)
|
1868
|
+
step = randrange(-100, 100)
|
1869
|
+
s = "fixed(shape=%s, step=%s) * %s" % (shape, step, s)
|
1870
|
+
|
1871
|
+
t = ndt(s)
|
1872
|
+
self.assertTrue(verify_datasize(t))
|
1873
|
+
|
1874
|
+
def test_steps_brute_force(self):
|
1875
|
+
# Test all possible shapes and steps within a range.
|
1876
|
+
skip_if(SKIP_BRUTE_FORCE, "use --all argument to enable these tests")
|
1877
|
+
|
1878
|
+
for dtype, _ in DTYPE_TEST_CASES:
|
1879
|
+
for m in range(10):
|
1880
|
+
for i in range(-10, 10):
|
1881
|
+
s = "fixed(shape=%s, step=%s) * %s" % (m, i, dtype)
|
1882
|
+
t = ndt(s)
|
1883
|
+
self.assertTrue(verify_datasize(t))
|
1884
|
+
|
1885
|
+
for m in range(10):
|
1886
|
+
for n in range(10):
|
1887
|
+
for i in range(-10, 10):
|
1888
|
+
for j in range(-10, 10):
|
1889
|
+
s = """fixed(shape=%s, step=%s) *
|
1890
|
+
fixed(shape=%s, step=%s) * %s
|
1891
|
+
""" % (m, i, n, j, dtype)
|
1892
|
+
t = ndt(s)
|
1893
|
+
self.assertTrue(verify_datasize(t))
|
1894
|
+
|
1895
|
+
for m in range(3):
|
1896
|
+
for n in range(3):
|
1897
|
+
for p in range(3):
|
1898
|
+
for i in range(-5, 5):
|
1899
|
+
for j in range(-5, 5):
|
1900
|
+
for k in range(-5, 5):
|
1901
|
+
s = """fixed(shape=%s, step=%s) *
|
1902
|
+
fixed(shape=%s, step=%s) *
|
1903
|
+
fixed(shape=%s, step=%s) * %s
|
1904
|
+
""" % (m, i, n, j, p, k, dtype)
|
1905
|
+
t = ndt(s)
|
1906
|
+
self.assertTrue(verify_datasize(t))
|
1907
|
+
|
1908
|
+
|
1909
|
+
ALL_TESTS = [
|
1910
|
+
TestModule,
|
1911
|
+
TestFunction,
|
1912
|
+
TestVoid,
|
1913
|
+
TestAny,
|
1914
|
+
TestFixedDim,
|
1915
|
+
TestFortran,
|
1916
|
+
TestVarDim,
|
1917
|
+
TestSymbolicDim,
|
1918
|
+
TestEllipsisDim,
|
1919
|
+
TestTuple,
|
1920
|
+
TestRecord,
|
1921
|
+
TestRef,
|
1922
|
+
TestConstr,
|
1923
|
+
TestNominal,
|
1924
|
+
TestScalarKind,
|
1925
|
+
TestCategorical,
|
1926
|
+
TestFixedStringKind,
|
1927
|
+
TestFixedString,
|
1928
|
+
TestFixedBytesKind,
|
1929
|
+
TestFixedBytes,
|
1930
|
+
TestString,
|
1931
|
+
TestBytes,
|
1932
|
+
TestChar,
|
1933
|
+
TestBool,
|
1934
|
+
TestSignedKind,
|
1935
|
+
TestSigned,
|
1936
|
+
TestUnsignedKind,
|
1937
|
+
TestUnsigned,
|
1938
|
+
TestFloatKind,
|
1939
|
+
TestFloat,
|
1940
|
+
TestComplexKind,
|
1941
|
+
TestComplex,
|
1942
|
+
TestTypevar,
|
1943
|
+
TestCopy,
|
1944
|
+
TestBufferProtocol,
|
1945
|
+
TestConstruction,
|
1946
|
+
TestError,
|
1947
|
+
TestMatch,
|
1948
|
+
TestApply,
|
1949
|
+
TestBroadcast,
|
1950
|
+
TestTypedef,
|
1951
|
+
TestSerialize,
|
1952
|
+
TestPickle,
|
1953
|
+
LongFixedDimTests,
|
1954
|
+
]
|
1955
|
+
|
1956
|
+
if __name__ == '__main__':
|
1957
|
+
parser = argparse.ArgumentParser()
|
1958
|
+
parser.add_argument("-f", "--failfast", action="store_true",
|
1959
|
+
help="stop the test run on first error")
|
1960
|
+
parser.add_argument('--long', action="store_true", help="run long slice tests")
|
1961
|
+
parser.add_argument('--all', action="store_true", help="run brute force tests")
|
1962
|
+
args = parser.parse_args()
|
1963
|
+
SKIP_LONG = not (args.long or args.all)
|
1964
|
+
SKIP_BRUTE_FORCE = not args.all
|
1965
|
+
|
1966
|
+
suite = unittest.TestSuite()
|
1967
|
+
loader = unittest.TestLoader()
|
1968
|
+
|
1969
|
+
for case in ALL_TESTS:
|
1970
|
+
s = loader.loadTestsFromTestCase(case)
|
1971
|
+
suite.addTest(s)
|
1972
|
+
|
1973
|
+
runner = unittest.TextTestRunner(failfast=args.failfast, verbosity=2)
|
1974
|
+
result = runner.run(suite)
|
1975
|
+
ret = not result.wasSuccessful()
|
1976
|
+
|
1977
|
+
sys.exit(ret)
|