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 os |
26
|
|
|
import unicodedata |
27
|
|
|
from random import choice, randint, random |
28
|
|
|
from string import printable |
29
|
|
|
|
30
|
|
|
from six import unichr |
31
|
|
|
|
32
|
|
|
from .. import EXTREME_TEST as SUPER_EXTREME_TEST |
33
|
|
|
from .. import _corpus_file as _super_corpus_file |
34
|
|
|
|
35
|
|
|
CORPORA = os.path.join(os.path.dirname(__file__), 'corpora') |
36
|
|
|
|
37
|
|
|
EXTREME_TEST = SUPER_EXTREME_TEST # inherit setting from base tests |
38
|
|
|
EXTREME_TEST = False # Set to True to test EVERY single case (NB: takes hours) |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
if not EXTREME_TEST and os.path.isfile(os.path.join(os.path.dirname(__file__), |
42
|
|
|
'EXTREME_TEST')): |
43
|
|
|
# EXTREME_TEST file detected -- switching to EXTREME_TEST mode... |
44
|
|
|
EXTREME_TEST = True |
45
|
|
|
if not EXTREME_TEST and os.path.isfile(os.path.join(os.path.dirname(__file__), |
46
|
|
|
'..', 'EXTREME_TEST')): |
47
|
|
|
# EXTREME_TEST file detected -- switching to EXTREME_TEST mode... |
48
|
|
|
EXTREME_TEST = True |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
def _corpus_file(name, corpora_dir=CORPORA): |
52
|
|
|
"""Return the path to a corpus file. |
53
|
|
|
|
54
|
|
|
:param str name: corpus file |
55
|
|
|
:param str corpora_dir: the directory containing the corpora |
56
|
|
|
:return: path |
57
|
|
|
""" |
58
|
|
|
return _super_corpus_file(name, corpora_dir) |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
def _random_char(below=0x10ffff, must_be=None): |
62
|
|
|
"""Generate a random Unicode character below U+{below}.""" |
63
|
|
|
while True: |
64
|
|
|
char = unichr(randint(0, below)) # noqa: S311 |
65
|
|
|
try: |
66
|
|
|
name = unicodedata.name(char) |
67
|
|
|
if must_be is None or must_be in name: |
68
|
|
|
return char |
69
|
|
|
except ValueError: |
70
|
|
|
pass |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
def _fuzz(word, fuzziness=0.2): |
74
|
|
|
"""Fuzz a word with noise.""" |
75
|
|
|
while True: |
76
|
|
|
new_word = [] |
77
|
|
|
for ch in word: |
78
|
|
|
if random() > fuzziness: # noqa: S311 |
79
|
|
|
new_word.append(ch) |
80
|
|
|
else: |
81
|
|
|
if random() > 0.5: # noqa: S311 |
82
|
|
|
new_word.append(choice(printable)) # noqa: S311 |
83
|
|
|
elif random() > 0.8: # noqa: S311 |
84
|
|
|
new_word.append(unichr(randint(0, 0x10ffff))) # noqa: S311 |
85
|
|
|
else: |
86
|
|
|
new_word.append(unichr(randint(0, 0xffff))) # noqa: S311 |
87
|
|
|
if random() > 0.5: # noqa: S311 |
88
|
|
|
new_word.append(ch) |
89
|
|
|
new_word = ''.join(new_word) |
90
|
|
|
if new_word != word: |
91
|
|
|
return new_word |
92
|
|
|
|