@@ -249,6 +249,46 @@ def test_resize(self):
249
249
# CRASHES resize(NULL, 0, False)
250
250
# CRASHES resize(NULL, 3, False)
251
251
252
+ def test_join (self ):
253
+ """Test PyBytes_Join()"""
254
+ bytes_join = _testcapi .bytes_join
255
+
256
+ self .assertEqual (bytes_join (b'' , []), b'' )
257
+ self .assertEqual (bytes_join (b'sep' , []), b'' )
258
+
259
+ self .assertEqual (bytes_join (b'' , [b'a' , b'b' , b'c' ]), b'abc' )
260
+ self .assertEqual (bytes_join (b'-' , [b'a' , b'b' , b'c' ]), b'a-b-c' )
261
+ self .assertEqual (bytes_join (b' - ' , [b'a' , b'b' , b'c' ]), b'a - b - c' )
262
+ self .assertEqual (bytes_join (b'-' , [bytearray (b'abc' ),
263
+ memoryview (b'def' )]),
264
+ b'abc-def' )
265
+
266
+ self .assertEqual (bytes_join (b'-' , iter ([b'a' , b'b' , b'c' ])), b'a-b-c' )
267
+
268
+ # invalid 'sep' argument
269
+ with self .assertRaises (TypeError ):
270
+ bytes_join (bytearray (b'sep' ), [])
271
+ with self .assertRaises (TypeError ):
272
+ bytes_join (memoryview (b'sep' ), [])
273
+ with self .assertRaises (TypeError ):
274
+ bytes_join ('' , []) # empty Unicode string
275
+ with self .assertRaises (TypeError ):
276
+ bytes_join ('unicode' , [])
277
+ with self .assertRaises (TypeError ):
278
+ bytes_join (123 , [])
279
+ with self .assertRaises (SystemError ):
280
+ self .assertEqual (bytes_join (NULL , [b'a' , b'b' , b'c' ]), b'abc' )
281
+
282
+ # invalid 'iterable' argument
283
+ with self .assertRaises (TypeError ):
284
+ bytes_join (b'' , [b'bytes' , 'unicode' ])
285
+ with self .assertRaises (TypeError ):
286
+ bytes_join (b'' , [b'bytes' , 123 ])
287
+ with self .assertRaises (TypeError ):
288
+ bytes_join (b'' , 123 )
289
+ with self .assertRaises (SystemError ):
290
+ bytes_join (b'' , NULL )
291
+
252
292
253
293
if __name__ == "__main__" :
254
294
unittest .main ()
0 commit comments