PageRenderTime 42ms CodeModel.GetById 13ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

/Lib/ctypes/test/test_strings.py

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