Debian Codegraph @

Lib/test/test_ctypes/test_wintypes.py

raw
1 # See <https://learn.microsoft.com/en-us/windows/win32/winprog/windows-data-types>
2 # for reference.
3 #
4 # Tests also work on POSIX
5
6 import unittest
7 from ctypes import POINTER, cast, c_int16
8 from ctypes import wintypes
9
10
11 class WinTypesTest(unittest.TestCase):
12 def test_variant_bool(self):
13 # reads 16-bits from memory, anything non-zero is True
14 for true_value in (1, 32767, 32768, 65535, 65537):
15 true = POINTER(c_int16)(c_int16(true_value))
16 value = cast(true, POINTER(wintypes.VARIANT_BOOL))
17 self.assertEqual(repr(value.contents), 'VARIANT_BOOL(True)')
18
19 vb = wintypes.VARIANT_BOOL()
20 self.assertIs(vb.value, False)
21 vb.value = True
22 self.assertIs(vb.value, True)
23 vb.value = true_value
24 self.assertIs(vb.value, True)
25
26 for false_value in (0, 65536, 262144, 2**33):
27 false = POINTER(c_int16)(c_int16(false_value))
28 value = cast(false, POINTER(wintypes.VARIANT_BOOL))
29 self.assertEqual(repr(value.contents), 'VARIANT_BOOL(False)')
30
31 # allow any bool conversion on assignment to value
32 for set_value in (65536, 262144, 2**33):
33 vb = wintypes.VARIANT_BOOL()
34 vb.value = set_value
35 self.assertIs(vb.value, True)
36
37 vb = wintypes.VARIANT_BOOL()
38 vb.value = [2, 3]
39 self.assertIs(vb.value, True)
40 vb.value = []
41 self.assertIs(vb.value, False)
42
43 def assertIsSigned(self, ctype):
44 self.assertLess(ctype(-1).value, 0)
45
46 def assertIsUnsigned(self, ctype):
47 self.assertGreater(ctype(-1).value, 0)
48
49 def test_signedness(self):
50 for ctype in (wintypes.BYTE, wintypes.WORD, wintypes.DWORD,
51 wintypes.BOOLEAN, wintypes.UINT, wintypes.ULONG):
52 with self.subTest(ctype=ctype):
53 self.assertIsUnsigned(ctype)
54
55 for ctype in (wintypes.BOOL, wintypes.INT, wintypes.LONG):
56 with self.subTest(ctype=ctype):
57 self.assertIsSigned(ctype)
58
59
60 if __name__ == "__main__":
61 unittest.main()