1# ticket: 676
2# tag: cpp
3
4from cython cimport typeof
5
6cdef extern from "arithmetic_analyse_types_helper.h":
7    cdef struct short_return:
8        char *msg
9    cdef struct int_return:
10        char *msg
11    cdef struct longlong_return:
12        char *msg
13    cdef short_return f(short)
14    cdef int_return f(int)
15    cdef longlong_return f(long long)
16
17def short_binop(short val):
18    """
19    Arithmetic in C is always done with at least int precision.
20
21    >>> print(short_binop(3))
22    int called
23    """
24    assert typeof(val + val) == "int", typeof(val + val)
25    assert typeof(val - val) == "int", typeof(val - val)
26    assert typeof(val & val) == "int", typeof(val & val)
27    cdef int_return x = f(val + val)
28    return x.msg.decode('ASCII')
29
30def short_unnop(short val):
31    """
32    Arithmetic in C is always done with at least int precision.
33
34    >>> print(short_unnop(3))
35    int called
36    """
37    cdef int_return x = f(-val)
38    return x.msg.decode('ASCII')
39
40def longlong_binop(long long val):
41    """
42    >>> print(longlong_binop(3))
43    long long called
44    """
45    cdef longlong_return x = f(val * val)
46    return x.msg.decode('ASCII')
47
48def longlong_unnop(long long val):
49    """
50    >>> print(longlong_unnop(3))
51    long long called
52    """
53    cdef longlong_return x = f(~val)
54    return x.msg.decode('ASCII')
55
56
57def test_bint(bint a):
58    """
59    >>> test_bint(True)
60    """
61    assert typeof(a + a) == "int", typeof(a + a)
62    assert typeof(a & a) == "bint", typeof(a & a)
63