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