ยปCore Development>Code coverage>Lib/ctypes/test/test_libc.py

Python code coverage for Lib/ctypes/test/test_libc.py

#countcontent
1n/aimport unittest
2n/a
3n/afrom ctypes import *
4n/aimport _ctypes_test
5n/a
6n/alib = CDLL(_ctypes_test.__file__)
7n/a
8n/adef three_way_cmp(x, y):
9n/a """Return -1 if x < y, 0 if x == y and 1 if x > y"""
10n/a return (x > y) - (x < y)
11n/a
12n/aclass LibTest(unittest.TestCase):
13n/a def test_sqrt(self):
14n/a lib.my_sqrt.argtypes = c_double,
15n/a lib.my_sqrt.restype = c_double
16n/a self.assertEqual(lib.my_sqrt(4.0), 2.0)
17n/a import math
18n/a self.assertEqual(lib.my_sqrt(2.0), math.sqrt(2.0))
19n/a
20n/a def test_qsort(self):
21n/a comparefunc = CFUNCTYPE(c_int, POINTER(c_char), POINTER(c_char))
22n/a lib.my_qsort.argtypes = c_void_p, c_size_t, c_size_t, comparefunc
23n/a lib.my_qsort.restype = None
24n/a
25n/a def sort(a, b):
26n/a return three_way_cmp(a[0], b[0])
27n/a
28n/a chars = create_string_buffer(b"spam, spam, and spam")
29n/a lib.my_qsort(chars, len(chars)-1, sizeof(c_char), comparefunc(sort))
30n/a self.assertEqual(chars.raw, b" ,,aaaadmmmnpppsss\x00")
31n/a
32n/aif __name__ == "__main__":
33n/a unittest.main()