TestBuffer.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. class TestBufferParsing(CythonTest):
  7. # First, we only test the raw parser, i.e.
  8. # the number and contents of arguments are NOT checked.
  9. # However "dtype"/the first positional argument is special-cased
  10. # to parse a type argument rather than an expression
  11. def parse(self, s):
  12. return self.should_not_fail(lambda: self.fragment(s)).root
  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. def test_basic(self):
  17. t = self.parse(u"cdef object[float, 4, ndim=2, foo=foo] x")
  18. bufnode = t.stats[0].base_type
  19. self.assert_(isinstance(bufnode, TemplatedTypeNode))
  20. self.assertEqual(2, len(bufnode.positional_args))
  21. # print bufnode.dump()
  22. # should put more here...
  23. def test_type_pos(self):
  24. self.parse(u"cdef object[short unsigned int, 3] x")
  25. def test_type_keyword(self):
  26. self.parse(u"cdef object[foo=foo, dtype=short unsigned int] x")
  27. def test_pos_after_key(self):
  28. self.not_parseable("Non-keyword arg following keyword arg",
  29. u"cdef object[foo=1, 2] x")
  30. # See also tests/error/e_bufaccess.pyx and tets/run/bufaccess.pyx
  31. # THESE TESTS ARE NOW DISABLED, the code they test was pretty much
  32. # refactored away
  33. class TestBufferOptions(CythonTest):
  34. # Tests the full parsing of the options within the brackets
  35. def nonfatal_error(self, error):
  36. # We're passing self as context to transform to trap this
  37. self.error = error
  38. self.assert_(self.expect_error)
  39. def parse_opts(self, opts, expect_error=False):
  40. assert opts != ""
  41. s = u"def f():\n cdef object[%s] x" % opts
  42. self.expect_error = expect_error
  43. root = self.fragment(s, pipeline=[NormalizeTree(self), PostParse(self)]).root
  44. if not expect_error:
  45. vardef = root.stats[0].body.stats[0]
  46. assert isinstance(vardef, CVarDefNode) # use normal assert as this is to validate the test code
  47. buftype = vardef.base_type
  48. self.assert_(isinstance(buftype, TemplatedTypeNode))
  49. self.assert_(isinstance(buftype.base_type_node, CSimpleBaseTypeNode))
  50. self.assertEqual(u"object", buftype.base_type_node.name)
  51. return buftype
  52. else:
  53. self.assert_(len(root.stats[0].body.stats) == 0)
  54. def non_parse(self, expected_err, opts):
  55. self.parse_opts(opts, expect_error=True)
  56. # e = self.should_fail(lambda: self.parse_opts(opts))
  57. self.assertEqual(expected_err, self.error.message_only)
  58. def __test_basic(self):
  59. buf = self.parse_opts(u"unsigned short int, 3")
  60. self.assert_(isinstance(buf.dtype_node, CSimpleBaseTypeNode))
  61. self.assert_(buf.dtype_node.signed == 0 and buf.dtype_node.longness == -1)
  62. self.assertEqual(3, buf.ndim)
  63. def __test_dict(self):
  64. buf = self.parse_opts(u"ndim=3, dtype=unsigned short int")
  65. self.assert_(isinstance(buf.dtype_node, CSimpleBaseTypeNode))
  66. self.assert_(buf.dtype_node.signed == 0 and buf.dtype_node.longness == -1)
  67. self.assertEqual(3, buf.ndim)
  68. def __test_ndim(self):
  69. self.parse_opts(u"int, 2")
  70. self.non_parse(ERR_BUF_NDIM, u"int, 'a'")
  71. self.non_parse(ERR_BUF_NDIM, u"int, -34")
  72. def __test_use_DEF(self):
  73. t = self.fragment(u"""
  74. DEF ndim = 3
  75. def f():
  76. cdef object[int, ndim] x
  77. cdef object[ndim=ndim, dtype=int] y
  78. """, pipeline=[NormalizeTree(self), PostParse(self)]).root
  79. stats = t.stats[0].body.stats
  80. self.assert_(stats[0].base_type.ndim == 3)
  81. self.assert_(stats[1].base_type.ndim == 3)
  82. # add exotic and impossible combinations as they come along...
  83. if __name__ == '__main__':
  84. import unittest
  85. unittest.main()