cortex
aien-dev/cortex
Brought by drakestapleton
cortex / tests / fts / gen_like.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Generator (test input only) for the LIKE differential: the cortex-rs shape '%<query>%' |
| 3 | for real title words, plus '_' / '%' placements, ASCII and non-ASCII case variants, |
| 4 | invalid UTF-8, embedded NUL and the 50000-byte pattern limit. Records: u32 length + bytes. |
| 5 | Usage: gen_like.py DB OUT [N]""" |
| 6 | import random, re, sqlite3, struct, sys |
| 7 | db, out = sys.argv[1], sys.argv[2] |
| 8 | target = int(sys.argv[3]) if len(sys.argv) > 3 else 600 |
| 9 | con = sqlite3.connect('file:%s?mode=ro' % db, uri=True) |
| 10 | titles = [r[0] for r in con.execute("SELECT canonical_name FROM entities ORDER BY rowid")] |
| 11 | rnd = random.Random(7) |
| 12 | words = [w for t in titles for w in re.split(r'\s+', t) if w] |
| 13 | na = [w for w in words if any(ord(c) > 127 for c in w)] |
| 14 | P = [b'', b'%', b'%%', b'_', b'%_%', b'___', b'%a%', b'%A%', b'%gb10%', b'%GB10%', b'%Gb_0%', b'%nvrm\\_alloc%', |
| 15 | b'%nvrm_alloc%', b'%#225%', b'%caf\xc3\xa9%', b'%CAF\xc3\x89%', b'%\xc3\xa9%', b'%_\xc3\xa9%', b'%\xe2\x80\x94%', |
| 16 | b'%\xff%', b'%\xc0%', b'%\x80%', b'%\xc3%', b'%a\x00b%', b'\x00', b'%\x00', b'%%%%%x%%%%', b'%_%_%_%', |
| 17 | b'x%', b'%x', b'%e', b'E%', b'%' + b'a' * 49998, b'%' + b'a' * 49999, b'%' + b'a' * 50000 + b'%', |
| 18 | b'%[a]%', b'%*%', b'%?%', b'%\\%', b'%\'%', b'%"%', b'%' + ('\U0001F600'.encode()) + b'%'] |
| 19 | for t in titles[:150]: |
| 20 | P.append(b'%' + t.strip().encode() + b'%') |
| 21 | while len(P) < target: |
| 22 | k = rnd.randrange(8) |
| 23 | wd = rnd.choice(words) |
| 24 | if k == 0: s = '%' + wd + '%' |
| 25 | elif k == 1: s = '%' + wd.upper() + '%' |
| 26 | elif k == 2: s = '%' + wd.lower() + '%' |
| 27 | elif k == 3: s = '%' + ''.join('_' if rnd.random() < 0.3 else c for c in wd) + '%' |
| 28 | elif k == 4: s = '%' + rnd.choice(na) + '%' if na else '%é%' |
| 29 | elif k == 5: s = '%' + wd[:rnd.randint(1, 3)] + '%' + rnd.choice(words)[:3] + '%' |
| 30 | elif k == 6: s = wd + '%' |
| 31 | else: s = '%' + wd.swapcase() + '%' |
| 32 | P.append(s.encode()) |
| 33 | with open(out, 'wb') as f: |
| 34 | for p in P: |
| 35 | f.write(struct.pack('<I', len(p)) + p) |
| 36 | print(len(P), 'patterns') |