| 1 |
import pytest |
| 2 |
from hypothesis import given, strategies |
| 3 |
|
| 4 |
from rpython.rlib.unicodedata.dawg import (Dawg, lookup, inverse_lookup, |
| 5 |
build_compression_dawg, _inverse_lookup, |
| 6 |
encode_varint_unsigned, decode_varint_unsigned, |
| 7 |
number_add_bits, number_split_bits) |
| 8 |
from rpython.rlib.unicodedata.codegen import CodeWriter |
| 9 |
|
| 10 |
def test_1(): |
| 11 |
dawg = Dawg() |
| 12 |
dawg.insert("a", -4) |
| 13 |
dawg.insert("c", -2) |
| 14 |
dawg.insert("cat", -1) |
| 15 |
dawg.insert("catarr", 0) |
| 16 |
dawg.insert("catnip", 1) |
| 17 |
dawg.insert("zcatnip", 5) |
| 18 |
packed, data, inverse = dawg.finish() |
| 19 |
|
| 20 |
assert lookup(packed, data, "a") == -4 |
| 21 |
assert lookup(packed, data, "c") == -2 |
| 22 |
assert lookup(packed, data, "cat") == -1 |
| 23 |
assert lookup(packed, data, "catarr") == 0 |
| 24 |
assert lookup(packed, data, "catnip") == 1 |
| 25 |
assert lookup(packed, data, "zcatnip") == 5 |
| 26 |
assert inverse_lookup(packed, inverse, -4) == "a" |
| 27 |
assert inverse_lookup(packed, inverse, -2) == "c" |
| 28 |
assert inverse_lookup(packed, inverse, -1) == "cat" |
| 29 |
assert inverse_lookup(packed, inverse, 0) == "catarr" |
| 30 |
assert inverse_lookup(packed, inverse, 1) == "catnip" |
| 31 |
assert inverse_lookup(packed, inverse, 5) == "zcatnip" |
| 32 |
|
| 33 |
def test_2(): |
| 34 |
dawg = Dawg() |
| 35 |
dawg.insert("aaaaaa", -2) |
| 36 |
dawg.insert("baaaaa", -4) |
| 37 |
dawg.insert("bbbbbaaaaaaa", 0) |
| 38 |
dawg.insert("bbbbbbbbb", -1) |
| 39 |
packed, data, inverse = dawg.finish() |
| 40 |
|
| 41 |
def test_bug_match_past_string_end(): |
| 42 |
dawg = Dawg() |
| 43 |
dawg.insert("a", -2) |
| 44 |
dawg.insert("ba", 2) |
| 45 |
packed, data, inverse = dawg.finish() |
| 46 |
with pytest.raises(KeyError): |
| 47 |
lookup(packed, data, "b") |
| 48 |
|
| 49 |
def test_bug_1(): |
| 50 |
dawg = Dawg() |
| 51 |
dawg.insert("a", -2) |
| 52 |
dawg.insert("aa", 2) |
| 53 |
dawg.insert("b", 56) |
| 54 |
packed, data, inverse = dawg.finish() |
| 55 |
with pytest.raises(KeyError): |
| 56 |
lookup(packed, data, "ba") |
| 57 |
|
| 58 |
def test_missing_key_inverse(): |
| 59 |
dawg = Dawg() |
| 60 |
dawg.insert("aaaaaa", -2) |
| 61 |
dawg.insert("baaaaa", -4) |
| 62 |
dawg.insert("bbbbbaaaaaaa", 0) |
| 63 |
dawg.insert("bbbbbbbbb", -1) |
| 64 |
packed, data, inverse = dawg.finish() |
| 65 |
with pytest.raises(KeyError): |
| 66 |
_inverse_lookup(packed, 5) |
| 67 |
|
| 68 |
def test_generate(): |
| 69 |
import py |
| 70 |
tmpdir = py.test.ensuretemp(__name__) |
| 71 |
lines = lines = map(hex,map(hash, map(str, range(100)))) |
| 72 |
|
| 73 |
lines.extend([ 'AAA', 'AAAA', 'AAAB', 'AAB', 'AABB' ]) |
| 74 |
out = tmpdir.join('dawg.py') |
| 75 |
|
| 76 |
o = out.open('w') |
| 77 |
d = dict(map(lambda (x,y):(y,x), enumerate(lines))) |
| 78 |
trie = build_compression_dawg(CodeWriter(o), d) |
| 79 |
o.close() |
| 80 |
|
| 81 |
dmod = out.pyimport() |
| 82 |
for i, line in enumerate(lines): |
| 83 |
assert dmod.lookup_charcode(i) == line |
| 84 |
assert dmod.dawg_lookup(line) == i |
| 85 |
|
| 86 |
|
| 87 |
@given(strategies.integers(min_value=0), strategies.binary()) |
| 88 |
def test_varint_hypothesis(i, prefix): |
| 89 |
b = [] |
| 90 |
encode_varint_unsigned(i, b) |
| 91 |
b = b"".join(b) |
| 92 |
res, pos = decode_varint_unsigned(b) |
| 93 |
assert res == i |
| 94 |
assert pos == len(b) |
| 95 |
res, pos = decode_varint_unsigned(prefix + b, len(prefix)) |
| 96 |
assert res == i |
| 97 |
assert pos == len(b) + len(prefix) |
| 98 |
|
| 99 |
@given(strategies.integers()) |
| 100 |
def test_add_bits(i): |
| 101 |
for bit1, bit2 in ((0, 0), (0, 1), (1, 0), (1, 1)): |
| 102 |
assert number_split_bits(number_add_bits(i, bit1, bit2), 2) == (i, bit1, bit2) |
| 103 |
|
| 104 |
|
| 105 |
START = ord('A') |
| 106 |
STOP = ord('G') |
| 107 |
|
| 108 |
@given(strategies.lists(strategies.text(strategies.characters(min_codepoint=START, max_codepoint=STOP), min_size=1, max_size=10), min_size=5, max_size=50)) |
| 109 |
def test_random_dawg(l): |
| 110 |
l = [s.encode('ascii') for s in l] |
| 111 |
|
| 112 |
|
| 113 |
d = {s: i for i, s in enumerate(l)} |
| 114 |
tmpdir = pytest.ensuretemp(__name__) |
| 115 |
out = tmpdir.join('%s.py' % hash(str(l))) |
| 116 |
o = out.open('w') |
| 117 |
|
| 118 |
|
| 119 |
trie = build_compression_dawg(CodeWriter(o), d) |
| 120 |
o.close() |
| 121 |
s = out.read() |
| 122 |
dmod = {} |
| 123 |
exec s in dmod |
| 124 |
dawg_lookup = dmod['dawg_lookup'] |
| 125 |
lookup_charcode = dmod['lookup_charcode'] |
| 126 |
def near_misses(s): |
| 127 |
for replacement_char in range(START, STOP): |
| 128 |
replacement_char = chr(replacement_char) |
| 129 |
yield s + replacement_char |
| 130 |
yield replacement_char + s |
| 131 |
for pos in range(len(s)): |
| 132 |
news = s[:pos] + replacement_char + s[pos + 1:] |
| 133 |
yield news |
| 134 |
for s, i in d.items(): |
| 135 |
assert dawg_lookup(s) == d[s] |
| 136 |
assert lookup_charcode(i) == s |
| 137 |
|
| 138 |
|
| 139 |
for news in near_misses(s): |
| 140 |
if news in d: |
| 141 |
continue |
| 142 |
with pytest.raises(KeyError): |
| 143 |
dawg_lookup(news) |
| 144 |
valid_values = {i for s, i in d.items()} |
| 145 |
for i in range(-100, len(l) + 100): |
| 146 |
if i in valid_values: |
| 147 |
continue |
| 148 |
with pytest.raises(KeyError): |
| 149 |
lookup_charcode(i) |