aggiunge test random
This commit is contained in:
parent
9881d09fc5
commit
68fe1102e4
3 changed files with 77 additions and 7 deletions
|
@ -1,6 +1,9 @@
|
|||
"""Main module."""
|
||||
import logging
|
||||
import random
|
||||
|
||||
log = logging.getLogger("bananalib")
|
||||
|
||||
|
||||
class Codec:
|
||||
def __init__(self, dictstart=0, shiftend=0, minlength=0, dictionary=None):
|
||||
|
@ -61,18 +64,18 @@ class Codec:
|
|||
|
||||
return True
|
||||
|
||||
def random(self, minlength=6):
|
||||
|
||||
def random(self, minlength=6, prng=random.Random()):
|
||||
numdict = len(self.dictionary)
|
||||
word = ""
|
||||
|
||||
if minlength < 1:
|
||||
return ""
|
||||
|
||||
curr_dict = (numdict - 1 + self.dictstart + self.shiftend) % numdict
|
||||
final_dict = (numdict - 1 + self.dictstart) % numdict
|
||||
print("final", final_dict)
|
||||
while curr_dict != final_dict or len(word) < minlength:
|
||||
word = random.choice(self.dictionary[curr_dict]) + word
|
||||
word = prng.choice(self.dictionary[curr_dict]) + word
|
||||
curr_dict = (curr_dict - 1) % numdict
|
||||
print("curr", curr_dict)
|
||||
|
||||
return word
|
||||
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
"""Console script for banana."""
|
||||
import argparse
|
||||
import logging
|
||||
import random
|
||||
import sys
|
||||
|
||||
import banana
|
||||
|
@ -10,6 +12,10 @@ def get_codec(args):
|
|||
return banana.BananaCodec()
|
||||
if args.ananas:
|
||||
return banana.AnanasCodec()
|
||||
if args.ribes:
|
||||
return banana.RibesCodec()
|
||||
if args.avocado:
|
||||
return banana.AvocadoCodec()
|
||||
kwargs = {}
|
||||
if args.dictionary:
|
||||
kwargs["dictionary"] = args.dictionary
|
||||
|
@ -40,16 +46,31 @@ def main_check(args):
|
|||
|
||||
|
||||
def main_random(args):
|
||||
print(get_codec(args).random(minlength=args.minlength))
|
||||
codec = get_codec(args)
|
||||
kwargs = dict(minlength=args.minlength)
|
||||
if args.seed:
|
||||
kwargs["prng"] = random.Random(args.seed)
|
||||
print(codec.random(**kwargs))
|
||||
|
||||
|
||||
def colon_separated_list(s):
|
||||
return s.split(":")
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Convert dec number to banana")
|
||||
parser.add_argument(
|
||||
"--log-level", choices=["DEBUG", "INFO", "WARN", "ERROR"], default="WARN"
|
||||
)
|
||||
parser.add_argument("--ananas", action="store_true")
|
||||
parser.add_argument("--avocado", action="store_true")
|
||||
parser.add_argument("--banana", action="store_true")
|
||||
parser.add_argument("--ribes", action="store_true")
|
||||
parser.add_argument("--dictionary", help="Set dictionary", type=list, nargs="+")
|
||||
parser.add_argument(
|
||||
"--dictionary",
|
||||
help="Set dictionary in colon-separated list",
|
||||
type=colon_separated_list,
|
||||
)
|
||||
parser.add_argument(
|
||||
"--dictstart", help="Set starting dictionary", type=int, default=0
|
||||
)
|
||||
|
@ -72,6 +93,7 @@ def main():
|
|||
|
||||
rand = sub.add_parser("random", help="Generate random banana")
|
||||
rand.add_argument("--minlength", help="Set minimum length", type=int, default=6)
|
||||
rand.add_argument("--seed", type=int, default=None)
|
||||
rand.set_defaults(func=main_random)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
@ -79,6 +101,7 @@ def main():
|
|||
print("You need to select one subcommand. \nUse --help", file=sys.stderr)
|
||||
# parser.print_help()
|
||||
sys.exit(1)
|
||||
logging.basicConfig(level=args.log_level)
|
||||
args.func(args)
|
||||
|
||||
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
"""Tests for `banana` package."""
|
||||
import random
|
||||
|
||||
import pytest
|
||||
|
||||
from banana import AnanasCodec, AvocadoCodec, BananaCodec, RibesCodec
|
||||
|
@ -174,3 +176,45 @@ def test_answer_to_life_the_universe_and_everything():
|
|||
banana = banana_codec.decode("banana")
|
||||
assert banana != 42
|
||||
assert banana == 2485
|
||||
|
||||
|
||||
def test_random_len_0():
|
||||
assert banana_codec.random(minlength=0) == ""
|
||||
assert ananas_codec.random(minlength=0) == ""
|
||||
assert avocado_codec.random(minlength=0) == ""
|
||||
assert ribes_codec.random(minlength=0) == ""
|
||||
|
||||
|
||||
def test_banana_random_minlength_even():
|
||||
for l in (0, 2, 4, 6, 8, 10, 12):
|
||||
assert len(banana_codec.random(minlength=l)) == l
|
||||
assert len(ananas_codec.random(minlength=l)) == l
|
||||
|
||||
|
||||
def test_banana_random_minlength_odd():
|
||||
for l in (1, 3, 5, 7, 9):
|
||||
assert len(banana_codec.random(minlength=l)) == l + 1
|
||||
assert len(ananas_codec.random(minlength=l)) == l + 1
|
||||
|
||||
|
||||
def test_ribes_random_minlength_even():
|
||||
for l in (2, 4, 6, 8, 10, 12):
|
||||
assert len(ribes_codec.random(minlength=l)) == l + 1
|
||||
assert len(avocado_codec.random(minlength=l)) == l + 1
|
||||
|
||||
|
||||
def test_ribes_random_minlength_odd():
|
||||
for l in (1, 3, 5, 7, 9):
|
||||
assert len(ribes_codec.random(minlength=l)) == l
|
||||
assert len(avocado_codec.random(minlength=l)) == l
|
||||
|
||||
|
||||
def test_random_coherence_please():
|
||||
for codec in (ribes_codec, ananas_codec, avocado_codec, banana_codec):
|
||||
for seed in range(30):
|
||||
word = codec.random(prng=random.Random(seed))
|
||||
assert codec.is_valid(word), "%s (seed %d) non valido per %s" % (
|
||||
word,
|
||||
seed,
|
||||
codec.__class__.__name__,
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue