Skip to content

Commit 1557a28

Browse files
committed
[GR-13202] split and rsplit is missing for bytes and arrays.
PullRequest: graalpython/343
2 parents 5ba3d97 + 7dd3dd9 commit 1557a28

File tree

2 files changed

+561
-2
lines changed

2 files changed

+561
-2
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_bytes.py

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3838
# SOFTWARE.
3939

40+
import unittest
4041

4142
def assert_raises(err, fn, *args, **kwargs):
4243
raised = False
@@ -535,6 +536,80 @@ def test_strip_bytes():
535536
assert b'abc'.lstrip(b'ac') == b'bc'
536537
assert b'abc'.rstrip(b'ac') == b'ab'
537538

539+
class BaseTestSplit:
540+
541+
def test_string_error(self):
542+
self.assertRaises(TypeError, self.type2test(b'a b').split, ' ')
543+
self.assertRaises(TypeError, self.type2test(b'a b').rsplit, ' ')
544+
545+
def test_int_error(self):
546+
self.assertRaises(TypeError, self.type2test(b'a b').split, 32)
547+
self.assertRaises(TypeError, self.type2test(b'a b').rsplit, 32)
548+
549+
def test_split_unicodewhitespace(self):
550+
for b in (b'a\x1Cb', b'a\x1Db', b'a\x1Eb', b'a\x1Fb'):
551+
b = self.type2test(b)
552+
self.assertEqual(b.split(), [b])
553+
b = self.type2test(b"\x09\x0A\x0B\x0C\x0D\x1C\x1D\x1E\x1F")
554+
self.assertEqual(b.split(), [b'\x1c\x1d\x1e\x1f'])
555+
556+
def test_rsplit_unicodewhitespace(self):
557+
b = self.type2test(b"\x09\x0A\x0B\x0C\x0D\x1C\x1D\x1E\x1F")
558+
self.assertEqual(b.rsplit(), [b'\x1c\x1d\x1e\x1f'])
559+
560+
def test_memoryview(self):
561+
self.assertEqual(self.type2test(b'a b').split(memoryview(b' ')), [b'a', b'b'])
562+
self.assertEqual(self.type2test(b'c d').rsplit(memoryview(b' ')), [b'c', b'd'])
563+
564+
def test_split(self):
565+
self.assertEqual(self.type2test(b'ahoj jak\tse\nmas').split(), [b'ahoj', b'jak', b'se', b'mas'])
566+
self.assertEqual(self.type2test(b'ahoj jak\tse\nmas').rsplit(), [b'ahoj', b'jak', b'se', b'mas'])
567+
568+
def test_maxsplit(self):
569+
self.assertEqual(self.type2test(b'ahoj jak\tse\nmas').split(maxsplit=1), [b'ahoj', b'jak\tse\nmas'])
570+
self.assertEqual(self.type2test(b'ahoj jak\tse\nmas').rsplit(maxsplit=1), [b'ahoj jak\tse', b'mas'])
571+
572+
def test_maxsplit_zero(self):
573+
self.assertEqual(self.type2test(b'ahoj jak\tse\nmas').split(maxsplit=0), [b'ahoj jak\tse\nmas'])
574+
self.assertEqual(self.type2test(b'ahoj jak\tse\nmas').rsplit(maxsplit=0), [b'ahoj jak\tse\nmas'])
575+
576+
def test_maxsplit_negative(self):
577+
self.assertEqual(self.type2test(b'ahoj jak\tse\nmas').split(maxsplit=-10), [b'ahoj', b'jak', b'se', b'mas'])
578+
self.assertEqual(self.type2test(b'ahoj jak\tse\nmas').rsplit(maxsplit=-10), [b'ahoj', b'jak', b'se', b'mas'])
579+
580+
def test_separator(self):
581+
self.assertEqual(self.type2test(b'ahoj jak\tse\nmas').split(b' '), [b'ahoj', b'jak\tse\nmas'])
582+
self.assertEqual(self.type2test(b'ahoj jak\tse\nmas').rsplit(b' '), [b'ahoj', b'jak\tse\nmas'])
583+
584+
def test_empty(self):
585+
self.assertEqual(self.type2test(b'').split(), [])
586+
self.assertEqual(self.type2test(b'').rsplit(), [])
587+
588+
def test_empty_delim(self):
589+
self.assertEqual(self.type2test(b'').split(b' '), [b''])
590+
self.assertEqual(self.type2test(b'').rsplit(b' '), [b''])
591+
592+
def test_empty_separator(self):
593+
self.assertRaises(ValueError, self.type2test(b'a b').split, b'')
594+
self.assertRaises(ValueError, self.type2test(b'a b').rsplit, b'')
595+
596+
def test_indexable_object(self):
597+
598+
class MyIndexable(object):
599+
def __init__(self, value):
600+
self.value = value
601+
def __index__(self):
602+
return self.value
603+
604+
self.assertEqual(self.type2test(b'ahoj jak\tse\nmas').split(maxsplit=MyIndexable(1)), [b'ahoj', b'jak\tse\nmas'])
605+
self.assertEqual(self.type2test(b'ahoj jak\tse\nmas').rsplit(maxsplit=MyIndexable(1)), [b'ahoj jak\tse', b'mas'])
606+
607+
class BytesSplitTest(BaseTestSplit, unittest.TestCase):
608+
type2test = bytes
609+
610+
class ByteArraySplitTest(BaseTestSplit, unittest.TestCase):
611+
type2test = bytearray
612+
538613
def test_add_mv_to_bytes():
539614
b = b'hello '
540615
mv = memoryview(b'world')
@@ -546,4 +621,3 @@ def test_add_mv_to_bytearray():
546621
mv = memoryview(b'world')
547622
ba += mv
548623
assert ba == b'hello world'
549-

0 commit comments

Comments
 (0)