-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #100 from chrislit/fuzz
fuzz branch
- Loading branch information
Showing
9 changed files
with
2,816 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
# Copyright 2018 by Christopher C. Little. | ||
# This file is part of Abydos. | ||
# | ||
# Abydos is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# Abydos is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with Abydos. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
|
||
"""abydos.tests.fuzz. | ||
This module contains fuzz tests for Abydos | ||
""" | ||
|
||
import random | ||
import unicodedata | ||
from string import printable | ||
|
||
from six import unichr | ||
|
||
|
||
def random_char(below=0x10ffff, must_be=None): | ||
"""Generate a random Unicode character below U+{below}.""" | ||
while True: | ||
char = unichr(random.randint(0, below)) | ||
try: | ||
name = unicodedata.name(char) | ||
if must_be is None or must_be in name: | ||
return char | ||
except ValueError: | ||
pass | ||
|
||
|
||
def fuzz(word, fuzziness=0.2): | ||
"""Fuzz a word with noise.""" | ||
while True: | ||
new_word = [] | ||
for ch in word: | ||
if random.random() > fuzziness: | ||
new_word.append(ch) | ||
else: | ||
if random.random() > 0.5: | ||
new_word.append(random.choice(printable)) | ||
elif random.random() > 0.8: | ||
new_word.append(unichr(random.randint(0, 0x10ffff))) | ||
else: | ||
new_word.append(unichr(random.randint(0, 0xffff))) | ||
if random.random() > 0.5: | ||
new_word.append(ch) | ||
new_word = ''.join(new_word) | ||
if new_word != word: | ||
return new_word |
Oops, something went wrong.