Adding bytes.join() -- a class methods that concatenates an iterable of bytes.

The name and API are questionable, but the functionality isn't.
This commit is contained in:
Guido van Rossum 2006-05-05 15:15:40 +00:00
parent a0867f79bb
commit 2018831b2b
2 changed files with 89 additions and 2 deletions

View file

@ -347,6 +347,30 @@ class BytesTest(unittest.TestCase):
self.failIf(bytes("dab") in b)
self.failIf(bytes("abd") in b)
def test_alloc(self):
b = bytes()
alloc = b.__alloc__()
self.assert_(alloc >= 0)
seq = [alloc]
for i in range(100):
b += bytes("x")
alloc = b.__alloc__()
self.assert_(alloc >= len(b))
if alloc not in seq:
seq.append(alloc)
print seq
def test_join(self):
self.assertEqual(bytes.join([]), bytes())
self.assertEqual(bytes.join([bytes()]), bytes())
for part in [("abc",), ("a", "bc"), ("ab", "c"), ("a", "b", "c")]:
lst = map(bytes, part)
self.assertEqual(bytes.join(lst), bytes("abc"))
self.assertEqual(bytes.join(tuple(lst)), bytes("abc"))
self.assertEqual(bytes.join(iter(lst)), bytes("abc"))
# XXX more...
# Optimizations:
# __iter__? (optimization)
# __reversed__? (optimization)