fix failing testcase in Py2 because of long suffix
[cython.git] / tests / run / ctypedef_int_types_T333.pyx
1 #cython: autotestdict=True
2
3 # -------------------------------------------------------------------
4
5 cdef extern from "ctypedef_int_types_chdr_T333.h":
6      ctypedef int SChar     ## "signed char"
7      ctypedef int UChar     ## "unsigned char"
8      ctypedef int SShort    ## "signed short"
9      ctypedef int UShort    ## "unsigned short"
10      ctypedef int SInt      ## "signed int"
11      ctypedef int UInt      ## "unsigned int"
12      ctypedef int SLong     ## "signed long"
13      ctypedef int ULong     ## "unsigned long"
14      ctypedef int SLongLong ## "signed PY_LONG_LONG"
15      ctypedef int ULongLong ## "unsigned PY_LONG_LONG"
16
17 # -------------------------------------------------------------------
18
19 SCHAR_MAX = <SChar>((<UChar>-1)>>1)
20 SCHAR_MIN = (-SCHAR_MAX-1)
21
22 def test_schar(SChar x):
23    u"""
24    >>> test_schar(-129) #doctest: +ELLIPSIS
25    Traceback (most recent call last):
26        ...
27    OverflowError: ...
28    >>> test_schar(-128)
29    -128
30    >>> test_schar(0)
31    0
32    >>> test_schar(127)
33    127
34    >>> test_schar(128) #doctest: +ELLIPSIS
35    Traceback (most recent call last):
36        ...
37    OverflowError: ...
38    """
39    return x
40
41 def test_add_schar(x, y):
42    u"""
43    >>> test_add_schar(SCHAR_MIN, -1) #doctest: +ELLIPSIS
44    Traceback (most recent call last):
45        ...
46    OverflowError: ...
47    >>> test_add_schar(SCHAR_MIN, 0) == SCHAR_MIN
48    True
49    >>> test_add_schar(SCHAR_MIN, 1) == SCHAR_MIN+1
50    True
51    >>> test_add_schar(SCHAR_MAX, -1) == SCHAR_MAX-1
52    True
53    >>> test_add_schar(SCHAR_MAX, 0) == SCHAR_MAX
54    True
55    >>> test_add_schar(SCHAR_MAX, 1) #doctest: +ELLIPSIS
56    Traceback (most recent call last):
57        ...
58    OverflowError: ...
59    """
60    cdef SChar r = x + y
61    return r
62
63 UCHAR_MAX = <UChar>((<UChar>-1))
64
65 def test_uchar(UChar x):
66    u"""
67    >>> test_uchar(-1) #doctest: +ELLIPSIS
68    Traceback (most recent call last):
69        ...
70    OverflowError: ...
71    >>> test_uchar(0)
72    0
73    >>> test_uchar(1)
74    1
75    >>> test_uchar(UCHAR_MAX) == UCHAR_MAX
76    True
77    >>> test_uchar(UCHAR_MAX+1) #doctest: +ELLIPSIS
78    Traceback (most recent call last):
79        ...
80    OverflowError: ...
81    """
82    return x
83
84 def test_add_uchar(x, y):
85    u"""
86    >>> test_add_uchar(UCHAR_MAX, 0) == UCHAR_MAX
87    True
88    >>> test_add_uchar(UCHAR_MAX, 1) #doctest: +ELLIPSIS
89    Traceback (most recent call last):
90        ...
91    OverflowError: ...
92    """
93    cdef UChar r = x + y
94    return r
95
96 # -------------------------------------------------------------------
97
98 SSHORT_MAX = <SShort>((<UShort>-1)>>1)
99 SSHORT_MIN = (-SSHORT_MAX-1)
100
101 def test_sshort(short x):
102    u"""
103    >>> test_sshort(SSHORT_MIN-1) #doctest: +ELLIPSIS
104    Traceback (most recent call last):
105        ...
106    OverflowError: ...
107    >>> test_sshort(SSHORT_MIN) == SSHORT_MIN
108    True
109    >>> test_sshort(-1)
110    -1
111    >>> test_sshort(0)
112    0
113    >>> test_sshort(1)
114    1
115    >>> test_sshort(SSHORT_MAX) == SSHORT_MAX
116    True
117    >>> test_sshort(SSHORT_MAX+1) #doctest: +ELLIPSIS
118    Traceback (most recent call last):
119        ...
120    OverflowError: ...
121    """
122    return x
123
124 def test_add_sshort(x, y):
125    u"""
126    >>> test_add_sshort(SSHORT_MIN, -1) #doctest: +ELLIPSIS
127    Traceback (most recent call last):
128        ...
129    OverflowError: ...
130    >>> test_add_sshort(SSHORT_MIN, 0) == SSHORT_MIN
131    True
132    >>> test_add_sshort(SSHORT_MIN, 1) == SSHORT_MIN+1
133    True
134    >>> test_add_sshort(SSHORT_MAX, -1) == SSHORT_MAX-1
135    True
136    >>> test_add_sshort(SSHORT_MAX, 0) == SSHORT_MAX
137    True
138    >>> test_add_sshort(SSHORT_MAX, 1) #doctest: +ELLIPSIS
139    Traceback (most recent call last):
140        ...
141    OverflowError: ...
142    """
143    cdef SShort r = x + y
144    return r
145
146 USHORT_MAX = <UShort>((<UShort>-1))
147
148 def test_ushort(UShort x):
149    u"""
150    >>> test_ushort(-1) #doctest: +ELLIPSIS
151    Traceback (most recent call last):
152        ...
153    OverflowError: ...
154    >>> test_ushort(0)
155    0
156    >>> test_ushort(1)
157    1
158    >>> test_ushort(USHORT_MAX) == USHORT_MAX
159    True
160    >>> test_ushort(USHORT_MAX+1) #doctest: +ELLIPSIS
161    Traceback (most recent call last):
162        ...
163    OverflowError: ...
164    """
165    return x
166
167 def test_add_ushort(x, y):
168    u"""
169    >>> test_add_ushort(USHORT_MAX, 0) == USHORT_MAX
170    True
171    >>> test_add_ushort(USHORT_MAX, 1) #doctest: +ELLIPSIS
172    Traceback (most recent call last):
173        ...
174    OverflowError: ...
175    """
176    cdef UShort r = x + y
177    return r
178
179 # -------------------------------------------------------------------
180
181 SINT_MAX = <SInt>((<UInt>-1)>>1)
182 SINT_MIN = (-SINT_MAX-1)
183
184 def test_sint(int x):
185    u"""
186    >>> test_sint(SINT_MIN-1) #doctest: +ELLIPSIS
187    Traceback (most recent call last):
188        ...
189    OverflowError: ...
190    >>> test_sint(SINT_MIN) == SINT_MIN
191    True
192    >>> test_sint(-1)
193    -1
194    >>> test_sint(0)
195    0
196    >>> test_sint(1)
197    1
198    >>> test_sint(SINT_MAX) == SINT_MAX
199    True
200    >>> test_sint(SINT_MAX+1) #doctest: +ELLIPSIS
201    Traceback (most recent call last):
202        ...
203    OverflowError: ...
204    """
205    return x
206
207 def test_add_sint(x, y):
208    u"""
209    >>> test_add_sint(SINT_MIN, -1) #doctest: +ELLIPSIS
210    Traceback (most recent call last):
211        ...
212    OverflowError: ...
213    >>> test_add_sint(SINT_MIN, 0) == SINT_MIN
214    True
215    >>> test_add_sint(SINT_MIN, 1) == SINT_MIN+1
216    True
217    >>> test_add_sint(SINT_MAX, -1) == SINT_MAX-1
218    True
219    >>> test_add_sint(SINT_MAX, 0) == SINT_MAX
220    True
221    >>> test_add_sint(SINT_MAX, 1) #doctest: +ELLIPSIS
222    Traceback (most recent call last):
223        ...
224    OverflowError: ...
225    """
226    cdef SInt r = x + y
227    return r
228
229 UINT_MAX = <UInt>(<UInt>-1)
230
231 def test_uint(UInt x):
232    u"""
233    >>> test_uint(-1) #doctest: +ELLIPSIS
234    Traceback (most recent call last):
235        ...
236    OverflowError: ...
237    >>> print(test_uint(0))
238    0
239    >>> print(test_uint(1))
240    1
241    >>> test_uint(UINT_MAX) == UINT_MAX
242    True
243    >>> test_uint(UINT_MAX+1) #doctest: +ELLIPSIS
244    Traceback (most recent call last):
245        ...
246    OverflowError: ...
247    """
248    return x
249
250 def test_add_uint(x, y):
251    u"""
252    >>> test_add_uint(UINT_MAX, 0) == UINT_MAX
253    True
254    >>> test_add_uint(UINT_MAX, 1) #doctest: +ELLIPSIS
255    Traceback (most recent call last):
256        ...
257    OverflowError: ...
258    """
259    cdef UInt r = x + y
260    return r
261
262 # -------------------------------------------------------------------
263
264 SLONG_MAX = <SLong>((<ULong>-1)>>1)
265 SLONG_MIN = (-SLONG_MAX-1)
266
267 def test_slong(long x):
268    u"""
269    >>> test_slong(SLONG_MIN-1) #doctest: +ELLIPSIS
270    Traceback (most recent call last):
271        ...
272    OverflowError: ...
273    >>> test_slong(SLONG_MIN) == SLONG_MIN
274    True
275    >>> test_slong(-1)
276    -1
277    >>> test_slong(0)
278    0
279    >>> test_slong(1)
280    1
281    >>> test_slong(SLONG_MAX) == SLONG_MAX
282    True
283    >>> test_slong(SLONG_MAX+1) #doctest: +ELLIPSIS
284    Traceback (most recent call last):
285        ...
286    OverflowError: ...
287    """
288    return x
289
290 def test_add_slong(x, y):
291    u"""
292    >>> test_add_slong(SLONG_MIN, -1) #doctest: +ELLIPSIS
293    Traceback (most recent call last):
294        ...
295    OverflowError: ...
296    >>> test_add_slong(SLONG_MIN, 0) == SLONG_MIN
297    True
298    >>> test_add_slong(SLONG_MIN, 1) == SLONG_MIN+1
299    True
300    >>> test_add_slong(SLONG_MAX, -1) == SLONG_MAX-1
301    True
302    >>> test_add_slong(SLONG_MAX, 0) == SLONG_MAX
303    True
304    >>> test_add_slong(SLONG_MAX, 1) #doctest: +ELLIPSIS
305    Traceback (most recent call last):
306        ...
307    OverflowError: ...
308    """
309    cdef SLong r = x + y
310    return r
311
312 ULONG_MAX = <ULong>(<ULong>-1)
313
314 def test_ulong(ULong x):
315    u"""
316    >>> test_ulong(-1) #doctest: +ELLIPSIS
317    Traceback (most recent call last):
318        ...
319    OverflowError: ...
320    >>> print(test_ulong(0))
321    0
322    >>> print(test_ulong(1))
323    1
324    >>> test_ulong(ULONG_MAX) == ULONG_MAX
325    True
326    >>> test_ulong(ULONG_MAX+1) #doctest: +ELLIPSIS
327    Traceback (most recent call last):
328        ...
329    OverflowError: ...
330    """
331    return x
332
333 def test_add_ulong(x, y):
334    u"""
335    >>> test_add_ulong(ULONG_MAX, 0) == ULONG_MAX
336    True
337    >>> test_add_ulong(ULONG_MAX, 1) #doctest: +ELLIPSIS
338    Traceback (most recent call last):
339        ...
340    OverflowError: ...
341    """
342    cdef ULong r = x + y
343    return r
344
345 # -------------------------------------------------------------------
346
347 SLONGLONG_MAX = <SLongLong>((<ULongLong>-1)>>1)
348 SLONGLONG_MIN = (-SLONGLONG_MAX-1)
349
350 def test_slonglong(long long x):
351    u"""
352    >>> test_slonglong(SLONGLONG_MIN-1) #doctest: +ELLIPSIS
353    Traceback (most recent call last):
354        ...
355    OverflowError: ...
356    >>> test_slonglong(SLONGLONG_MIN) == SLONGLONG_MIN
357    True
358    >>> print(test_slonglong(-1))
359    -1
360    >>> print(test_slonglong(0))
361    0
362    >>> print(test_slonglong(1))
363    1
364    >>> test_slonglong(SLONGLONG_MAX) == SLONGLONG_MAX
365    True
366    >>> test_slonglong(SLONGLONG_MAX+1) #doctest: +ELLIPSIS
367    Traceback (most recent call last):
368        ...
369    OverflowError: ...
370    """
371    return x
372
373 def test_add_slonglong(x, y):
374    u"""
375    >>> test_add_slonglong(SLONGLONG_MIN, -1) #doctest: +ELLIPSIS
376    Traceback (most recent call last):
377        ...
378    OverflowError: ...
379    >>> test_add_slonglong(SLONGLONG_MIN, 0) == SLONGLONG_MIN
380    True
381    >>> test_add_slonglong(SLONGLONG_MIN, 1) == SLONGLONG_MIN+1
382    True
383    >>> test_add_slonglong(SLONGLONG_MAX, -1) == SLONGLONG_MAX-1
384    True
385    >>> test_add_slonglong(SLONGLONG_MAX, 0) == SLONGLONG_MAX
386    True
387    >>> test_add_slonglong(SLONGLONG_MAX, 1) #doctest: +ELLIPSIS
388    Traceback (most recent call last):
389        ...
390    OverflowError: ...
391    """
392    cdef SLongLong r = x + y
393    return r
394
395 ULONGLONG_MAX = <ULongLong>(<ULongLong>-1)
396
397 def test_ulonglong(ULongLong x):
398    u"""
399    >>> test_ulonglong(-1) #doctest: +ELLIPSIS
400    Traceback (most recent call last):
401        ...
402    OverflowError: ...
403    >>> print(test_ulonglong(0))
404    0
405    >>> print(test_ulonglong(1))
406    1
407    >>> test_ulonglong(ULONGLONG_MAX) == ULONGLONG_MAX
408    True
409    >>> test_ulonglong(ULONGLONG_MAX+1) #doctest: +ELLIPSIS
410    Traceback (most recent call last):
411        ...
412    OverflowError: ...
413    """
414    return x
415
416 def test_add_ulonglong(x, y):
417    u"""
418    >>> test_add_ulonglong(ULONGLONG_MAX, 0) == ULONGLONG_MAX
419    True
420    >>> test_add_ulonglong(ULONGLONG_MAX, 1) #doctest: +ELLIPSIS
421    Traceback (most recent call last):
422        ...
423    OverflowError: ...
424    """
425    cdef ULongLong r = x + y
426    return r
427
428 # -------------------------------------------------------------------
429
430 cdef class MyClass:
431     """
432     >>> a = MyClass()
433
434     >>> vals = (SCHAR_MIN,     UCHAR_MAX,
435     ...         SSHORT_MIN,    USHORT_MAX,
436     ...         SINT_MIN,      UINT_MAX,
437     ...         SLONG_MIN,     ULONG_MAX,
438     ...         SLONGLONG_MIN, ULONGLONG_MAX)
439     >>> a.setvalues(*vals)
440     >>> a.getvalues() == vals
441     True
442
443     >>> vals = (SCHAR_MAX,     UCHAR_MAX,
444     ...         SSHORT_MAX,    USHORT_MAX,
445     ...         SINT_MAX,      UINT_MAX,
446     ...         SLONG_MAX,     ULONG_MAX,
447     ...         SLONGLONG_MAX, ULONGLONG_MAX)
448     >>> a.setvalues(*vals)
449     >>> a.getvalues() == vals
450     True
451
452     >>> vals = (0,) * 10
453     >>> a.setvalues(*vals)
454     >>> a.getvalues() == vals
455     True
456
457
458     """
459     cdef:
460        SChar     attr_schar
461        UChar     attr_uchar
462        SShort    attr_sshort
463        UShort    attr_ushort
464        SInt      attr_sint
465        UInt      attr_uint
466        SLong     attr_slong
467        ULong     attr_ulong
468        SLongLong attr_slonglong
469        ULongLong attr_ulonglong
470
471     cpdef setvalues(self,
472                     SChar     arg_schar     ,
473                     UChar     arg_uchar     ,
474                     SShort    arg_sshort    ,
475                     UShort    arg_ushort    ,
476                     SInt      arg_sint      ,
477                     UInt      arg_uint      ,
478                     SLong     arg_slong     ,
479                     ULong     arg_ulong     ,
480                     SLongLong arg_slonglong ,
481                     ULongLong arg_ulonglong ):
482         self.attr_schar     = arg_schar
483         self.attr_uchar     = arg_uchar
484         self.attr_sshort    = arg_sshort
485         self.attr_ushort    = arg_ushort
486         self.attr_sint      = arg_sint
487         self.attr_uint      = arg_uint
488         self.attr_slong     = arg_slong
489         self.attr_ulong     = arg_ulong
490         self.attr_slonglong = arg_slonglong
491         self.attr_ulonglong = arg_ulonglong
492
493     cpdef getvalues(self):
494         return (self.attr_schar     ,
495                 self.attr_uchar     ,
496                 self.attr_sshort    ,
497                 self.attr_ushort    ,
498                 self.attr_sint      ,
499                 self.attr_uint      ,
500                 self.attr_slong     ,
501                 self.attr_ulong     ,
502                 self.attr_slonglong ,
503                 self.attr_ulonglong )
504
505
506 # -------------------------------------------------------------------
507
508 cdef extern from *:
509     ctypedef signed   MySInt1 "signed short"
510     ctypedef unsigned MyUInt1 "unsigned short"
511
512 def test_MySInt1(MySInt1 x):
513    u"""
514    >>> test_MySInt1(-1)
515    -1
516    >>> test_MySInt1(0)
517    0
518    >>> test_MySInt1(1)
519    1
520    """
521    return x
522
523 def test_MyUInt1(MyUInt1 x):
524    u"""
525    >>> test_MyUInt1(-1) #doctest: +ELLIPSIS
526    Traceback (most recent call last):
527        ...
528    OverflowError: ...
529    >>> test_MyUInt1(0)
530    0
531    >>> test_MyUInt1(1)
532    1
533    """
534    return x
535
536 cdef extern from *:
537     ctypedef signed   MySInt2 "signed short"
538     ctypedef unsigned MyUInt2 "unsigned short"
539
540 def test_MySInt2(MySInt2 x):
541    u"""
542    >>> test_MySInt2(-1)
543    -1
544    >>> test_MySInt2(0)
545    0
546    >>> test_MySInt2(1)
547    1
548    """
549    return x
550
551 def test_MyUInt2(MyUInt2 x):
552    u"""
553    >>> test_MyUInt2(-1) #doctest: +ELLIPSIS
554    Traceback (most recent call last):
555        ...
556    OverflowError: ...
557    >>> test_MyUInt2(0)
558    0
559    >>> test_MyUInt2(1)
560    1
561    """
562    return x
563
564 # -------------------------------------------------------------------
565
566 cimport ctypedef_int_types_defs_T333 as defs
567
568 def test_DefSInt(defs.SInt x):
569    u"""
570    >>> test_DefSInt(-1)
571    -1
572    >>> test_DefSInt(0)
573    0
574    >>> test_DefSInt(1)
575    1
576    """
577    return x
578
579 def test_DefUChar(defs.UChar x):
580    u"""
581    >>> test_DefUChar(-1) #doctest: +ELLIPSIS
582    Traceback (most recent call last):
583        ...
584    OverflowError: ...
585    >>> test_DefUChar(0)
586    0
587    >>> test_DefUChar(1)
588    1
589    """
590    return x
591
592 def test_ExtSInt(defs.ExtSInt x):
593    u"""
594    >>> test_ExtSInt(-1)
595    -1
596    >>> test_ExtSInt(0)
597    0
598    >>> test_ExtSInt(1)
599    1
600    """
601    return x
602
603 def test_ExtUInt(defs.ExtUInt x):
604    u"""
605    >>> test_ExtUInt(-1) #doctest: +ELLIPSIS
606    Traceback (most recent call last):
607        ...
608    OverflowError: ...
609    >>> test_ExtUInt(0)
610    0
611    >>> test_ExtUInt(1)
612    1
613    """
614    return x
615
616
617 ctypedef defs.SShort LocSInt
618 ctypedef defs.UShort LocUInt
619
620 def test_LocSInt(LocSInt x):
621    u"""
622    >>> test_LocSInt(-1)
623    -1
624    >>> test_LocSInt(0)
625    0
626    >>> test_LocSInt(1)
627    1
628    """
629    return x
630
631 def test_LocUInt(LocUInt x):
632    u"""
633    >>> test_LocUInt(-1) #doctest: +ELLIPSIS
634    Traceback (most recent call last):
635        ...
636    OverflowError: ...
637    >>> test_LocUInt(0)
638    0
639    >>> test_LocUInt(1)
640    1
641    """
642    return x
643
644 # -------------------------------------------------------------------