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

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

#countcontent
1n/aimport unittest
2n/afrom ctypes import *
3n/afrom ctypes.test import need_symbol
4n/a
5n/aclass StringArrayTestCase(unittest.TestCase):
6n/a def test(self):
7n/a BUF = c_char * 4
8n/a
9n/a buf = BUF(b"a", b"b", b"c")
10n/a self.assertEqual(buf.value, b"abc")
11n/a self.assertEqual(buf.raw, b"abc\000")
12n/a
13n/a buf.value = b"ABCD"
14n/a self.assertEqual(buf.value, b"ABCD")
15n/a self.assertEqual(buf.raw, b"ABCD")
16n/a
17n/a buf.value = b"x"
18n/a self.assertEqual(buf.value, b"x")
19n/a self.assertEqual(buf.raw, b"x\000CD")
20n/a
21n/a buf[1] = b"Z"
22n/a self.assertEqual(buf.value, b"xZCD")
23n/a self.assertEqual(buf.raw, b"xZCD")
24n/a
25n/a self.assertRaises(ValueError, setattr, buf, "value", b"aaaaaaaa")
26n/a self.assertRaises(TypeError, setattr, buf, "value", 42)
27n/a
28n/a def test_c_buffer_value(self):
29n/a buf = c_buffer(32)
30n/a
31n/a buf.value = b"Hello, World"
32n/a self.assertEqual(buf.value, b"Hello, World")
33n/a
34n/a self.assertRaises(TypeError, setattr, buf, "value", memoryview(b"Hello, World"))
35n/a self.assertRaises(TypeError, setattr, buf, "value", memoryview(b"abc"))
36n/a self.assertRaises(ValueError, setattr, buf, "raw", memoryview(b"x" * 100))
37n/a
38n/a def test_c_buffer_raw(self):
39n/a buf = c_buffer(32)
40n/a
41n/a buf.raw = memoryview(b"Hello, World")
42n/a self.assertEqual(buf.value, b"Hello, World")
43n/a self.assertRaises(TypeError, setattr, buf, "value", memoryview(b"abc"))
44n/a self.assertRaises(ValueError, setattr, buf, "raw", memoryview(b"x" * 100))
45n/a
46n/a def test_param_1(self):
47n/a BUF = c_char * 4
48n/a buf = BUF()
49n/a## print c_char_p.from_param(buf)
50n/a
51n/a def test_param_2(self):
52n/a BUF = c_char * 4
53n/a buf = BUF()
54n/a## print BUF.from_param(c_char_p("python"))
55n/a## print BUF.from_param(BUF(*"pyth"))
56n/a
57n/a@need_symbol('c_wchar')
58n/aclass WStringArrayTestCase(unittest.TestCase):
59n/a def test(self):
60n/a BUF = c_wchar * 4
61n/a
62n/a buf = BUF("a", "b", "c")
63n/a self.assertEqual(buf.value, "abc")
64n/a
65n/a buf.value = "ABCD"
66n/a self.assertEqual(buf.value, "ABCD")
67n/a
68n/a buf.value = "x"
69n/a self.assertEqual(buf.value, "x")
70n/a
71n/a buf[1] = "Z"
72n/a self.assertEqual(buf.value, "xZCD")
73n/a
74n/a @unittest.skipIf(sizeof(c_wchar) < 4,
75n/a "sizeof(wchar_t) is smaller than 4 bytes")
76n/a def test_nonbmp(self):
77n/a u = chr(0x10ffff)
78n/a w = c_wchar(u)
79n/a self.assertEqual(w.value, u)
80n/a
81n/aclass StringTestCase(unittest.TestCase):
82n/a @unittest.skip('test disabled')
83n/a def test_basic_strings(self):
84n/a cs = c_string("abcdef")
85n/a
86n/a # Cannot call len on a c_string any longer
87n/a self.assertRaises(TypeError, len, cs)
88n/a self.assertEqual(sizeof(cs), 7)
89n/a
90n/a # The value property is the string up to the first terminating NUL.
91n/a self.assertEqual(cs.value, "abcdef")
92n/a self.assertEqual(c_string("abc\000def").value, "abc")
93n/a
94n/a # The raw property is the total buffer contents:
95n/a self.assertEqual(cs.raw, "abcdef\000")
96n/a self.assertEqual(c_string("abc\000def").raw, "abc\000def\000")
97n/a
98n/a # We can change the value:
99n/a cs.value = "ab"
100n/a self.assertEqual(cs.value, "ab")
101n/a self.assertEqual(cs.raw, "ab\000\000\000\000\000")
102n/a
103n/a cs.raw = "XY"
104n/a self.assertEqual(cs.value, "XY")
105n/a self.assertEqual(cs.raw, "XY\000\000\000\000\000")
106n/a
107n/a self.assertRaises(TypeError, c_string, "123")
108n/a
109n/a @unittest.skip('test disabled')
110n/a def test_sized_strings(self):
111n/a
112n/a # New in releases later than 0.4.0:
113n/a self.assertRaises(TypeError, c_string, None)
114n/a
115n/a # New in releases later than 0.4.0:
116n/a # c_string(number) returns an empty string of size number
117n/a self.assertEqual(len(c_string(32).raw), 32)
118n/a self.assertRaises(ValueError, c_string, -1)
119n/a self.assertRaises(ValueError, c_string, 0)
120n/a
121n/a # These tests fail, because it is no longer initialized
122n/a## self.assertEqual(c_string(2).value, "")
123n/a## self.assertEqual(c_string(2).raw, "\000\000")
124n/a self.assertEqual(c_string(2).raw[-1], "\000")
125n/a self.assertEqual(len(c_string(2).raw), 2)
126n/a
127n/a @unittest.skip('test disabled')
128n/a def test_initialized_strings(self):
129n/a
130n/a self.assertEqual(c_string("ab", 4).raw[:2], "ab")
131n/a self.assertEqual(c_string("ab", 4).raw[:2:], "ab")
132n/a self.assertEqual(c_string("ab", 4).raw[:2:-1], "ba")
133n/a self.assertEqual(c_string("ab", 4).raw[:2:2], "a")
134n/a self.assertEqual(c_string("ab", 4).raw[-1], "\000")
135n/a self.assertEqual(c_string("ab", 2).raw, "a\000")
136n/a
137n/a @unittest.skip('test disabled')
138n/a def test_toolong(self):
139n/a cs = c_string("abcdef")
140n/a # Much too long string:
141n/a self.assertRaises(ValueError, setattr, cs, "value", "123456789012345")
142n/a
143n/a # One char too long values:
144n/a self.assertRaises(ValueError, setattr, cs, "value", "1234567")
145n/a
146n/a @unittest.skip('test disabled')
147n/a def test_perf(self):
148n/a check_perf()
149n/a
150n/a@need_symbol('c_wchar')
151n/aclass WStringTestCase(unittest.TestCase):
152n/a def test_wchar(self):
153n/a c_wchar("x")
154n/a repr(byref(c_wchar("x")))
155n/a c_wchar("x")
156n/a
157n/a
158n/a @unittest.skip('test disabled')
159n/a def test_basic_wstrings(self):
160n/a cs = c_wstring("abcdef")
161n/a
162n/a # XXX This behaviour is about to change:
163n/a # len returns the size of the internal buffer in bytes.
164n/a # This includes the terminating NUL character.
165n/a self.assertEqual(sizeof(cs), 14)
166n/a
167n/a # The value property is the string up to the first terminating NUL.
168n/a self.assertEqual(cs.value, "abcdef")
169n/a self.assertEqual(c_wstring("abc\000def").value, "abc")
170n/a
171n/a self.assertEqual(c_wstring("abc\000def").value, "abc")
172n/a
173n/a # The raw property is the total buffer contents:
174n/a self.assertEqual(cs.raw, "abcdef\000")
175n/a self.assertEqual(c_wstring("abc\000def").raw, "abc\000def\000")
176n/a
177n/a # We can change the value:
178n/a cs.value = "ab"
179n/a self.assertEqual(cs.value, "ab")
180n/a self.assertEqual(cs.raw, "ab\000\000\000\000\000")
181n/a
182n/a self.assertRaises(TypeError, c_wstring, "123")
183n/a self.assertRaises(ValueError, c_wstring, 0)
184n/a
185n/a @unittest.skip('test disabled')
186n/a def test_toolong(self):
187n/a cs = c_wstring("abcdef")
188n/a # Much too long string:
189n/a self.assertRaises(ValueError, setattr, cs, "value", "123456789012345")
190n/a
191n/a # One char too long values:
192n/a self.assertRaises(ValueError, setattr, cs, "value", "1234567")
193n/a
194n/a
195n/adef run_test(rep, msg, func, arg):
196n/a items = range(rep)
197n/a from time import clock
198n/a start = clock()
199n/a for i in items:
200n/a func(arg); func(arg); func(arg); func(arg); func(arg)
201n/a stop = clock()
202n/a print("%20s: %.2f us" % (msg, ((stop-start)*1e6/5/rep)))
203n/a
204n/adef check_perf():
205n/a # Construct 5 objects
206n/a
207n/a REP = 200000
208n/a
209n/a run_test(REP, "c_string(None)", c_string, None)
210n/a run_test(REP, "c_string('abc')", c_string, 'abc')
211n/a
212n/a# Python 2.3 -OO, win2k, P4 700 MHz:
213n/a#
214n/a# c_string(None): 1.75 us
215n/a# c_string('abc'): 2.74 us
216n/a
217n/a# Python 2.2 -OO, win2k, P4 700 MHz:
218n/a#
219n/a# c_string(None): 2.95 us
220n/a# c_string('abc'): 3.67 us
221n/a
222n/a
223n/aif __name__ == '__main__':
224n/a## check_perf()
225n/a unittest.main()