Skip to content

Commit 5fc294c

Browse files
gpsheadclaude
andcommitted
test_bytes: Add SIMD-specific test cases for bytes.hex()
Add targeted tests for corner cases relevant to SIMD optimization: - test_hex_simd_boundaries: Test lengths around the 16-byte SIMD threshold (14, 15, 16, 17, 31, 32, 33, 64, 65 bytes) - test_hex_nibble_boundaries: Test the 9/10 nibble value boundary where digits become letters, verifying the signed comparison optimization works correctly - test_hex_simd_separator: Test SIMD separator insertion path (triggered when sep >= 8 and len >= 16) with various group sizes and both positive/negative bytes_per_sep Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent e8650f3 commit 5fc294c

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

Lib/test/test_bytes.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,87 @@ def test_hex_separator_six_bytes(self):
584584
self.assertEqual(six_bytes.hex(':', -6), '0306090c0f12')
585585
self.assertEqual(six_bytes.hex(' ', -95), '0306090c0f12')
586586

587+
def test_hex_simd_boundaries(self):
588+
# Test lengths around the SIMD threshold (16 bytes).
589+
# SIMD processes 16 bytes at a time; smaller inputs use scalar code.
590+
for length in (14, 15, 16, 17, 31, 32, 33, 64, 65):
591+
data = self.type2test(bytes(range(length)))
592+
expected = ''.join(f'{b:02x}' for b in range(length))
593+
with self.subTest(length=length):
594+
self.assertEqual(data.hex(), expected)
595+
596+
def test_hex_nibble_boundaries(self):
597+
# Test the nibble value boundary at 9/10 (where '9' becomes 'a').
598+
# SIMD uses signed comparison for efficiency; verify correctness
599+
# at this boundary for various nibble combinations.
600+
boundary_bytes = self.type2test(bytes([
601+
0x09, # both nibbles: 0, 9
602+
0x0a, # both nibbles: 0, 10
603+
0x90, # both nibbles: 9, 0
604+
0x99, # both nibbles: 9, 9 (max all-digit)
605+
0x9a, # both nibbles: 9, 10
606+
0xa0, # both nibbles: 10, 0
607+
0xa9, # both nibbles: 10, 9
608+
0xaa, # both nibbles: 10, 10 (min all-letter)
609+
0x00, # min value
610+
0xff, # max value
611+
]))
612+
self.assertEqual(boundary_bytes.hex(), '090a90999aa0a9aa00ff')
613+
614+
# Repeat with 16+ bytes to exercise SIMD path
615+
simd_boundary = self.type2test(boundary_bytes * 2)
616+
self.assertEqual(simd_boundary.hex(), '090a90999aa0a9aa00ff' * 2)
617+
618+
def test_hex_simd_separator(self):
619+
# Test SIMD path for separator insertion (sep >= 8 bytes, len >= 16).
620+
# SIMD hexlifies then shuffles in-place to insert separators.
621+
622+
# 32 bytes exercises SIMD; test various separator group sizes
623+
data = self.type2test(bytes(range(32)))
624+
625+
# bytes_per_sep=8: 4 groups of 8 bytes, 3 separators
626+
self.assertEqual(
627+
data.hex('-', 8),
628+
'0001020304050607-08090a0b0c0d0e0f-'
629+
'1011121314151617-18191a1b1c1d1e1f'
630+
)
631+
# bytes_per_sep=9: groups of 9 from start, 5 byte remainder at end
632+
self.assertEqual(
633+
data.hex('.', 9),
634+
'0001020304.05060708090a0b0c0d.'
635+
'0e0f10111213141516.1718191a1b1c1d1e1f'
636+
)
637+
# bytes_per_sep=16: 2 groups of 16 bytes
638+
self.assertEqual(
639+
data.hex(' ', 16),
640+
'000102030405060708090a0b0c0d0e0f '
641+
'101112131415161718191a1b1c1d1e1f'
642+
)
643+
# Negative bytes_per_sep: groups from end, remainder at start
644+
self.assertEqual(
645+
data.hex('|', -8),
646+
'0001020304050607|08090a0b0c0d0e0f|'
647+
'1011121314151617|18191a1b1c1d1e1f'
648+
)
649+
self.assertEqual(
650+
data.hex('_', -9),
651+
'000102030405060708_090a0b0c0d0e0f1011_'
652+
'12131415161718191a_1b1c1d1e1f'
653+
)
654+
655+
# 20 bytes: SIMD (16) + 4 byte scalar remainder
656+
data20 = self.type2test(bytes(range(20)))
657+
# Positive: groups from start, remainder at end
658+
self.assertEqual(
659+
data20.hex('#', 8),
660+
'00010203#0405060708090a0b#0c0d0e0f10111213'
661+
)
662+
# Negative: groups from end, remainder at start
663+
self.assertEqual(
664+
data20.hex('@', -8),
665+
'0001020304050607@08090a0b0c0d0e0f@10111213'
666+
)
667+
587668
def test_join(self):
588669
self.assertEqual(self.type2test(b"").join([]), b"")
589670
self.assertEqual(self.type2test(b"").join([b""]), b"")

0 commit comments

Comments
 (0)