4 class StringArrayTestCase(unittest
.TestCase
):
8 buf
= BUF("a", "b", "c")
9 self
.assertEqual(buf
.value
, "abc")
10 self
.assertEqual(buf
.raw
, "abc\000")
13 self
.assertEqual(buf
.value
, "ABCD")
14 self
.assertEqual(buf
.raw
, "ABCD")
17 self
.assertEqual(buf
.value
, "x")
18 self
.assertEqual(buf
.raw
, "x\000CD")
21 self
.assertEqual(buf
.value
, "xZCD")
22 self
.assertEqual(buf
.raw
, "xZCD")
24 self
.assertRaises(ValueError, setattr, buf
, "value", "aaaaaaaa")
25 self
.assertRaises(TypeError, setattr, buf
, "value", 42)
27 def test_c_buffer_value(self
):
30 buf
.value
= "Hello, World"
31 self
.assertEqual(buf
.value
, "Hello, World")
33 self
.assertRaises(TypeError, setattr, buf
, "value", buffer("Hello, World"))
34 self
.assertRaises(TypeError, setattr, buf
, "value", buffer("abc"))
35 self
.assertRaises(ValueError, setattr, buf
, "raw", buffer("x" * 100))
37 def test_c_buffer_raw(self
):
40 buf
.raw
= buffer("Hello, World")
41 self
.assertEqual(buf
.value
, "Hello, World")
42 self
.assertRaises(TypeError, setattr, buf
, "value", buffer("abc"))
43 self
.assertRaises(ValueError, setattr, buf
, "raw", buffer("x" * 100))
45 def test_param_1(self
):
48 ## print c_char_p.from_param(buf)
50 def test_param_2(self
):
53 ## print BUF.from_param(c_char_p("python"))
54 ## print BUF.from_param(BUF(*"pyth"))
61 class WStringArrayTestCase(unittest
.TestCase
):
65 buf
= BUF(u
"a", u
"b", u
"c")
66 self
.assertEqual(buf
.value
, u
"abc")
69 self
.assertEqual(buf
.value
, u
"ABCD")
72 self
.assertEqual(buf
.value
, u
"x")
75 self
.assertEqual(buf
.value
, u
"xZCD")
77 class StringTestCase(unittest
.TestCase
):
78 def XX_test_basic_strings(self
):
79 cs
= c_string("abcdef")
81 # Cannot call len on a c_string any longer
82 self
.assertRaises(TypeError, len, cs
)
83 self
.assertEqual(sizeof(cs
), 7)
85 # The value property is the string up to the first terminating NUL.
86 self
.assertEqual(cs
.value
, "abcdef")
87 self
.assertEqual(c_string("abc\000def").value
, "abc")
89 # The raw property is the total buffer contents:
90 self
.assertEqual(cs
.raw
, "abcdef\000")
91 self
.assertEqual(c_string("abc\000def").raw
, "abc\000def\000")
93 # We can change the value:
95 self
.assertEqual(cs
.value
, "ab")
96 self
.assertEqual(cs
.raw
, "ab\000\000\000\000\000")
99 self
.assertEqual(cs
.value
, "XY")
100 self
.assertEqual(cs
.raw
, "XY\000\000\000\000\000")
102 self
.assertRaises(TypeError, c_string
, u
"123")
104 def XX_test_sized_strings(self
):
106 # New in releases later than 0.4.0:
107 self
.assertRaises(TypeError, c_string
, None)
109 # New in releases later than 0.4.0:
110 # c_string(number) returns an empty string of size number
111 self
.assertTrue(len(c_string(32).raw
) == 32)
112 self
.assertRaises(ValueError, c_string
, -1)
113 self
.assertRaises(ValueError, c_string
, 0)
115 # These tests fail, because it is no longer initialized
116 ## self.assertTrue(c_string(2).value == "")
117 ## self.assertTrue(c_string(2).raw == "\000\000")
118 self
.assertTrue(c_string(2).raw
[-1] == "\000")
119 self
.assertTrue(len(c_string(2).raw
) == 2)
121 def XX_test_initialized_strings(self
):
123 self
.assertTrue(c_string("ab", 4).raw
[:2] == "ab")
124 self
.assertTrue(c_string("ab", 4).raw
[:2:] == "ab")
125 self
.assertTrue(c_string("ab", 4).raw
[:2:-1] == "ba")
126 self
.assertTrue(c_string("ab", 4).raw
[:2:2] == "a")
127 self
.assertTrue(c_string("ab", 4).raw
[-1] == "\000")
128 self
.assertTrue(c_string("ab", 2).raw
== "a\000")
130 def XX_test_toolong(self
):
131 cs
= c_string("abcdef")
132 # Much too long string:
133 self
.assertRaises(ValueError, setattr, cs
, "value", "123456789012345")
135 # One char too long values:
136 self
.assertRaises(ValueError, setattr, cs
, "value", "1234567")
138 ## def test_perf(self):
146 class WStringTestCase(unittest
.TestCase
):
147 def test_wchar(self
):
149 repr(byref(c_wchar(u
"x")))
153 def X_test_basic_wstrings(self
):
154 cs
= c_wstring(u
"abcdef")
156 # XXX This behaviour is about to change:
157 # len returns the size of the internal buffer in bytes.
158 # This includes the terminating NUL character.
159 self
.assertTrue(sizeof(cs
) == 14)
161 # The value property is the string up to the first terminating NUL.
162 self
.assertTrue(cs
.value
== u
"abcdef")
163 self
.assertTrue(c_wstring(u
"abc\000def").value
== u
"abc")
165 self
.assertTrue(c_wstring(u
"abc\000def").value
== u
"abc")
167 # The raw property is the total buffer contents:
168 self
.assertTrue(cs
.raw
== u
"abcdef\000")
169 self
.assertTrue(c_wstring(u
"abc\000def").raw
== u
"abc\000def\000")
171 # We can change the value:
173 self
.assertTrue(cs
.value
== u
"ab")
174 self
.assertTrue(cs
.raw
== u
"ab\000\000\000\000\000")
176 self
.assertRaises(TypeError, c_wstring
, "123")
177 self
.assertRaises(ValueError, c_wstring
, 0)
179 def X_test_toolong(self
):
180 cs
= c_wstring(u
"abcdef")
181 # Much too long string:
182 self
.assertRaises(ValueError, setattr, cs
, "value", u
"123456789012345")
184 # One char too long values:
185 self
.assertRaises(ValueError, setattr, cs
, "value", u
"1234567")
188 def run_test(rep
, msg
, func
, arg
):
190 from time
import clock
193 func(arg
); func(arg
); func(arg
); func(arg
); func(arg
)
195 print "%20s: %.2f us" % (msg
, ((stop
-start
)*1e6
/5/rep
))
198 # Construct 5 objects
202 run_test(REP
, "c_string(None)", c_string
, None)
203 run_test(REP
, "c_string('abc')", c_string
, 'abc')
205 # Python 2.3 -OO, win2k, P4 700 MHz:
207 # c_string(None): 1.75 us
208 # c_string('abc'): 2.74 us
210 # Python 2.2 -OO, win2k, P4 700 MHz:
212 # c_string(None): 2.95 us
213 # c_string('abc'): 3.67 us
216 if __name__
== '__main__':