| 1 |
import unittest |
| 2 |
from ctypes import (Structure, POINTER, pointer, c_char_p, c_int) |
| 3 |
|
| 4 |
|
| 5 |
class SimpleTestCase(unittest.TestCase): |
| 6 |
def test_cint(self): |
| 7 |
x = c_int() |
| 8 |
self.assertEqual(x._objects, None) |
| 9 |
x.value = 42 |
| 10 |
self.assertEqual(x._objects, None) |
| 11 |
x = c_int(99) |
| 12 |
self.assertEqual(x._objects, None) |
| 13 |
|
| 14 |
def test_ccharp(self): |
| 15 |
x = c_char_p() |
| 16 |
self.assertEqual(x._objects, None) |
| 17 |
x.value = b"abc" |
| 18 |
self.assertEqual(x._objects, b"abc") |
| 19 |
x = c_char_p(b"spam") |
| 20 |
self.assertEqual(x._objects, b"spam") |
| 21 |
|
| 22 |
|
| 23 |
class StructureTestCase(unittest.TestCase): |
| 24 |
def test_cint_struct(self): |
| 25 |
class X(Structure): |
| 26 |
_fields_ = [("a", c_int), |
| 27 |
("b", c_int)] |
| 28 |
|
| 29 |
x = X() |
| 30 |
self.assertEqual(x._objects, None) |
| 31 |
x.a = 42 |
| 32 |
x.b = 99 |
| 33 |
self.assertEqual(x._objects, None) |
| 34 |
|
| 35 |
def test_ccharp_struct(self): |
| 36 |
class X(Structure): |
| 37 |
_fields_ = [("a", c_char_p), |
| 38 |
("b", c_char_p)] |
| 39 |
x = X() |
| 40 |
self.assertEqual(x._objects, None) |
| 41 |
|
| 42 |
x.a = b"spam" |
| 43 |
x.b = b"foo" |
| 44 |
self.assertEqual(x._objects, {"0": b"spam", "1": b"foo"}) |
| 45 |
|
| 46 |
def test_struct_struct(self): |
| 47 |
class POINT(Structure): |
| 48 |
_fields_ = [("x", c_int), ("y", c_int)] |
| 49 |
class RECT(Structure): |
| 50 |
_fields_ = [("ul", POINT), ("lr", POINT)] |
| 51 |
|
| 52 |
r = RECT() |
| 53 |
r.ul.x = 0 |
| 54 |
r.ul.y = 1 |
| 55 |
r.lr.x = 2 |
| 56 |
r.lr.y = 3 |
| 57 |
self.assertEqual(r._objects, None) |
| 58 |
|
| 59 |
r = RECT() |
| 60 |
pt = POINT(1, 2) |
| 61 |
r.ul = pt |
| 62 |
self.assertEqual(r._objects, {'0': {}}) |
| 63 |
r.ul.x = 22 |
| 64 |
r.ul.y = 44 |
| 65 |
self.assertEqual(r._objects, {'0': {}}) |
| 66 |
r.lr = POINT() |
| 67 |
self.assertEqual(r._objects, {'0': {}, '1': {}}) |
| 68 |
|
| 69 |
|
| 70 |
class ArrayTestCase(unittest.TestCase): |
| 71 |
def test_cint_array(self): |
| 72 |
INTARR = c_int * 3 |
| 73 |
|
| 74 |
ia = INTARR() |
| 75 |
self.assertEqual(ia._objects, None) |
| 76 |
ia[0] = 1 |
| 77 |
ia[1] = 2 |
| 78 |
ia[2] = 3 |
| 79 |
self.assertEqual(ia._objects, None) |
| 80 |
|
| 81 |
class X(Structure): |
| 82 |
_fields_ = [("x", c_int), |
| 83 |
("a", INTARR)] |
| 84 |
|
| 85 |
x = X() |
| 86 |
x.x = 1000 |
| 87 |
x.a[0] = 42 |
| 88 |
x.a[1] = 96 |
| 89 |
self.assertEqual(x._objects, None) |
| 90 |
x.a = ia |
| 91 |
self.assertEqual(x._objects, {'1': {}}) |
| 92 |
|
| 93 |
|
| 94 |
class PointerTestCase(unittest.TestCase): |
| 95 |
def test_p_cint(self): |
| 96 |
i = c_int(42) |
| 97 |
x = pointer(i) |
| 98 |
self.assertEqual(x._objects, {'1': i}) |
| 99 |
|
| 100 |
|
| 101 |
class PointerToStructure(unittest.TestCase): |
| 102 |
def test(self): |
| 103 |
class POINT(Structure): |
| 104 |
_fields_ = [("x", c_int), ("y", c_int)] |
| 105 |
class RECT(Structure): |
| 106 |
_fields_ = [("a", POINTER(POINT)), |
| 107 |
("b", POINTER(POINT))] |
| 108 |
r = RECT() |
| 109 |
p1 = POINT(1, 2) |
| 110 |
|
| 111 |
r.a = pointer(p1) |
| 112 |
r.b = pointer(p1) |
| 113 |
|
| 114 |
r.a[0].x = 42 |
| 115 |
r.a[0].y = 99 |
| 116 |
|
| 117 |
|
| 118 |
if __name__ == "__main__": |
| 119 |
unittest.main() |