circuitpython/tests/basics/bytes_count.py
Jim Mussared 9d6f474ea4 py/objstr: Don't treat bytes as unicode in str.count.
`b'\xaa \xaa'.count(b'\xaa')` now (correctly) returns 2 instead of 1.

Fixes issue #9404.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-09-26 00:54:18 +10:00

62 lines
1.5 KiB
Python

try:
bytes.count
except AttributeError:
print("SKIP")
raise SystemExit
print(b"".count(b""))
print(b"".count(b"a"))
print(b"a".count(b""))
print(b"a".count(b"a"))
print(b"a".count(b"b"))
print(b"b".count(b"a"))
print(b"aaa".count(b""))
print(b"aaa".count(b"a"))
print(b"aaa".count(b"aa"))
print(b"aaa".count(b"aaa"))
print(b"aaa".count(b"aaaa"))
print(b"aaaa".count(b""))
print(b"aaaa".count(b"a"))
print(b"aaaa".count(b"aa"))
print(b"aaaa".count(b"aaa"))
print(b"aaaa".count(b"aaaa"))
print(b"aaaa".count(b"aaaaa"))
print(b"aaa".count(b"", 1))
print(b"aaa".count(b"", 2))
print(b"aaa".count(b"", 3))
print(b"aaa".count(b"", 1, 2))
print(b"asdfasdfaaa".count(b"asdf", -100))
print(b"asdfasdfaaa".count(b"asdf", -8))
print(b"asdf".count(b's', True))
print(b"asdf".count(b'a', True))
print(b"asdf".count(b'a', False))
print(b"asdf".count(b'a', 1 == 2))
print(b"hello world".count(b'l'))
print(b"hello world".count(b'l', 5))
print(b"hello world".count(b'l', 3))
print(b"hello world".count(b'z', 3, 6))
print(b"aaaa".count(b'a'))
print(b"aaaa".count(b'a', 0, 3))
print(b"aaaa".count(b'a', 0, 4))
print(b"aaaa".count(b'a', 0, 5))
print(b"aaaa".count(b'a', 1, 5))
print(b"aaaa".count(b'a', -1, 5))
print(b"abbabba".count(b"abba"))
print(b'\xaa \xaa'.count(b'\xaa'))
print(b'\xaa \xaa \xaa \xaa'.count(b'\xaa'))
print(b'\xaa \xaa \xaa \xaa'.count(b'\xaa'), 1)
print(b'\xaa \xaa \xaa \xaa'.count(b'\xaa'), 2)
print(b'\xaa \xaa \xaa \xaa'.count(b'\xaa'), 1, 3)
print(b'\xaa \xaa \xaa \xaa'.count(b'\xaa'), 2, 3)
def t():
return True
print(b"0000".count(b'0', t()))