1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
|
|
# Copyright 2018 by Christopher C. Little. |
4
|
|
|
# This file is part of Abydos. |
5
|
|
|
# |
6
|
|
|
# Abydos is free software: you can redistribute it and/or modify |
7
|
|
|
# it under the terms of the GNU General Public License as published by |
8
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
# (at your option) any later version. |
10
|
|
|
# |
11
|
|
|
# Abydos is distributed in the hope that it will be useful, |
12
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
# GNU General Public License for more details. |
15
|
|
|
# |
16
|
|
|
# You should have received a copy of the GNU General Public License |
17
|
|
|
# along with Abydos. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
"""abydos.tests.fuzz. |
21
|
|
|
|
22
|
|
|
This module contains fuzz tests for Abydos |
23
|
|
|
""" |
24
|
|
|
|
25
|
|
|
import unicodedata |
26
|
|
|
from random import choice, randint, random |
27
|
|
|
from string import printable |
28
|
|
|
|
29
|
|
|
from six import unichr |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
def _random_char(below=0x10ffff, must_be=None): |
33
|
|
|
"""Generate a random Unicode character below U+{below}.""" |
34
|
|
|
while True: |
35
|
|
|
char = unichr(randint(0, below)) # noqa: S311 |
36
|
|
|
try: |
37
|
|
|
name = unicodedata.name(char) |
38
|
|
|
if must_be is None or must_be in name: |
39
|
|
|
return char |
40
|
|
|
except ValueError: |
41
|
|
|
pass |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
def _fuzz(word, fuzziness=0.2): |
45
|
|
|
"""Fuzz a word with noise.""" |
46
|
|
|
while True: |
47
|
|
|
new_word = [] |
48
|
|
|
for ch in word: |
49
|
|
|
if random() > fuzziness: # noqa: S311 |
50
|
|
|
new_word.append(ch) |
51
|
|
|
else: |
52
|
|
|
if random() > 0.5: # noqa: S311 |
53
|
|
|
new_word.append(choice(printable)) # noqa: S311 |
54
|
|
|
elif random() > 0.8: # noqa: S311 |
55
|
|
|
new_word.append(unichr(randint(0, 0x10ffff))) # noqa: S311 |
56
|
|
|
else: |
57
|
|
|
new_word.append(unichr(randint(0, 0xffff))) # noqa: S311 |
58
|
|
|
if random() > 0.5: # noqa: S311 |
59
|
|
|
new_word.append(ch) |
60
|
|
|
new_word = ''.join(new_word) |
61
|
|
|
if new_word != word: |
62
|
|
|
return new_word |
63
|
|
|
|