1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
# mode: run
# tag: cpp, werror
# cython: c_string_encoding=ascii, c_string_type=unicode
from libcpp.string cimport string
from libcpp.vector cimport vector
b_asdf = b'asdf'
s_asdf = 'asdf'
u_asdf = u'asdf'
u_s = u's'
def test_conversion(py_obj):
"""
>>> test_conversion(b_asdf) == u_asdf or test_conversion(b_asdf)
True
>>> test_conversion(u_asdf) == u_asdf or test_conversion(u_asdf)
True
>>> test_conversion(123) # doctest: +ELLIPSIS
Traceback (most recent call last):
TypeError: expected ..., int found
"""
cdef string s = py_obj
assert <size_t>len(py_obj) == s.length(), '%d != %d' % (len(py_obj), s.length())
return s
def test_empty(py_obj):
"""
>>> test_empty('')
True
>>> test_empty('abc')
False
>>> test_empty(u_asdf[:0])
True
>>> test_empty(u_asdf)
False
"""
cdef string a = py_obj
return a.empty()
def test_push_back(a):
"""
>>> test_push_back(b_asdf) == u_asdf + u_s
True
>>> test_push_back(u_asdf) == u_asdf + u_s
True
"""
cdef string s = a
s.push_back(<char>ord('s'))
return s
def test_clear(a):
"""
>>> test_clear(u_asdf) == u_s[:0]
True
>>> test_clear(b_asdf) == u_s[:0]
True
"""
cdef string s = a
s.clear()
return s
def test_assign(char *a):
"""
>>> test_assign(b_asdf) == 'ggg'
True
"""
cdef string s = string(a)
s.assign(<char *>"ggg")
return s.c_str()
def test_bytes_cast(a):
"""
>>> b = test_bytes_cast(b'abc')
>>> isinstance(b, bytes)
True
>>> print(b.decode('ascii'))
abc
>>> b = test_bytes_cast(b'abc\\xe4\\xfc')
>>> isinstance(b, bytes)
True
>>> len(b)
5
>>> print(b[:3].decode('ascii'))
abc
>>> print(ord(b[3:4]))
228
>>> print(ord(b[4:5]))
252
"""
cdef string s = a
assert s.length() == <size_t>len(a), "%d != %d" % (s.length(), len(a))
return <bytes>s
def test_bytearray_cast(a):
"""
>>> b = test_bytearray_cast(b'abc')
>>> isinstance(b, bytearray)
True
>>> print(b.decode('ascii'))
abc
>>> b = test_bytearray_cast(b'abc\\xe4\\xfc')
>>> isinstance(b, bytearray)
True
>>> len(b)
5
>>> print(b[:3].decode('ascii'))
abc
>>> print(ord(b[3:4]))
228
>>> print(ord(b[4:5]))
252
"""
cdef string s = a
assert s.length() == <size_t>len(a), "%d != %d" % (s.length(), len(a))
return <bytearray>s
def test_unicode_cast(a):
"""
>>> u = test_unicode_cast(b'abc')
>>> type(u) is type(u_asdf) or type(u)
True
>>> print(u)
abc
"""
cdef string s = a
assert s.length() == <size_t>len(a), "%d != %d" % (s.length(), len(a))
return <unicode>s
def test_str_cast(a):
"""
>>> s = test_str_cast(b'abc')
>>> type(s) is type(s_asdf) or type(s)
True
>>> print(s)
abc
"""
cdef string s = a
assert s.length() == <size_t>len(a), "%d != %d" % (s.length(), len(a))
return <str>s
def test_vector_of_strings(*strings):
"""
>>> results = test_vector_of_strings(b_asdf, u_asdf)
>>> results == [u_asdf, u_asdf] or results
True
>>> type(results[0]) is type(u_asdf) or type(results[0])
True
>>> type(results[1]) is type(u_asdf) or type(results[1])
True
"""
cdef vector[string] v = strings
return v
|