TestSignatureMatching.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import unittest
  2. from Cython.Compiler import PyrexTypes as pt
  3. from Cython.Compiler.ExprNodes import NameNode
  4. from Cython.Compiler.PyrexTypes import CFuncTypeArg
  5. def cfunctype(*arg_types):
  6. return pt.CFuncType(pt.c_int_type,
  7. [ CFuncTypeArg("name", arg_type, None) for arg_type in arg_types ])
  8. def cppclasstype(name, base_classes):
  9. return pt.CppClassType(name, None, 'CPP_'+name, base_classes)
  10. class SignatureMatcherTest(unittest.TestCase):
  11. """
  12. Test the signature matching algorithm for overloaded signatures.
  13. """
  14. def assertMatches(self, expected_type, arg_types, functions):
  15. match = pt.best_match(arg_types, functions)
  16. if expected_type is not None:
  17. self.assertNotEqual(None, match)
  18. self.assertEqual(expected_type, match.type)
  19. def test_cpp_reference_single_arg(self):
  20. function_types = [
  21. cfunctype(pt.CReferenceType(pt.c_int_type)),
  22. cfunctype(pt.CReferenceType(pt.c_long_type)),
  23. cfunctype(pt.CReferenceType(pt.c_double_type)),
  24. ]
  25. functions = [ NameNode(None, type=t) for t in function_types ]
  26. self.assertMatches(function_types[0], [pt.c_int_type], functions)
  27. self.assertMatches(function_types[1], [pt.c_long_type], functions)
  28. self.assertMatches(function_types[2], [pt.c_double_type], functions)
  29. def test_cpp_reference_two_args(self):
  30. function_types = [
  31. cfunctype(
  32. pt.CReferenceType(pt.c_int_type), pt.CReferenceType(pt.c_long_type)),
  33. cfunctype(
  34. pt.CReferenceType(pt.c_long_type), pt.CReferenceType(pt.c_long_type)),
  35. ]
  36. functions = [ NameNode(None, type=t) for t in function_types ]
  37. self.assertMatches(function_types[0], [pt.c_int_type, pt.c_long_type], functions)
  38. self.assertMatches(function_types[1], [pt.c_long_type, pt.c_long_type], functions)
  39. self.assertMatches(function_types[1], [pt.c_long_type, pt.c_int_type], functions)
  40. def test_cpp_reference_cpp_class(self):
  41. classes = [ cppclasstype("Test%d"%i, []) for i in range(2) ]
  42. function_types = [
  43. cfunctype(pt.CReferenceType(classes[0])),
  44. cfunctype(pt.CReferenceType(classes[1])),
  45. ]
  46. functions = [ NameNode(None, type=t) for t in function_types ]
  47. self.assertMatches(function_types[0], [classes[0]], functions)
  48. self.assertMatches(function_types[1], [classes[1]], functions)
  49. def test_cpp_reference_cpp_class_and_int(self):
  50. classes = [ cppclasstype("Test%d"%i, []) for i in range(2) ]
  51. function_types = [
  52. cfunctype(pt.CReferenceType(classes[0]), pt.c_int_type),
  53. cfunctype(pt.CReferenceType(classes[0]), pt.c_long_type),
  54. cfunctype(pt.CReferenceType(classes[1]), pt.c_int_type),
  55. cfunctype(pt.CReferenceType(classes[1]), pt.c_long_type),
  56. ]
  57. functions = [ NameNode(None, type=t) for t in function_types ]
  58. self.assertMatches(function_types[0], [classes[0], pt.c_int_type], functions)
  59. self.assertMatches(function_types[1], [classes[0], pt.c_long_type], functions)
  60. self.assertMatches(function_types[2], [classes[1], pt.c_int_type], functions)
  61. self.assertMatches(function_types[3], [classes[1], pt.c_long_type], functions)