interplane.Get Omega
Read in your language

cortex

aien-dev/cortex

Brought by drakestapleton

branch main Branches Tags History

cortex / tests / fts / gen_tokf.py

35 lines · 1617 bytes Raw Blame History
1#!/usr/bin/env python3
2"""Generator (test input only) for the tokenizer stress file: every Unicode scalar value
3U+0080..U+10FFFF in three contexts (alone, after 'a', before 'a'), every lead byte with every
4continuation-run length 0..6, surrogates/overlongs/out-of-range encodings, and random bytes.
5Records: u32 length + bytes. Usage: gen_tokf.py OUT"""
6import random, struct, sys
7recs = []
8def 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])
14chunk = []
15for 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 = []
20if chunk: recs.append(b''.join(chunk))
21for 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)
26rnd = random.Random(20261026)
27for 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)))
30recs.append(b'A' * 40000 + b' ' + ('\u00c9' * 20000).encode()) # token longer than 32768 bytes
31recs.append(b'')
32with open(sys.argv[1], 'wb') as f:
33 for r in recs:
34 f.write(struct.pack('<I', len(r)) + r)
35print(len(recs), 'records')