aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/tools/cython/Cython/Compiler/Tests/TestSignatureMatching.py
blob: 166bb225b9b4cc403ae737fedc484c51ed337e24 (plain) (blame)
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
import unittest

from Cython.Compiler import PyrexTypes as pt
from Cython.Compiler.ExprNodes import NameNode
from Cython.Compiler.PyrexTypes import CFuncTypeArg

def cfunctype(*arg_types):
    return pt.CFuncType(pt.c_int_type,
        [ CFuncTypeArg("name", arg_type, None) for arg_type in arg_types ])

def cppclasstype(name, base_classes):
    return pt.CppClassType(name, None, 'CPP_'+name, base_classes)

class SignatureMatcherTest(unittest.TestCase):
    """
    Test the signature matching algorithm for overloaded signatures.
    """
    def assertMatches(self, expected_type, arg_types, functions):
        match = pt.best_match(arg_types, functions)
        if expected_type is not None:
            self.assertNotEqual(None, match)
        self.assertEqual(expected_type, match.type)

    def test_cpp_reference_single_arg(self):
        function_types = [
            cfunctype(pt.CReferenceType(pt.c_int_type)),
            cfunctype(pt.CReferenceType(pt.c_long_type)),
            cfunctype(pt.CReferenceType(pt.c_double_type)),
            ]

        functions = [ NameNode(None, type=t) for t in function_types ]
        self.assertMatches(function_types[0], [pt.c_int_type], functions)
        self.assertMatches(function_types[1], [pt.c_long_type], functions)
        self.assertMatches(function_types[2], [pt.c_double_type], functions)

    def test_cpp_reference_two_args(self):
        function_types = [
            cfunctype(
                pt.CReferenceType(pt.c_int_type), pt.CReferenceType(pt.c_long_type)),
            cfunctype(
                pt.CReferenceType(pt.c_long_type), pt.CReferenceType(pt.c_long_type)),
            ]

        functions = [ NameNode(None, type=t) for t in function_types ]
        self.assertMatches(function_types[0], [pt.c_int_type, pt.c_long_type], functions)
        self.assertMatches(function_types[1], [pt.c_long_type, pt.c_long_type], functions)
        self.assertMatches(function_types[1], [pt.c_long_type, pt.c_int_type], functions)

    def test_cpp_reference_cpp_class(self):
        classes = [ cppclasstype("Test%d"%i, []) for i in range(2) ]
        function_types = [
            cfunctype(pt.CReferenceType(classes[0])),
            cfunctype(pt.CReferenceType(classes[1])),
            ]

        functions = [ NameNode(None, type=t) for t in function_types ]
        self.assertMatches(function_types[0], [classes[0]], functions)
        self.assertMatches(function_types[1], [classes[1]], functions)

    def test_cpp_reference_cpp_class_and_int(self):
        classes = [ cppclasstype("Test%d"%i, []) for i in range(2) ]
        function_types = [
            cfunctype(pt.CReferenceType(classes[0]), pt.c_int_type),
            cfunctype(pt.CReferenceType(classes[0]), pt.c_long_type),
            cfunctype(pt.CReferenceType(classes[1]), pt.c_int_type),
            cfunctype(pt.CReferenceType(classes[1]), pt.c_long_type),
            ]

        functions = [ NameNode(None, type=t) for t in function_types ]
        self.assertMatches(function_types[0], [classes[0], pt.c_int_type], functions)
        self.assertMatches(function_types[1], [classes[0], pt.c_long_type], functions)
        self.assertMatches(function_types[2], [classes[1], pt.c_int_type], functions)
        self.assertMatches(function_types[3], [classes[1], pt.c_long_type], functions)