6
from libcpp.string cimport string, npos, to_string, stoi, stof
13
cdef int compare_to_asdf_ref(string& s) except -999:
14
return s.compare(b"asdf")
16
def test_coerced_literal_ref():
18
>>> test_coerced_literal_ref()
21
return compare_to_asdf_ref("asdf")
24
cdef int compare_to_asdf_const_ref(const string& s) except -999:
25
return s.compare(b"asdf")
27
def test_coerced_literal_const_ref():
29
>>> test_coerced_literal_const_ref()
32
return compare_to_asdf_const_ref("asdf")
35
cdef int compare_to_asdf_const(const string s) except -999:
36
return s.compare(b"asdf")
38
def test_coerced_literal_const():
40
>>> test_coerced_literal_const()
43
return compare_to_asdf_const("asdf")
46
def test_conversion(py_obj):
48
>>> test_conversion(b_asdf) == b_asdf or test_conversion(b_asdf)
50
>>> test_conversion(123) # doctest: +ELLIPSIS
51
Traceback (most recent call last):
52
TypeError: expected ..., int found
54
cdef string s = py_obj
57
def test_indexing(char *py_str):
59
>>> test_indexing(b_asdf)
64
return chr(s[1]), chr(s.at(1))
66
def test_size(char *py_str):
73
return s.size(), s.length()
75
def test_compare(char *a, char *b):
77
>>> test_compare(b_asdf, b_asdf)
80
>>> test_compare(b_asdf, b_asdg) < 0
83
cdef string s = string(a)
84
cdef string t = string(b)
92
cdef string a = string(<char *>b"")
93
cdef string b = string(<char *>b"aa")
94
return a.empty(), b.empty()
96
def test_push_back(char *a):
98
>>> test_push_back(b_asdf) == b_asdf + b_s
101
cdef string s = string(a)
102
s.push_back(<char>ord('s'))
105
def test_pop_back(char *a):
107
>>> test_pop_back(b'abc') == b'ab' or test_pop_back(b'abc')
110
cdef string s = string(a)
114
def test_insert(char *a, char *b, int i):
116
>>> test_insert('AAAA'.encode('ASCII'), 'BBBB'.encode('ASCII'), 2) == 'AABBBBAA'.encode('ASCII')
119
cdef string s = string(a)
120
cdef string t = string(b)
121
cdef string u = s.insert(i, t)
124
def test_copy(char *a):
126
>>> test_copy(b_asdf) == b_asdf[1:]
129
cdef string t = string(a)
131
cdef size_t length = t.copy(buffer, 4, 1)
132
buffer[length] = c'\0'
135
def test_find(char *a, char *b):
137
>>> test_find(b_asdf, 'df'.encode('ASCII'))
140
cdef string s = string(a)
141
cdef string t = string(b)
142
cdef size_t i = s.find(t)
145
def test_npos(char *a, char *b):
147
>>> test_npos(b'abc', b'x')
149
>>> test_npos(b'abc', b'a')
152
cdef string s = string(a)
153
cdef string st = string(b)
154
return s.find(st) == npos
158
>>> test_clear() == ''.encode('ASCII')
161
cdef string s = string(<char *>"asdf")
165
def test_erase(char *a, size_t pos=0, size_t count=npos):
167
>>> test_erase(b'abc') == b'' or test_erase(b'abc')
169
>>> test_erase(b'abc', 1) == b'a' or test_erase(b'abc', 1)
171
>>> test_erase(b'abc', 1, 1) == b'ac' or test_erase(b'abc', 1, 1)
174
cdef string s = string(a)
175
return s.erase(pos, count)
177
def test_assign(char *a):
179
>>> test_assign(b_asdf) == 'ggg'.encode('ASCII')
182
cdef string s = string(a)
183
s.assign(<char *>"ggg")
187
def test_substr(char *a):
189
>>> test_substr('ABCDEFGH'.encode('ASCII')) == ('BCDEFGH'.encode('ASCII'), 'BCDE'.encode('ASCII'), 'ABCDEFGH'.encode('ASCII'))
192
cdef string s = string(a)
197
return x.c_str(), y.c_str(), z.c_str()
199
def test_replace(char *a, char *b, char* fill):
201
>>> test_replace(b_asdf, b_asdg, b_s) == (b_asdg+b_asdf[2:], b_asdf[:-2]+b_asdg[-2:], (b_s*4)+b_asdg[2:])
204
cdef string s1 = string(a)
205
cdef string s2 = string(a)
206
cdef string s3 = string(b)
208
s2.replace(s2.size()-2, 2, s3, s3.size()-2, 2)
209
s3.replace(0, 2, 4, fill[0])
210
return s1.c_str(), s2.c_str(), s3.c_str()
212
def test_append(char *a, char *b):
214
>>> test_append(b_asdf, '1234'.encode('ASCII')) == b_asdf + '1234'.encode('ASCII')
217
cdef string s = string(a)
218
cdef string t = string(b)
219
cdef string j = s.append(t)
222
def test_char_compare(py_str):
224
>>> test_char_compare(b_asdf)
227
cdef char *a = py_str
228
cdef string b = string(a)
229
return b.compare(b) == 0
231
def test_cstr(char *a):
233
>>> test_cstr(b_asdf) == b_asdf
236
cdef string b = string(a)
239
@cython.test_assert_path_exists("//PythonCapiCallNode")
240
@cython.test_fail_if_path_exists("//AttributeNode")
241
def test_decode(char* a):
243
>>> print(test_decode(b_asdf))
246
cdef string b = string(a)
247
return b.decode('ascii')
250
@cython.test_assert_path_exists("//ReturnStatNode//PythonCapiCallNode")
251
def test_cstr_decode(char* a):
253
>>> print(test_cstr_decode(b_asdf))
256
cdef string b = string(a)
257
return b.c_str().decode('utf-8')
260
@cython.test_assert_path_exists("//ReturnStatNode//PythonCapiCallNode")
261
@cython.test_fail_if_path_exists("//ReturnStatNode//AttributeNode")
262
def test_cstr_ptr_decode(char* a):
264
>>> print(test_cstr_ptr_decode(b_asdf))
267
cdef string b = string(a)
269
return s.decode('utf-8')
272
@cython.test_assert_path_exists("//PythonCapiCallNode")
273
@cython.test_fail_if_path_exists("//AttributeNode")
274
def test_decode_sliced(char* a):
276
>>> print(test_decode_sliced(b_asdf))
279
cdef string b = string(a)
280
return b[1:3].decode('ascii')
282
@cython.test_assert_path_exists("//PythonCapiCallNode")
283
@cython.test_fail_if_path_exists("//AttributeNode")
284
def test_decode_sliced_negative(char* a):
286
>>> a,b,c,d = test_decode_sliced_negative(b_asdf)
296
cdef string b = string(a)
297
return b[-3:-1].decode('ascii'), b[-5:-3].decode('ascii'), b[-20:-4].decode('ascii'), b[-2:-20].decode('ascii')
299
@cython.test_assert_path_exists("//PythonCapiCallNode")
300
@cython.test_fail_if_path_exists("//AttributeNode")
301
def test_decode_sliced_end(char* a):
303
>>> a,b = test_decode_sliced_end(b_asdf)
309
cdef string b = string(a)
310
return b[:3].decode('ascii'), b[:42].decode('ascii')
312
@cython.test_assert_path_exists("//PythonCapiCallNode")
313
@cython.test_fail_if_path_exists("//AttributeNode")
314
def test_decode_sliced_end_negative(char* a):
316
>>> a,b,c = test_decode_sliced_end_negative(b_asdf)
324
cdef string b = string(a)
325
return b[:-1].decode('ascii'), b[:-3].decode('ascii'), b[:-4].decode('ascii')
327
@cython.test_assert_path_exists("//PythonCapiCallNode")
328
@cython.test_fail_if_path_exists("//AttributeNode")
329
def test_decode_sliced_start(char* a):
331
>>> print(test_decode_sliced_start(b_asdf))
334
cdef string b = string(a)
335
return b[2:].decode('ascii')
337
@cython.test_assert_path_exists("//PythonCapiCallNode")
338
@cython.test_fail_if_path_exists("//AttributeNode")
339
def test_decode_sliced_start_negative(char* a):
341
>>> a,b = test_decode_sliced_start_negative(b_asdf)
347
cdef string b = string(a)
348
return b[-2:].decode('ascii'), b[-20:].decode('ascii')
350
def test_equals_operator(char *a, char *b):
352
>>> test_equals_operator(b_asdf, b_asdf)
355
cdef string s = string(a)
356
cdef string t = string(b)
357
return t == s, t != <char *>"asdf"
359
def test_less_than(char *a, char *b):
361
>>> test_less_than(b_asdf[:-1], b_asdf)
364
>>> test_less_than(b_asdf[:-1], b_asdf[:-1])
367
cdef string s = string(a)
368
cdef string t = string(b)
369
return (s < t, s < b, s <= b)
371
def test_greater_than(char *a, char *b):
373
>>> test_greater_than(b_asdf[:-1], b_asdf)
374
(False, False, False)
376
>>> test_greater_than(b_asdf[:-1], b_asdf[:-1])
379
cdef string s = string(a)
380
cdef string t = string(b)
381
return (s > t, s > b, s >= b)
384
def test_iteration(string s):
386
>>> test_iteration(b'xyz')
388
>>> test_iteration(b'')
391
return [c for c in s]
394
def test_to_string(x):
396
>>> print(test_to_string(5))
398
>>> print(test_to_string(-5))
399
si=-5 sl=-5 ss=5 sss=-5
401
si = to_string(<int>x).decode('ascii')
402
sl = to_string(<long>x).decode('ascii')
403
ss = to_string(<size_t>abs(x)).decode('ascii')
404
sss = to_string(<ssize_t>x).decode('ascii')
405
return f"si={si} sl={sl} ss={ss} sss={sss}"
408
def test_stoi(char *a):
413
cdef string s = string(a)
417
def test_stof(char *a):
419
>>> test_stof(b'5.5')
422
cdef string s = string(a)
430
cdef string s1 = b_asdf, s_asdf = b_asdf
431
cdef string s2 = b_asdg, s_asdg = b_asdg
433
assert s1 == s_asdg and s2 == s_asdf
436
def test_float_parsing(bstring):
438
>>> test_float_parsing(b'0.5')
440
>>> try: test_float_parsing(b'xxx')
441
... except ValueError: pass
442
... else: print("NOT RAISED!")
443
>>> try: test_float_parsing(b'')
444
... except ValueError: pass
445
... else: print("NOT RAISED!")
447
cdef string s = bstring
452
21:31: Cannot pass Python object as C++ data structure reference (string &), will pass by copy.