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