Find the pagination bug that creates a phantom page
Seven fictional items split into groups of three appear to work. Six items expose the defect: the old function adds an empty page. Empty input is wrong too.
This is a useful debugging pattern because ordinary examples can pass while exact boundaries fail.
Run the example
All illustrative records are fictional. Save the following as debug_page_boundary.py.
The complete script was run in PythonIDE on iPhone. The actual output reproduced the old behavior for lengths 0, 3, 6 and 7, printed trace indices 0, 3 and 6, and passed all three unittest tests with All regression tests passed: True.
import unittest
# Fictional seven-item list; each page should hold 3 items.
items = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
def old_pages(values, size):
return [values[i:i + size] for i in range(0, len(values) + 1, size)]
def pages(values, size):
if type(size) is not int or size <= 0:
raise ValueError('size must be a positive integer')
return [values[i:i + size] for i in range(0, len(values), size)]
print('REPRODUCE THE BOUNDARY BUG')
for length in [0, 3, 6, 7]:
sample = items[:length]
before = old_pages(sample, 3)
after = pages(sample, 3)
print(f'Items={length}: old={before}; fixed={after}')
print('\nTRACE THE SIX-ITEM CASE')
for start in range(0, 6 + 1, 3):
print(f'start={start}; slice={items[:6][start:start+3]}')
class PaginationTests(unittest.TestCase):
def test_known_pages(self):
self.assertEqual(pages(items, 3), [['A', 'B', 'C'], ['D', 'E', 'F'], ['G']])
def test_conservation(self):
for length in range(8):
original = items[:length]
for size in range(1, 5):
result = pages(original, size)
flattened = [item for page in result for item in page]
self.assertEqual(flattened, original)
self.assertTrue(all(0 < len(page) <= size for page in result))
def test_empty_and_invalid(self):
self.assertEqual(pages([], 3), [])
for size in [0, -1, True, 2.5]:
with self.assertRaises(ValueError):
pages(items, size)
suite = unittest.defaultTestLoader.loadTestsFromTestCase(PaginationTests)
result = unittest.TextTestRunner(verbosity=2).run(suite)
print('All regression tests passed:', result.wasSuccessful())
Understand the checks and their limits
range stops before its upper bound. Adding one to len(values) includes an extra starting position exactly when the length is a multiple of the page size. Slicing from that position returns an empty list without raising an exception, so the defect is easy to miss.
The printed trace makes each starting index visible without claiming a specific debugger interface. The fix removes the extra one and rejects invalid sizes, including booleans.
Tests check:
- a known arrangement
- reconstruction of the original sequence
- nonempty bounded pages
- empty input
- invalid sizes
Reconstruction alone would not catch an extra empty page because flattening discards it. That is why the nonempty-page assertion is separate.
Try one change
Temporarily restore the +1 bug in pages. Which assertion catches it? Then remove only the nonempty-page assertion and explain why reconstruction can still pass. Restore the correct implementation afterward.
I build PythonIDE, a Python coding app for iPhone and iPad. The example is intended to teach a workflow you can inspect and adapt. AI assisted with drafting and translation. The script and printed tests were checked in PythonIDE; this is not a demonstration of an interactive debugger or breakpoint interface.
PythonIDE on the App Store
Comments
No comments yet. Start the discussion.