cortex
aien-dev/cortex
Brought by drakestapleton
cortex / tests / fts / gen_tokf.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Generator (test input only) for the tokenizer stress file: every Unicode scalar value |
| 3 | U+0080..U+10FFFF in three contexts (alone, after 'a', before 'a'), every lead byte with every |
| 4 | continuation-run length 0..6, surrogates/overlongs/out-of-range encodings, and random bytes. |
| 5 | Records: u32 length + bytes. Usage: gen_tokf.py OUT""" |
| 6 | import random, struct, sys |
| 7 | recs = [] |
| 8 | def cp8(c): |
| 9 | # encode any value < 2^21 with the generic UTF-8 bit layout (no validity checks) |
| 10 | if c < 0x80: return bytes([c]) |
| 11 | if c < 0x800: return bytes([0xC0 | c >> 6, 0x80 | c & 63]) |
| 12 | if c < 0x10000: return bytes([0xE0 | c >> 12, 0x80 | c >> 6 & 63, 0x80 | c & 63]) |
| 13 | return bytes([0xF0 | c >> 18, 0x80 | c >> 12 & 63, 0x80 | c >> 6 & 63, 0x80 | c & 63]) |
| 14 | chunk = [] |
| 15 | for c in range(0x80, 0x110000): |
| 16 | e = cp8(c) |
| 17 | chunk.append(e + b' a' + e + b' ' + e + b'a ') |
| 18 | if len(chunk) == 2048: |
| 19 | recs.append(b''.join(chunk)); chunk = [] |
| 20 | if chunk: recs.append(b''.join(chunk)) |
| 21 | for lead in range(0x80, 0x100): |
| 22 | rec = b'' |
| 23 | for n in range(7): |
| 24 | rec += b'x' + bytes([lead]) + b'\x9f' * n + b'y ' + bytes([lead]) + b'\xbf' * n + b' ' |
| 25 | recs.append(rec) |
| 26 | rnd = random.Random(20261026) |
| 27 | for i in range(4000): |
| 28 | n = rnd.randint(0, 300) |
| 29 | recs.append(bytes(rnd.choice(b'aZ09_ .-\x00\x80\xbf\xc0\xc3\xcc\xe0\xe2\xed\xef\xf0\xf4\xf8\xff') for _ in range(n))) |
| 30 | recs.append(b'A' * 40000 + b' ' + ('\u00c9' * 20000).encode()) # token longer than 32768 bytes |
| 31 | recs.append(b'') |
| 32 | with open(sys.argv[1], 'wb') as f: |
| 33 | for r in recs: |
| 34 | f.write(struct.pack('<I', len(r)) + r) |
| 35 | print(len(recs), 'records') |