Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix
  • Loading branch information
XChaitanyaX committed Nov 25, 2025
commit 4f47367a0801b18cb92a6a214899e3a3fc39cba0
72 changes: 23 additions & 49 deletions Lib/test/string_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ def checkcall(self, obj, methodname, *args):
args = self.fixtype(args)
getattr(obj, methodname)(*args)

def _get_teststrings(self, charset, digits):
base = len(charset)
teststrings = set()
for i in range(base ** digits):
entry = []
for j in range(digits):
i, m = divmod(i, base)
entry.append(charset[m])
teststrings.add(''.join(entry))
teststrings = [self.fixtype(ts) for ts in teststrings]
return teststrings

def test_count(self):
self.checkequal(3, 'aaa', 'count', 'a')
self.checkequal(0, 'aaa', 'count', 'b')
Expand Down Expand Up @@ -132,15 +144,7 @@ def test_count(self):
# replacing all occurrences and then differencing the string lengths
charset = ['', 'a', 'b']
digits = 7
base = len(charset)
teststrings = set()
for i in range(base ** digits):
entry = []
for j in range(digits):
i, m = divmod(i, base)
entry.append(charset[m])
teststrings.add(''.join(entry))
teststrings = [self.fixtype(ts) for ts in teststrings]
teststrings = self._get_teststrings(charset, digits)
Comment thread
XChaitanyaX marked this conversation as resolved.
Outdated
for i in teststrings:
n = len(i)
for j in teststrings:
Expand Down Expand Up @@ -199,15 +203,7 @@ def test_find(self):
# and that the found substring is really at that location
charset = ['', 'a', 'b', 'c']
digits = 5
base = len(charset)
teststrings = set()
for i in range(base ** digits):
entry = []
for j in range(digits):
i, m = divmod(i, base)
entry.append(charset[m])
teststrings.add(''.join(entry))
teststrings = [self.fixtype(ts) for ts in teststrings]
teststrings = self._get_teststrings(charset, digits)
for i in teststrings:
for j in teststrings:
loc = i.find(j)
Expand Down Expand Up @@ -246,15 +242,7 @@ def test_rfind(self):
# and that the found substring is really at that location
charset = ['', 'a', 'b', 'c']
digits = 5
base = len(charset)
teststrings = set()
for i in range(base ** digits):
entry = []
for j in range(digits):
i, m = divmod(i, base)
entry.append(charset[m])
teststrings.add(''.join(entry))
teststrings = [self.fixtype(ts) for ts in teststrings]
teststrings = self._get_teststrings(charset, digits)
for i in teststrings:
for j in teststrings:
loc = i.rfind(j)
Expand Down Expand Up @@ -300,20 +288,13 @@ def test_index(self):
# and that the found substring is really at that location
charset = ['', 'a', 'b', 'c']
digits = 5
base = len(charset)
teststrings = set()
for i in range(base ** digits):
entry = []
for j in range(digits):
i, m = divmod(i, base)
entry.append(charset[m])
teststrings.add(''.join(entry))
teststrings = [self.fixtype(ts) for ts in teststrings]
teststrings = self._get_teststrings(charset, digits)
for i in teststrings:
for j in teststrings:
try:
if j in i:
loc = i.index(j)
except ValueError:
else:
self.assertRaises(ValueError, i.index, j)
loc = -1
r1 = (loc != -1)
r2 = j in i
Expand Down Expand Up @@ -352,20 +333,13 @@ def test_rindex(self):
# and that the found substring is really at that location
charset = ['', 'a', 'b', 'c']
digits = 5
base = len(charset)
teststrings = set()
for i in range(base ** digits):
entry = []
for j in range(digits):
i, m = divmod(i, base)
entry.append(charset[m])
teststrings.add(''.join(entry))
teststrings = [self.fixtype(ts) for ts in teststrings]
teststrings = self._get_teststrings(charset, digits)
for i in teststrings:
for j in teststrings:
try:
if j in i:
loc = i.rindex(j)
except ValueError:
else:
self.assertRaises(ValueError, i.rindex, j)
loc = -1
r1 = (loc != -1)
r2 = j in i
Expand Down
Loading