1# mode: run
2# tag: syntax
3
4from __future__ import absolute_import
5
6cimport cython
7from cython cimport typeof
8
9import sys
10
11
12def valid_underscore_literals():
13    """
14    >>> valid_underscore_literals()
15    """
16    # Copied from CPython's test_grammar.py
17    assert 0_0_0 == 0
18    assert 4_2 == 42
19    assert 1_0000_0000 == 100000000
20    assert 0b1001_0100 == 0b10010100
21    assert 0xffff_ffff == 0xffffffff
22    assert 0o5_7_7 == 0o577
23    assert 1_00_00.5 == 10000.5
24    assert 1e1_0 == 1e10
25    assert .1_4 == .14
26    assert 1_0 == 1_0L == 1_0LL == 1_0UL == 1_0ULL
27    assert typeof(1_0ULL) == "unsigned long long"
28
29
30@cython.test_assert_path_exists(
31    '//IntNode[@longness = "LL"]',
32    '//IntNode[@longness = "L"]',
33    )
34@cython.test_fail_if_path_exists('//IntNode[@longness = ""]')
35def c_longs():
36    """
37    >>> c_longs() == (1, 1, -1, 18446744073709551615)  or  c_longs()
38    True
39    """
40    cdef long a = 1L
41    cdef unsigned long ua = 1UL
42    cdef long long aa = 0xFFFFFFFFFFFFFFFFLL
43    cdef unsigned long long uaa = 0xFFFFFFFFFFFFFFFFULL
44    return a, ua, int(aa), uaa
45
46@cython.test_assert_path_exists(
47    '//IntNode[@longness = "LL"]',
48    '//IntNode[@longness = "L"]',
49    )
50@cython.test_fail_if_path_exists('//IntNode[@longness = ""]')
51def negative_c_longs():
52    """
53    >>> negative_c_longs() == (-1, -9223285636854775809)  or  negative_c_longs()
54    True
55    """
56    cdef long a = -1L
57    cdef long long aa = -9223285636854775809LL
58    return a, aa
59
60def py_longs():
61    """
62    >>> py_longs() == (
63    ...     1, 1, 100000000000000000000000000000000, -100000000000000000000000000000000
64    ...     )  or  py_longs()
65    True
66    """
67    return 1, 1L, 100000000000000000000000000000000, -100000000000000000000000000000000
68
69@cython.test_fail_if_path_exists("//NumBinopNode", "//IntBinopNode")
70@cython.test_assert_path_exists("//ReturnStatNode/IntNode")
71def py_huge_calculated_long():
72    """
73    >>> py_huge_calculated_long() == (
74    ...     1606938044258990275541962092341162602522202993782792835301376
75    ...     )  or  py_huge_calculated_long()
76    True
77    """
78    return 1 << 200
79
80@cython.test_fail_if_path_exists("//NumBinopNode", "//IntBinopNode")
81@cython.test_assert_path_exists("//ReturnStatNode/IntNode")
82def py_huge_computation_small_result():
83    """
84    >>> py_huge_computation_small_result()
85    2
86    """
87    return (1 << 200) >> 199
88
89@cython.test_fail_if_path_exists("//NumBinopNode", "//IntBinopNode")
90#@cython.test_assert_path_exists("//ReturnStatNode/IntNode")
91def py_huge_computation_small_result_neg():
92    """
93    >>> py_huge_computation_small_result_neg() == (
94    ...    -2535301200456458802993406410752, -2535301200456458802993406410752
95    ...    )  or  py_huge_computation_small_result_neg()
96    True
97    """
98    return -(2 ** 101), (-2) ** 101
99
100def large_literal():
101    """
102    >>> type(large_literal()) is int
103    True
104    """
105    if sys.version_info[0] >= 3 or sys.maxint > 0xFFFFFFFFFFFF:
106        return 0xFFFFFFFFFFFF
107    else:
108        return 0xFFFFFFF
109
110def c_long_types():
111    """
112    >>> c_long_types()
113    long
114    long
115    long long
116    unsigned long
117    unsigned long
118    unsigned long long
119    """
120    print typeof(1)
121    print typeof(1L)
122    print typeof(1LL)
123    print typeof(1U)
124    print typeof(1UL)
125    print typeof(1ULL)
126
127# different ways to write an integer in Python
128
129def c_oct():
130    """
131    >>> c_oct()
132    (1, -17, 63)
133    """
134    cdef int a = 0o01
135    cdef int b = -0o21
136    cdef int c = 0o77
137    return a,b,c
138
139def c_oct_py2_legacy():
140    """
141    >>> c_oct_py2_legacy()
142    (1, -17, 63)
143    """
144    cdef int a = 001
145    cdef int b = -021
146    cdef int c = 077
147    return a,b,c
148
149def py_oct():
150    """
151    >>> py_oct()
152    (1, -17, 63)
153    """
154    return 0o01, -0o21, 0o77
155
156def py_oct_py2_legacy():
157    """
158    >>> py_oct_py2_legacy()
159    (1, -17, 63)
160    """
161    return 001, -021, 077
162
163def c_hex():
164    """
165    >>> c_hex()
166    (1, -33, 255)
167    """
168    cdef int a = 0x01
169    cdef int b = -0x21
170    cdef int c = 0xFF
171    return a,b,c
172
173def py_hex():
174    """
175    >>> py_hex()
176    (1, -33, 255)
177    """
178    return 0x01, -0x21, 0xFF
179
180def c_bin():
181    """
182    >>> c_bin()
183    (1, -2, 15)
184    """
185    cdef int a = 0b01
186    cdef int b = -0b10
187    cdef int c = 0b1111
188    return a,b,c
189
190def py_bin():
191    """
192    >>> py_bin()
193    (1, -2, 15)
194    """
195    return 0b01, -0b10, 0b1111
196