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