summaryrefslogtreecommitdiff
path: root/tests/run/cunion.pyx
blob: 7f1a86202db8a4c7a3b7093e271ed413ced7f9c1 (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
cdef union Spam:
    int i
    char c
    float *p[42]

cdef Spam spam, ham

cdef void eggs_i(Spam s):
    cdef int j
    j = s.i
    s.i = j

cdef void eggs_c(Spam s):
    cdef char c
    c = s.c
    s.c = c

cdef void eggs_p(Spam s):
    cdef float *p
    p = s.p[0]
    s.p[0] = p

spam = ham


def test_i():
    """
    >>> test_i()
    """
    spam.i = 1
    eggs_i(spam)


def test_c():
    """
    >>> test_c()
    """
    spam.c = c'a'
    eggs_c(spam)


def test_p():
    """
    >>> test_p()
    """
    cdef float f
    spam.p[0] = &f
    eggs_p(spam)


cdef union AllCharptr:
    char* s1
    char* s2
    char* s3


def test_charptr_to_py():
    """
    >>> result = test_charptr_to_py()
    >>> len(result)
    3
    >>> result['s1'] == b'abc'
    True
    >>> result['s2'] == b'abc'
    True
    >>> result['s3'] == b'abc'
    True
    """
    cdef AllCharptr u
    u.s1 = b"abc"
    return u


cdef union SafeMix:
    char c
    unsigned char uc
    signed char sc
    short w
    int i
    long l
    size_t z
    float f
    double d


def test_safe_type_mix_from_to_py(v):
    """
    >>> test_safe_type_mix_from_to_py({'l': 32, 'c': 32})
    Traceback (most recent call last):
    ValueError: More than one union attribute passed: 'c' and 'l'

    >>> result = test_safe_type_mix_from_to_py({'c': 32})
    >>> sorted(result)
    ['c', 'd', 'f', 'i', 'l', 'sc', 'uc', 'w', 'z']
    >>> result['c']
    32
    >>> result['z'] != 0
    True

    >>> result = test_safe_type_mix_from_to_py({'uc': 32})
    >>> len(result)
    9
    >>> result['uc']
    32

    >>> result = test_safe_type_mix_from_to_py({'l': 100})
    >>> result['l']
    100

    >>> result = test_safe_type_mix_from_to_py({'z': 0})
    >>> result['z']
    0
    >>> result['i']
    0
    >>> result['l']
    0

    >>> result = test_safe_type_mix_from_to_py({'d': 2**52 - 1})
    >>> result['d']
    4503599627370495.0
    >>> result['z'] != 0
    True
    """
    cdef SafeMix u = v
    return u