Skip to content

Test failure in test_find_periodic_pattern in string_tests.py #105639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sweeneyde opened this issue Jun 10, 2023 · 2 comments
Closed

Test failure in test_find_periodic_pattern in string_tests.py #105639

sweeneyde opened this issue Jun 10, 2023 · 2 comments
Labels
tests Tests in the Lib/test dir type-bug An unexpected behavior, bug, or error

Comments

@sweeneyde
Copy link
Member

sweeneyde commented Jun 10, 2023

A recent test failure:

FAIL: test_find_periodic_pattern (test.test_bytes.ByteArrayAsStringTest.test_find_periodic_pattern) (p='', text='')
Cover the special path for periodic patterns.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/work/cpython/cpython-ro-srcdir/Lib/test/string_tests.py", line 335, in test_find_periodic_pattern
    self.checkequal(reference_find(p, text),
  File "/home/runner/work/cpython/cpython-ro-srcdir/Lib/test/string_tests.py", line 60, in checkequal
    self.assertEqual(
AssertionError: -1 != 0

The issue is reference_find:

        def reference_find(p, s):
            for i in range(len(s)):
                if s.startswith(p, i):
                    return i
            return -1

When s == p == '', we get for i in range(0), and then -1 is returned instead of the actual result which should be 0.

Linked PRs

@sweeneyde sweeneyde added type-bug An unexpected behavior, bug, or error tests Tests in the Lib/test dir labels Jun 10, 2023
@terryjreedy
Copy link
Member

My immediate question was "What could have happened that this should suddenly fail?" The answer is "Nothing!" For each CI run, reference_find is called 1000 times with random input. There is obviously a non-zero probabilty of generating this corner case for which the test as a whole is wrong. Hence we get an occasional bogus failure. Some possible fixes:

  • Do not generate this corner case; I have no idea how right now.
  • Do not test this easy-to-detect case: if not p or not text: continue after text is generated, or perhaps if not p: continue after p is generated.
  • Change reference_find to return 0 for this case, making sure to not introduce another bug.

The test was added in #86138, with PR by Dennis Sweeny and Tim Peters.

@hugovk
Copy link
Member

hugovk commented Mar 7, 2025

Thanks for the report, however it's been subsequently resolved by PR #110170 and issue #110160.

@hugovk hugovk closed this as completed Mar 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tests Tests in the Lib/test dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants