-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtest_bindings.py
More file actions
157 lines (118 loc) · 2.66 KB
/
Copy pathtest_bindings.py
File metadata and controls
157 lines (118 loc) · 2.66 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
from ward import raises, test
from pointers import (
InvalidBindingParameter,
Struct,
StructPointer,
TypedCPointer,
VoidPointer,
)
from pointers import _cstd as std
from pointers import (
binds,
c_free,
c_malloc,
cast,
div,
isspace,
signal,
sprintf,
strcpy,
strlen,
to_c_ptr,
to_struct_ptr,
to_voidp,
toupper,
)
from pointers.std_structs import DivT
@test("c strings")
def _():
ptr = c_malloc(2)
strcpy(ptr, "a")
assert ~cast(ptr, bytes) == b"a"
c_free(ptr)
assert strlen(b"test") == 4
@test("format strings")
def _():
ptr = c_malloc(2)
sprintf(ptr, "%s", "a")
assert ~cast(ptr, bytes) == b"a"
c_free(ptr)
@test("argument validation")
def _():
with raises(InvalidBindingParameter):
strlen(1) # type: ignore
assert strlen("test") == 4
@test("functions")
def _():
def sighandler(signum: int):
...
def bad(signum: str):
...
signal(2, sighandler)
with raises(InvalidBindingParameter):
signal(2, bad) # type: ignore
signal(2, lambda x: ...)
@test("structs")
def _():
res = div(10, 1)
assert type(res) is DivT
assert res.quot == 10
class A(Struct):
one: int
two: int
a = A(1, 2)
class MyStruct(Struct):
a: str
b: str
c: StructPointer[A]
d: TypedCPointer[int]
e: VoidPointer
s = MyStruct(
"a",
"b",
to_struct_ptr(a),
to_c_ptr(1),
to_voidp(
to_c_ptr("hello"),
),
)
assert s.a == "a"
assert type(s.c) is StructPointer
assert type(s.d) is TypedCPointer
assert type(s.e) is VoidPointer
assert (~s.c) is a
assert (~s.c).one == a.one
assert ~s.d == 1
assert ~cast(s.e, str) == "hello"
with raises(TypeError):
class Foo(Struct):
bar: TypedCPointer
@test("custom bindings")
def _():
@binds(std.dll.strlen)
def strlen(a: str):
...
strlen("test")
with raises(InvalidBindingParameter):
strlen(1) # type: ignore
@test("chars")
def _():
assert toupper(97) == "A"
assert toupper("a") == "A"
with raises(InvalidBindingParameter):
isspace("hi")
with raises(InvalidBindingParameter):
isspace("")
assert isspace(" ") != 0
@test("c pointers")
def _():
ptr = to_c_ptr(1)
ptr2 = to_c_ptr("hi")
assert ~ptr == 1
assert ~ptr2 == "hi"
double_ptr = to_c_ptr(to_c_ptr(1))
assert type(~double_ptr) is TypedCPointer
assert ~(~double_ptr) == 1
voidp = to_voidp(to_c_ptr(1))
assert type(voidp) is VoidPointer
assert ~cast(voidp, int) == 1