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