Debian Codegraph @

extra_tests/ctypes_tests/test_array.py

raw symbols SCIP
1 import pytest
2 from ctypes import *
3
4 def test_slice():
5 values = list(range(5))
6 numarray = c_int * 5
7
8 na = numarray(*(c_int(x) for x in values))
9
10 assert list(na[0:0]) == []
11 assert list(na[:]) == values
12 assert list(na[:10]) == values
13
14 def test_init_again():
15 sz = (c_char * 3)()
16 addr1 = addressof(sz)
17 sz.__init__(*b"foo")
18 addr2 = addressof(sz)
19 assert addr1 == addr2
20
21 def test_array_of_structures():
22 class X(Structure):
23 _fields_ = [('x', c_int), ('y', c_int)]
24
25 Y = X * 2
26 y = Y()
27 x = X()
28 x.y = 3
29 y[1] = x
30 assert y[1].y == 3
31
32 def test_output_simple():
33 A = c_char * 10
34 TP = POINTER(A)
35 x = TP(A())
36 assert x[0] != b''
37
38 A = c_wchar * 10
39 TP = POINTER(A)
40 x = TP(A())
41 assert x[0] != b''
42
43 def test_output_simple_array():
44 A = c_char * 10
45 AA = A * 10
46 aa = AA()
47 assert aa[0] != b''
48
49 def test_output_complex_test():
50 class Car(Structure):
51 _fields_ = [("brand", c_char * 10),
52 ("speed", c_float),
53 ("owner", c_char * 10)]
54
55 assert isinstance(Car(b"abcdefghi", 42.0, b"12345").brand, bytes)
56 assert Car(b"abcdefghi", 42.0, b"12345").brand == b"abcdefghi"
57 assert Car(b"abcdefghio", 42.0, b"12345").brand == b"abcdefghio"
58 with pytest.raises(ValueError):
59 Car(b"abcdefghiop", 42.0, b"12345")
60
61 A = Car._fields_[2][1]
62 TP = POINTER(A)
63 x = TP(A())
64 assert x[0] != b''
65
66 def test_non_int():
67 class Index():
68 def __index__(self):
69 return 42
70
71 t0 = c_int * 42
72 t1 = c_int * Index()
73 assert t0 == t1
74 with pytest.raises(TypeError) as exc:
75 c_int * 4.5
76 assert 'non-int of type' in str(exc.value) and 'float' in str(exc.value)