OLD | NEW |
(Empty) | |
| 1 from Cython.TestUtils import CythonTest |
| 2 import Cython.Compiler.Errors as Errors |
| 3 from Cython.Compiler.Nodes import * |
| 4 from Cython.Compiler.ParseTreeTransforms import * |
| 5 from Cython.Compiler.Buffer import * |
| 6 |
| 7 |
| 8 class TestMemviewParsing(CythonTest): |
| 9 |
| 10 def parse(self, s): |
| 11 return self.should_not_fail(lambda: self.fragment(s)).root |
| 12 |
| 13 def not_parseable(self, expected_error, s): |
| 14 e = self.should_fail(lambda: self.fragment(s), Errors.CompileError) |
| 15 self.assertEqual(expected_error, e.message_only) |
| 16 |
| 17 def test_default_1dim(self): |
| 18 self.parse(u"cdef int[:] x") |
| 19 self.parse(u"cdef short int[:] x") |
| 20 |
| 21 def test_default_ndim(self): |
| 22 self.parse(u"cdef int[:,:,:,:,:] x") |
| 23 self.parse(u"cdef unsigned long int[:,:,:,:,:] x") |
| 24 self.parse(u"cdef unsigned int[:,:,:,:,:] x") |
| 25 |
| 26 def test_zero_offset(self): |
| 27 self.parse(u"cdef long double[0:] x") |
| 28 self.parse(u"cdef int[0:] x") |
| 29 |
| 30 def test_zero_offset_ndim(self): |
| 31 self.parse(u"cdef int[0:,0:,0:,0:] x") |
| 32 |
| 33 def test_def_arg(self): |
| 34 self.parse(u"def foo(int[:,:] x): pass") |
| 35 |
| 36 def test_cdef_arg(self): |
| 37 self.parse(u"cdef foo(int[:,:] x): pass") |
| 38 |
| 39 def test_general_slice(self): |
| 40 self.parse(u'cdef float[::ptr, ::direct & contig, 0::full & strided] x') |
| 41 |
| 42 def test_non_slice_memview(self): |
| 43 self.not_parseable(u"An axis specification in memoryview declaration doe
s not have a ':'.", |
| 44 u"cdef double[:foo, bar] x") |
| 45 self.not_parseable(u"An axis specification in memoryview declaration doe
s not have a ':'.", |
| 46 u"cdef double[0:foo, bar] x") |
| 47 |
| 48 def test_basic(self): |
| 49 t = self.parse(u"cdef int[:] x") |
| 50 memv_node = t.stats[0].base_type |
| 51 self.assert_(isinstance(memv_node, MemoryViewSliceTypeNode)) |
| 52 |
| 53 # we also test other similar declarations (buffers, anonymous C arrays) |
| 54 # since the parsing has to distinguish between them. |
| 55 |
| 56 def disable_test_no_buf_arg(self): # TODO |
| 57 self.not_parseable(u"Expected ']'", |
| 58 u"cdef extern foo(object[int, ndim=2])") |
| 59 |
| 60 def disable_test_parse_sizeof(self): # TODO |
| 61 self.parse(u"sizeof(int[NN])") |
| 62 self.parse(u"sizeof(int[])") |
| 63 self.parse(u"sizeof(int[][NN])") |
| 64 self.not_parseable(u"Expected an identifier or literal", |
| 65 u"sizeof(int[:NN])") |
| 66 self.not_parseable(u"Expected ']'", |
| 67 u"sizeof(foo[dtype=bar]") |
| 68 |
| 69 if __name__ == '__main__': |
| 70 import unittest |
| 71 unittest.main() |
OLD | NEW |